pytorch / android-demo-app

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

Android Object Detection bytecode.pkl error #247

Open codeHorasan opened 2 years ago

codeHorasan commented 2 years ago

Hello. I exported my Yolov5 model as xx.torchscript.ptl and put it into assets folder of the Android Project, however I got this error when I try to run the app:

Process: org.pytorch.demo.objectdetection, PID: 27578 java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.demo.objectdetection/org.pytorch.demo.objectdetection.MainActivity}: com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found () Exception raised from valid at ../caffe2/serialize/inline_container.cc:158 (most recent call first): (no backtrace available) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2858) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2933) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1612) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6710) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) Caused by: com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found () Exception raised from valid at ../caffe2/serialize/inline_container.cc:158 (most recent call first):

Any help by any chance?

vishnukarthik-1994 commented 2 years ago

@codeHorasan were you able to resolve this? Even I am facing the same issue. The documentation here: https://pytorch.org/tutorials/recipes/mobile_interpreter.html mentions to use: save_for_lite_interpreter. So in export.py of yolov5, I am setting optimize to true and exported the model. I still am facing the same issue.

codeHorasan commented 2 years ago

@vishnukarthik-1994 I have solved the issue but in a different way. I converted it into tflite model from "https://github.com/zldrobit/yolov5" link with this code "python export.py --weights xx.pt --include tflite --img 320". Then I cloned the android studio code and applied "4. Put TFLite models in assets folder of Android project, and change" basically I ended up having this:

if (modelFilename.equals("yolov5s.tflite")) { System.out.println("TFLIE"); labelFilename = "file:///android_asset/labels.txt"; isQuantized = false; inputSize = 320; output_width = new int[]{40, 20, 10}; masks = new int[][]{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; anchors = new int[]{ 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326 }; }

Don't forget to put your tflite model and your labels text file into assets folder. I renamed my file into "yolov5s.tflite" just for being lazy. I didn't changed the anchors, I tried the code but I got error, my model was built In yolov5l variant , I checked it's anchors and they were the same with the yolov5s so I didn't change it. Hope it helps, it worked for me.

someidiot commented 1 year ago

I fixed this problem by using the --optimize flag when calling the yolo's export script. By default it does not use the lite version unless this flag is passed.

HripsimeS commented 1 year ago

@codeHorasan Hello. Can you please send a link of export.py file which converts .pt model to .torchscript.ptl format. Thanks a lot in advance!

NeighborhoodCoding commented 1 year ago
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2858)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2933)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1612)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found ()
Exception raised from valid at ../caffe2/serialize/inline_container.cc:158 (most recent call first):

adding --optimize solve this issue.