pytorch / android-demo-app

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

PytorchStreamReader failed locating file bytecode.pkl: file not found () #282

Open HripsimeS opened 1 year ago

HripsimeS commented 1 year ago

@romovpa @kostmo @smacke @kit1980 Hello. I use the following export file to convert pt to torchscript.ptl https://github.com/ultralytics/yolov5/blob/master/export.py python export.py --weights yolov5s.pt --include torchscript

And while I am launch the object detection app with converted model, I got these errors

E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@78c7be1 E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist! E/: [ZeroHung]zrhung_get_config: Get config failed for wp[0x0102] E/AndroidRuntime: FATAL EXCEPTION: main Process: org.pytorch.demo.objectdetection, PID: 14590 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):

Can you please help to understand what is the issue. Maybe I am using a wrong export file or wrong command to convert ?

kit1980 commented 1 year ago

I don't have knowledge of this, but I've found on https://pytorch.org/tutorials/recipes/mobile_interpreter.html

If you see the error message: PytorchStreamReader failed locating file bytecode.pkl: file not found (), likely you are using a torch script model that requires the use of the PyTorch JIT interpreter (a version of our PyTorch interpreter that is not as size-efficient). In order to leverage our efficient interpreter, please regenerate the model by running: module._save_for_lite_interpreter(${model_path}).

fahim44 commented 1 year ago

I am facing the same issue

HripsimeS commented 1 year ago

@kit1980 @jeffxtang @IvanKobzarev @nairbv thanks for your reply! To convert my pt model to torchscript.ptl I used the following export file https://github.com/jeffxtang/yolov5/blob/master/models/export.py

The PytorchStreamReader issue is fixed, but I am facing now with other error

com.facebook.jni.CppException: Lite Interpreter version number does not match. The model version must be between 3 and 7 but the model version is 8 ()

Any ideas how to fix the Lite Interpreter version issue to be able to launch the app successfully? Thanks a lot in advance!

fahim44 commented 1 year ago

@HripsimeS looks like your new issue is a duplicate of: https://github.com/pytorch/android-demo-app/issues/202.

Maybe try increasing PyTorch lib versions to 1.12.2?

implementation 'org.pytorch:pytorch_android_lite:1.12.2'
implementation 'org.pytorch:pytorch_android_torchvision_lite:1.12.2'
HripsimeS commented 1 year ago

@fahim44 Hello. Indeed changing PyTorch lib versions to 1.12.2 helped to launch app successfully, but real time live detection is making strange predictions. It works fine with pictures, but live detection of objects does not show good reaults. So I don't know it is because of converted model or because I still need to do some more modifications in object detection scripts https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection

fahim44 commented 1 year ago

@HripsimeS may be some issue with camerax... not fully sure though

HripsimeS commented 1 year ago

@fahim44 I tried the initial model yolov5s.torchscript.ptl mentioned in Prepare the model part https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection

It works perfect with PyTorch lib versions 1.10.0, but with 1.12.2 does not work good. But when I use my custom model with 1.10.0 versions I get this error Lite Interpreter version number does not match. The model version must be between 3 and 7 but the model version is 8 ()

With 1.12.2 versions the app is launching, but I don't get good predictions for my cutom model. So I am a bit confused, don't know what is the reason and which PyTorch lib versions I should use for my custom model to get good predictions.

fahim44 commented 1 year ago

@HripsimeS https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection mentioned yolov5 repo commitId: cd35a009ba964331abccd30f6fa0614224105d39. Maybe you switch to this commit, generate the model & use it against pyTorch lib 1.10.0 should do the trick.

HripsimeS commented 1 year ago

@fahim44 thanks for your reply! I removed and imported ObjectDetection project on Android studio, now initial model yolov5s.torchscript.ptl works good on both PyTorch lib versions 1.10.0, but with 1.12.2.

