我下载了一个我在Azure认知服务中训练过的模型的retrained_graph.pb
和retrained_labels.txt
文件。现在我想使用该模型制作一个Android应用程序,为此我必须将其转换为TFLite格式。我使用了toco,我收到以下错误:
ValueError: Invalid tensors 'input' were found.
我基本上遵循本教程并在第4步遇到问题并直接复制粘贴终端代码:qazxsw poi
您可以使用实用程序https://heartbeat.fritz.ai/neural-networks-on-mobile-devices-with-tensorflow-lite-a-tutorial-85b41f53230c,它是tensorflow 1.10(或更高版本)程序包的一部分。
浮动推理的简单用法如下:
tflite_convert
输入和输出 - 我们的张量流图的输入和输出张量
我在这里疯狂猜测,也许你进入了tflite_convert \
--output_file=/tmp/retrained_graph.tflite \
--graph_def_file=/tmp/retrained_graph.pb \
--input_arrays=input \
--output_arrays=output
。这可能不是真的。使用此脚本可以找出冻结推理图的输入和输出数组的名称
input_arrays=input
在我的情况下,他们是:
import tensorflow as tf
gf = tf.GraphDef()
m_file = open('frozen_inference_graph.pb','rb')
gf.ParseFromString(m_file.read())
with open('somefile.txt', 'a') as the_file:
for n in gf.node:
the_file.write(n.name+'\n')
file = open('somefile.txt','r')
data = file.readlines()
print "output name = "
print data[len(data)-1]
print "Input name = "
file.seek ( 0 )
print file.readline()
该错误提示您没有输入正确的
output name: SemanticPredictions
input name: ImageTensor
从--input_arrays
我引用:
“设置input_array和output_array参数并不简单。找到这些值的最简单方法是使用TensorBoard探索图形。”
通过简单地运行此命令,使用Tensorboard也不难
TF Lite Developer Guide
查看TensorBoard
tensorboard --logdir=path/to/log-directory
很可能是因为在重新训练过程中输入和输出张量被重命名。如果这是一个重新训练的inceptionv3图,请尝试使用Mul作为输入张量名称,使用final_result作为输出张量名称。
localhost:6006
如果您使用tflife_convert作为Aleksandr建议的类似调整。
没有bazel你可以尝试以下代码
bazel run --config=opt //tensorflow/contrib/lite/toco:toco -- \
... other options ...
--input_shape=1,299,299,3 \
--input_array=Mul \
--output_array=final_result
如果protobuf是版本3.6.1,则继续安装3.7.0的预发行版本。
pip uninstall tensorflow
pip install tf-nightly
pip show protobuf
我仍然无法使命令行版本工作。它一直返回错误:“tflite_convert:error:-input_arrays和-output_arrays是-graph_def_file所必需的”,尽管提供了两个参数。然而,它适用于Python。
pip uninstall protobuf
pip install protobuf==3.7.0rc2
import tensorflow as tf
graph_def_file = "model.pb"
input_arrays = ["model_inputs"]
output_arrays = ["model_outputs"]
converter = tf.lite.TFLiteConverter.from_frozen_graph(
graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
第一个是input_arrays,姓氏是output_arrays(可能多于一个取决于你的模型输出数量)
我的输出