简体   繁体   English

Tensorflow TOCO python API

[英]Tensorflow TOCO python API

I'm following the tensorflow for poets (tflite) tutorial here: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-tflite/#3 我在这里关注诗人(tflite)的tensorflow教程: https ://codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-tflite/#3

I'm attempting to convert a customised graph from .pb to tflite using the python TOCO API : https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/python_api.md 我正在尝试使用python TOCO API将自定义图从.pb转换为tflite: https : //github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/python_api.md

The following code loads the retrained_graph.pb file, finds the input and output tensor, then calls toco_convert and writes the .tflite file. 以下代码加载retrained_graph.pb文件,找到输入和输出张量,然后调用toco_convert并写入.tflite文件。

    import tensorflow as tf
     def load_graph(graph_filename):
     with tf.gfile.GFile(graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

     with tf.Graph().as_default() as graph:
        tf.import_graph_def(
          graph_def,
          input_map=None,
          return_elements=None,
          name="prefix",
          op_dict=None,
          producer_op_list=None
         )


    graph = load_graph("retrained_graph.pb")
    x = graph.get_tensor_by_name('prefix/input:0') #input tensor
    y = graph.get_tensor_by_name('prefix/final_result:0') #output tensor


    with tf.Session(graph=graph) as sess:
       tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [x], [y])
       open("test.tflite", "wb").write(tflite_model)

This produces a test.tflite file. 这将生成一个test.tflite文件。 To check to see if it works, I run the label_image script from tf for poets, which produces this error: 要检查它是否有效,我从tf运行诗人的label_image脚本,这会产生此错误:

KeyError: "The name 'import/input' refers to an Operation not in the graph." KeyError:“名称'import / input'指的是图中未包含的操作。”

Looking for solutions, I tried changing input_layer = "input" to input_layer = "Mul", but this only produces the error: 寻找解决方案时,我尝试将input_layer =“ input”更改为input_layer =“ Mul”,但这只会产生错误:

KeyError: "The name 'import/Mul' refers to an Operation not in the graph." KeyError:“名称'import / Mul'指的是图中未包含的操作”。

If there are any suggestions to what I am doing wrong they would be greatly appreciated. 如果对我做错的事情有任何建议,将不胜感激。

您是否尝试过使用summary_graph检查模型的潜在输入/输出节点名称?

According to your code, your input_layer name is "prefix/input" not "input". 根据您的代码,您的input_layer名称是“前缀/输入”而不是“输入”。 Changing to input_layer="prefix/input" should solve your issues. 更改为input_layer="prefix/input"应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tensorflow TocoConverter 给出 toco_from_protos 错误 - Tensorflow TocoConverter gives toco_from_protos error Tensorflow [Toco]将模型转换为优化格式导致ValueError - Tensorflow [Toco] convert Model to optimized format cause ValueError TensorFlow Lite:toco_convert 用于任意大小的输入张量 - TensorFlow Lite: toco_convert for arbitrary sized input tensor 在Ubuntu 16.04上使用bazel从源构建张量流。 错误是--->规则'// tensorflow / contrib / lite / toco:toco'的链接失败(出口1) - Building tensor flow from source using bazel on Ubuntu 16.04. Error is ---> Linking of rule '//tensorflow/contrib/lite/toco:toco' failed (Exit 1) 在运行toco时尝试将TensorFlow模型转换为TensorFlow lite --help给我一个错误 - Trying to convert TensorFlow model to TensorFlow lite, when running toco --help gives me an error 如何获取TOCO tf_convert的冻结Tensorflow模型的input_shape - How do i get the input_shape of a frozen Tensorflow-Model for TOCO tf_convert 为什么tensorflow /模型不属于TensorFlow API? Python模块? - Why isn't tensorflow/models not part of the TensorFlow API? Python module? 使用 TensorFlow C API 的 Python 扩展模块 - Python extension module using TensorFlow C API 在Android gnuroot上安装Tensorflow Python API - Install tensorflow python API on Android gnuroot python中的RESTful API请求[tensorflow服务] - RESTful API request in python [ tensorflow serving ]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM