无效的输出张量索引:当 运行 Google 的 TFLite 对象检测示例上的自定义 yolov3-tiny 模型时为 1

Invalid output Tensor index: 1 when running a custom yolov3-tiny model on Google's TFLite Object Detection example

我在 TensorFlow Lite's Object Detection Android Demo 上尝试 运行 微型 yolov3 模型时遇到错误。 当我尝试 运行 移动应用 phone 时,应用崩溃并出现以下错误

E/AndroidRuntime: FATAL EXCEPTION: inference
    Process: org.tensorflow.lite.examples.detection, PID: 5535
    java.lang.IllegalArgumentException: Invalid output Tensor index: 1
        at org.tensorflow.lite.NativeInterpreterWrapper.getOutputTensor(NativeInterpreterWrapper.java:292)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:166)
        at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:314)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:204)
        at org.tensorflow.lite.examples.detection.DetectorActivity.run(DetectorActivity.java:181)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:65)

Here 是我的 tflite 和 labelfile。

我在 DetectorActivity.java 上更改了以下内容以避免 错误

TF_OD_API_INPUT_SIZE from 300 to 416
TF_OD_API_IS_QUANTIZED from true to false

然后我在 TFLiteObjectDetectionAPIModel.java

上更改了以下内容
NUM_DETECTIONS from 10 to 2535
d.outputLocations = new float[1][NUM_DETECTIONS][4] to d.outputLocations = new float[1][NUM_DETECTIONS][7];

Here 是我使用

的 DetectorActivity.java 和 TFLiteObjectDetectionAPIModel.java

Here 是我的模型 .weight、cfg 和 .pb(如果需要)

如有任何帮助,我们将不胜感激

我可以使用您的自定义模型和源代码重现该问题。感谢您提供它们。

主要问题是您的自定义 detect.tflite 模型的输出规格与对象检测示例应用程序预期的不同。

您可以使用 netron 等模型可视化工具查看差异。

示例应用 (mobilenet_ssd) 使用的原始模型如下所示:

如您所见,有 4 个标量 float32 输出,它们本质上是从最终的 TFLite_Detection_PostProcess 节点拆分出来的。

另一方面,您的模型有一个 [1,2535,7] 形状的输出张量。

因此,当应用程序的 Java 代码运行 tfLite.runForMultipleInputsOutputs(inputArray, outputMap) 时,它会尝试根据您在 outputMap 中输入的内容分配多个输出。但是,由于您的模型中只有一个输出张量,因此当它尝试将索引 1 处的输出分配到 outputClasses 数组时,它会失败并显示错误消息。

我对 yolov3 模型的了解不够详细,无法帮助您了解用于转换模型的确切命令,但 this doc 应该提供有关如何转换原始模型的更多详细信息。