Android - TFLite OD - 无法从具有 4320000 字节的 Java 缓冲区复制到具有 307200 字节的 TensorFlowLite 张量 (normalized_input_image_tensor)
Android - TFLite OD - Cannot copy to a TensorFlowLite tensor (normalized_input_image_tensor) with 307200 bytes from a Java Buffer with 4320000 bytes
我正在尝试 运行 我自己的对象检测自定义模型。我从 Google cloud - 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.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 应用程序上编译,然后我会改进它)
有一个很棒的可视化工具,叫做Netron。我使用了您的 .tflite 文件,您模型的输入是:
所以在你计算字节缓冲区的代码行
1 * d.inputSize * d.inputSize * 3 * numBytesPerChannel
你必须输入
1* 320 * 320 * 3 * 1
最后一个“1”用于 uint8....如果你有浮点数,你应该放“4”。
我将 TensorImage DataType 从 UINT8 更改为 FLOAT32 后,它起作用了。
val tfImageBuffer = TensorImage(DataType.UINT8)
->
val tfImageBuffer = TensorImage(DataType.FLOAT32)
我正在尝试 运行 我自己的对象检测自定义模型。我从 Google cloud - 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.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 应用程序上编译,然后我会改进它)
有一个很棒的可视化工具,叫做Netron。我使用了您的 .tflite 文件,您模型的输入是:
所以在你计算字节缓冲区的代码行
1 * d.inputSize * d.inputSize * 3 * numBytesPerChannel
你必须输入
1* 320 * 320 * 3 * 1
最后一个“1”用于 uint8....如果你有浮点数,你应该放“4”。
我将 TensorImage DataType 从 UINT8 更改为 FLOAT32 后,它起作用了。
val tfImageBuffer = TensorImage(DataType.UINT8)
->
val tfImageBuffer = TensorImage(DataType.FLOAT32)