pytorch / android-demo-app

PyTorch android examples of usage in applications
1.46k stars 604 forks source link

Custom Yolov5s TorchScript model does not work on Android #227

Closed MjiS closed 2 years ago

MjiS commented 2 years ago

I have custom model trained on yolov5s v5 and I converted it to torchscript.ptl using ultralytics export.py with code modification as told here. When I build on android i get the following error: image

error that appears in debugging: E/AndroidRuntime: FATAL EXCEPTION: Thread-2 Process: org.pytorch.demo.objectdetection, PID: 27891 java.lang.ArrayIndexOutOfBoundsException: length=1310400; index=1310449 at org.pytorch.demo.objectdetection.PrePostProcessor.outputsToNMSPredictions(PrePostProcessor.java:123) at org.pytorch.demo.objectdetection.MainActivity.run(MainActivity.java:248)

I changed my torch version also, I am still facing the same issue.

yolov5s.torchscript.ptl model file provided in this repo. works fine. I'm facing this issue when I use my custom model

cloveropen commented 2 years ago

maybe the labels array and scores array are not has same length,please check it

MjiS commented 2 years ago

Thank you for your reply. I changed the value as calculated by 25200*(num_of_class+5) but I’m still facing the same issue.

private static int mOutputRow = 1310400; // as decided by the YOLOv5 model for input image of size 640*640 private static int mOutputColumn = 52; // left, top, right, bottom, score and 80 class probability

AlanWan2000 commented 2 years ago

How do you convert the .pt file to .ptl? I tried to convert it using export.py be it doesn't work. This error pops up "PytorchStreamReader failed locating file bytecode.pkl: file not found ()" Mind if you share your export.py with me? Thanks a lot

MjiS commented 2 years ago

I used the one given in repo. https://github.com/pytorch/android-demo-app/issues/157 This might help you.

MjiS commented 2 years ago

So I solved this issue. You need to change only mOutputColumn value with you number of classes+5. I was changing mOutputRow = 25200 with value calculated by 25200*(num_of_class+5) which caused this issue.

// model output is of size 25200(num_of_class+5) private static int mOutputRow = 25200; // as decided by the YOLOv5 model for input image of size 640640 private static int mOutputColumn = 52; // left, top, right, bottom, score and 80 class probability private static float mThreshold = 0.50f; // score above which a detection is generated //