object detection模型轉換成TensorFlow Lite,在Android應用


環境

tensorflow = 1.12.0
bazel = 0.18.1
ubuntu = 16.04
python = 3.6.2

安裝 bazel (0.18.1)

如果tensorflow是1.12.0,那么必須安裝指定版本0.18.1的bazel,不然會出現很多的錯誤無法解決。

wget https://github.com/bazelbuild/bazel/releases/download/0.18.1/bazel-0.18.1-installer-linux-x86_64.sh chmod +x bazel-0.18.1-installer-linux-x86_64.sh ./bazel-0.18.1-installer-linux-x86_64.sh --user

下載tensorflow工程代碼

git clone https://github.com/tensorflow/tensorflow.git

編譯轉換工具

cd tensorflow/ bazel build tensorflow/python/tools:freeze_graph bazel build tensorflow/contrib/lite/toco:toco

生成tflite_graph.pb文件

cd models/research/object_detection

python export_tflite_ssd_graph.py \
--pipeline_config_path=data/ssd_mobilenet_v1_coco.config \
--trained_checkpoint_prefix=data/training/model.ckpt-28189 \
--output_directory=data/output \
--add_postprocessing_op=true

利用bazel生成tflite文件

bazel-bin/tensorflow/contrib/lite/toco/toco \
--input_file=tflite_graph.pb \
--output_file=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 \ --default_ranges_min=0 \ --default_ranges_max=6 \ --allow_custom_ops

在Android上測試

在下面的目錄中有tensorflow lite的例子,可以替換原來的detect.tflite文件,修改對應的coco_labels_list.txt文件,建議改成不一樣的名稱,修改代碼,不然運行的時候,detect.tflitecoco_labels_list.txt會重新下載,又被覆蓋掉了




 


免責聲明!

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



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