'java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor with shape [1, 2535, 4] to a Java object with shape [1, 10, 4]

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.



Solution 1:[1]

You should pass job_id and job_body as ARGV, instead of KEYS.

# script.lua
local job_id = ARGV[1]; local job_body = ARGV[2]

# other code

I'm not familiar with Jedis, but you can try the following code:

connection.evalsha(script, 1, "JOB:WORK_BODY", jobId, jobBody)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1