我正在尝试在 Android 上运行 tflite 模型来进行对象检测。对于相同的,
- 我已经用我的图像集成功训练了模型,如下所示:
(一) 培训:
!python3 object_detection/model_main.py \
--pipeline_config_path=/content/drive/My\ Drive/Detecto\ Tutorial/models/research/object_detection/samples/configs/ssd_mobilenet_v2_coco.config \
--model_dir=training/
(修改配置文件以指向提到我的特定 TFrecords 的位置)
(b) 导出推理图
!python /content/drive/'My Drive'/'Detecto Tutorial'/models/research/object_detection/export_inference_graph.py \
--input_type=image_tensor \
--pipeline_config_path=/content/drive/My\ Drive/Detecto\ Tutorial/models/research/object_detection/samples/configs/ssd_mobilenet_v2_coco.config \
--output_directory={output_directory} \
--trained_checkpoint_prefix={last_model_path}
(c) 创建 tflite 就绪图
!python /content/drive/'My Drive'/'Detecto Tutorial'/models/research/object_detection/export_tflite_ssd_graph.py \
--pipeline_config_path=/content/drive/My\ Drive/Detecto\ Tutorial/models/research/object_detection/samples/configs/ssd_mobilenet_v2_coco.config \
--output_directory={output_directory} \
--trained_checkpoint_prefix={last_model_path} \
--add_postprocessing_op=true
-
我使用图形文件中的 tflite_convert 创建了一个 tflite 模型,如下所示
!tflite_convert
--graph_def_file=/content/drive/My\ Drive/Detecto\ 教程/模型/研究/fine_tuned_model/tflite_graph.pb
--output_file=/content/drive/My\ Drive/Detecto\ 教程/模型/研究/fine_tuned_model/detect3.tflite
--output_format=TFLITE
--input_shapes=1,300,300,3
--input_arrays=标准化_输入_图像_张量
--output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3'
--inference_type=浮点数
--允许自定义操作
上述 tflite 模型经过独立验证并且运行良好(在 Android 之外)。
现在需要使用元数据填充 tflite 模型,以便可以在下面链接提供的示例 Android 代码中对其进行处理(否则我会收到错误:不是有效的 Zip 文件,并且运行时没有关联的文件)在 Android Studio 上)。
https://github.com/tensorflow/examples/blob/master/lite/examples/object_detection/android/README.md https://github.com/tensorflow/examples/blob/master/lite/examples/object_detection/android/README.md
作为同一链接的一部分提供的示例 .TFlite 填充了元数据并且工作正常。
当我尝试使用以下链接时:https://www.tensorflow.org/lite/convert/metadata#deep_dive_into_the_image_classification_example https://www.tensorflow.org/lite/convert/metadata#deep_dive_into_the_image_classification_example
populator = _metadata.MetadataPopulator.with_model_file('/content/drive/My Drive/Detecto Tutorial/models/research/fine_tuned_model/detect3.tflite')
populator.load_metadata_buffer(metadata_buf)
populator.load_associated_files(['/content/drive/My Drive/Detecto Tutorial/models/research/fine_tuned_model/labelmap.txt'])
populator.populate()
添加元数据(代码的其余部分实际上与对象检测的元描述的一些更改而不是图像分类并指定 labelmap.txt 的位置相同),它给出以下错误:
<ipython-input-6-173fc798ea6e> in <module>()
1 populator = _metadata.MetadataPopulator.with_model_file('/content/drive/My Drive/Detecto Tutorial/models/research/fine_tuned_model/detect3.tflite')
----> 2 populator.load_metadata_buffer(metadata_buf)
3 populator.load_associated_files(['/content/drive/My Drive/Detecto Tutorial/models/research/fine_tuned_model/labelmap.txt'])
4 populator.populate()
1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_lite_support/metadata/metadata.py in _validate_metadata(self, metadata_buf)
540 "The number of output tensors ({0}) should match the number of "
541 "output tensor metadata ({1})".format(num_output_tensors,
--> 542 num_output_meta))
543
544
ValueError: The number of output tensors (4) should match the number of output tensor metadata (1)
4 个输出张量是步骤 2 中的 output_arrays 中提到的(有人可能会纠正我)。我不确定如何相应地更新输出张量元数据。
最近使用自定义模型进行对象检测(然后在 Android 上应用)的任何人都可以提供帮助吗?或者帮助了解如何将张量元数据更新为 4 而不是 1。