tensorflow lite 之生成 tflite 模型文件


下載最新的的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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM