longcw / pytorch2caffe

Convert PyTorch model to Caffemodel
541 stars 151 forks source link

Add ConvTranspose2d #7

Open ssdutHB opened 6 years ago

ssdutHB commented 6 years ago

Could you please add nn.ConvTranspose2d into the transformation? Because the ConvTranspose2d operation is now very common in neural networks.

longcw commented 6 years ago

I think it's feasible and easy to convert torch.nn.ConvTransposed2d to Deconvolution since I already did that for UpsamplingBilinear2d. But I don't have enough time to implement it now. You can follow here to implement it. You need to 1) convert the layer parameters of ConvTransposed2d to that of Deconvolution in caffe, 2) and check if weight and bias of ConvTransposed2d are saved properly for caffe: https://github.com/longcw/pytorch2caffe/blob/master/pytorch2caffe.py#L101

ssdutHB commented 6 years ago

Hi, longcw. Thanks for your quick reply. Yesterday, I transferred "conv_transpose2d" from pytorch to caffe as what you said above. But I found that there is a parameter "output_padding" in "conv_transpose2d". That optional parameter will add specific padding to the result of deconvolution, so that the user can get a specific output shape. But the Decovolution layer in caffe has no such parameter. So it's a problem now for me. Consider, if we want to convert a deconv layer from pytorch, which is "nn.ConvTranspose2d(128,128,kernel_size =3,stride=2,padding =1,output_padding = 1)", How can we write a caffe layer?

The code above can convert a tensor with shape(batch,channel,width,height) to shape(batch,channel,widthx2,heightx2).

if we write a caffe layer with above parameter layer { name: "ConvNdBackward73" type: "Deconvolution" bottom: "AddBackward72" top: "ConvNdBackward73" convolution_param { num_output: 128 pad: 1 kernel_size: 3 stride: 2 bias_term: false } }

we get (batch,channel,width,height) to (batch,channel,widthx2-1.heightx2-1)

In conclusion, I think the deconv operation in pytorch can't be perfectly converted to caffe. Am I right?

longcw commented 6 years ago

So the only difference is the output_padding. If the output_padding is not zero then we will need an extra "padding layer" (?) in caffe. BTW, welcome to send pull requests for your improvement.

OPPOA113 commented 5 years ago

@longcw @ssdutHB so finally, if "conv_transpose2d" can be transferred to "Deconvolution"? and how? thank you