Search code examples
pythonandroidpytorchyolov5

Cannot export Yolov5 model using export


I've trained Yolov5s model in colab.research env. After training I've moved best.pt to main yolov5 directory and renamed file to yolov5s.pt.

After calling export.py i got error listed below

!python models/export.py
Namespace(batch_size=1, img_size=[640, 640], weights='./yolov5s.pt')
Traceback (most recent call last):
  File "models/export.py", line 33, in <module>
    model = attempt_load(opt.weights, map_location=torch.device('cpu'))  # load FP32 model
  File "./models/experimental.py", line 137, in attempt_load
    model.append(torch.load(w, map_location=map_location)['model'].float().fuse().eval())  # load FP32 model
  File "/usr/local/lib/python3.7/dist-packages/torch/serialization.py", line 587, in load
    with _open_zipfile_reader(opened_file) as opened_zipfile:
  File "/usr/local/lib/python3.7/dist-packages/torch/serialization.py", line 242, in __init__
    super(_open_zipfile_reader, self).__init__(torch._C.PyTorchFileReader(name_or_buffer))
RuntimeError: [enforce fail at inline_container.cc:145] . PytorchStreamReader failed reading zip archive: failed finding central directory

I'm trying to export this model to use it in Android App. If I use best.pt I've got other error on loading network:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.pytorch.demo.objectdetection, PID: 6935
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.demo.objectdetection/org.pytorch.demo.objectdetection.MainActivity}: com.facebook.jni.CppException: [enforce fail at inline_container.cc:222] . file not found: archive/constants.pkl
    (no backtrace available)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: com.facebook.jni.CppException: [enforce fail at inline_container.cc:222] . file not found: archive/constants.pkl
    (no backtrace available)
        at org.pytorch.NativePeer.initHybridAndroidAsset(Native Method)
        at org.pytorch.NativePeer.<init>(NativePeer.java:27)
        at org.pytorch.PyTorchAndroid.loadModuleFromAsset(PyTorchAndroid.java:31)
        at org.pytorch.demo.objectdetection.MainActivity.onCreate(MainActivity.java:165)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
            ... 11 more
I/Process: Sending signal. PID: 6935 SIG: 9

I haven't found any solutions on my own. Do you have any idea how can I fix this error?

Edit: Fixed - I could export model if I linked to directory:

!python models/export.py --weights ./runs/train/exp7/weights/best.pt --img 640 --batch 1 

Solution

  • Fixed with linking model in runs directory:

    !python models/export.py --weights ./runs/train//exp/weights/best.pt --img 640 --batch 1