减小 .tflite 模型大小 [英] Reducing .tflite model size
问题描述
我看到的任何动物园 .tflite 模型的大小都不超过 3MB.在 edgetpu 上,它们运行良好.但是,当我训练自己的对象检测模型时,.pb 文件是 60MB,而 .tflite 也很大,有 20MB!它也被量化如下.最终结果是在 edgetpu object_detection 模型上出现分段错误.是什么导致这个文件这么大?输入模型的未调整大小的图像是否会导致模型变大(某些照片为 4096×2160 且未调整大小)?
Any of the zoo .tflite models I see are no more than 3MB in size. On an edgetpu they run fine. However, when I train my own object detection model the .pb file is 60MB and the .tflite is also huge at 20MB! It's also quantized as per below. The end result is segmentation faults on an edgetpu object_detection model. What's causing this file to be so large? Do non-resized images being fed into the model cause the model to be large (some photos were 4096×2160 and not resized)?
来自object_detection
From object_detection
训练模型
python train.py \
--logtostderr \
--train_dir=training \
--pipeline_config_path=training/ssd_mobilenet_v1_coco.config
冻结图形 - 创建 60MB .pb 文件
Freeze the graph - creates 60MB .pb file
python export_tflite_ssd_graph.py \
--pipeline_config_path=training/ssd_mobilenet_v2_coco.config \
--trained_checkpoint_prefix=training/model.ckpt-2020 \
--output_directory=inference_graph \
--add_postprocessing_op=true
转换为 .tflite - 创建 20MB .tflite 文件
Convert to .tflite - creates 20MB .tflite file
tflite_convert
--graph_def_file=inference_graph/tflite_graph.pb \
--output_file=inference_graph/detect.tflite \
--inference_type=QUANTIZED_UINT8 \
--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 \
--mean_values=128 \
--std_dev_values=127 \
--allow_custom_ops \
--default_ranges_min=0 \
--default_ranges_max=6
在这个阶段,.tflite 文件被推送到谷歌珊瑚 edgetpu,并在连接到 TPU 的 USB 摄像头上试用模型.
At this stage the .tflite file is pushed to the google coral edgetpu and the model is trialed on a USB camera attached to the TPU.
export DISPLAY=:0 && edgetpu_detect \
--source /dev/video1:YUY2:1280x720:20/1 \
--model ${DEMO_FILES}/detect.tflite
最终结果是分段错误.
INFO: Initialized TensorFlow Lite runtime.
glvideomixer name=mixer background=black ! glimagesink sync=False name=glsink qos=False
v4l2src device=/dev/video1 ! video/x-raw,height=720,framerate=20/1,format=YUY2,width=1280 ! glupload ! tee name=t
t. ! glupload ! queue ! mixer.
overlaysrc name=overlay ! video/x-raw,height=720,width=1280,format=BGRA ! glupload ! queue max-size-buffers=1 ! mixer.
t. ! queue max-size-buffers=1 leaky=downstream ! glfilterbin filter=glcolorscale ! video/x-raw,height=168,width=300,format=RGBA ! videoconvert ! video/x-raw,height=168,width=300,format=RGB ! videobox autocrop=True ! video/x-raw,height=300,width=300 ! appsink max-buffers=1 sync=False emit-signals=True drop=True name=appsink
Segmentation fault
推荐答案
这里的问题可能是由于每个步骤都有 2 个不同的配置文件:
The issue here may be due to the fact that you have 2 different config files for each step:
python train.py \
...
--pipeline_config_path=training/ssd_mobilenet_v1_coco.config
python export_tflite_ssd_graph.py \
--pipeline_config_path=training/ssd_mobilenet_v2_coco.config \
...
这是故意的吗?此外,看起来您在训练后立即部署了模型而没有编译它.有关 edgetpu_compiler 的更多信息,请参阅此文档:https://coral.withgoogle.com/docs/edgetpu/compiler/
Was this meant to be? Also, it looks like you deployed the model immediately after training without compiling it. Please refer to this doc for more info on the edgetpu_compiler: https://coral.withgoogle.com/docs/edgetpu/compiler/
AFAIK,只要满足页面上列出的所有要求,20MB 的模型应该可以正常运行:
AFAIK, a 20MB model should run just fine as long as it meets all requirements listed on the page:
- 张量参数被量化(8 位定点数).
- 张量大小在编译时保持不变(无动态大小).
- 模型参数(例如偏置张量)在编译时保持不变.
- 张量是 1 维、2 维或 3 维的.如果张量的维度超过 3 个,那么只有最里面的 3 个维度的大小可能大于 1.
- 该模型仅使用 Edge TPU 支持的操作.列出的操作在这里:https://coral.withgoogle.com/docs/edgetpu/models-intro/#supported-operations
您的整个管道应该是:
1) 训练模型
2) 转换为 tflite
2) Convert to tflite
3) 为 EdgeTPU 编译(实际将工作委托给 TPU 的步骤)
3) Compiled for EdgeTPU (the step that actually delegates the work onto the TPU)
希望这会有所帮助.
这篇关于减小 .tflite 模型大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!