快速导航
ubuntu16.04中tensorflow转换为tflite模型的方式
- 压缩模型需要从源代码编译,需要安装bazel
- 官网的安装方式:https://docs.bazel.build/versions/master/install-ubuntu.html
- 这里采用Installing using binary installer方式,即使用sh文件安装
-
- sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python
-
- wget https://github.com/bazelbuild/bazel/releases/download/0.21.0/bazel-0.21.0-installer-linux-x86_64.sh
-
- sudo chmod +x bazel-0.21.0-installer-linux-x86_64.sh
-
- ./bazel-0.21.0-installer-linux-x86_64.sh --user
-
- 设置环境变量:
- sudo gedit ~/.bashrc,最后一行加上
-
- export PATH="$PATH:$HOME/bin"
-
- 然后使之生效:source ~/.bashrc
-
- 下载最新的TensorFlow库
- git clone https://github.com/tensorflow/tensorflow.git
- cd ./tensorflow
- 编译graph_transforms模块,需要等待一段时间,编译很耗费CPU资源和时间
- bazel build tensorflow/lite/toco:toco
-
- 从链接中下载预训练模型,然后使用object detection的官方训练代码训练网络,http://download.tensorflow.org/models/object_detection/ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz
- 使用ssdlite训练生成的模型,使用research/object_detection/export_tflite_ssd_graph.py转换为tflite_graph.pb、tflite_graph.pbtxt。
-
- 然后在tensorflow/tensorflow文件夹中使用命令,将其转换为detect.tflite文件。
- 转换为UINT8的方式会压缩模型
- bazel run tensorflow/lite/toco:toco -- --input_file=/home/shzy/disks/project/fingerdtpaper/models/data/stept_finger/model/ssdlite/tflite_graph.pb --output_file=/home/shzy/detect.tflite --input_shapes=1,300,300,3 --input_arrays=normalized_input_image_tensor --output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' --inference_type=QUANTIZED_UINT8 --mean_values=128 --std_values=128 --change_concat_input_ranges=false --allow_custom_ops --default_ranges_min=0 --default_ranges_max=255
-
- 转换为FLOAT的方式不会压缩模型
- bazel run tensorflow/lite/toco:toco -- --input_file=/home/shzy/disks/project/fingerdtpaper/models/data/stept_finger/model/ssdlite/tflite_graph.pb --output_file=/home/shzy/detect.tflite --input_shapes=1,300,300,3 --input_arrays=normalized_input_image_tensor --output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' --inference_type=FLOAT --mean_values=128 --std_values=128 --change_concat_input_ranges=false --allow_custom_ops --default_ranges_min=0 --default_ranges_max=255
-
- 各种选项的解释:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/convert/cmdline_reference.md
-
- 参考的内容:https://medium.com/tensorflow/training-and-serving-a-realtime-mobile-object-detector-in-30-minutes-with-cloud-tpus-b78971cf1193
- https://blog.csdn.net/aslily1234/article/details/84840885