java.lang.IllegalArgumentException:无法从具有形状[1,2535,4]的Tensorflowlite张量复制到具有形状的Java对象[1,10,4]

发布于 2025-01-30 16:52:23 字数 2797 浏览 3 评论 0原文

我想使用openCV使用openCV进行手机摄像机的应用程序,并使用tensorflow lite来检测对象。因此,我从github https://github.com/bendahouwael/bendahouwael/vehicle-detection中找到了不错的代码。 -App-android 使用OpenCV进行相机和Tensorflow Lite。要测试我自己的TensorFlow Lite文件,我更改了ObjectDetectorClass = new ObjectDetectorClass(getAssets(),“ SSD_MobiLenet.tflite”,“ labelmap.txt”,300),300); getAssets(),“ tiny-416.tflite”,“ car.txt”,416); 位于cameraactivity.java中。

另外,在objectDetectorClass.java我更改了


if(quant==0){
            byteBuffer=ByteBuffer.allocateDirect(4*1*416*416*3);
        }
        else {
            byteBuffer=ByteBuffer.allocateDirect(1*1*416*416*3);
        }

我在github https://github.com/dohnutseo/my-own-tensorflow-lite-lite-file/tree/tree/main 带有readme中的标签txt。

我在Android Studio中搜索了整个错误消息

E/AndroidRuntime: FATAL EXCEPTION: Thread-2
    Process: com.example.Detection, PID: 19290
    java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor (Identity) with shape [1, 2535, 4] to a Java object with shape [1, 10, 4].
        at org.tensorflow.lite.Tensor.throwIfDstShapeIsIncompatible(Tensor.java:482)
        at org.tensorflow.lite.Tensor.copyTo(Tensor.java:252)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:175)
        at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:360)
        at com.example.imagepro.objectDetectorClass.recognizeImage(objectDetectorClass.java:92)
        at com.example.imagepro.CameraActivity.onCameraFrame(CameraActivity.java:125)
        at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
        at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)
        at java.lang.Thread.run(Thread.java:923)

,并发现了许多问题,这些问题与原始TFLITE文件不同。因此,我试图更改此代码中的大小,但找不到如何更改它。帮助我解决这个问题。

我还使用了此站点 https://netron.app/ 看到我自己的Tensorflow Lite Lite文件的形状输入

name: input_1
type: float32[1,416,416,3]
location: 0

感谢所有答复!

edit1) 要回答@farmaker,您是说来自ObjectDetectorClass.java的代码吗?

float[][][]boxes =new float[1][10][4];
float[][] scores=new float[1][10];
float[][] classes=new float[1][10];

我将其更改为

float[][][]boxes =new float[1][2535][4];
float[][] scores=new float[1][10];
float[][] classes=new float[1][10];

,但仍然有错误。

I want to make application using opencv for phone camera using and tensorflow lite to detect objects. So I found nice code from github https://github.com/bendahouwael/Vehicle-Detection-App-Android that uses opencv for camera and tensorflow lite. To test my own tensorflow lite files, I changed objectDetectorClass=new objectDetectorClass(getAssets(),"ssd_mobilenet.tflite","labelmap.txt",300); to objectDetectorClass=new objectDetectorClass(getAssets(),"tiny-416.tflite","car.txt",416); which is located in CameraActivity.java.

Also, in objectDetectorClass.java I changed


if(quant==0){
            byteBuffer=ByteBuffer.allocateDirect(4*1*416*416*3);
        }
        else {
            byteBuffer=ByteBuffer.allocateDirect(1*1*416*416*3);
        }

I uploaded my tflite file in my github https://github.com/dohnutSeo/my-own-tensorflow-lite-file/tree/main with label txt in readme.

And the whole error message in Android Studio is

E/AndroidRuntime: FATAL EXCEPTION: Thread-2
    Process: com.example.Detection, PID: 19290
    java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor (Identity) with shape [1, 2535, 4] to a Java object with shape [1, 10, 4].
        at org.tensorflow.lite.Tensor.throwIfDstShapeIsIncompatible(Tensor.java:482)
        at org.tensorflow.lite.Tensor.copyTo(Tensor.java:252)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:175)
        at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:360)
        at com.example.imagepro.objectDetectorClass.recognizeImage(objectDetectorClass.java:92)
        at com.example.imagepro.CameraActivity.onCameraFrame(CameraActivity.java:125)
        at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
        at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)
        at java.lang.Thread.run(Thread.java:923)

I searched for this issue and found many question that said input tensor size is different with original tflite file. So I tried to change the size in this code but could not find how to change it. Help me to solve this issue.

Also I used this site https://netron.app/ to see the shaped of my own tensorflow lite file which says input is

name: input_1
type: float32[1,416,416,3]
location: 0

Thanks for all replies!

Edit1)
To answer @Farmaker, do you mean the code from objectDetectorClass.java?

float[][][]boxes =new float[1][10][4];
float[][] scores=new float[1][10];
float[][] classes=new float[1][10];

I changed it to

float[][][]boxes =new float[1][2535][4];
float[][] scores=new float[1][10];
float[][] classes=new float[1][10];

but still I had errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文