我在这里遵循tensorflow for poets (tflite)教程:https://codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-tflite/#3
我正在尝试使用python TOCO API:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/python_api.md将定制的图形从.pb转换为tflite
下面的代码加载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)这将生成一个test.tflite文件。为了检查它是否正常工作,我从poets的tf运行label_image脚本,这会产生以下错误:
KeyError:“名称'import/input‘指的是不在图形中的操作。”
为了寻找解决方案,我尝试将input_layer = "input“更改为input_layer = "Mul",但这只会产生以下错误:
KeyError:“名称'import/Mul‘引用不在图形中的操作。”
如果对我的错误之处有任何建议,我将不胜感激。
发布于 2018-05-01 09:51:04
您是否尝试过使用summarize_graph检查模型的潜在输入/输出节点名称?
发布于 2018-05-01 12:10:46
根据您的代码,您的输入名称是“input_layer /input”,而不是"input“。改用input_layer="prefix/input"应该可以解决你的问题。
https://stackoverflow.com/questions/50109656
复制相似问题