Pth to onnx : _convolution_mode error

Hi,

I am trying to convert .pth to .onnx file,

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        cnn = nn.Sequential()
        cnn.add_module('c1', nn.Conv2d(3, 32, 3, 1, 1))
        cnn.add_module('r1', nn.ReLU())
        .....
        .....
        .....
        cnn.add_module('l3', nn.Linear(512, 2))
        cnn.add_module('ls', nn.LogSoftmax())

        self.cnn = cnn

    def forward(self, x):
        output = self.cnn(x)
        return output

sample_batch_image = torch.zeros(64, 3, 224, 224).to(device)

torch.onnx.export(model, sample_batch_image, 'output.onnx', verbose=True, opset_version=13, export_params=True, training=torch.onnx.TrainingMode.EVAL, do_constant_folding=True, input_names = ['cnn.c1'], output_names=['cnn.ls'], dynamic_axes={'cnn.c1': {0: 'batch'}, 'cnn.ls': {0: 'batch'} ,})

I got this error,

RuntimeError: Exporting the operator _convolution_mode to ONNX opset version 13 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub.

1 Like

I got the same error, did you solve it?

@Matt2 , yes, I update the pytorch version to 1.11, the issue is solved and changed the padding to integer

I’ve updated pytorch version to 1.11.0-cpu, but I still get the same error.

@yanw,

Then, you can replace the nn.Conv2d with this

from functools import reduce
from operator import __add__
class Conv2dSamePadding(nn.Conv2d):
    def __init__(self,*args,**kwargs):
        super(Conv2dSamePadding, self).__init__(*args, **kwargs)
        self.zero_pad_2d = nn.ZeroPad2d(reduce(__add__,
            [(k // 2 + (k - 2 * (k // 2)) - 1, k // 2) for k in self.kernel_size[::-1]]))

    def forward(self, input):
        return  self._conv_forward(self.zero_pad_2d(input), self.weight, self.bias)

For example,
cnn.add_module('c1', Conv2dSamePadding(3, 64, 2, 1))

I used this custom Conv. layer, which works same as nn.Conv2d and it wont make issue when converting the model to .onnx file

Happy Coding

1 Like