【问题标题】:Cannot convert between a TensorFlowLite buffer with 307200 bytes and a Java Buffer with 270000 bytes无法在 307200 字节的 TensorFlowLite 缓冲区和 270000 字节的 Java 缓冲区之间进行转换
【发布时间】:2020-05-24 09:57:18
【问题描述】:

我正在尝试从 Tensorflow detection model zoo 运行预训练的对象检测 TensorFlowLite 模型。我在 Mobile Models 标题下使用了来自该站点的 ssd_mobilenet_v3_small_coco 模型。根据在Android上运行我们的模型下的instructions,我注释掉了模型下载脚本以避免资产被覆盖:build.gradle文件中的// apply from:'download_model.gradle'并替换了detect.tflite和@资产目录中的 987654327@ 文件。构建成功,没有任何错误,该应用程序已安装在我的 android 设备中,但它一启动就崩溃了,logcat 显示:

E/AndroidRuntime: FATAL EXCEPTION: inference
Process: org.tensorflow.lite.examples.detection, PID: 16960
java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 307200 bytes and a Java Buffer with 270000 bytes.
    at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:425)
    at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:392)
    at org.tensorflow.lite.Tensor.setTo(Tensor.java:188)
    at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:150)
    at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:314)
    at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:196)
    at org.tensorflow.lite.examples.detection.DetectorActivity$2.run(DetectorActivity.java:185)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:201)
    at android.os.HandlerThread.run(HandlerThread.java:65)

我已经搜索了许多 TensorFlowLite 文档,但没有找到与此错误相关的任何内容,并且我发现有关 stackoverflow 的一些问题具有相同的错误消息,但针对的是自定义训练模型,因此没有帮助。即使在自定义训练模型上,同样的错误也会不断出现。我应该怎么做才能消除这个错误?

【问题讨论】:

    标签: android tensorflow object-detection tensorflow-lite


    【解决方案1】:

    您应该调整输入张量的大小,以便您的模型可以获取任何大小、像素或批次的数据。

    以下代码用于图像分类,您的代码用于对象检测: TFLiteObjectDetectionAPIModel 负责获取大小。尝试在 TFLiteObjectDetectionAPIModel 的某些地方操作大小。

    标签长度需要与您的输出张量长度相匹配 训练好的模型。

      int[] dimensions = new int[4];
      dimensions[0] = 1; // Batch_size // No of frames at a time
      dimensions[1] = 224; // Image Width required by model
      dimensions[2] = 224; // Image Height required by model
      dimensions[3] = 3; // No of Pixels
      Tensor tensor = c.tfLite.getInputTensor(0);
      c.tfLite.resizeInput(0, dimensions);
      Tensor tensor1 = c.tfLite.getInputTensor(0);
    

    Change input size

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2022-10-31
    • 2014-03-16
    • 2010-10-13
    • 1970-01-01
    相关资源
    最近更新 更多