Answers.Body: <p>The term &quot;filter&quot; is (usually) a synonym for &quot;kernel&quot; in the context of convolutional neural networks and image processing.</p>
  <p>The reason why the <code>kernel_size</code> is specified as <span class="math-container">$3 \times 3$</span> 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 <code>in_channels</code>, 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.</p>
  <p>As a side note, in other contexts, like support vector machines, the word &quot;kernel&quot; refers to something different, but I will not dwell on this topic here.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-03T10:14:52.267Z
Answers.LastActivityDate: 2021-08-03T10:39:14.907Z
Answers.LastEditDate: 2021-08-03T10:39:14.907Z
Answers.UserDisplayName: nbro
Body: <p>Recently I asked <a href="https://ai.stackexchange.com/questions/29970/confusion-about-conversion-of-rgb-image-to-grayscale-image-using-a-convolutional">a question</a> 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.</p>
  <p>Consider <a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html" rel="nofollow noreferrer">Conv2d</a> in PyTorch.</p>
  <pre><code>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)
  </code></pre>
  <p>The parameters <code>in_channel</code>, <code>out_channel</code> and <code>kernel_size</code> are key to our discussion here.</p>
  <p>I have no doubt about <code>in_channel</code>. It simply says the number of channels in the input image. It is 3 for our task since we have RGB images as input.</p>
  <p>The doubt is regarding the parameters <code>out_channel</code> and <code>kernel_size</code>. <code>out_channel</code> 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. <code>kernel_size</code> is the size of the kernel which is showing <span class="math-container">$3 \times 3$</span> in our case. Now, my convolution layer is</p>
  <pre><code>&gt;&gt;&gt; in_ch = 3
  &gt;&gt;&gt; out_ch = 1
  &gt;&gt;&gt; m = nn.Conv2d(in_ch, out_ch, 3, 1, 1)
  &gt;&gt;&gt; print(m)
  Conv2d(3, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  </code></pre>
  <p>Since I have doubt about the conversion of RGB image to grayscale image using a single filter and whose size is showing <span class="math-container">$3 \times 3$</span>, I checked the shape of weights in the filter and realized that the single filter is a 3-dimensional filter of size <span class="math-container">$ 3 \times 3 \times 3$</span></p>
  <pre><code>&gt;&gt;&gt; print(m.weight.shape)
  torch.Size([1, 3, 3, 3])
  </code></pre>
  <p>Now, the filter size is <span class="math-container">$3 \times 3 \times 3$</span> and <code>kernel_size</code> is <span class="math-container">$3 \times 3$</span>.</p>
  <p>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?</p>
  
Tags: <comparison><terminology><pytorch><filters><convolutional-layers>
Title: Is "kernel" different from "filter" in convolutional neural networks?
UserDisplayName: hanugm
ThreadText: # Is "kernel" different from "filter" in convolutional neural networks?
  
  (Asked by: username_0 on 2021-08-02)
  
  Recently I asked [a question](https://ai.stackexchange.com/questions/29970/confusion-about-conversion-of-rgb-image-to-grayscale-image-using-a-convolutional) 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](https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html) 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?
  
  **Tags:** comparison, terminology, pytorch, filters, convolutional-layers
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-03. Score: 2 | Accepted answer
  
  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.
  
  
  ---
  
  