Answers.Body: <p>Yes, the kernel is 3D in this case - or 4D as in 3x3x3x1. In the general case you can have multiple output channels, making it 3x3x3x8 for example. The number of channels isn't a convolution dimension because the filter does not &quot;slide&quot;/&quot;translate&quot;/&quot;move&quot; over this dimension. It's still a 2D convolution, and then the channel part of this operation is thought of separately from the convolution part. The 4D kernel is a bunch of 2D kernels. If you have 1 input channel and 1 output channel then it's just one 2D kernel. Or you can think of it as a bunch of 3D kernels if you like. Or a single 4D kernel. These are just arrays of numbers... you can slice an array up however you like, if you can find a way to think about it.</p>
  <hr />
  <p>Note the <a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html" rel="nofollow noreferrer"><code>groups</code> parameter</a> of <code>Conv2d</code>, which affects how the channels are convolved. The default is 1, which means:</p>
  <blockquote>
  <p>At groups=1, all inputs are convolved to all outputs.</p>
  </blockquote>
  <p>If you set it to 3 (and 3 output channels) then the Conv2d layer would maintain the channel separation.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-02T22:32:02.150Z
Answers.Text: Okay! seems that kernel size is misleading. Actually the kernel size is not $3 \times 3$. It is $3 \times 3 \times 3$.
Answers.UserDisplayName: hanugm
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-02T23:30:59.383Z
Answers.Text: (+1) for **Yes, it is really a 3D convolution (in this case)**, because I need to understand remaining to appreciate the answer further.
Answers.UserDisplayName: hanugm
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-01T07:26:33.510Z
Answers.Text: Yes, it is really a 3D **kernel** (in this case),?
Answers.UserDisplayName: hanugm
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-01T08:01:43.717Z
Answers.Text: @hanugm The convolution kernel is actually 3x3x3. There could also be multiple output channels so you can say it's 3x3x3x1. It's still a 2D convolution because there are two dimensions where the kernel translates/slides/moves.
Answers.UserDisplayName: Criticize SE actions means ban
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-01T10:47:24.333Z
Answers.Text: Ha. So, if possible try to change 3D to 2D in your answer. It may be misleading.
Answers.UserDisplayName: hanugm
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-01T11:03:48.690Z
Answers.Text: @hanugm I edited the answer
Answers.UserDisplayName: Criticize SE actions means ban
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-02T16:39:11.740Z
Answers.LastActivityDate: 2021-10-01T11:03:39.677Z
Answers.LastEditDate: 2021-10-01T11:03:39.677Z
Answers.UserDisplayName: Criticize SE actions means ban
Body: <p>Let us imagine <span class="math-container">$x$</span> as a tensor containing 1000 RGB images, each of size <span class="math-container">$64 \times 32$</span>.</p>
  <pre><code>&gt;&gt;&gt; x = torch.randn(1000, 3, 64, 32)
  &gt;&gt;&gt; print(x.shape)
  torch.Size([1000, 3, 64, 32])
  </code></pre>
  <p>I am using a 2d convolutional layer that converts RGB images to single channel (say grayscale) images</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>I passed the tensor <span class="math-container">$x$</span> in the convolutional layer and obtained another tensor of 1000 grayscale images, each of size <span class="math-container">$64 \times 32$</span>.</p>
  <pre><code>&gt;&gt;&gt; output = m(x)
  &gt;&gt;&gt; print(output.shape)
  torch.Size([1000, 1, 64, 32])
  </code></pre>
  <p>Now, I can say that my convolutional layer converted an RGB image into a grayscale image <strong>using 2d kernel</strong>.</p>
  <p>How it is doing?</p>
  <p>RGB image has 3 planes each of size <span class="math-container">$64 \times 32$</span>. If a kernel of 2 dimensions is used, then we will get 3 planes in output, corresponding to R, G, and B. How is it possible to convert an image with 3 channels into an image with one channel <strong>using 2d kernel?</strong></p>
  <p>I can <a href="https://i.sstatic.net/QZsRB.png" rel="nofollow noreferrer">visualize easily</a> if I use a 3d kernel since the kernel considers three channels simultaneously and produces a single feature map for an RGB image.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-02T22:32:28.500Z
