How to achieve same dimension or padding='same' after Conv2d in Pytorch?

Hello there,
I am using following code in model ,

I=torch.randn(40,1,128,186)
m=nn.Sequential(
nn.Conv2d(1,16, kernel_size=5, stride=1),
nn.BatchNorm2d(16),
nn.ReLU())
O=m(I)

I want my output to be of same size as input ie (40,1,128,186)
I tried using

O=m(F.pad(I,(0,0,2,2)))
But getting (40, 16, 128, 182)

Can anyone help plz.

From what I know, Pytorch doesn’t support this as an inbuilt option, TensorFlow does. Checkout this discussion which mentions how dynamic loading makes it hard.
However, there could be ways to hack it by combining asymmtric padding layers with conv2d layers. I wouldn’t bother doing it, unless super useful and just go with the inbuilt padding options. More discussion here.

1 Like