Answers.Body: <p>Your input has 16 channels, each of dimension <span class="math-container">$m \times n$</span>. There are 3 filters, namely <span class="math-container">$f_1$</span>, <span class="math-container">$f_2$</span> and <span class="math-container">$f_3$</span> of spatial dimensions <span class="math-container">$k \times h$</span>.</p>
  <p>We say that a filter is applied to a channel when it is superimposed on the image, starting left-most, performing the operation of multiplying the weights of the filter with the corresponding value in the image and then summing up to a single value and moving the filter to right (then down when it reaches rightmost part) across the image according to the stride of the filter.</p>
  <p>When a filter, e.g. <span class="math-container">$f_1$</span>, is applied to a channel say <span class="math-container">$c$</span>, there is a single value. Now, apply them to all channels, we get 16 values and all of them are added up to a single value. <span class="math-container">$f_1$</span> is moved according to the stride and the same operation is repeated to get an output with a single channel (the number of rows and columns are determined by padding, stride, dilation, and kernel size of the filers).</p>
  <p>The aforesaid process is done by all the 3 filters giving rise to 3 channels. In this way, the convolutional layer makes the input go from 16 to 3 channels.</p>
  <p>More detailed explanations can be found <a href="http://colah.github.io/posts/2014-07-Understanding-Convolutions/" rel="nofollow noreferrer">here</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-18T12:19:18.730Z
Answers.Text: It seems that this explanation is slightly wrong. The filter's depth is equal to the input's depth. Here, it seems that you're suggesting that the filter is a matrix, i.e. 2d, so it has depth 1, and that you apply this 2d matrix to each channel of the image separately, but this is not correct.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-29T06:15:42.357Z
Answers.LastActivityDate: 2021-12-18T12:18:13.117Z
Answers.LastEditDate: 2021-12-18T12:18:13.117Z
Answers.UserDisplayName: varsh
Answers.Body: <p>The reason why you go from 16 to 3 channels is that, in a 2d convolution, filters span the <strong>entire depth</strong> of the input. Therefore, your filters would actually be <span class="math-container">$7 \times 7 \times 16$</span> in order to cover all channels of the input.</p>
  <h2>Detailed procedure</h2>
  <p>The output of the convolution automatically has a depth equal to the number of filters (so in your case this is <span class="math-container">$3$</span>) because you have an <span class="math-container">$m \times k$</span> filter matrix, where <span class="math-container">$m$</span> is the number of filters and <span class="math-container">$k$</span> is the number of elements in the <strong>unrolled filter</strong> (in your case, <span class="math-container">$m = 3$</span> and <span class="math-container">$k = 7 \times 7 \times 16 = 784$</span>, so the filter matrix is <span class="math-container">$3 \times 784$</span>).</p>
  <p>The input is usually unrolled according to the im2col procedure, where each tile corresponding to a single filter location is stretched into a column equal to the <strong>unrolled</strong> filter size. This is repeated for each filter location, so you end up with a very large matrix of size <span class="math-container">$k \times n$</span>, where <span class="math-container">$k$</span> is the same as <span class="math-container">$k$</span> above in the filter matrix, and <span class="math-container">$n$</span> depends on your padding and stride.</p>
  <p>Multiplying the <span class="math-container">$m \times k$</span> filter matrix with the <span class="math-container">$k \times n$</span> input matrix gives you an <span class="math-container">$m \times n$</span> output matrix, where <span class="math-container">$m$</span> is the number of filters.</p>
  <h2>Further reading</h2>
  <p>You can find some very nice visual explanations of the convolution procedure <a href="https://cs231n.github.io/convolutional-networks/" rel="nofollow noreferrer">here</a> and <a href="https://leonardoaraujosantos.gitbooks.io/artificial-inteligence/content/making_faster.html" rel="nofollow noreferrer">here</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-30T02:19:00.453Z
Answers.Text: Ok, that is explaining a lot of things. Thank you very much. Now I also understand why caffe2 is taking a NCVHW ordering for 3D convolutions (N=minibatch size, C=number of channels, V=Video frames, H=height, W=width).
Answers.UserDisplayName: Du Bois Eloi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-29T01:01:34.533Z
Answers.LastActivityDate: 2021-09-26T22:00:22.997Z
Answers.LastEditDate: 2021-09-26T22:00:22.997Z
Answers.UserDisplayName: cantordust
Body: <p>Let's suppose I have an image with 16 channels that goes to a convolutional layer, which has 3 trainable <span class="math-container">$7 \times 7$</span> filters, so the output of this layer has depth 3.</p>
  <p>How does the convolutional layer go from 16 to 3 channels? What mathematical operation is applied?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-09-26T21:55:10.393Z