But the same issues with my custom model, with 1.10.0 version getting Lite Interpreter version error and with 1.12.2 version app is launching but can't get good predictions. As the export.py in official YOLO5 does not really convert pt model to torchscript.ptl (in converts to torchscript) https://github.com/ultralytics/yolov5/blob/master/export.py I used this export.py which converts to torchscript.ptl https://github.com/jeffxtang/yolov5/blob/master/models/export.py

So I hope the issue is not in converted torchscript.ptl model... And you also recommend to use this command? git reset --hard cd35a009ba964331abccd30f6fa0614224105d39

I use Windows OS, in that case where I need to write that command? Thank you in advance :)

fahim44 commented 1 year ago

@HripsimeS I assume you have git installed on your machine. Go to your yolov5 directory, right-click with your mouse, and from the context menu select git bash here. It will open the terminal. Now run the command here. After that, generate the ptl file again.

HripsimeS commented 1 year ago

@fahim44 thank you, it was helpful. I wrote in git bash here git reset --hard cd35a009ba964331abccd30f6fa0614224105d39 This error came out eventually fatal: Could not parse object 'cd35a009ba964331abccd30f6fa0614224105d39'.

When I run this command to check git checkout cd35a009ba964331abccd30f6fa0614224105d39 This error we can see fatal: reference is not a tree: cd35a009ba964331abccd30f6fa0614224105d39

Seems that reference is not acctual anymore and maybe there is a new number ?

fahim44 commented 1 year ago

@HripsimeS can you try to follow the steps on https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection#1-prepare-the-model? I think you need to clone https://github.com/ultralytics/yolov5

HripsimeS commented 1 year ago

@fahim44 I did following all steps git clone, then I modify https://github.com/ultralytics/yolov5/blob/master/export.py file to convert my model to .torchscript.ptl format. On pictures it works more or less okay, but it does not work with LIVE real time detection. I don't think the issue in my converted model, maybe PyTorch lib version or need to change other parameters in scripts to oprimize better for my model. So yeah, I am stuck on this because it is not clear why LIVE detection is bad on android.

NeighborhoodCoding commented 1 year ago

You may check model size, above 200MB, I have memory crash error, it work wrong. Also downgrading the torch may help. The app source and API may change when the time flows. I checked 6 month ago, it works well and I have my YOLO model for antenna detection

NeighborhoodCoding commented 1 year ago

Also, I think this solution may help to build real-time your app.

https://pytorchvideo.org/docs/tutorial_accelerator_build_your_model

HripsimeS commented 1 year ago

@NeighborhoodCoding thanks for your reply! My model.torchscript.ptl is just 27MB, so I don't think it is so big to cause a memory crash error. And thanks for the link about build an accelerator for the model, I will check it 🥇

WhitekingWing commented 1 year ago

@fahim44感谢您的回复!我在Android studio上删除并导入了ObjectDetection项目,现在初始模型yolov5s.torchscript.ptl在PyTorch lib版本1.10.0上都运行良好,但使用1.12.2。

但是我的自定义模型也存在同样的问题,1.10.0 版本出现 Lite 解释器版本错误,1.12.2 版本应用程序正在启动,但无法获得良好的预测。由于官方 YOLO5 中的 export.py 并没有真正将 pt 模型转换为 torchscript.ptl(转换为火炬脚本),https://github.com/ultralytics/yolov5/blob/master/export.py 我使用了这个转换为 torchscript.ptl export.py https://github.com/jeffxtang/yolov5/blob/master/models/export.py

所以我希望问题不在转换后的火炬脚本.ptl 模型中......并且您还建议使用此命令? Git 重置 --硬 CD35A009BA964331abccd30f6fa0614224105d39

我使用 Windows 操作系统,在这种情况下我需要编写该命令?提前谢谢你:)

Traceback (most recent call last): File "F:\yolov5-master\yolov5-master\export.py", line 32, in model = attempt_load(opt.weights, map_location=torch.device('cpu')) # load FP32 model TypeError: attempt_load() got an unexpected keyword argument 'map_location' I use your export.py and occur this error.