无法使用导出导出 Yolov5 模型

Cannot export Yolov5 model using export

我在 colab.research 环境中训练了 Yolov5s 模型。训练后,我将 best.pt 移动到主 yolov5 目录并将文件重命名为 yolov5s.pt.

调用 export.py 后我得到下面列出的错误

!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

我正在尝试导出此模型以在 Android 应用程序中使用它。如果我使用 best.pt 我在加载网络时遇到其他错误:

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

我自己还没有找到任何解决方案。你知道我该如何解决这个错误吗?

编辑: 固定 - 如果我链接到目录,我可以导出模型:

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

修复了运行目录中的链接模型:

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