[英]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.