tensorflow: Calling tf.contrib.lite.toco_convert results in global name 'tempfile' is not defined error

Please go to Stack Overflow for help and support:

https://stackoverflow.com/questions/tagged/tensorflow

Related SO post - https://stackoverflow.com/questions/47645056/tensorflow-lite-toco-python-apl-nameerror-name-tempfile-is-not-defined

If you open a GitHub issue, here is our policy:

  1. It must be a bug or a feature request.
  2. The form below must be filled out.
  3. It shouldn’t be a TensorBoard issue. Those go here.

Here’s why we have that policy: TensorFlow developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.


System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Followed example on Tensorflow Lite documentation

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MacOS High Sierra

  • TensorFlow installed from (source or binary): Source

  • TensorFlow version (use command below): (‘v1.3.0-rc1-5910-ge2174cc943’, ‘1.4.0’)

  • Python version: 2.7.10

  • Bazel version (if compiling from source): bazel release 0.5.4-homebrew

  • GCC/Compiler version (if compiling from source): Apple LLVM version 9.0.0 (clang-900.0.34.1)

  • CUDA/cuDNN version: N/A (CPU only)

  • GPU model and memory: N/A

  • Exact command to reproduce:

import tensorflow as tf
img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
val = img + tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
out = tf.identity(val, name="out")
with tf.Session() as sess:
  tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [img], [out])
  open("converteds_model.tflite", "wb").write(tflite_model)

You can collect some of this information using our environment capture script:

https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh

You can obtain the TensorFlow version with

python -c “import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)”

Describe the problem

Describe the problem clearly here. Be sure to convey here why it’s a bug in TensorFlow or a feature request.

Opened python on terminal and ran

import tensorflow as tf
img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
val = img + tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
out = tf.identity(val, name="out")
with tf.Session() as sess:
  tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [img], [out])
  open("converteds_model.tflite", "wb").write(tflite_model)

Resulting output is

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Library/Python/2.7/site-packages/tensorflow/contrib/lite/python/lite.py", line 198, in toco_convert
    input_data.SerializeToString())
  File "/Library/Python/2.7/site-packages/tensorflow/contrib/lite/python/lite.py", line 91, in toco_convert_protos
    with tempfile.NamedTemporaryFile() as fp_toco, \
NameError: global name 'tempfile' is not defined

As a sanity check I ran the following

import tempfile
with tempfile.NamedTemporaryFile() as fp_toco:
     print fp_toco.name

Output was /var/folders/hd/7yc9wgwj5wvd43_d94b4m47m0000gn/T/tmpxxdYMY

Source code / logs

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Sorry about this. It seems it has to do with the interface sealing in tensorflow (where we try to prevent exposing symbols unrelated to the interfaces). You can work around this with the following code


import tensorflow as tf
# manually put back imported modules
import tempfile
import subprocess
tf.contrib.lite.tempfile = tempfile
tf.contrib.lite.subprocess = subprocess

img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
val = img + tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
out = tf.identity(val, name="out")
with tf.Session() as sess:
  tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [img], [out])
  open("converteds_model.tflite", "wb").write(tflite_model)

I’ll make a more permanent fix soon.