android - 无法在 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.gradle
detect.tflite
labelmap.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 的一些问题具有相同的错误消息,但针对的是自定义训练模型,因此没有帮助。即使在自定义训练模型上,同样的错误也会不断出现。我应该怎么做才能消除这个错误?
解决方案
您应该调整输入张量的大小,以便您的模型可以获取任何大小、像素或批次的数据。
以下代码用于图像分类,您的代码用于对象检测: 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);
推荐阅读
- java - Java 单一继承
- python - 如何将文本列表格式化为 2 列
- ios - 方向转换后来自 UIView 的渐变
- c# - C#.Net 检查“进程”是否有 gui
- python - AWS lambda URL 请求并将二进制文件写入 /tmp/
- r - 任何人都知道为什么我的 SankeyNetwork 没有绘制图表(显然没有错误)?
- javascript - 获取符合条件的对象数组中的第一个对象
- flutter - Flutter:传递到下一个屏幕的变量变为null
- google-apps-script - Google 表格 SQL 查询超时
- c++ - 将 arrayfire 与 boost::odeint 耦合