【问题标题】:Android - TFLite OD - Cannot copy to a TensorFlowLite tensor (normalized_input_image_tensor) with 307200 bytes from a Java Buffer with 4320000 bytesAndroid - TFLite OD - 无法从 4320000 字节的 Java 缓冲区复制到 307200 字节的 TensorFlowLite 张量 (normalized_input_image_tensor)
【发布时间】:2020-07-12 21:20:18
【问题描述】:

我正在尝试运行我自己的自定义模型来进行对象检测。我从 Google 云创建了我的数据集 - Vision (https://console.cloud.google.com/vision/)(我对图像进行了装箱和标记),它看起来像这样:

训练模型后,我从这里下载了 TFLite 文件(labelmap.txt、model.tflite 和一个 json 文件):

然后,我将它们添加到 Android 对象检测示例 (https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android)。

但是当我运行项目时它崩溃了:

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 更改为 false 并将 labelOffset 更改为 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 应用程序上编译,然后我会改进它)

【问题讨论】:

  • 欢迎回来!很高兴看到你取得了进展。错误提示输入大小错误。如果您在某个地方上传了您的项目,我很乐意看看。
  • 哈哈是的,又是我。我在这里使用了对象检测示例github.com/tensorflow/examples/tree/master/lite/examples/…。这是我修改参数的类github.com/tensorflow/examples/blob/master/lite/examples/…
  • 当你在谷歌云上训练模型时,你使用了哪些维度?这里的例子是 300x300
  • 就是这个问题,我没找到办法知道用什么架构来生成模型,也不知道能不能改。我为“对象检测”配置了模型,因此我相信该模型具有正确的架构和参数。
  • 那里!我用文件链接更新了帖子,以防其他人需要它们。非常感谢!

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


【解决方案1】:

有一个很棒的可视化工具叫做Netron。我使用了你的 .tflite 文件,你的模型的输入是:

所以在你的代码中计算字节缓冲区的行

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

你必须输入

1*320*320*3*1

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

【讨论】:

    【解决方案2】:

    在我将 TensorImage DataType 从 UINT8 更改为 FLOAT32 后,它可以工作了。

     val tfImageBuffer = TensorImage(DataType.UINT8)
     ->
     val tfImageBuffer = TensorImage(DataType.FLOAT32)
    

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多