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

PNNX无法转换torchvision.vit model #4149

Open JiahangXu opened 2 years ago

JiahangXu commented 2 years ago

error log | 日志或报错信息 | ログ

我使用的python代码如下:

import torch
import torchvision.models as models

model = models.vit_l_16(pretrained=True)
model = model.eval()

model_input = torch.rand(1, 3, 224, 224)
mod = torch.jit.trace(model, model_input, check_trace=False)

mod.save("torchvit_l_16.pt")

然后使用https://github.com/pnnx/pnnx/releases 下载的windows版pnnx进行convert,command为

.\pnnx-20220720-windows\pnnx-20220720-windows\pnnx.exe torchvit_l_16.pt inputshape=[1,3,224,224]

然后报错:

pnnxparam = torchvit_l_16.pnnx.param
pnnxbin = torchvit_l_16.pnnx.bin
pnnxpy = torchvit_l_16_pnnx.py
ncnnparam = torchvit_l_16.ncnn.param
ncnnbin = torchvit_l_16.ncnn.bin
ncnnpy = torchvit_l_16_ncnn.py
optlevel = 2
device = cpu
inputshape = [1,3,224,224]f32
inputshape2 =
customop =
moduleop =
Load torchscript failed: version_ <= kMaxSupportedFileFormatVersionINTERNAL ASSERT FAILED at "..\\..\\caffe2\\serialize\\inline_container.cc":147, please report a bug to PyTorch. Attempted to read a PyTorch file with version 10, but the maximum supported version for reading is 6. Your PyTorch installation may be too old.
Exception raised from init at ..\..\caffe2\serialize\inline_container.cc:147 (most recent call first):
00007FFCE246A29200007FFCE246A230 c10.dll!c10::Error::Error [<unknown file> @ <unknown line number>]
00007FFCE2469D1E00007FFCE2469CD0 c10.dll!c10::detail::torchCheckFail [<unknown file> @ <unknown line number>]      
00007FFCE2469DCF00007FFCE2469D90 c10.dll!c10::detail::torchInternalAssertFail [<unknown file> @ <unknown line number>]
00007FFC84D9011200007FFC84D8F850 torch_cpu.dll!caffe2::serialize::PyTorchStreamReader::init [<unknown file> @ <unknown line number>]
00007FFC84D8E46B00007FFC84D8E3C0 torch_cpu.dll!caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader [<unknown file> @ <unknown line number>]
00007FFC85FBD5CA00007FFC85FBD4C0 torch_cpu.dll!torch::jit::load [<unknown file> @ <unknown line number>]
00007FFC85FBD16300007FFC85FBD040 torch_cpu.dll!torch::jit::load [<unknown file> @ <unknown line number>]
00007FF68B946DFE00007FF68B945420 pnnx.exe!c10::ivalue::Future::waitAndThrow [<unknown file> @ <unknown line number>]
00007FF68BA7347C00007FF68B945420 pnnx.exe!c10::ivalue::Future::waitAndThrow [<unknown file> @ <unknown line number>]
00007FFD192354E000007FFD192354D0 KERNEL32.DLL!BaseThreadInitThunk [<unknown file> @ <unknown line number>]
00007FFD197C485B00007FFD197C4830 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")
------------------------------------------

context | 编译/运行环境 | バックグラウンド

torch-1.12.1, torchvision-0.13.1, windows 11

JiahangXu commented 2 years ago

@nihui 求大佬回复~

parrotsky commented 1 year ago

pytorch版本太旧了.

duany049 commented 1 year ago

halo,这个问题你解决了么?

parrotsky commented 1 year ago

halo,这个问题你解决了么?

Pytorch新版本没有问题啊

duany049 commented 1 year ago

您好,也就是说torch2.0转的模型,在onnx和pnnx工具上都能转NCNN了? 刚用的是1.13,我去试试torch2.0

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