android-demo-app icon indicating copy to clipboard operation
android-demo-app copied to clipboard

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

Open qichuangguo opened this issue 3 years ago • 13 comments

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)

qichuangguo avatar Jun 21 '21 04:06 qichuangguo

Have you resolved this problem ?

ZhongXianer avatar Jul 08 '21 11:07 ZhongXianer

@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 ?

danudeep90 avatar Jul 12 '21 13:07 danudeep90

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 avatar Jul 25 '21 19:07 gleb-papchihin

I have the same issue on windows

myasser63 avatar Sep 07 '21 22:09 myasser63

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

l-atome avatar Nov 28 '21 16:11 l-atome

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

jithin8mathew avatar Jan 17 '22 03:01 jithin8mathew

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!

YeetMasterYeet avatar Jan 29 '22 11:01 YeetMasterYeet

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.

codgeek avatar Apr 19 '22 11:04 codgeek

  1. use the same version of torch(yolov5 & android)
  2. use statment _save_for_lite_interpreter() to save the model

xugaoxiang avatar Apr 20 '22 16:04 xugaoxiang

@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!

HripsimeS avatar Dec 13 '22 14:12 HripsimeS

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

jithin8mathew avatar Dec 30 '22 03:12 jithin8mathew

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

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

WhitekingWing avatar Apr 03 '23 12:04 WhitekingWing

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

lhy011218 avatar Apr 20 '23 14:04 lhy011218