0
$\begingroup$

Recently I asked a question on how a convolution 2d layer changes an RGB image in to a grayscale image. Assume that our task is to convert an RGB image into grayscale image. I use to believe that filter and kernel are one and 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 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 grayscale image. kernel_size is the size of 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 a same tag for both filter and kernel?

$\endgroup$
1
$\begingroup$

The term "filter" is (usually) a synonym for "kernel" in the context of convolutional neural networks and image processing.

The reason why the kernel_size is specified as $3 \times 3$ and then you see that the actual size of the kernel (aka filter) is 3d is that the depth of the kernel can be automatically inferred from in_channels, the depth of the input to the convolutional layer: the depth of the kernel is exactly equal to the depth of the input to that 2d convolutional layer. This would not necessarily be the case in the 3d convolutional layer.

As a side note, in other contexts, like support vector machines, the word "kernel" refers to something different, but I will not dwell on this topic here.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.