首页 > 解决方案 > 无法在 307200 字节的 TensorFlowLite 缓冲区和 270000 字节的 Java 缓冲区之间进行转换

问题描述

我正在尝试从Tensorflow 检测模型 zoo运行预训练的 Object Detection TensorFlowLite 模型。我在移动模型标题ssd_mobilenet_v3_small_coco下使用了这个站点的模型。根据Running our model on Android下的说明,我将模型下载脚本注释掉以避免资产被覆盖:in file 并替换了assets 目录中的and文件。构建成功,没有任何错误,该应用程序已安装在我的 android 设备中,但它一启动就崩溃了,logcat 显示:// apply from:'download_model.gradle'build.gradledetect.tflitelabelmap.txt

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 的一些问题具有相同的错误消息,但针对的是自定义训练模型,因此没有帮助。即使在自定义训练模型上,同样的错误也会不断出现。我应该怎么做才能消除这个错误?

标签: androidtensorflowobject-detectiontensorflow-lite

解决方案


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

以下代码用于图像分类,您的代码用于对象检测: 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);

更改输入大小


推荐阅读