如何在移动设备上训练和部署自定义目标检测模型

发布人:技术推广工程师 khanh leviet,代表 tensorflow lite 团队
在今年的 google i/o 大会上,我们很高兴地宣布推出了多项产品更新。这些更新可以帮助简化移动设备上,目标检测模型的训练和部署的过程:
设备端 ml 学习路径:关于如何在移动设备上,训练和部署自定义目标检测模型的分步教程,无需机器学习专业知识。
设备端 ml 学习路径
https://developers.google.com/learn/pathways/going-further-object-detection
efficientdet-lite:针对移动设备优化的精尖目标检测模型架构。
efficientdet-lite
https://hub.tensorflow.google.cn/s?deployment-format=lite&q=efficientdet-lite
用于目标检测的 tensorflow lite model maker:只需几行代码即可训练自定义模型
目标检测
http://tensorflow.google.cn/lite/tutorials/model_maker_object_detection
tensorflow lite metadata writer api:简化元数据创建以生成与 tflite task library 兼容的自定义目标检测模型。
metadata writer api
http://tensorflow.google.cn/lite/convert/metadata_writer_tutorial
tflite task library
http://tensorflow.google.cn/lite/inference_with_metadata/task_library/object_detector
尽管目标检测是非常常见的 ml 用例,但可能也是最难执行的用例之一。我们一直致力于为您简化相关操作。在这篇文章中,我们将向您介绍如何利用 tensorflow lite 的最新产品、通过使用您自己的训练数据构建最先进的移动端目标检测。
设备端 ml 学习路径:
在 12 分钟内了解如何训练和部署自定义 tensorflow lite 目标检测模型
有了 tensorflow lite,训练自定义目标检测模型并将其部署到 android 应用变得非常容易。我们已经发布了学习路径,可指导您按步骤顺利完成相关操作。
您可以通过视频了解构建自定义目标检测器的具体步骤:
1. 准备训练数据。
2. 使用 tensorflow lite model maker 训练自定义目标检测模型。
3. 使用 tensorflow lite task library 在您的移动应用上部署模型。
我们还在 github 上提供了带有源代码的 codelab,供您自行试运行代码。请尝试一下,并告诉我们您的反馈!
codelab
https://codelabs.developers.google.com/tflite-object-detection-android
告诉我们
https://github.com/googlecodelabs/odml-pathways/issues
efficientdet-lite:
移动端设备目标检测最优模型架构之一
在移动设备上运行机器学习模型意味着我们始终需要在模型精度、推理速度和模型大小之间进行权衡。最优的移动端模型不仅需要更加精准,还需要更快地运行速度和更小的体积。我们采用了 efficientdet 论文中发布的神经架构搜索技术,并优化了在移动设备上运行的模型架构,最终推出了名为 efficientdet-lite 的新型移动设备目标检测模型系列。
efficientdet
https://arxiv.org/abs/1911.09070
efficientdet-lite 有 5 个不同的版本:从 lite0 到 lite4。较小的版本运行速度更快,而较大的版本精度更高。您可以先用多个版本的 efficientnet-lite 进行试验,然后选择最适合您用例的版本。
* 整型量化模型的大小
** 在 pixel 4 上使用 4 个 cpu 线程测量得出的延迟时间
*** 平均精度是 coco 2017 验证数据集上的 map(平均精度均值)
我们已将在 coco 数据集上训练的 efficientdet-lite 模型发布到 tensorflow hub。您还可以使用自己的训练数据,通过 tensorflow lite model maker 训练 efficientdet-lite 自定义模型。
tensorflow hub
https://hub.tensorflow.google.cn/s?q=efficientdet/lite
tensorflow lite model maker:几行代码使用迁移学习训练自定义目标检测模型
tensorflow lite model maker 是 python 库,可显著简化使用自定义数据集训练机器学习模型的过程。该库利用迁移学习,仅使用少量图像即可训练高质量模型。
tensorflow lite model maker
http://tensorflow.google.cn/lite/guide/model_maker
model maker 接受 pascal voc 格式和 cloud automl 的 csv 格式的数据集。和使用 labelimg 或 makesense.ai 等开源 gui 工具创建自己的数据集一样,每位开发者都可以创建用于 model maker 的训练数据,且无需编写任何代码。
csv
https://cloud.google.com/vision/automl/object-detection/docs/csv-format
labelimg
https://github.com/tzutalin/labelimg
makesense.ai
https://github.com/skalskip/make-sense
获得训练数据后,您就可以开始训练自定义的 tensorflow lite 目标检测模型了。
# step 1: choose the model architecture
spec = model_spec.get(‘efficientdet_lite2’)
# step 2: load your training data
train_data, validation_data, test_data = object_detector.dataloader.from_csv(‘gs://cloud-ml-data/img/openimage/csv/salads_ml_use.csv’)
# step 3: train a custom object detector
model = object_detector.create(train_data, model_spec=spec, validation_data=validation_data)
# step 4: export the model in the tensorflow lite format
model.export(export_dir=‘。’)
# step 5: evaluate the tensorflow lite model
model.evaluate_tflite(‘model.tflite’, test_data)
查看此 codelab 了解详情。
codelab
http://tensorflow.google.cn/lite/tutorials/model_maker_object_detection
tensorflow lite task library:使用几行代码在移动设备上部署目标检测模型
tensorflow lite task library 是跨平台库,它简化了 tensorflow lite 模型在移动设备上的部署。使用 tensorflow lite model maker 训练的自定义目标检测模型只需使用几行 kotlin 代码即可部署到 android 应用:
// step 1: load the tensorflow lite model
val detector = objectdetector.createfromfile(context, “model.tflite”)
// step 2: convert the input bitmap into a tensorflow lite‘s tensorimage object
val image = tensorimage.frombitmap(bitmap)
// step 3: feed given image to the model and get the detection result
val results = detector.detect(image)
您可参阅一下文档,了解有关 task library 中,包括如何配置最小检测阈值或最大检测对象数量等更多自定义选项信息。
文档
https://tensorflow.google.cn/lite/inference_with_metadata/task_library/object_detector
tensorflow lite metadata writer api:
用于简化使用 tensorflow object detection api 训练的自定义模型的部署
task library 依赖 tensorflow lite 模型中捆绑的模型元数据来执行必要的预处理和后处理逻辑,以便使用该模型来运行推理。这其中包括如何将输入图像归一化,或如何将类 id 映射到可人工读取的标签。因为使用 model maker 训练的模型会默认包含此类元数据,所以它能够与 task library 兼容。但是,如果您使用除 model maker 以外的训练流水线,来训练 tensorflow lite 目标检测,则可以使用 tensorflow lite metadata writer api 来添加元数据。
模型元数据
https://tensorflow.google.cn/lite/convert/metadata
例如,如果您使用 tensorflow object detection api 来训练模型,则可以使用以下 python 代码将元数据添加到 tensorflow lite 模型:
label_path = ’label_map.txt‘
model_path = “ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/model.tflite”
save_to_path = “ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/model_with_metadata.tflite”# step 1: specify the preprocessing parameters and label file
writer = object_detector.metadatawriter.create_for_inference(
writer_utils.load_file(model_path), input_norm_mean=[0],
input_norm_std=[255], label_file_paths=[label_path])
# step 2: export the model with metadata
writer_utils.save_file(writer.populate(), save_to_path)
在示例中,我们指定归一化参数 (input_norm_mean=[0], input_norm_std=[255]) ,以便将输入图像归一化到 [0..1] 范围内。您需要指定与模型训练期间使用的预处理逻辑中相同的归一化参数。
归一化
http://tensorflow.google.cn/lite/convert/metadata#normalization_and_quantization_parameters
参阅此 colab,获取有关如何将使用 tensorflow object detection api 训练的模型转换为 tensorflow lite 模型并添加元数据的完整教程。
colab
https://colab.research.google.com/github/tensorflow/models/blob/master/research/object_detection/colab_tutorials/convert_odt_model_to_tflite.ipynb
未来计划
我们的目标是让每位开发者(无论是否具备机器学习专业知识)都能更轻松使用机器学习。目前,我们正在与 tf model garden 团队合作,希望推出更多适用于 model maker 的目标检测模型架构。
未来,我们还将继续与 google 研究人员合作,通过 model maker 提供更多面向未来的更优秀的目标检测模型,缩短从前沿研究到投入实际应用并且供所有开发者使用的整个流程。敬请关注更多动态!


美国或封锁半导体出口到中国 受伤的不止是中国企业
电路设计的基本流程
温湿度传感器应用在智慧稻草人中 智能稻草人
FA5310应用电路图
易图通EHMS高精度地图以地图云服务赋能仿真测试
如何在移动设备上训练和部署自定义目标检测模型
节能变压器如何保养
剃须刀充电器原理与电路解剖
光学膜片之棱镜膜
荣耀9什么时候上市?荣耀9最新消息:真的配置强悍,价格还便宜嘛
小米6工程机曝光 搭载高通骁龙835
一文解析电路反馈框图
北航在纳米柔性器件先进制造方面取得重大进展
ST展示安全微控制器和射频存储器解决方案
台积电做出涨价决定的原因分析
长安欧尚X7 Geeker版:搭载车内外双摄人脸智控系统
Python基础教程
瑞萨科技北京后道工序厂完成扩建
微型机器人可以给我们带来什么好处
电磁操动安排的断路器操控回路