Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
tensorflow lite toco python APl:NameError:“;名称';tempfile';“未定义”;_Tensorflow_Tensorflow Lite - Fatal编程技术网

tensorflow lite toco python APl:NameError:“;名称';tempfile';“未定义”;

tensorflow lite toco python APl:NameError:“;名称';tempfile';“未定义”;,tensorflow,tensorflow-lite,Tensorflow,Tensorflow Lite,我从中运行了这个示例 但是我在python3中得到的错误是“namererror:name'tempfile'未定义”和python2中的“namererror:global name'tempfile'未定义” NameError Traceback (most recent call last) <ipython-input-21-24c36564faa4> in <module>() 5 o

我从中运行了这个示例

但是我在python3中得到的错误是“namererror:name'tempfile'未定义”和python2中的“namererror:global name'tempfile'未定义”

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
namererror回溯(最近一次调用)
在()
5 out=tf.identity(val,name=“out”)
6将tf.Session()作为SES:
---->7 tflite_model=tf.contrib.lite.toco_convert(sess.graph_def,[img],[out])
8打开(“test.tflite”、“wb”).write(tflite\u模型)
toco_convert中的python3.5/site-packages/tensorflow/contrib/lite/python/lite.py(输入数据、输入张量、输出张量、推断类型、输入格式、输出格式、量化输入统计、下拉控制依赖)
196 data=toco_convert_protos(model.SerializeToString(),
197 toco.SerializeToString(),
-->198输入_data.SerializeToString())
199返回数据
200
toco_convert_protos(model_flags_str、toco_flags_str、input_data_str)中的python3.5/site-packages/tensorflow/contrib/lite/python/lite.py
89返回转换协议(模型标志、输入数据)
90
--->91使用tempfile.NamedTemporaryFile()作为fp_toco\
92 tempfile.NamedTemporaryFile()作为fp_模型\
93 tempfile.NamedTemporaryFile()作为fp_输入\
NameError:未定义名称“tempfile”

如何使其工作?

自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模型converteds_model.TFLite将保存到文件。

我遇到了完全相同的问题您可以在此处跟踪此问题:
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)