pytorch / android-demo-app

PyTorch android examples of usage in applications
1.45k stars 596 forks source link

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

Open gusdkim opened 1 year ago

gusdkim commented 1 year ago

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.

Absentation commented 1 year ago

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 commented 1 year ago

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.

aravinthk00 commented 1 year ago

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' ...

Absentation commented 1 year ago

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.

aravinthk00 commented 1 year ago

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 : aravinthk1809@gmail.com

aravinthk00 commented 1 year ago

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 commented 1 year ago

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 :-)

AnnaKholkina commented 1 year ago

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.

Absentation commented 1 year ago

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?

AnnaKholkina commented 1 year ago

Yes, I did. @Absentation

AnnaKholkina commented 1 year ago

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?

aravinthk00 commented 1 year ago

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 :-)

mikel-brostrom commented 1 year ago

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...

rohinr7 commented 1 year ago

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

Absentation commented 1 year ago

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

Clean gradle and Rebuild gradle

rohinr7 commented 1 year ago

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