Nae*_*wad 4 python tensorflow tensorflow-lite
我是Tensorflow的新手。训练后我将模型保存为pb文件,我想使用tensorflow mobile,并且使用TFLITE文件很重要。问题是在谷歌搜索转换器后发现的大多数示例都是在终端或cmd上命令的。能否请您分享一个使用python代码转换为tflite文件的示例?
谢谢
您可以直接在python中直接转换为tflite。您必须冻结图形并使用toco_convert。就像在命令行情况下一样,它需要在调用API之前确定输入和输出的名称和形状。
从文档复制,其中“冻结”(无变量)图被定义为代码的一部分:
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)
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,由于没有变量,因此没有冻结图步骤。如果您有变量并且在不冻结图的情况下运行toco,即先将这些变量转换为常量,那么toco会抱怨!
然后,您不需要会话。您可以直接调用toco API:
path_to_frozen_graphdef_pb = '...'
input_tensors = [...]
output_tensors = [...]
frozen_graph_def = tf.GraphDef()
with open(path_to_frozen_graphdef_pb, 'rb') as f:
frozen_graph_def.ParseFromString(f.read())
tflite_model = tf.contrib.lite.toco_convert(frozen_graph_def, input_tensors, output_tensors)
Run Code Online (Sandbox Code Playgroud)
然后,您必须先加载会话并先冻结图,然后再调用toco:
path_to_graphdef_pb = '...'
g = tf.GraphDef()
with open(path_to_graphdef_pb, 'rb') as f:
g.ParseFromString(f.read())
output_node_names = ["..."]
input_tensors = [..]
output_tensors = [...]
with tf.Session(graph=g) as sess:
frozen_graph_def = tf.graph_util.convert_variables_to_constants(
sess, sess.graph_def, output_node_names)
# Note here we are passing frozen_graph_def obtained in the previous step to toco.
tflite_model = tf.contrib.lite.toco_convert(frozen_graph_def, input_tensors, output_tensors)
Run Code Online (Sandbox Code Playgroud)
如果未定义图形,则可能发生这种情况。您从某处下载了图形或使用了高级API(例如tf.estimators)将图形隐藏起来。在这种情况下,您需要在调用toco之前加载图形并四处摸索以找出输入和输出。看到我对这个问题的回答。
按照这个TF 示例,您可以在运行 retrain.py 脚本之前传递“--Saved_model_dir”参数以将 saved_model.pb 和 variables 文件夹导出到某个目录(不存在的目录):
python retrain.py ...... --saved_model_dir /home/../export
为了将您的模型转换为 tflite,您需要使用以下行:
convert_saved_model.convert(saved_model_dir='/home/.../export',output_arrays="final_result",output_tflite='/home/.../export/graph.tflite')
Run Code Online (Sandbox Code Playgroud)
注意:需要导入convert_saved_model:
从 tensorflow.contrib.lite.python 导入 convert_saved_model
请记住,您可以通过两种方式转换为 tflite:
但最简单的方法是导出带有变量的 saved_model.pb,以防您想避免使用像 Bazel 这样的构建工具。
这对我有用:(SSD_InceptionV2 模型)
Run Code Online (Sandbox Code Playgroud)python /tensorflow/models/research/object_detection/export_tflite_ssd_graph.py --pipeline_config_path annotations/ssd_inception_v2_coco.config --trained_checkpoint_prefix trained-inference-graphs/inference_graph_v7.pb/model.ckpt --output_directory trained-inference-graphs/inference_graph_v7.pb/tflite --max_detections 3
Run Code Online (Sandbox Code Playgroud)tflite_convert --output_file=test.tflite --graph_def_file=tflite_graph.pb --input_arrays=normalized_input_image_tensor --output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' --input_shape=1,300,300,3 --allow_custom_ops
现在输入/输出我不是 100 确定如何得到这个,但这段代码以前对我有帮助:
import tensorflow as tf
frozen='/tensorflow/mobilenets/mobilenet_v1_1.0_224.pb'
gf = tf.GraphDef()
gf.ParseFromString(open(frozen,'rb').read())
[n.name + '=>' + n.op for n in gf.node if n.op in ( 'Softmax','Placeholder')]
[n.name + '=>' + n.op for n in gf.node if n.op in ( 'Softmax','Mul')]
Run Code Online (Sandbox Code Playgroud)