Comments.Text: Useful : https://stackoverflow.com/questions/56675943/meaning-of-parameters-in-torch-nn-conv2d
Comments.UserDisplayName: hanugm
Tags: <convolutional-layers>
Title: Confusion about conversion of RGB image to grayscale image using a convolutional layer with 2-dimensional filters
UserDisplayName: hanugm
ThreadText: # Confusion about conversion of RGB image to grayscale image using a convolutional layer with 2-dimensional filters
  
  (Asked by: username_0 on 2021-08-02)
  
  Let us imagine $x$ as a tensor containing 1000 RGB images, each of size $64 \times 32$.
  >>> x = torch.randn(1000, 3, 64, 32)
  >>> print(x.shape)
  torch.Size([1000, 3, 64, 32])
  
  I am using a 2d convolutional layer that converts RGB images to single channel (say grayscale) images
  >>> 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))
  
  I passed the tensor $x$ in the convolutional layer and obtained another tensor of 1000 grayscale images, each of size $64 \times 32$.
  >>> output = m(x)
  >>> print(output.shape)
  torch.Size([1000, 1, 64, 32])
  
  Now, I can say that my convolutional layer converted an RGB image into a grayscale image using 2d kernel.
  How it is doing?
  RGB image has 3 planes each of size $64 \times 32$. If a kernel of 2 dimensions is used, then we will get 3 planes in output, corresponding to R, G, and B. How is it possible to convert an image with 3 channels into an image with one channel using 2d kernel?
  I can visualize easily if I use a 3d kernel since the kernel considers three channels simultaneously and produces a single feature map for an RGB image.
  
  **Tags:** convolutional-layers
  > **Comments:**
  > **username_0**: Useful : https://stackoverflow.com/questions/56675943/meaning-of-parameters-in-torch-nn-conv2d  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-02. Score: 1
  
  Yes, the kernel is 3D in this case - or 4D as in 3x3x3x1. In the general case you can have multiple output channels, making it 3x3x3x8 for example. The number of channels isn't a convolution dimension because the filter does not "slide"/"translate"/"move" over this dimension. It's still a 2D convolution, and then the channel part of this operation is thought of separately from the convolution part. The 4D kernel is a bunch of 2D kernels. If you have 1 input channel and 1 output channel then it's just one 2D kernel. Or you can think of it as a bunch of 3D kernels if you like. Or a single 4D kernel. These are just arrays of numbers... you can slice an array up however you like, if you can find a way to think about it.
  
  ---
  
  Note the [`groups` parameter](https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html) of `Conv2d`, which affects how the channels are convolved. The default is 1, which means:
  
  > At groups=1, all inputs are convolved to all outputs.
  
  If you set it to 3 (and 3 output channels) then the Conv2d layer would maintain the channel separation.
  
  > **Comments:**
  > **username_0**: Okay! seems that kernel size is misleading. Actually the kernel size is not $3 \times 3$. It is $3 \times 3 \times 3$.  
  > **username_0**: (+1) for **Yes, it is really a 3D convolution (in this case)**, because I need to understand remaining to appreciate the answer further.  
  > **username_0**: Yes, it is really a 3D **kernel** (in this case),?  
  > **username_1**: @hanugm The convolution kernel is actually 3x3x3. There could also be multiple output channels so you can say it's 3x3x3x1. It's still a 2D convolution because there are two dimensions where the kernel translates/slides/moves.  
  > **username_0**: Ha. So, if possible try to change 3D to 2D in your answer. It may be misleading.  
  > **username_1**: @hanugm I edited the answer  
  
  ---
  
  