tensorflow lite toco python APl: NameError: имя «tempfile» не определено

Я запустил пример с https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/python_api.md

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("test.tflite", "wb").write(tflite_modeL)

но я получил ошибку «NameError: имя« tempfile »не определено» в python3 и «NameError: глобальное имя« tempfile »не определено» в python2

NameError                                 Traceback (most recent call last)
<ipython-input-21-24c36564faa4> in <module>()
      5 out = tf.identity(val, name="out")
      6 with tf.Session() as sess:
----> 7   tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [img], [out])
  8   open("test.tflite", "wb").write(tflite_modeL)

python3.5/site-packages/tensorflow/contrib/lite/python/lite.py in toco_convert(input_data, input_tensors, output_tensors, inference_type, input_format, output_format, quantized_input_stats, drop_control_dependency)
    196   data = toco_convert_protos(model.SerializeToString(),
    197                              toco.SerializeToString(),
--> 198                              input_data.SerializeToString())
    199   return data
    200 

python3.5/site-packages/tensorflow/contrib/lite/python/lite.py in toco_convert_protos(model_flags_str, toco_flags_str, input_data_str)
     89     return _toco_convert_protos(model_flags_str, toco_flags_str, input_data_str)
     90 
---> 91   with tempfile.NamedTemporaryFile() as fp_toco, \
     92            tempfile.NamedTemporaryFile() as fp_model, \
     93            tempfile.NamedTemporaryFile() as fp_input, \

NameError: name 'tempfile' is not defined

Как заставить это работать?


person magenta    schedule 05.12.2017    source источник
comment
у меня точно такая же проблема   -  person Ushnish    schedule 16.12.2017
comment
Вы можете отслеживать эту проблему здесь: github.com/tensorflow/tensorflow/issues/15410   -  person Leandro Borges Ferreira    schedule 16.12.2017


Ответы (1)


Эта проблема была исправлена, начиная с TensorFlow 1.7 (см. github ошибка).

Я только что успешно выполнил ваш фрагмент в последней версии TensorFlow 1.9:

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)

Модель TFLite converts_model.tflite сохраняется в файл.

person raziel    schedule 27.07.2018