Comments.Text: [Here](https://ai.stackexchange.com/q/17783/2444) is a related question (if not an exact duplicate).
Comments.UserDisplayName: nbro
Tags: <convolutional-neural-networks><convolution><convolutional-layers><convolution-arithmetic><2d-convolution>
Title: How is the depth of the input related to the depth of the output of a convolutional layer?
UserDisplayName: Du Bois Eloi
ThreadText: # How is the depth of the input related to the depth of the output of a convolutional layer?
  
  (Asked by: username_0 on 2018-06-28)
  
  Let's suppose I have an image with 16 channels that goes to a convolutional layer, which has 3 trainable $7 \times 7$ filters, so the output of this layer has depth 3.
  
  How does the convolutional layer go from 16 to 3 channels? What mathematical operation is applied?
  
  **Tags:** convolutional-neural-networks, convolution, convolutional-layers, convolution-arithmetic, 2d-convolution
  > **Comments:**
  > **username_1**: [Here](https://ai.stackexchange.com/q/17783/2444) is a related question (if not an exact duplicate).  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2018-06-29. Score: 3 | Accepted answer
  
  The reason why you go from 16 to 3 channels is that, in a 2d convolution, filters span the **entire depth** of the input. Therefore, your filters would actually be $7 \times 7 \times 16$ in order to cover all channels of the input.
  
  ## Detailed procedure
  
  The output of the convolution automatically has a depth equal to the number of filters (so in your case this is $3$) because you have an $m \times k$ filter matrix, where $m$ is the number of filters and $k$ is the number of elements in the **unrolled filter** (in your case, $m = 3$ and $k = 7 \times 7 \times 16 = 784$, so the filter matrix is $3 \times 784$).
  
  The input is usually unrolled according to the im2col procedure, where each tile corresponding to a single filter location is stretched into a column equal to the **unrolled** filter size. This is repeated for each filter location, so you end up with a very large matrix of size $k \times n$, where $k$ is the same as $k$ above in the filter matrix, and $n$ depends on your padding and stride.
  
  Multiplying the $m \times k$ filter matrix with the $k \times n$ input matrix gives you an $m \times n$ output matrix, where $m$ is the number of filters.
  
  ## Further reading
  
  You can find some very nice visual explanations of the convolution procedure [here](https://cs231n.github.io/convolutional-networks/) and [here](https://leonardoaraujosantos.gitbooks.io/artificial-inteligence/content/making_faster.html).
  
  > **Comments:**
  > **username_0**: Ok, that is explaining a lot of things. Thank you very much. Now I also understand why caffe2 is taking a NCVHW ordering for 3D convolutions (N=minibatch size, C=number of channels, V=Video frames, H=height, W=width).  
  
  ---
  
  ### Answer by username_2 on 2018-06-29. Score: 0
  
  Your input has 16 channels, each of dimension $m \times n$. There are 3 filters, namely $f\_1$, $f\_2$ and $f\_3$ of spatial dimensions $k \times h$.
  
  We say that a filter is applied to a channel when it is superimposed on the image, starting left-most, performing the operation of multiplying the weights of the filter with the corresponding value in the image and then summing up to a single value and moving the filter to right (then down when it reaches rightmost part) across the image according to the stride of the filter.
  
  When a filter, e.g. $f\_1$, is applied to a channel say $c$, there is a single value. Now, apply them to all channels, we get 16 values and all of them are added up to a single value. $f\_1$ is moved according to the stride and the same operation is repeated to get an output with a single channel (the number of rows and columns are determined by padding, stride, dilation, and kernel size of the filers).
  
  The aforesaid process is done by all the 3 filters giving rise to 3 channels. In this way, the convolutional layer makes the input go from 16 to 3 channels.
  
  More detailed explanations can be found [here](http://colah.github.io/posts/2014-07-Understanding-Convolutions/).
  
  > **Comments:**
  > **username_1**: It seems that this explanation is slightly wrong. The filter's depth is equal to the input's depth. Here, it seems that you're suggesting that the filter is a matrix, i.e. 2d, so it has depth 1, and that you apply this 2d matrix to each channel of the image separately, but this is not correct.  
  
  ---
  
  