yolov5-4.0训练模型pt文件转onnx出现的问题:Process finished with exit code -1073741819 (0xC0000005)

问题描述:

环境:
pytorch1.7.1 cuda10.1 opencv4.5.3 vs2019
首先:看了网上很多教程,要修改focus函数的实现方式,具体修改如下:
modols/commen.py,原因是onnx不支持切片操作,focus中原有的::2这样的操作是无法转出的。办法就是利用common函数中子带的
Contract操作。


class Focus(nn.Module):
    # Focus wh information into c-space
    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):  # ch_in, ch_out, kernel, stride, padding, groups
        super(Focus, self).__init__()
        self.conv = Conv(c1 * 4, c2, k, s, p, g, act)

    def forward(self, x):  # x(b,c,w,h) -> y(b,4c,w/2,h/2)
        # return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) #修改前
        #============修改后=================#
        contract = Contract(gain=2)
        return self.conv(contract(x))

之后,在官方自带的models/export.py中转出。
终端输出:

Namespace(batch_size=1, img_size=[640, 640], weights='./yolov5s.pt')
Fusing layers... 
Model Summary: 224 layers, 7266973 parameters, 0 gradients, 17.0 GFLOPS
A:\Anaconda\envs\Torch17\lib\site-packages\torch\jit\_trace.py:940: TracerWarning: Encountering a list at the output of the tracer might cause the trace to be incorrect, this is only valid if the container structure does not change based on the module's inputs. Consider using a constant container instead (e.g. for `list`, use a `tuple` instead. for `dict`, use a `NamedTuple` instead). If you absolutely need this and know the side effects, pass strict=False to trace() to allow this behavior.
  _force_outplace,
TorchScript export success, saved as ./yolov5s.torchscript.pt
Starting ONNX export with onnx 1.8.1...
Process finished with exit code -1073741819 (0xC0000005)

转出程序就触发终止了,报此类错误,并不明白为什么。看别的文章有说是win10更新了驱动,导致目前的NVCUDA64.DLL版本与nvcc-V查看的不一致,目前确实有这个情况,我目前NVIDA控制面板中的驱动显示版本11.1,nvcc-V 显示CUDA10.1;但是网络训练和推理都没问题,就转ONNX的时候出现这个问题,我觉得不是pytorch版本问题吧。希望有想法的朋友指导一下,是源码哪里设置输入有问题吗?

onnx换成1.7.0的版本看看,不行的话换成yolov5-v5最新版本,一样的修改方法导出来看看能不能导出onnx文件