Answers.Body: <p>The actual tensor that performs the dot product operation on the image is called a kernel. Each filter has a collection of kernels — one kernel for every input channel flowing into this layer. Each kernel is its own tensor, with its own completely unique values. Once each of the input channels have been transformed by their corresponding kernel, the channels are combined together to create one “output channel”.</p>
  <p><a href="https://i.sstatic.net/8MSwQRmT.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8MSwQRmT.gif" alt="enter image description here" /></a></p>
  <p><a href="https://i.sstatic.net/19Yy7s43.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/19Yy7s43.gif" alt="enter image description here" /></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-02T08:12:58.320Z
Answers.LastActivityDate: 2024-09-02T08:12:58.320Z
Answers.UserDisplayName: M a m a D
Answers.Body: <p><strong>In a CNN, each filter produces one feature map regardless of the number of input channels.</strong></p>
  <p>For your example:</p>
  <ol>
  <li><p>Single channel input : The input image has 1 channel of size <span class="math-container">$ 224 \times 224$</span>.</p>
  </li>
  <li><p>First Layer:</p>
  <ul>
  <li>64 filters (each <span class="math-container">$ 3 \times 3$</span>) are applied to the input.</li>
  <li>Each filter convolves over the input channel, producing 1 output channel (per filter).</li>
  <li>This results in 64 output channels, each of size <span class="math-container">$ 224 \times 224$</span>.</li>
  </ul>
  </li>
  <li><p>Second Layer:</p>
  <ul>
  <li>Each of the 64 filters in this layer has a depth equal to the number of input channels (64). In other words, we can say that each filter in a convolutional layer has the same depth as the number of input channels. If the input has 64 channels, each filter will also have a depth of 64.</li>
  <li><strong>Each filter convolves over all 64 input channels. The output at each position is the sum of the convolutions of the filter with each input channel.</strong></li>
  <li>This sum forms a single output channel. Since there are 64 filters, we get 64 output channels, each of size <span class="math-container">$ 224 \times 224$</span>.</li>
  </ul>
  </li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-02T11:21:46.193Z
Answers.Text: You said, for example, Filter 1, convolves over all 64 previous feature maps. Let feature map $i$, contains only number $i$ on its matrix cells. If  Filter 1 contains only $1$, then what would be the summation result?
Answers.UserDisplayName: M a m a D
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-02T15:41:24.037Z
Answers.Text: Your example isn't clear. Could you clarify what you mean by "contains only number i on its matrix cells"? Watching 3D animations for CNN implementation would help you visualize.
Answers.UserDisplayName: Kulin Patel
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-02T19:32:08.283Z
Answers.Text: Given feature map 1 of size 2×2
   as `1&1\1&1`, and feature map 2 of size 2×2
   as `2&2\2&2`. If one kernel 2×2
   as `3&3\3&3` is applied, then output is 3×1+3×1+3×1+3×1+3×2+3×2+3×2+3×2
  ?
Answers.UserDisplayName: M a m a D
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-02T08:14:17.240Z
Answers.Text: I found the answer and posted as answer
Answers.UserDisplayName: M a m a D
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-02T03:02:45.380Z
Answers.LastActivityDate: 2024-08-02T15:33:18.587Z
Answers.LastEditDate: 2024-08-02T15:33:18.587Z
Answers.UserDisplayName: Kulin Patel
Body: <p>I understand how kernel size, stride, and the basics of CNNs. My question may look simple, but, despite my efforts, I haven't found the answer yet.</p>
  <p>In the following figure, let a <span class="math-container">$224\times224$</span> image be given to a convolutional layer with kernel size <span class="math-container">$3 \times 3$</span>, stride <span class="math-container">$1$</span>, padding <span class="math-container">$1$</span>, and <span class="math-container">$64$</span> filters. The result is <span class="math-container">$64\times224\times224$</span>. However, when the same convolutional layer is applied, the result is still <span class="math-container">$64\times224\times224$</span>.</p>
  <p><a href="https://i.sstatic.net/Ffg4rXVo.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ffg4rXVo.png" alt="enter image description here" /></a></p>
  <p>My question is:</p>
  <p>The output of the first layer is <span class="math-container">$64$</span> feature maps of size <span class="math-container">$224\times224$</span>, the second layer applies its kernel on each feature map of layer1, then I expect to see <span class="math-container">$64\times64=4096$</span> feature map of size <span class="math-container">$224\times224$</span>, but the result is still <span class="math-container">$64\times224\times224$</span> feature maps.</p>
  <p>I would like to know, how multiple filters in a CNN work.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-08-02T08:25:16.657Z
