Hi there. I tried the following code to convert a pytorch deconvolution layer to caffe2 through ONNX, but ended up with inconsistent output shapes.  I am new to ONNX and Caffe2, just wondering if there is anything wrong here. Thanks!


Code:

import torch
import torch.nn as nn
import torch.onnx as torch_onnx
from torch.autograd import Variable
import onnx
import onnx_caffe2.backend

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self._deconv = nn.ConvTranspose2d(512, 256, 3, 2, 1, output_padding = 1)

    def forward(self, inputs):
        return self._deconv(inputs)

input_shape = (1, 512, 4, 4)
model_onnx_path = "convTranspose_model.onnx"

model = Model()
model.train(False)

dummy_input = Variable(torch.randn(input_shape))
onnx_out = torch_onnx._export(model, dummy_input, model_onnx_path, export_params=True, verbose=True)
model = onnx.load(model_onnx_path)
onnx.checker.check_model(model)
prepared_backend = onnx_caffe2.backend.prepare(model)

W = {model.graph.input[0].name: dummy_input.data.numpy()}
caffe2_out = prepared_backend.run(W)[0]

print(onnx_out.shape, caffe2_out.shape)




Results:

graph(%0 : Float(1, 512, 4, 4)
      %1 : Float(512, 256, 3, 3)
      %2 : Float(256)) {
  %3 : Float(1, 256, 8, 8) = onnx::ConvTranspose[dilations=[1, 1], group=1, kernel_shape=[3, 3], output_padding=[1, 1], pads=[1, 1, 1, 1], strides=[2, 2]](%0, %1, %2), scope: Model/ConvTranspose2d[_deconv]
  return (%3);
}

torch.Size([1, 256, 8, 8]) (1, 256, 7, 7)




Environment:

# Name                    Version                   Build  Channel
pytorch                   0.4.0           py36_cuda9.1.85_cudnn7.1.2_1  [cuda91]  pytorch
torchvision               0.2.1                    py36_1    pytorch
caffe2                    0.8.dev         py36_2018.05.14    caffe2
onnx-caffe2               0.2.1            py36h71a8c3b_1    ezyang