Recently I asked a question on how a convolution 2d layer changes an RGB image into a grayscale image. Assume that our task is to convert an RGB image into a grayscale image. I use to believe that filter and kernel are one and the same.
Consider Conv2d in PyTorch.
class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
The parameters in_channel
, out_channel
and kernel_size
are key to our discussion here.
I have no doubt about in_channel
. It simply says the number of channels in the input image. It is 3 for our task since we have RGB images as input.
The doubt is regarding the parameters out_channel
and kernel_size
. out_channel
refers to the number of channels in the output image. It is 1 for our task since we want grayscale images as output. It is also equal to the number of filters we are needing. So, we just use one filter to convert an RGB image into a grayscale image. kernel_size
is the size of the kernel which is showing $3 \times 3$ in our case. Now, my convolution layer is
>>> in_ch = 3
>>> out_ch = 1
>>> m = nn.Conv2d(in_ch, out_ch, 3, 1, 1)
>>> print(m)
Conv2d(3, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
Since I have doubt about the conversion of RGB image to grayscale image using a single filter and whose size is showing $3 \times 3$, I checked the shape of weights in the filter and realized that the single filter is a 3-dimensional filter of size $ 3 \times 3 \times 3$
>>> print(m.weight.shape)
torch.Size([1, 3, 3, 3])
Now, the filter size is $3 \times 3 \times 3$ and kernel_size
is $3 \times 3$.
So, can I safely conclude that the filter is different from the kernel? Can I conclude that kernel is just a part of filter and filter may comprise several kernels? Or is it true that the usage in PyTorch is a bit misleading since I found that our site is also using the same tag for both filter and kernel?