2
$\begingroup$

From the Pytorch docs on Conv2Transpose2d, the formula to compute the output of the upsampled convolution (assuming square input and no kernel dilation) is:

$$H_{out} = (H_{in} - 1) \times S - 2P_{in}+P_{out}+1.$$

Here, $H_{out}$ and $H_{in}$ are the heights of the input and output images, $S$ is stride, and $P_{in}$ and $P_{out}$ are input and output padding.

I want to 2x upsample a 256x256 image to 512x512, using a 3x3 convolution, to simulate a convolution with fractional stride of $1/2$.

Given a stride of 2, I worked out that we must have $2 p_{in} - p_{out} = 1$, so $(p_{in}, p_{out}) = (1,1)$ is a solution.

My question is: why is $p_{out}$ necessary to achieve 2x sampling here? From this PyTorch forum post, I learned that $p_{out}$ is asymmetric padding for the right and bottom sides.

However, everywhere I look online (such as slide 54 from Stanford's CS231n 2016 lecture), it says stride 2 and pad 1 produces 2x upsampling, using 3x3 convolution, with no distinction between input/output padding.

Consider the following PyTorch code:

nn.ConvTranspose2d(in_channels, out_channels, stride=2, kernel_size=3, padding=1)

Applying that function to a 256x256 image, the dimensions of the output are 513x513. It is only until I add the parameter output_padding=1 that the dimensions are properly upsampled 2x, as confirmed by the earlier equation.

I'm hoping someone can help clarify the upsampling process in better detail, because I don't understand why "stride 2, pad 1" doesn't work unless the output is padded as well.

Thanks

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.