Comments.Text: This question is similar to: [How is the depth of a convolutional layer determined?](https://ai.stackexchange.com/questions/3287/how-is-the-depth-of-a-convolutional-layer-determined). If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-08-02T08:26:29.900Z
Comments.Text: If you want a practice example with code, see my answer here: https://ai.stackexchange.com/a/17804/2444
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-08-02T11:26:59.663Z
Comments.Text: @nbro Your answer is irrelevant to my question. what I don't understand is the summation formula when convolution is applied with multiple filters. If there are 10 filters and 10 feature maps from the previous layer, how do 10 filters are applied to feature maps? I know how does 1 kernel work, but I don't know how do multiple work
Comments.UserDisplayName: M a m a D
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-08-02T11:39:06.363Z
Comments.Text: What do you mean by "my answer is irrelevant", when basically my answer is telling and showing you (with a concrete code example) that the filter will have the same depth as the input, and so the output will have a depth equal to the number of filters, which exactly answers the question why the last layer has a depth 64
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-08-02T11:44:18.600Z
Comments.Text: It seems that what is causing you the confusion is that you're assuming that you apply a 2D filter for each feature map. However, the filter will actually be 3D and have a depth equal to the number of feature maps in the input. My answer and other answers already are stating this. So, you apply one filter to all feature maps at the same time and you produce 1 feature map in the output.
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-08-02T12:17:57.853Z
Comments.Text: @nbro Given feature map 1 of size $2\times 2$ as `1&1\1&1`, and feature map 2 of size $2\times 2$ as `2&2\2&2`. If one kernel $2\times 2$ as `3&3\3&3` is applied, then output is  $3 \times 1 +  3 \times 1 + 3  \times 1 + 3  \times 1 + 3 \times 2 + 3 \times 2 + 3 \times 2 + 3 \times 2$?
Comments.UserDisplayName: M a m a D
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-09-02T08:14:26.587Z
Comments.Text: @nbro I found the answer and posted as answer
Comments.UserDisplayName: M a m a D
Tags: <convolutional-neural-networks><filters>
Title: How do multiple filters in a CNN work?
UserDisplayName: M a m a D
ThreadText: # How do multiple filters in a CNN work?
  
  (Asked by: username_0 on 2024-08-01)
  
  I understand how kernel size, stride, and the basics of CNNs. My question may look simple, but, despite my efforts, I haven't found the answer yet.
  
  In the following figure, let a $224\times224$ image be given to a convolutional layer with kernel size $3 \times 3$, stride $1$, padding $1$, and $64$ filters. The result is $64\times224\times224$. However, when the same convolutional layer is applied, the result is still $64\times224\times224$.
  
  [![enter image description here](https://i.sstatic.net/Ffg4rXVo.png)](https://i.sstatic.net/Ffg4rXVo.png)
  
  My question is:
  
  The output of the first layer is $64$ feature maps of size $224\times224$, the second layer applies its kernel on each feature map of layer1, then I expect to see $64\times64=4096$ feature map of size $224\times224$, but the result is still $64\times224\times224$ feature maps.
  
  I would like to know, how multiple filters in a CNN work.
  
  **Tags:** convolutional-neural-networks, filters
  > **Comments:**
  > **username_1**: This question is similar to: [How is the depth of a convolutional layer determined?](https://ai.stackexchange.com/questions/3287/how-is-the-depth-of-a-convolutional-layer-determined). If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.  
  > **username_1**: If you want a practice example with code, see my answer here: https://ai.stackexchange.com/a/17804/2444  
  > **username_0**: @nbro Your answer is irrelevant to my question. what I don't understand is the summation formula when convolution is applied with multiple filters. If there are 10 filters and 10 feature maps from the previous layer, how do 10 filters are applied to feature maps? I know how does 1 kernel work, but I don't know how do multiple work  
  > **username_1**: What do you mean by "my answer is irrelevant", when basically my answer is telling and showing you (with a concrete code example) that the filter will have the same depth as the input, and so the output will have a depth equal to the number of filters, which exactly answers the question why the last layer has a depth 64  
  > **username_1**: It seems that what is causing you the confusion is that you're assuming that you apply a 2D filter for each feature map. However, the filter will actually be 3D and have a depth equal to the number of feature maps in the input. My answer and other answers already are stating this. So, you apply one filter to all feature maps at the same time and you produce 1 feature map in the output.  
  > **username_0**: @nbro Given feature map 1 of size $2\times 2$ as `1&1\1&1`, and feature map 2 of size $2\times 2$ as `2&2\2&2`. If one kernel $2\times 2$ as `3&3\3&3` is applied, then output is  $3 \times 1 +  3 \times 1 + 3  \times 1 + 3  \times 1 + 3 \times 2 + 3 \times 2 + 3 \times 2 + 3 \times 2$?  
  > **username_0**: @nbro I found the answer and posted as answer  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2024-09-02. Score: 0 | Accepted answer
  
  The actual tensor that performs the dot product operation on the image is called a kernel. Each filter has a collection of kernels — one kernel for every input channel flowing into this layer. Each kernel is its own tensor, with its own completely unique values. Once each of the input channels have been transformed by their corresponding kernel, the channels are combined together to create one “output channel”.
  
  [![enter image description here](https://i.sstatic.net/8MSwQRmT.gif)](https://i.sstatic.net/8MSwQRmT.gif)
  
  [![enter image description here](https://i.sstatic.net/19Yy7s43.gif)](https://i.sstatic.net/19Yy7s43.gif)
  
  
  ---
  
  ### Answer by username_2 on 2024-08-02. Score: 2
  
  **In a CNN, each filter produces one feature map regardless of the number of input channels.**
  
  For your example:
  
  1. Single channel input : The input image has 1 channel of size $ 224 \times 224$.
  2. First Layer:
  
     * 64 filters (each $ 3 \times 3$) are applied to the input.
     * Each filter convolves over the input channel, producing 1 output channel (per filter).
     * This results in 64 output channels, each of size $ 224 \times 224$.
  3. Second Layer:
  
     * Each of the 64 filters in this layer has a depth equal to the number of input channels (64). In other words, we can say that each filter in a convolutional layer has the same depth as the number of input channels. If the input has 64 channels, each filter will also have a depth of 64.
     * **Each filter convolves over all 64 input channels. The output at each position is the sum of the convolutions of the filter with each input channel.**
     * This sum forms a single output channel. Since there are 64 filters, we get 64 output channels, each of size $ 224 \times 224$.
  
  > **Comments:**
  > **username_0**: You said, for example, Filter 1, convolves over all 64 previous feature maps. Let feature map $i$, contains only number $i$ on its matrix cells. If  Filter 1 contains only $1$, then what would be the summation result?  
  > **username_2**: Your example isn't clear. Could you clarify what you mean by "contains only number i on its matrix cells"? Watching 3D animations for CNN implementation would help you visualize.  
  > **username_0**: Given feature map 1 of size 2×2  as `1&1\1&1`, and feature map 2 of size 2×2  as `2&2\2&2`. If one kernel 2×2  as `3&3\3&3` is applied, then output is 3×1+3×1+3×1+3×1+3×2+3×2+3×2+3×2 ?  
  > **username_0**: I found the answer and posted as answer  
  
  ---
  
  