Hi,
I exported a small model successfully to ONNX. However, there are two issues, I would like to resolve.
My model is similar to Lenet with a few modifications:
class NetConvOnly(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout2d(0.25)
self.dropout2 = nn.Dropout2d(0.5)
self.conv3 = nn.Conv2d(64, 128, 1, 1)
self.conv4 = nn.Conv2d(128, 10, 1, 1)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = self.conv3(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.conv4(x)
output = F.avg_pool2d(x, (12, 12), 1)
return output
The export function is:
dummy_input = torch.randn(1, 1, 28, 28, device=device)
torch.onnx.export(model,
dummy_input,
"mnist_cnn.onnx",
export_params=True,
opset_version=11,
input_names=["input"],
output_names=["output"],
verbose=True)
The verbose output is:
graph(%input : Float(1, 1, 28, 28),
%conv1.weight : Float(32, 1, 3, 3),
%conv1.bias : Float(32),
%conv2.weight : Float(64, 32, 3, 3),
%conv2.bias : Float(64),
%conv3.weight : Float(128, 64, 1, 1),
%conv3.bias : Float(128),
%conv4.weight : Float(10, 128, 1, 1),
%conv4.bias : Float(10)):
%9 : Float(1, 32, 26, 26) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[0, 0, 0, 0], strides=[1, 1]](%input, %conv1.weight, %conv1.bias) # /home/martin/.local/lib/python3.6/site-packages/torch/nn/modules/conv.py:342:0
%10 : Float(1, 32, 26, 26) = onnx::Relu(%9) # /home/martin/.local/lib/python3.6/site-packages/torch/nn/functional.py:914:0
%11 : Float(1, 64, 24, 24) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[0, 0, 0, 0], strides=[1, 1]](%10, %conv2.weight, %conv2.bias) # /home/martin/.local/lib/python3.6/site-packages/torch/nn/modules/conv.py:342:0
%12 : Float(1, 64, 24, 24) = onnx::Relu(%11) # /home/martin/.local/lib/python3.6/site-packages/torch/nn/functional.py:914:0
%13 : Float(1, 64, 12, 12) = onnx::MaxPool[ceil_mode=0, kernel_shape=[2, 2], pads=[0, 0, 0, 0], strides=[2, 2]](%12) # /home/martin/.local/lib/python3.6/site-packages/torch/nn/functional.py:845:0
%14 : Float(1, 128, 12, 12) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%13, %conv3.weight, %conv3.bias) # /home/martin/.local/lib/python3.6/site-packages/torch/nn/modules/conv.py:342:0
%15 : Float(1, 128, 12, 12) = onnx::Relu(%14) # /home/martin/.local/lib/python3.6/site-packages/torch/nn/functional.py:845:0
%16 : Float(1, 10, 12, 12) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%15, %conv4.weight, %conv4.bias) # /home/martin/.local/lib/python3.6/site-packages/torch/nn/modules/conv.py:342:0
%17 : Tensor = onnx::Constant[value= 0 0 0 0 0 0 0 0 [ CPULongType{8} ]]()
%18 : Tensor = onnx::Pad[mode="constant"](%16, %17)
%output : Float(1, 10, 1, 1) = onnx::AveragePool[ceil_mode=0, kernel_shape=[12, 12], pads=[0, 0, 0, 0], strides=[1, 1]](%18) # lenet.py:71:0
return (%output)
The two issues are:
- As far as I got, it is only possible to assign the input names manually with a flat list. Is it possible to programmatically assign names to the inputs 9 to 18, like it is done for the weights and biases?
- Why is there a padding layer and how can I remove it? I guess there is some wrong parameter in the model definition. But at least the dimensions of the convolution and following global average pooling look ok to me.
If needed, I can also try to make a MWE.