Answers.Body: <p>If you have a gray scale image, that means you are getting data from one sensor.
  If you have an RGB image, that means you are getting data from three sensors.
  If you have a CMYK image, that means you are getting data from four sensors. </p>
  
  <p>So, channels can be considered as same information seen from different perspective. (here color)</p>
  
  <p>If you see how the kernel (for example 5*5*3) moves, it moves only in XY direction and not in the channel direction. So, you are trying to learn features in XY direction from all the channels together. </p>
  
  <p>But, if you exchange the dimensions like you mentioned your XY dimensions become 200*3 or 3*200 and your channels become 200. In this case you are moving kernel not in the actual XY spatial space of image. So, it doesn't make any sense according to me. You are contradicting the basic concept of CNN by doing so.</p>
  
  <p>The concept of CNN itself is that you want to learn features from the spatial domain of the image which is XY dimension. So, you cannot change dimensions like you mentioned.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-01T07:01:58.827Z
Answers.LastActivityDate: 2019-01-01T07:01:58.827Z
Answers.UserDisplayName: Kartik Podugu
Answers.Body: <blockquote>
    <p>Say you have a colored image that is 200x200 pixels. The standard is such that the input matrix is a 200x200 matrix with 3 channels. The first convolutional layer would have a filter that is size <span class="math-container">$N×M×3$</span>, where <span class="math-container">$N,M&lt;200$</span> (I think they're usually set to 3 or 5). </p>
    
    <p>Would it be possible to structure the input data differently, such that the number of channels now becomes the width or height of the image? i.e., the number of channels would be 200, the input matrix would then be 200x3 or 3x200. What would be the advantage/disadvantage of this formulation versus the standard (# of channels = 3)? Obviously, this would limit your filter's spatial size, but dramatically increase it in the depth direction. </p>
  </blockquote>
  
  <p>The different dimensions (width, height, and number of channels) do have different meanings, the intuition behind them is different, and this is important. You could rearrange your data, but if you then plug it into an implementation of CNNs that expects data in the original format, it would likely perform poorly.</p>
  
  <p>The important observation to make is that the intuition behind CNNs is that they encode the "prior" assumption or knowledge, the "heuristic", the "rule of thumb", of <strong>location invariance</strong>. The intuition is that, when looking at images, we often want our Neural Network to be able to consistently (in the same way) detect features (maybe low-level features such as edges, corners, or maybe high-level features such as complete faces) <strong>regardless of where they are</strong>. It should not matter whether a face is located in the top-left corner or the bottom-right corner of an image, detecting that it is there should still be performed in the same way (i.e. likely requires exactly the same combination of learned weights in our network). That is what we mean with location invariance.</p>
  
  <p>That intution of location invariance is implemented by using "filters" or "feature detectors" that we "slide" along the entire image. These are the things you mentioned having dimensionality <span class="math-container">$N \times M \times 3$</span>. The intuition of location invariance is implemented by taking the exact same filter, and re-applying it in different locations of the image.</p>
  
  <p>If you change the order in which you present your data, you will break this property of location invariance. Instead, you will replace it with a rather strange property of... for example, "width-colour" invariance. You might get a filter that can detect the same type of feature regardless its <span class="math-container">$x$</span>-coordinate in an image, and regarldess of the colour in which it was drawn, but the <span class="math-container">$y$</span>-coordinate will suddenly become relevant; your filter may be able to detect edges of any colour in the bottom of an image, but fail to recognize the same edges in the top-side of an image. This is not an intuition that I would expect to work successfully in most image recognition tasks.</p>
  
  <hr>
  
  <p>Note that there may also be advantages in terms of computation time in having the data ordered in a certain way, depending on what calculations you're going to perform using that data afterwards (typically lots of matrix multiplications). It is best to have the data stored in RAM in such a way that the inner-most loops of algorithms using the data (matrix multiplication) access the data sequentially, in the same order that it is stored in. This is the most efficient way in which to access data from RAM, and will result in the fastest computations. You can generally safely expect that implementations in large frameworks like Tensorflow and PyTorch will already require you to supply data in whatever format is the most efficient by default.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-30T19:07:00.067Z
Answers.Text: Thanks for the detailed answer. To clarify, when I said to "rearrange the data," I mean that the CNN is ALSO trained under the new format. I am not suggesting that we use a CNN trained on the previous format and use it to make a predictions on an image in the new format. Apologies for the miscommunication. As a side note, this question was inspired by my question in https://datascience.stackexchange.com/questions/43291/wrangling-data-for-cnn/43303#43303  where I am trying to use CNNs on non-image data.
Answers.UserDisplayName: anonuser01
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-30T19:46:20.873Z
Answers.Text: @Iamanon Yes that's what I assumed in my answer as well, that you'd do both training and evaluation with the "rearranged" data. I still expect that would generally perform less well. The dimensions that your filters are "sliding" along should by the dimensions where you want the "invariance". That should be in the spatial dimensions (width and height), but NOT in other dimensions (e.g. channels for different colours).
Answers.UserDisplayName: Dennis Soemers
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-30T19:55:44.607Z
Answers.Text: @Iamanon So, intuitively you could say that dimensions for which you expect the invariance property to be beneficial are not "channels", and all other dimensions are "channels". For example, TensorFlow has `conv2d` and `conv3d` for inputs with invariance in 2D or 3D spaces (and any other input dimensions are "channels"). It also has `convolution` (https://www.tensorflow.org/api_docs/python/tf/nn/convolution) with an arbitrary number `input_spatial_shape` of "spatial" inputs, and an arbitrary number `in_channels` of remaining input dimensions.
Answers.UserDisplayName: Dennis Soemers
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-30T20:01:23.983Z
Answers.Text: @Iamanon For your physics application... it's difficult for me to judge where you'd want the property of invariance. I could easily see it being a useful property in all of the dimensions. Spatial and temporal invariance both can make sense. You have to ask yourself: whatever kind of high-level features I expect my NN to "learn inside hidden layers", should the manner in which it detects them be invariant to time and/or space? If yes, filters should slide along those dimensions. If no, treat them as "channels".
Answers.UserDisplayName: Dennis Soemers
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-30T20:37:20.400Z
Answers.Text: Thanks again. The idea that the channels are NOT the dimension(s) in which you expect the invariance property makes sense! What happens if there is more than one dimension that fits under this idea of channels? It seems these deep learning libraries only allow for a single input as the "channels." With one dimension, say, of size 5, you would simply set channels = 5. What if you had some data where you had 2 dimensions of size 5 and 10, respectively, in which you are NOT expecting the invariance property, how would you define the number of channels?
Answers.UserDisplayName: anonuser01
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-30T20:38:02.733Z
Answers.Text: Would you map the 2 dimensions to 1-D? So basically channels = 5*10 = 50?
Answers.UserDisplayName: anonuser01
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-30T20:41:23.413Z
Answers.Text: For my physics application, I think both spatial and temporal invariance makes the most sense -- I think this requires a little bit of domain knowledge. So I would have only a single channel.
Answers.UserDisplayName: anonuser01
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-30T20:42:15.083Z
Answers.Text: @Iamanon Ehm I think you'd just want to concatenate them, so 5 + 10 = 15 in your example. How many features would you have in your example per instance/data-point? That's how many channels you'd have. In 2D RGB images, you have three "features" per pixel: a Red feature, a Green Feature, and a Blue feature. So, also three channels. That's how it works basically.
Answers.UserDisplayName: Dennis Soemers
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-30T11:47:16.283Z
Answers.LastActivityDate: 2018-12-30T11:47:16.283Z
Answers.UserDisplayName: Dennis Soemers
Body: <p>I am trying to understand what channels mean in convolutional neural networks. When working with grayscale and colored images, I understand that the number of channels is set to 1 and 3 (in the first conv layer), respectively, where 3 corresponds to red, green, and blue.</p>
  <p>Say you have a colored image that is <span class="math-container">$200 \times 200$</span> pixels. The standard is such that the input matrix is a <span class="math-container">$200 \times 200$</span> matrix with 3 channels. The first convolutional layer would have a filter that is size <span class="math-container">$N \times M \times 3$</span>, where <span class="math-container">$N,M &lt; 200$</span> (I think they're usually set to 3 or 5).</p>
  <p>Would it be possible to structure the input data differently, such that the number of channels now becomes the width or height of the image? i.e., the number of channels would be 200, the input matrix would then be <span class="math-container">$200 \times 3$</span> or <span class="math-container">$3 \times 200$</span>. What would be the advantage/disadvantage of this formulation versus the standard (# of channels = 3)? Obviously, this would limit your filter's spatial size, but dramatically increase it in the depth direction.</p>
  <p>I am really posing this question because I don't quite understand the concept of channels in CNNs.</p>
  
Tags: <machine-learning><deep-learning><convolutional-neural-networks><channel>
Title: What is the concept of channels in CNNs?
UserDisplayName: anonuser01
ThreadText: # What is the concept of channels in CNNs?
  
  (Asked by: username_0 on 2018-12-30)
  
  I am trying to understand what channels mean in convolutional neural networks. When working with grayscale and colored images, I understand that the number of channels is set to 1 and 3 (in the first conv layer), respectively, where 3 corresponds to red, green, and blue.
  
  Say you have a colored image that is $200 \times 200$ pixels. The standard is such that the input matrix is a $200 \times 200$ matrix with 3 channels. The first convolutional layer would have a filter that is size $N \times M \times 3$, where $N,M < 200$ (I think they're usually set to 3 or 5).
  
  Would it be possible to structure the input data differently, such that the number of channels now becomes the width or height of the image? i.e., the number of channels would be 200, the input matrix would then be $200 \times 3$ or $3 \times 200$. What would be the advantage/disadvantage of this formulation versus the standard (# of channels = 3)? Obviously, this would limit your filter's spatial size, but dramatically increase it in the depth direction.
  
  I am really posing this question because I don't quite understand the concept of channels in CNNs.
  
  **Tags:** machine-learning, deep-learning, convolutional-neural-networks, channel
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2018-12-30. Score: 5 | Accepted answer
  
  > Say you have a colored image that is 200x200 pixels. The standard is such that the input matrix is a 200x200 matrix with 3 channels. The first convolutional layer would have a filter that is size $N×M×3$, where $N,M<200$ (I think they're usually set to 3 or 5).
  >
  > Would it be possible to structure the input data differently, such that the number of channels now becomes the width or height of the image? i.e., the number of channels would be 200, the input matrix would then be 200x3 or 3x200. What would be the advantage/disadvantage of this formulation versus the standard (# of channels = 3)? Obviously, this would limit your filter's spatial size, but dramatically increase it in the depth direction.
  
  The different dimensions (width, height, and number of channels) do have different meanings, the intuition behind them is different, and this is important. You could rearrange your data, but if you then plug it into an implementation of CNNs that expects data in the original format, it would likely perform poorly.
  
  The important observation to make is that the intuition behind CNNs is that they encode the "prior" assumption or knowledge, the "heuristic", the "rule of thumb", of **location invariance**. The intuition is that, when looking at images, we often want our Neural Network to be able to consistently (in the same way) detect features (maybe low-level features such as edges, corners, or maybe high-level features such as complete faces) **regardless of where they are**. It should not matter whether a face is located in the top-left corner or the bottom-right corner of an image, detecting that it is there should still be performed in the same way (i.e. likely requires exactly the same combination of learned weights in our network). That is what we mean with location invariance.
  
  That intution of location invariance is implemented by using "filters" or "feature detectors" that we "slide" along the entire image. These are the things you mentioned having dimensionality $N \times M \times 3$. The intuition of location invariance is implemented by taking the exact same filter, and re-applying it in different locations of the image.
  
  If you change the order in which you present your data, you will break this property of location invariance. Instead, you will replace it with a rather strange property of... for example, "width-colour" invariance. You might get a filter that can detect the same type of feature regardless its $x$-coordinate in an image, and regarldess of the colour in which it was drawn, but the $y$-coordinate will suddenly become relevant; your filter may be able to detect edges of any colour in the bottom of an image, but fail to recognize the same edges in the top-side of an image. This is not an intuition that I would expect to work successfully in most image recognition tasks.
  
  ---
  
  Note that there may also be advantages in terms of computation time in having the data ordered in a certain way, depending on what calculations you're going to perform using that data afterwards (typically lots of matrix multiplications). It is best to have the data stored in RAM in such a way that the inner-most loops of algorithms using the data (matrix multiplication) access the data sequentially, in the same order that it is stored in. This is the most efficient way in which to access data from RAM, and will result in the fastest computations. You can generally safely expect that implementations in large frameworks like Tensorflow and PyTorch will already require you to supply data in whatever format is the most efficient by default.
  
  > **Comments:**
  > **username_0**: Thanks for the detailed answer. To clarify, when I said to "rearrange the data," I mean that the CNN is ALSO trained under the new format. I am not suggesting that we use a CNN trained on the previous format and use it to make a predictions on an image in the new format. Apologies for the miscommunication. As a side note, this question was inspired by my question in https://datascience.stackexchange.com/questions/43291/wrangling-data-for-cnn/43303#43303  where I am trying to use CNNs on non-image data.  
  > **username_2**: @Iamanon Yes that's what I assumed in my answer as well, that you'd do both training and evaluation with the "rearranged" data. I still expect that would generally perform less well. The dimensions that your filters are "sliding" along should by the dimensions where you want the "invariance". That should be in the spatial dimensions (width and height), but NOT in other dimensions (e.g. channels for different colours).  
  > **username_2**: @Iamanon So, intuitively you could say that dimensions for which you expect the invariance property to be beneficial are not "channels", and all other dimensions are "channels". For example, TensorFlow has `conv2d` and `conv3d` for inputs with invariance in 2D or 3D spaces (and any other input dimensions are "channels"). It also has `convolution` (https://www.tensorflow.org/api_docs/python/tf/nn/convolution) with an arbitrary number `input_spatial_shape` of "spatial" inputs, and an arbitrary number `in_channels` of remaining input dimensions.  
  > **username_2**: @Iamanon For your physics application... it's difficult for me to judge where you'd want the property of invariance. I could easily see it being a useful property in all of the dimensions. Spatial and temporal invariance both can make sense. You have to ask yourself: whatever kind of high-level features I expect my NN to "learn inside hidden layers", should the manner in which it detects them be invariant to time and/or space? If yes, filters should slide along those dimensions. If no, treat them as "channels".  
  > **username_0**: Thanks again. The idea that the channels are NOT the dimension(s) in which you expect the invariance property makes sense! What happens if there is more than one dimension that fits under this idea of channels? It seems these deep learning libraries only allow for a single input as the "channels." With one dimension, say, of size 5, you would simply set channels = 5. What if you had some data where you had 2 dimensions of size 5 and 10, respectively, in which you are NOT expecting the invariance property, how would you define the number of channels?  
  > **username_0**: Would you map the 2 dimensions to 1-D? So basically channels = 5*10 = 50?  
  > **username_0**: For my physics application, I think both spatial and temporal invariance makes the most sense -- I think this requires a little bit of domain knowledge. So I would have only a single channel.  
  > **username_2**: @Iamanon Ehm I think you'd just want to concatenate them, so 5 + 10 = 15 in your example. How many features would you have in your example per instance/data-point? That's how many channels you'd have. In 2D RGB images, you have three "features" per pixel: a Red feature, a Green Feature, and a Blue feature. So, also three channels. That's how it works basically.  
  
  ---
  
  ### Answer by username_1 on 2019-01-01. Score: 1
  
  If you have a gray scale image, that means you are getting data from one sensor.
  If you have an RGB image, that means you are getting data from three sensors.
  If you have a CMYK image, that means you are getting data from four sensors.
  
  So, channels can be considered as same information seen from different perspective. (here color)
  
  If you see how the kernel (for example 5\*5\*3) moves, it moves only in XY direction and not in the channel direction. So, you are trying to learn features in XY direction from all the channels together.
  
  But, if you exchange the dimensions like you mentioned your XY dimensions become 200\*3 or 3\*200 and your channels become 200. In this case you are moving kernel not in the actual XY spatial space of image. So, it doesn't make any sense according to me. You are contradicting the basic concept of CNN by doing so.
  
  The concept of CNN itself is that you want to learn features from the spatial domain of the image which is XY dimension. So, you cannot change dimensions like you mentioned.
  
  
  ---
  
  