[TF Lite] Build Training Platform for TensorFlow Lite Model

Resource


TF Lite 资源

TensorFlow Lite Object Detection on Android and Raspberry Pi

Relevant Github: https://github.com/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi#step-1-train-quantized-ssd-mobilenet-model-and-export-frozen-tensorflow-lite-graph

Intro to Machine Learning on Android — How to convert a custom model to TensorFlow Lite

 

一、简单的例子

Ref: Converter command line examples

注意,这里不是 ssd_mobilenet.

curl https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_0.1_224_frozen.tgz \
  | tar xzv -C /tmp
tflite_convert \
  --output_file=/tmp/foo.tflite \
  --graph_def_file=/tmp/mobilenet_v1_1.0_224/frozen_graph.pb \
  --input_arrays=input \
  --output_arrays=MobilenetV1/Predictions/Reshape_1 
ModelMillion MACsMillion ParametersTop-1 AccuracyTop-5 Accuracy
MobileNet_v1_1.0_224 569 4.24 70.9 89.9
MobileNet_v1_1.0_224_quant 569 4.24 70.1 88.9

 

 

二、SSD 版本

Hosted models: coco_ssd_mobilenet_v1_1.0_quant_2018_06_29

Ref: Training and serving a realtime mobile object detector in 30 minutes with Cloud TPUs

Ref: https://ai.googleblog.com/2018/07/accelerated-training-and-inference-with.html

A novel SSD-based architecture called the Pooling Pyramid Network (PPN) whose model size is >3x smaller than that of SSD MobileNet v1 with minimal loss in accuracy.

未来可以考虑,前期非必须。

Note that in addition to training an object detection model in the cloud, you can alternatively run training on your own hardware or in Colab.

云端,本地都可以。

 

  • Setting up your environment

/* more details, please check the link */

 

  • Training a quantized model with Cloud TPUs on Cloud ML Engine

The output of a machine learning model is a binary file containing the trained weights of our model — these files are often quite large, but since we’ll be serving this model directly on a mobile device we’ll need to make it as small as possible.

 

  • Installing Bazel (.pb --> .tflite)

Goto: https://github.com/bazelbuild/bazel/releases/tag/0.24.1

复制代码
bazel run -c opt tensorflow/contrib/lite/toco:toco -- \
--input_file=$OUTPUT_DIR/tflite_graph.pb \
--output_file=$OUTPUT_DIR/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 
复制代码

27.6M 压缩到 6.8M,效果不错。

复制代码
(base) jeffrey@unsw-ThinkPad-T490:ssd_mobilenet_v1_quantized_300x300_coco14_sync_2018_07_18$ ll
total 139512
drwxrwxrwx 2 jeffrey jeffrey     4096 Jan 24 00:52 ./
drwx------ 7 jeffrey jeffrey     4096 Jan 23 23:23 ../
-rw-r--r-- 1 jeffrey jeffrey  6899008 Jan 24 00:56 detect.tflite
-rw-r----- 1 jeffrey jeffrey 27381492 Jul 19  2018 model.ckpt.data-00000-of-00001
-rw-r----- 1 jeffrey jeffrey    14948 Jul 19  2018 model.ckpt.index
-rw-r----- 1 jeffrey jeffrey  2229805 Jul 19  2018 model.ckpt.meta
-rw-r----- 1 jeffrey jeffrey     4469 Jul  8  2018 pipeline.config
-rwxrwxrwx 1 jeffrey jeffrey 27692743 Jul 18  2018 tflite_graph.pb*
复制代码

 

  • OpenCV DNN

Ref: TensorFlow Object Detection API

ModelVersion  
MobileNet-SSD v1 2017_11_17 weights config
MobileNet-SSD v1 PPN 2018_07_03 weights config
MobileNet-SSD v2 2018_03_29 weights config
Inception-SSD v2 2017_11_17 weights config
Faster-RCNN Inception v2 2018_01_28 weights config
Faster-RCNN ResNet-50 2018_01_28 weights config
Mask-RCNN Inception v2 2018_01_28 weights config

 

 

三、自定义数据集训练

如何自定义训练模型:coco_ssd_mobilenet_v1_1.0_quant_2018_06_29 

从中找线索: Training and serving a realtime mobile object detector in 30 minutes with Cloud TPUs

對應的中文版:TensorFlow:使用Cloud TPU在30分钟内训练出实时移动对象检测器

 

  • 何爲Quant

Goto: tensorflow模型权重量化(weight quantization)实践

需要解決:ssd_mobilenet_v1_coco.pb --> ssd_mobile_v1_coco_quantized.pb 的過程。

之後通過bazel依次變爲對應的tflite格式。

From: https://android.googlesource.com/platform/test/mlts/models/+/47a6811e7c7d4ae22e50665a5fa23f9d684ddd8c/README.txt

- ssd_mobilenet_v1_coco_float.tflite
Float version of MobileNet SSD tensorflow model based on:
"Speed/accuracy trade-offs for modern convolutional object detectors."
https://arxiv.org/abs/1611.10012
Apache License, Version 2.0
 
Generated from
http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2018_01_28.tar.gz
on Sep 24 2018.
See also: https://github.com/tensorflow/models/tree/master/research/object_detection
Golden output generated with ToT tflite (Linux, x86_64 CPU).
 
 
- ssd_mobilenet_v1_coco_quantized.tflite
8bit quantized MobileNet SSD tensorflow lite model based on:
"Speed/accuracy trade-offs for modern convolutional object detectors."
https://arxiv.org/abs/1611.10012
Apache License, Version 2.0
 
Generated from
http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_quantized_300x300_coco14_sync_2018_07_18.tar.gz
on Sep 19 2018.
See also: https://github.com/tensorflow/models/tree/master/research/object_detection
Golden output generated with ToT tflite (Linux, CPU).

 

  • Let's do it!

Goto: [TF Lite] How to convert a custom model to TensorFlow Lite

 

 

 

 

TensorFlow-GPU


卸载 CUDA

复制代码
    ===========
    = Summary =
    ===========

    Driver:   Not Selected
    Toolkit:  Installed in /usr/local/cuda-10.2/
    Samples:  Installed in /home/jeffrey/

    Please make sure that
     -   PATH includes /usr/local/cuda-10.2/bin
     -   LD_LIBRARY_PATH includes /usr/local/cuda-10.2/lib64, or, add /usr/local/cuda-10.2/lib64 to /etc/ld.so.conf and run ldconfig as root

    To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-10.2/bin

    Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-10.2/doc/pdf for detailed information on setting up CUDA.
    ***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 440.00 is required for CUDA 10.2 functionality to work.
    To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
        sudo <CudaInstaller>.run --silent --driver

    Logfile is /var/log/cuda-installer.log
复制代码

 

 

Tensorflow-gpu-1.14.0 安装

Which TensorFlow and CUDA version combinations are compatible?

 

gcc-6, g++-6 are required.

 

去掉警告:FutureWarning: Deprecated numpy API calls in tf.python.framework.dtypes #30427

FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.

$ pip install "numpy<1.17"

 

 

开始训练 

详见 :[AI] 深度网络 - DNN

训练脚本执行日志私人参考:

  

End.

posted @   郝壹贰叁  阅读(712)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示