Android - TFLite OD - 无法从 4320000 字节的 Java 缓冲区复制到 307200 字节的 TensorFlowLite 张量 (normalized_input_image_tensor)

标签 android tensorflow google-cloud-platform tensorflow-lite

我正在尝试运行我自己的自定义模型来进行对象检测。我从 Google 云 - Vision (https://console.cloud.google.com/vision/) 创建了我的数据集(我对图像进行了装箱和标记),它看起来像这样:
enter image description here
训练模型后,我从这里下载了 TFLite 文件(labelmap.txt、model.tflite 和一个 json 文件):
enter image description here
然后,我将它们添加到 Android 对象检测示例 (https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android) 中。
enter image description here
但是当我运行项目时它崩溃了:

2020-07-12 18:03:05.160 14845-14883/? E/AndroidRuntime: FATAL EXCEPTION: inference
    Process: org.tensorflow.lite.examples.detection, PID: 14845
    java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (normalized_input_image_tensor) with 307200 bytes from a Java Buffer with 4320000 bytes.
        at org.tensorflow.lite.Tensor.throwIfSrcShapeIsIncompatible(Tensor.java:423)
        at org.tensorflow.lite.Tensor.setTo(Tensor.java:189)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:154)
        at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:343)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:197)
        at org.tensorflow.lite.examples.detection.DetectorActivity$2.run(DetectorActivity.java:182)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:67)
我尝试更改参数 TF_OD_API_IS_QUANTIZED 为假和标签偏移 为 0,我还将这一行从 TFLiteObjectDetectionAPIModel.java 修改为 d.imgData = ByteBuffer.allocateDirect(_4_ * d.inputSize * d.inputSize * 3 * numBytesPerChannel); (我把 1 换成了 4)
我是新手,如果有人能帮助我理解和解决错误,我将不胜感激。谢谢!

更新 :
以下是 tflite 文件:https://drive.google.com/drive/folders/11QT8CgaYF2EseORgGCceh4DT80_pMiFM?usp=sharing (我不在乎模型是否正确识别正方形和圆形,我只想检查它是否在 android 应用程序上编译,然后我会改进它)

最佳答案

有一个很棒的可视化工具,叫做 Netron .我使用了你的 .tflite 文件,你的模型的输入是:
enter image description here
因此,在您计算字节缓冲区的行的代码中

1 * d.inputSize * d.inputSize * 3 * numBytesPerChannel


你必须输入

1* 320 * 320 * 3 * 1


最后一个“1”用于 uint8 ....如果你有浮点数,你应该放“4”。

关于Android - TFLite OD - 无法从 4320000 字节的 Java 缓冲区复制到 307200 字节的 TensorFlowLite 张量 (normalized_input_image_tensor),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62866577/

相关文章:

java - 如何在不导致Overdraw的情况下为UI中的特定组件添加背景色?

google-cloud-platform - 无法删除 Google Cloud Platform 项目

bash - 无需打开浏览器即可进行 gcloud 身份验证?

google-cloud-platform - 在 GCP 中编辑虚拟机实例时出现错误 : UEFI setting must be the same for the instance and the boot disk.

android - 我可以将 SupportMapFragment 更改为 Google Maps v2 上的 Mapview 吗?

android - 从相机捕获图像并在我们保存之前覆盖另一个位图

android - SEND_SMS 十月政策更新

python - 具有矢量输出和 2D 图像图输入的 CNN(输入是数组)

python - DropoutWrapper 在运行中是不确定的?

python - Tensorflow MaxPool 不接受 float64