pytorch / android-demo-app

PyTorch android examples of usage in applications
1.47k stars 606 forks source link

com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found () #157

Open qichuangguo opened 3 years ago

qichuangguo commented 3 years ago

Using yolov5 model to convert torchscript and Android object detection to report errors:

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:157 (most recent call first): (no backtrace available)

ZhongXianer commented 3 years ago

Have you resolved this problem ?

danudeep90 commented 3 years ago

@qichuangguo : Were you able to find a solution to this problem ?.. Looks like it is working on windows , mac seems to be an issue. did you run it on mac ?

gleb-papchihin commented 3 years ago

Hi, buddies) This information solved the problem for me. Hope this will be helpful for you as well. https://pytorch.org/tutorials/recipes/mobile_interpreter.html.

spoiler alert. You should use script._save_for_lite_interpreter() instead script.save()

myasser63 commented 3 years ago

I have the same issue on windows

l-atome commented 2 years ago

Hello, i change the name of the file "yolov5s.torchscript.ptl" to "uu.ptl" and also in the file "pytorch/demo/objectdetection/MainActivity.java" and now, it's work (don't ask me why!!!)

jithin8mathew commented 2 years ago

Hello, i change the name of the file "yolov5s.torchscript.ptl" to "uu.ptl" and also in the file "pytorch/demo/objectdetection/MainActivity.java" and now, it's work (don't ask me why!!!)

removing _ from name worked for me. For, eg: best.torchscript_s.ptl to best.torchscripts.ptl

YeetMasterYeet commented 2 years ago

Hi, buddies) This information solved the problem for me. Hope this will be helpful for you as well. https://pytorch.org/tutorials/recipes/mobile_interpreter.html.

spoiler alert. You should use script._save_for_lite_interpreter() instead script.save() @gleb-papchihin, hello can you please elaborate furthermore, i have a custom .pt file that i want to convert and i cannot seem to do it, thank you in andvance!

codgeek commented 2 years ago

Hi, buddies) This information solved the problem for me. Hope this will be helpful for you as well. https://pytorch.org/tutorials/recipes/mobile_interpreter.html.

spoiler alert. You should use script._save_for_lite_interpreter() instead script.save()

This works for me on pytorch 1.10.2 ! Seem The pytorch mobile is only compatible with _save_for_lite_interpreter() model.

xugaoxiang commented 2 years ago
  1. use the same version of torch(yolov5 & android)
  2. use statment _save_for_lite_interpreter() to save the model
HripsimeS commented 1 year ago

@jithin8mathew Hello. Can you please send a link of export.py file you used to convert .pt model to .torchscripts.ptl Thanks a lot in advance!

jithin8mathew commented 1 year ago

Hi @HripsimeS, I have a couple of export.py scripts in my project directory right now and can't figure out which one I used for conversion (it is been a while since I haven't worked on it). For some reason, I had an issue when converting a model trained on Windows OS, but training the same model with Linux OS solved this issue, and I still don't know why it happened.

I trained my model (in Google Colab) with the following versions of packages:

Pytorch 1.9.0 cuda (GPU) at batch size 16 (torch==1.9.0, torchvision==0.10.0, ) Model: YOLOv5s (I downloaded the repo on August 2021, if you want to check the version of YOLOv5)

Here are some changes I made to export.py

def export_torchscript(model, im, file, optimize, prefix=colorstr('TorchScript:')):
    # YOLOv5 TorchScript model export
    try:
        LOGGER.info(f'\n{prefix} starting export with torch {torch.__version__}...')
        f = file.with_suffix('.torchscript.pt')

        ts = torch.jit.trace(model, im, strict=False)
        d = {"shape": im.shape, "stride": int(max(model.stride)), "names": model.names}
        extra_files = {'config.txt': json.dumps(d)}  # torch._C.ExtraFilesMap()
        (optimize_for_mobile(ts) if optimize else ts).save(f, _extra_files=extra_files)

        LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
    except Exception as e:
        LOGGER.info(f'{prefix} export failure: {e}')

Let me know if this helps. Good luck!!!

WhitekingWing commented 1 year ago

您好,我将文件“yolov5s.torchscript.ptl”的名称更改为“uu.ptl”,并在文件“pytorch/demo/objectdetection/MainActivity.java”中更改,现在,它是工作(不要问我为什么!!!)

It can't work for me!!!!!!

lhyin01 commented 1 year ago

I have the same problem,and I try to use script._save_for_lite_interpreter() instead script.save().Now,it works successfully!