Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.21k stars 4.15k forks source link

trouble with converted a custom yolov5s.pt #4261

Open NacerFaraj opened 1 year ago

NacerFaraj commented 1 year ago

Hi, I am having trouble with a converted custom yolov5s.pt to ncnn:

I have trained a custom yolov5s.pt object detector with two classes, say my_yolov5s.pt. Then, I converted it to my_yolov5s.onnx using the yolov5/export.py:

python export.py --weights my_yolov5s.pt --include onnx --simplify

Later, I tried to convert the model into ncnn using onnx2ncnn.exe: onnx2ncnn.exe my_yolov5s.onnx my_yolov5s.param my_yolov5s.bin

Up to this point, everything went smoothly. However, while I am trying to inference using ncnn version of my model, it crashes at ex.extract("output0", out)

I am attaching the screenshot of the Exception message. I appreciate any help or suggestions.

2022-10-11_9-52-19

wzyforgit commented 1 year ago

ncnn is open source, you can debug with source code. And if your model is trained by pytorch, please use pnnx to convert it to ncnn.

NacerFaraj commented 1 year ago

I tried pnnx as follows: pnnx.exe my_yolov5s.pt inputshape=[1,3,640,640]

But I got the following messages:

pnnxparam = my_yolov5s.pnnx.param
pnnxbin = my_yolov5s.pnnx.bin
pnnxpy = my_yolov5s_pnnx.py
ncnnparam = my_yolov5s.ncnn.param
ncnnbin = my_yolov5s.ncnn.bin
ncnnpy = my_yolov5s_ncnn.py
optlevel = 2
device = cpu
inputshape = [1,3,640,640]f32
inputshape2 =
customop =
moduleop =
Load torchscript failed: PytorchStreamReader failed locating file constants.pkl: file not found
Exception raised from valid at ..\..\caffe2\serialize\inline_container.cc:158 (most recent call first):
00007FFE9A2BA29200007FFE9A2BA230 c10.dll!c10::Error::Error [<unknown file> @ <unknown line number>]
00007FFE9A2B9D1E00007FFE9A2B9CD0 c10.dll!c10::detail::torchCheckFail [<unknown file> @ <unknown line number>]
00007FFE2F1D08A300007FFE2F1D0810 torch_cpu.dll!caffe2::serialize::PyTorchStreamReader::valid [<unknown file> @ <unknown line number>]
00007FFE2F1CF4AD00007FFE2F1CF3F0 torch_cpu.dll!caffe2::serialize::PyTorchStreamReader::getRecordID [<unknown file> @ <unknown line number>]
00007FFE2F1CF30200007FFE2F1CF2B0 torch_cpu.dll!caffe2::serialize::PyTorchStreamReader::getRecord [<unknown file> @ <unknown line number>]
00007FFE303FF68600007FFE303FF490 torch_cpu.dll!torch::jit::readArchiveAndTensors [<unknown file> @ <unknown line number>]
00007FFE303FDC1F00007FFE303FD4C0 torch_cpu.dll!torch::jit::load [<unknown file> @ <unknown line number>]
00007FFE303FB98900007FFE303F9400 torch_cpu.dll!torch::jit::CallStackDebugInfoUnpickler::unpickle [<unknown file> @ <unknown line number>]
00007FFE303FD6CB00007FFE303FD4C0 torch_cpu.dll!torch::jit::load [<unknown file> @ <unknown line number>]
00007FFE303FD16300007FFE303FD040 torch_cpu.dll!torch::jit::load [<unknown file> @ <unknown line number>]
00007FF69E5F6DFE00007FF69E5F5420 pnnx.exe!c10::ivalue::Future::waitAndThrow [<unknown file> @ <unknown line number>]
00007FF69E72347C00007FF69E5F5420 pnnx.exe!c10::ivalue::Future::waitAndThrow [<unknown file> @ <unknown line number>]
00007FFEC5A7703400007FFEC5A77020 KERNEL32.DLL!BaseThreadInitThunk [<unknown file> @ <unknown line number>]
00007FFEC61226A100007FFEC6122680 ntdll.dll!RtlUserThreadStart [<unknown file> @ <unknown line number>]

Please export model to torchscript as follows
------------------------------------------
import torch
import torchvision.models as models

net = models.resnet18(pretrained=True)
net = net.eval()

x = torch.rand(1, 3, 224, 224)
mod = torch.jit.trace(net, x)
mod.save("resnet18.pt")
------------------------------------------

BTW: I trained my model using the main repository of yolov5 ([https://github.com/ultralytics/yolov5])

Ghustwb commented 1 year ago

I tried to convert YOLOX to NCNN by PNNX, I also met this issue "Load torchscript failed: PytorchStreamReader failed locating file constants.pkl: file not found"

nihui commented 1 month ago

针对onnx模型转换的各种问题,推荐使用最新的pnnx工具转换到ncnn In view of various problems in onnx model conversion, it is recommended to use the latest pnnx tool to convert your model to ncnn

pip install pnnx
pnnx model.onnx inputshape=[1,3,224,224]

详细参考文档 Detailed reference documentation https://github.com/pnnx/pnnx https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#how-to-use-pnnx

jackynew8669 commented 3 weeks ago

我尝试通过 PNNX 将 YOLOX 转换为 NCNN,也遇到了这个问题 “加载 torchscript 失败:PytorchStreamReader 无法定位文件 constants.pkl:找不到文件”

我也是一样的

jackynew8669 commented 3 weeks ago

ch) root@wjmqtuiaofmgpalu-snow-78c7b646d-5vb4w:/data/miniconda/envs/torch/lib/python3.10/site-packages/pnnx# ./pnnx /data/coding/best.pt inputshape=[1,3,224, 224] pnnxparam = /data/coding/best.pnnx.param pnnxbin = /data/coding/best.pnnx.bin pnnxpy = /data/coding/best_pnnx.py pnnxonnx = /data/coding/best.pnnx.onnx ncnnparam = /data/coding/best.ncnn.param ncnnbin = /data/coding/best.ncnn.bin ncnnpy = /data/coding/best_ncnn.py fp16 = 1 optlevel = 2 device = cpu inputshape = [1,3,224,224]f32 inputshape2 = customop = moduleop = Load torchscript failed: PytorchStreamReader failed locating file constants.pkl: file not found Exception raised from valid at /project/pytorch-v2.4.0/caffe2/serialize/inline_container.cc:236 (most recent call first): frame #0: ./pnnx() [0x2a8929e] frame #1: ./pnnx() [0x2a88c9d] frame #2: ./pnnx() [0x486b05] frame #3: ./pnnx() [0x24d0f79] frame #4: ./pnnx() [0x24d10f5] frame #5: ./pnnx() [0x24d146c] frame #6: ./pnnx() [0x2843980] frame #7: ./pnnx() [0x283cc4b] frame #8: ./pnnx() [0x2840cac] frame #9: ./pnnx() [0x2842459] frame #10: ./pnnx() [0x284260e] frame #11: ./pnnx() [0x28426bc] frame #12: ./pnnx() [0x5ec2c3] frame #13: ./pnnx() [0x489d33] frame #14: + 0x29d90 (0x7f30ff429d90 in /usr/lib/x86_64-linux-gnu/libc.so.6) frame #15: __libc_start_main + 0x80 (0x7f30ff429e40 in /usr/lib/x86_64-linux-gnu/libc.so.6) frame #16: ./pnnx() [0x4ce107]