tensorflow - 如何获取一个小的 tflite 文件
问题描述
我正在尝试从 Tensorflow 对象检测模型中获取最小的 tflite 文件,以用于微控制器(带有 4MB 闪存的 ESP32)(指南)。我正在做的只是遵循本指南,以便训练模型仅检测一个类别(图像中的特定对象)。
我已经训练了很多模型(在主机上),但最终输出(由 tflite 文件获得的 .cc 文件)太大(>4MB,当由 xtensa 编译时)无法部署在微控制器中(最大4MB 闪存)。
我还使用ssd_mobilenet_v3_small_coco配置进行了测试,它创建了 1MB 的 tflite,这非常好,我可以在 ESP32 上部署它,但我不能使用 v3,因为 C++ 的 Tensorflow Lite 没有 MUL 操作,从 ESP32 的日志中:
找不到内置操作码“MUL”版本“1”的操作
所以,如果我理解得很好,我认为我只能使用 v2 移动网络,因为到目前为止,只有有限的操作子集可以与 Tensorflow lite 一起使用(从这里开始)。
我训练、转换和部署模型的步骤是:
火车:
python train.py --logtostderr --train_dir=training/
--pipeline_config_path=training/my_extractor.config
转换为 TFLITE:
python export_tflite_ssd_graph.py
--pipeline_config_path="training/pipeline.config"
--trained_checkpoint_prefix="training/model.ckpt-xxx"
--output_directory="tflite_output"
--add_postprocessing_op=true
然后
tflite_convert --graph_def_file=tflite_output/tflite_graph.pb
--output_file=tflite_output/detect.tflite --output_format=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_dev_values=127
--change_concat_input_ranges=false
--allow_custom_ops --default_ranges_min=0
--default_ranges_max=6 --quantize_weights
最后(从 C++ 访问模型):
xxd -i model.tflite > model_data.cc
最后的问题是:
- 如何使用 mobilenet v2 拥有最小尺寸的对象检测模型(如果严格要求卡在 v2 上)?
- 如果我坚持使用 v3 mobilenet,我该如何实现 MUL 操作?
- 我在这个过程中遗漏了什么吗?
如果你能给我一些建议,我真的很感激。
解决方案
不确定您是否已经解决了您的问题。但是从最新的 tensorflow lite 源代码来看,似乎MUL
已经添加了支持。
推荐阅读
- python - 带有 Pandas .pipe() 的 Pandas 类
- google-sheets - 谷歌表格查询不会带来第三级过滤条件
- github - 如何使用环境变量或类似的东西在 pyproject.toml 中指定 github 访问令牌?
- java - 仅将我的应用程序中的数据更新给活动用户
- javascript - 如何在引导下拉菜单中定位插入符号
- javascript - 如何不清除输入字段onClick
- javascript - 后台脚本中的 chrome.webRequest 侦听器仅在打开扩展程序时触发?
- python - 如何为张量的每个切片运行迭代 2D 卷积?
- swift - SwiftUI 动画错误?
- python - 无法将 Tensorflow ssd_mobilenet_v1_fpn_coco 模型冻结到冻结 (.pb) 文件中