Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (serving_default_input_1:0) with 4915200 bytes from a Java Buffer with 1228800 bytes #57

Open
hanspres1999 opened this issue Sep 11, 2024 · 10 comments

Comments

@hanspres1999
Copy link

I am running my custom trained YOLO5 model on the example app and end up with the error:

E/AndroidRuntime(17777): java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (serving_default_input_1:0) with 4915200 bytes from a Java Buffer with 1228800 bytes.
E/AndroidRuntime(17777): 	at org.tensorflow.lite.TensorImpl.throwIfSrcShapeIsIncompatible(TensorImpl.java:416)
E/AndroidRuntime(17777): 	at org.tensorflow.lite.TensorImpl.setTo(TensorImpl.java:140)
E/AndroidRuntime(17777): 	at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:251)
E/AndroidRuntime(17777): 	at org.tensorflow.lite.InterpreterImpl.runForMultipleInputsOutputs(InterpreterImpl.java:101)
E/AndroidRuntime(17777): 	at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:95)
E/AndroidRuntime(17777): 	at com.ultralytics.ultralytics_yolo.predict.detect.TfliteDetector.runInference(TfliteDetector.java:250)
E/AndroidRuntime(17777): 	at com.ultralytics.ultralytics_yolo.predict.detect.TfliteDetector.lambda$predict$0$com-ultralytics-ultralytics_yolo-predict-detect-TfliteDetector(TfliteDetector.java:196)
E/AndroidRuntime(17777): 	at com.ultralytics.ultralytics_yolo.predict.detect.TfliteDetector$$ExternalSyntheticLambda0.run(Unknown Source:2)
E/AndroidRuntime(17777): 	at android.os.Handler.handleCallback(Handler.java:958)
E/AndroidRuntime(17777): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(17777): 	at android.os.Looper.loopOnce(Looper.java:230)
E/AndroidRuntime(17777): 	at android.os.Looper.loop(Looper.java:319)
E/AndroidRuntime(17777): 	at android.app.ActivityThread.main(ActivityThread.java:8906)
E/AndroidRuntime(17777): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(17777): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
E/AndroidRuntime(17777): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
I/Process (17777): Sending signal. PID: 17777 SIG: 9
Lost connection to device.

Exited.

I've changed the imgsz parameter on the metadata.yaml file to 640, 640 (and tried keeping it at 320,320) and yet no difference.

@glenn-jocher
Copy link
Member

@hanspres1999 it seems like there's a mismatch between the input tensor size and the buffer size. Ensure your model's input dimensions match the expected input size in the app. Double-check the model's input shape and adjust the imgsz parameter accordingly. If the issue persists, try updating to the latest version of the app and model.

@dingniaoji
Copy link

exactly same error with yolov8 f16 imgsz=640 tflite model.
I have tried f16 with imgsz=320, it still outputs same error.

do I must use int8 model with imgsz=320?

@glenn-jocher
Copy link
Member

@dingniaoji it seems like a tensor size mismatch. Ensure your input dimensions match the model's expected size. Try using int8 with imgsz=320 if the issue persists, and verify with the latest package version.

@ice6
Copy link

ice6 commented Sep 24, 2024

after a source code debugging, I finally solve the problem.

just to make sure that the imgsz in your metadata.yaml is same with your model.

I tried 320 with fp16 and 320 with fp32, both are ok.

@ice6
Copy link

ice6 commented Sep 24, 2024

@hanspres1999 in your case, it seems that in the metadata.yaml, imgsz should be 320 x 320, because 320 * 320 * 3 * 4 = 1228800.
and the imgsz in your model is 640x640, because640 * 640 * 3 * 4 = 4915200.
3 is the channel count(r/g/b) , 4 is the float32 size in Byte.

make the the size same may solve your problem.

@hanspres1999
Copy link
Author

@ice6 I've kept my imgsz in metadata as 640 x 640. It still throws the same error. If it helps, the model was developed with the YOLOv5s backend.

@dingniaoji
Copy link

I have never tried yolov5, I think it should be fine, because the underlying is tensorflow-lite.

use netron(https://netron.app/) to inspect your model. and click the inputs_0, and paste the INPUTS and OUTPUTS in the right panel here.

paste the content of metadata.yaml here will also help.

@glenn-jocher
Copy link
Member

Please inspect your model using Netron to verify the input dimensions. Share the INPUTS and OUTPUTS details, along with the metadata.yaml content, so we can assist you further.

@johnebere58
Copy link

Search for this and rename it to the size of the model you used

public static final int INPUT_SIZE = 320;

@pderrenger
Copy link
Member

Ensure the INPUT_SIZE in your code matches the model's input dimensions. Adjust it to the size specified in your metadata.yaml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants