【问题标题】:Reducing .tflite model size减小 .tflite 模型大小
【发布时间】:2019-10-25 15:40:26
【问题描述】:

我看到的任何 zoo .tflite 模型的大小都不超过 3MB。在 edgetpu 上,它们运行良好。但是,当我训练自己的对象检测模型时,.pb 文件为 60MB,而 .tflite 文件也很大,为 20MB!它也被量化如下。最终结果是 edgetpu object_detection 模型上的分段错误。是什么导致这个文件这么大?将未调整大小的图像输入模型是否会导致模型变大(某些照片为 4096×2160 且未调整大小)?

来自 object_detection

训练模型

python train.py \
--logtostderr \
--train_dir=training \
--pipeline_config_path=training/ssd_mobilenet_v1_coco.config

冻结图形 - 创建 60MB .pb 文件

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 文件

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 摄像头上进行试验。

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

【问题讨论】:

    标签: python tensorflow tensorflow-lite google-coral


    【解决方案1】:

    这里的问题可能是因为每个步骤都有 2 个不同的配置文件:

    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/

    AFAIK,只要满足页面上列出的所有要求,20MB 的模型应该可以正常运行:

    • 张量参数已量化(8 位定点数)。
    • 张量大小在编译时保持不变(无动态大小)。
    • 模型参数(例如偏置张量)在编译时保持不变。
    • 张量是 1 维、2 维或 3 维的。如果张量的维度超过 3 个,则只有最内层的 3 个维度的大小可能大于 1。
    • 该模型仅使用 Edge TPU 支持的操作。 列出的操作在这里: https://coral.withgoogle.com/docs/edgetpu/models-intro/#supported-operations

    你的整个管道应该是:

    1) 训练模型

    2) 转换为 tflite

    3) 为 EdgeTPU 编译(实际上将工作委托给 TPU 的步骤)

    希望这会有所帮助。

    【讨论】:

    • 天哪,那是一个真正的 derrrrp 时刻。明天我会试一试,看看它是如何编译的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2021-09-07
    • 2017-12-22
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    相关资源
    最近更新 更多