首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >tensorflow lite toco python APl: NameError:“未定义名称'tempfile‘”

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

Stack Overflow用户
提问于 2017-12-05 10:18:55
回答 1查看 1.4K关注 0票数 1

我运行了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)

但是我得到的错误是: python3中的"NameError:名称‘临时文件’未定义“和python2中的"NameError:全局名称‘临时文件’未定义

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

如何让它工作?

EN

回答 1

Stack Overflow用户

发布于 2018-07-28 05:17:32

此问题已从github 1.7 (请参阅github bug)修复。

我刚刚在最新的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将保存到文件中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47645056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档