Tensorflow Lite Android Object Detection -- Mobile SSD models expected to have exactly 4 outputs, 找到 8

Tensorflow Lite Android Object Detection -- Mobile SSD models are expected to have exactly 4 outputs, found 8

遇到的问题:

E/AndroidRuntime: FATAL EXCEPTION: main Process: org.tensorflow.lite.examples.detection, PID: 14719 java.lang.AssertionError: Error occurred when initializing ObjectDetector: Mobile SSD models are expected to have exactly 4 outputs, found 8

问题描述

型号说明

系统信息

使用的已保存模型转换命令:

1. Saved_Model.pb 出口:

python ./exporter_main_v2.py
--input_type image_tensor
--pipeline_config_path ./models/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/pipeline.config
--trained_checkpoint_dir ./models/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8
--output_directory exported_models/tflite

2。将保存的模型 (.pb) 转换为 tflite

toco
--saved_model_dir ./exported-models/tflite/saved_model
--emit-select-tf-ops true
--allow_custom_ops
--graph_def_file ./exported-models/tflite/saved_model/saved_model.pb
--output_file ./exported-models/tflite/tflite/detect.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=FLOAT
--allow_custom_ops

备注 我正在尝试在 Google TensorFlow lite 提供的示例中使用经过训练的自定义模型。只是每次我打开应用程序,它returns这样的错误,Mobile SSD models expected to have exactly 4 outputs, found 8. model is trained to identify 4 类, all stated in the labelmap.txt 和管道配置。

有人知道这个错误吗?

经过进一步研究,我认为出现了上述问题,因为该模型具有8个张量输出,但是Android中编写的应用程序Java只能支持4个张量输出(至少提供的示例by Google 只支持4张量输出)

我不太确定在不同模型上输出的张量数量。据我了解并弄乱了不同的模型,fixed_shape_resizer 为 640 x 640 的模型可能需要超过 4 个张量输出(通常是 8 个张量输出),这与 Android 不兼容应用程序写在 Java.

对于像我这样的业余用户,请在 Android application
中找到以下使用自定义模型的先决条件 建议设置(假设您使用的 TensorFlow 版本 >= 2.3):

  • TensorFlow 模型:SSD 模型 fixed_shape_resizer 为 320 x 320
    (在我的例子中,SSD MobileNet v2 320x320 工作得很好) (张量输出必须是 4)

  • Colab(非常适合模型训练和转换)
    (我试过在本地机器的 Linux 和 Windows 平台上进行训练和转换,不同工具和包的不兼容让我很头疼。我原来使用 Colab 来执行培训和转换。它更强大,并且与那些培训工具和脚本具有很好的兼容性。)

  • that was written by @lu-wang-g
    (在我的例子中,在将训练模型转换为 .tflite 后,如果您直接将 .tflite 模型迁移到 Android 应用程序,该应用程序将报告有关 .tflite 模型配置的大量问题。假设您正确训练和转换模型,你只需要上面的metadata writer库,它会根据.tflite模型自动为你配置元数据,然后你可以直接将模型迁移到应用程序中。)

详情请访问我的GitHub问题:
https://github.com/tensorflow/tensorflow/issues/47595