android-demo-app icon indicating copy to clipboard operation
android-demo-app copied to clipboard

An android app with a custom model of object detection runs failed

Open gusdkim opened this issue 2 years ago • 16 comments

I'm working on an object detection project using yolov5 model and custom datasets. I followed this tutorial. https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection

I modified the export.py in the yolov5 https://github.com/ultralytics/yolov5/blob/master/export.py following the tutorial. image I moved the best.torchscript.ptl file to the Android assets folder and modified MainActivity.java.

But an error was occured that "Lite Interpreter version number does not match. The model version must be between 3 and 7 but the model version is 8 ()". I saw a solution https://github.com/pytorch/android-demo-app/issues/202 and tried the torch.jit.mobile which has a _backport_for_mobile function to "backport" a model to a given version image

I used the file best_v5.torchscript.ptl obtained by the above function, but the Android app does not run. The build and launch were successful, but an error occurs with the following screen in the emulator. How can I fix this error? image FATAL EXCEPTION: main Process: org.pytorch.demo.objectdetection, PID: 10077 java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.demo.objectdetection/org.pytorch.demo.objectdetection.MainActivity}: com.facebook.jni.CppException: isTuple()INTERNAL ASSERT FAILED at "../aten/src/ATen/core/ivalue_inl.h":1396, please report a bug to PyTorch. Expected Tuple but got String () Exception raised from toTuple at ../aten/src/ATen/core/ivalue_inl.h:1396 (most recent call first):

FYI the app runs successfully with the model file yolov5s.torchscript.ptl in the tutorial.

gusdkim avatar Oct 03 '22 11:10 gusdkim

Similar problem here. i didn't back-port my custom model, instead. I updated torchvision lite to 1.12 and I still got this in the app debug logs:

E/AndroidRuntime: FATAL EXCEPTION: main Process: org.pytorch.demo.objectdetection, PID: 13240 java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.demo.objectdetection/org.pytorch.demo.objectdetection.MainActivity}: com.facebook.jni.CppException: isTuple()INTERNAL ASSERT FAILED at "../aten/src/ATen/core/ivalue_inl.h":1908, please report a bug to PyTorch. Expected Tuple but got String () Exception raised from toTuple at ../aten/src/ATen/core/ivalue_inl.h:1908

Will try to back-port model to version 5 and test it with torchvision lite version 1.10

Absentation avatar Oct 08 '22 11:10 Absentation

Yup. Same problem here even after backport. Caused by: com.facebook.jni.CppException: isTuple()INTERNAL ASSERT FAILED at "../aten/src/ATen/core/ivalue_inl.h":1396, please report a bug to PyTorch. Expected Tuple but got String () Exception raised from toTuple at ../aten/src/ATen/core/ivalue_inl.h:1396

Please, help is needed.

Absentation avatar Oct 08 '22 12:10 Absentation

I also encountered this issue, I can't get idea about resolving. I'm working on an object detection project using yolov5 model and custom datasets. I followed this tutorial. https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection

I modified the export.py in the yolov5 https://github.com/ultralytics/yolov5/blob/master/export.py following the tutorial.

Please response here ... Anyone assist me, we need your help.

Advanced Thanks Lots' ...

aravinthk00 avatar Nov 17 '22 09:11 aravinthk00

I fixed it eventually by changing the input tensor shape to match the one of my model. It should work with torchlite 1.12 iirc.

If you forgot to check on your input and output tensor shapes in the app code then perhaps you should check again, hope it works for you. Albeit it's going to be slow, I'm still working on figuring out a workaround for that detection delay.

Here's something I tried: Check around line 39 in Prepostprocessor class. The column value for input tensor could be different from the one in your model. Try changing it to a suitable one, I remember that was one of the problems related to this error. and a few tweaks in Prepostprocessor class and changing to a proper version of torchlite in gradle build script sloved it. I'll post more details on how I fixed it once I'm on my computer.

This app still works. so It's not an actual issue with the code, it's just outdated torchlite and input mismatch irrc. Hope this helps 🙏 Please update the issue if it does.

Absentation avatar Nov 17 '22 10:11 Absentation

Hi, Great thanks for your response. Sorry I am fresh for this ( Pytorch, Tensorflow, etc) I didn't get your exact point. How do checks input & output tensor shapes for my app code, models? if you don't mind, please assist me from scratch in Object Detection Pytorch App. if you wish, you are feel free to contact and teach me for better communication use this mail : [email protected]

aravinthk00 avatar Nov 17 '22 12:11 aravinthk00

I try to catch input and output tensor shapes in my understading ,given below inoutshapethen I did changes in prepostprocessor.java prepostjava But till get same issueerrorinas please anyone help for resolve this issue ...

aravinthk00 avatar Nov 18 '22 02:11 aravinthk00

Thankyou @Absentation, You are answer helped to resolve my issue. I fix this issue, just I upadate on " implementation 'org.pytorch:pytorch_android_lite:1.12.1' implementation 'org.pytorch:pytorch_android_torchvision_lite:1.10.0' in my build.gradle file. it's work for me . It will helps for others.

Thankyou :-)

aravinthk00 avatar Nov 24 '22 04:11 aravinthk00

Hey, @aravinthk00. Did you train yolo last version? I have the same problem and outputs, but org.pytorch:pytorch_android_lite:1.12.1 didn't solve problem.

AnnaKholkina avatar Nov 24 '22 10:11 AnnaKholkina

Hey, @aravinthk00. Did you train yolo last version? I have the same problem and outputs, but org.pytorch:pytorch_android_lite:1.12.1 didn't solve problem.

@AnnaKholkina did you check if your model input and output sizes match the values in the app code?

Absentation avatar Nov 24 '22 11:11 Absentation

Yes, I did. @Absentation

AnnaKholkina avatar Nov 24 '22 11:11 AnnaKholkina

I also have bad predictions. On PC model results with thresholds like 0.9+, in the Android-app I have same predictions with 0.3-0.4. Maybe, if you have a good Android-model, can you help me with correct export?

AnnaKholkina avatar Nov 24 '22 11:11 AnnaKholkina

please try this comment " ### python train.py --batch 32 --cfg yolov5m6.yaml --weights '' --data coco.yaml --img 1280 --epochs 300 " to train the your model then export the model pt to ptl after import the model in your app source code and increase your threshold value also. I think it will be helpful for you . Thankyou :-)

aravinthk00 avatar Nov 24 '22 12:11 aravinthk00

Changing:

implementation 'org.pytorch:pytorch_android_lite:1.10.1

to:

implementation 'org.pytorch:pytorch_android_lite:1.12.1

in

build.gradle

didn't solve it for me either...

mikel-brostrom avatar Mar 06 '23 13:03 mikel-brostrom

the version (implementation 'org.pytorch:pytorch_android_lite:1.12.1) not supporting in my android studio

rohinr7 avatar Mar 18 '23 08:03 rohinr7

the version (implementation 'org.pytorch:pytorch_android_lite:1.12.1) not supporting in my android studio

Clean gradle and Rebuild gradle

Absentation avatar Mar 18 '23 18:03 Absentation

the version (implementation 'org.pytorch:pytorch_android_lite:1.12.1) not supporting in my android studio

Clean gradle and Rebuild gradle

i have rebuild it still my app is not running

rohinr7 avatar Mar 20 '23 06:03 rohinr7