Operations made by Conv2D in Pytorch

Viewed 16

According to Pytorch documentation of Conv2D(c_in,c_out) (the other parameters are irrelevant for this question):

  • c_in is the number of channels of the input image.
  • c_out is the number of channel produced by the convolution layer

What I don't understand is how many kernels/filters there are. In many post I have seen that c_out is indeed the number of kernels and that would mean that if I have an input image of 3 channels, and I set c_out=10, the output would be 30 channels, but in reality I get 10 channels.

1 Answers

A convolution operation is performed by "sliding" a kernel over the input, and computing the correlation between the kernel and the corresponding input. This computation yields a single scalar number for each "window".
Therefore, if you have an input with a shape c_inxhxw, a single kernel has c_inxk_hxk_w parameters. Sliding this kernel over the input will yield an output of shape 1xhxw (assuming proper padding). As you can see, the number of input channels, c_in, does not affect the output shape, but rather the size of the kernel. Consequently, if you want to have c_out output channels, you need to have c_out filters each with a shape c_inxhxw.

Related