下载最新的的tensorflow源码。
1.配置 tflite 文件转换所需环境
安装 bazel 编译工具
https://docs.bazel.build/versions/master/install.html
bazel build 出现问题:
图片来自https://github.com/tensorflow/tensorflow/issues/29053
解决方法:
在WORKSPACE中加入:
图片来自https://github.com/bazelbuild/rules_docker
利用 bazel 编译转换 tflite 所需的工具:
cd tensorflow-master/
bazel build tensorflow/python/tools:freeze_graph
bazel build tensorflow/lite/toco:toco
bazel build tensorflow/tools/graph_transforms:summarize_graph
2 tensorflow 模型固化,将 tensorflow 模型和计算图上变量的之合二为一。
利用 freeze_graph 工具,生成 tflite_graph.pb 文件(frozen的GraphDef文件)
cd /models-master/research/
python object_detection/export_tflite_ssd_graph.py
--pipeline_config_path=pipeline.config 路径
--trained_checkpoint_prefix=model.ckpt 保存的模型文件路径
--output_directory=model_graph 生成文件的路径
--add_postprocessing_op=true
注:生成文件夹 model_graph: 存放着 tflite_graph.pb 和 tflite_graph.pbtxt 文件
3 通过 tf 的 summarize_graph 函数可视化冻结图节点的输入输出信息
cd /tensorflow-master/
bazel-bin/tensorflow/tools/graph_transforms/summarize_graph
--in_graph=tflite_graph.pb 文件frozen.pb的路径
4 通过toco工具生成tflite文件
cd /tensorfolw_master/tensorflow/lite/toco
浮点型文件:
toco --graph_def_file=$path/tflite_graph.pb 文件frozen.pb的路径
--input_format=TENSORFLOW_GRAPHDEF
--output_file=model.tflite 存放生成文件的路径
--inference_type=FLOAT
--input_type=FLOAT
--input_arrays=normalized_input_image_tensor #根据第3步的结果修改
--input_shapes=1,300,300,3 #根据第3步的结果修改
--output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1', 'TFLite_Detection_PostProcess:2', 'TFLite_Detection_PostProcess:3' #根据第3步的结果修改
--allow_custom_ops
量化型文件:
toco --graph_def_file=$path/tflite_graph.pb 文件frozen.pb的路径
--input_format=TENSORFLOW_GRAPHDEF
--output_file=model_quantized.tflite 存放生成文件的路径
--inference_type=QUANTIZED_UINT8
--input_shapes=1,300,300,3 #根据第3步的结果修改
--input_arrays=normalized_input_image_tensor #根据第3步的结果修改
--output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1', 'TFLite_Detection_PostProcess:2', 'TFLite_Detection_PostProcess:3' #根据第3步的结果修改
--mean_values=128
--std_dev_values=127
--default_ranges_min=0
--default_ranges_max=6
--allow_custom_ops