Answers.Body: <p>Yes padding is an option, but you can actually do better than that.</p>
  <p>Consider a convolutional layer: in order to define its parameters you only need to know the number of input channels, and the number of output channels you want. Indeed, its parameters are invariant to the height and width of the input</p>
  <p>For this reason, you can exploit the fact that all PNGs have 3 (or 4 if you also consider the alpha channel) channels, and create a model like this:</p>
  <ol>
  <li>a bunch of convolutional layer</li>
  <li>a layer that aggregate over height and width (such as a global average pooling)</li>
  <li>some fully connected layers</li>
  <li>classification head</li>
  </ol>
  <p>This model will indeed be able to work as expected with any image size in input</p>
  <p>Spoiler: it will be a nightmare to train, because you'll have to deal with tensors of different sizes, thus training on GPUs will be very very hard if no caution is taken</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-11T20:32:12.083Z
Answers.LastActivityDate: 2024-04-11T20:32:12.083Z
Answers.UserDisplayName: Alberto
Body: <p>I have the notion that CNN input data must always be of the same dimensions. If we are feeding 1D tabular data, columns must be of the same numbers; if we are feeding 2D image data, all the images must be of the same dimensions. This resize may take place either in the input layer of the CNN or outside the CNN - in the data processing stage.</p>
  <p>So, I wrote my supervisor the following email:</p>
  <blockquote>
  <p>Hello.</p>
  <p>CNN can process both 1D tabular data and 2D data. However, it cannot process 1D tabular data with different column lengths.</p>
  <p>We cannot use the following files:</p>
  <p>ABC.dat</p>
  <pre><code>  -------------------data--------------------      --matrix--
    6 LYS C  4.768   7.342 10.221 1 0 1 0  1  0        1 2 3
    7 TYR C  8.992   6.431  4.110 0 1 0 1  0  0        1 2 3
    8 SER C  2.345   8.901 12.345 0 0 1 0  1  1        1 2 3
  </code></pre>
  <p>PQR.dat</p>
  <pre><code>-------------------data--------------------        ---matrix--
    0 ALA C  0.000   0.000  0.000 1 1 1 1  1  0        1 2 3 4 5 6
    1 GLU C  6.691   9.772  0.000 1 1 1 1  1  0        1 2 3 4 5 6
    2 PHE C  6.601   8.709 12.389 0 0 0 0  1  0        1 2 3 4 5 6
    3 ARG C  6.489   9.682 11.525 0 0 0 0  0  0        1 2 3 4 5 6
    4 HIS C  6.249   0.000  0.000 0 0 0 0  0  0        1 2 3 4 5 6
    5 ASP C  0.000   0.000  0.000 0 0 0 0  0  0        1 2 3 4 5 6
  </code></pre>
  <p>XYZ.dat</p>
  <pre><code>-------------------data--------------------        -------matrix-------
    0 GLU C  0.773   6.403  9.702 1 0 1 0 1 0 0        1 2 3 4 5 6 7 8 9 10
    1 TYR C  1.710   3.978  3.997 1 0 1 1 1 0 1        1 2 3 4 5 6 7 8 9 10
    2 ASP C  2.564   9.689  4.051 1 0 1 0 0 0 1        1 2 3 4 5 6 7 8 9 10
    3 TYR C  4.485   4.886  8.724 1 0 0 1 1 1 0        1 2 3 4 5 6 7 8 9 10
    4 HIS C  6.145   7.992  9.437 1 0 0 1 0 0 1        1 2 3 4 5 6 7 8 9 10
    5 ALA C  5.373   3.810  6.506 1 0 0 0 0 0 1        1 2 3 4 5 6 7 8 9 10
    6 HIS C  6.314   3.519  1.994 0 1 0 1 1 0 0        1 2 3 4 5 6 7 8 9 10
    7 ASP C  8.348   3.026  9.201 0 1 1 0 1 0 1        1 2 3 4 5 6 7 8 9 10
    8 TYR C  5.810   2.019  9.094 0 1 0 1 1 1 0        1 2 3 4 5 6 7 8 9 10
    9 LYS C  7.361   1.221  2.055 0 0 0 1 0 0 0        1 2 3 4 5 6 7 8 9 10
  </code></pre>
  <p>Can we feed these three files into CNN? Of course, we can. However, the issue will be the labels. In our data, each row has one label in the third column. If we feed these tables to a CNN as 2D data, we lose those labels. Because CNN will need one label for each 2D piece of data, which would be the protein name in our case,.</p>
  <p>Another issue is that a CNN must (and must) take 2D inputs of the same dimensions. In our case, each file has different dimensions. So, we must either resize the files to a common dimension (mean, median, or mode) or we need to zero-pad the smaller images to the maximum dimensions found among the files. In our case, this will achieve nothing. Because our 2D data will be useless in the first place because of the label issue described in the previous paragraph,.</p>
  <p>Kind regards.<br />
  The Student</p>
  </blockquote>
  <p>My professor disagrees with that.</p>
  <p>He wrote:</p>
  <blockquote>
  <blockquote>
  <p>Another issue is that a CNN must (and must) take 2D inputs of the same dimensions.</p>
  </blockquote>
  <p>No, it doesn't have to. Just take our previous paper as an example: the input is of size NxK for a protein of N residues, each residue has K features. The output layer is Nx2. For every protein the input has different size and we use the same network. which uses 1D convolution.</p>
  <blockquote>
  <p>In our case, each file has different dimensions. So, we must either resize the files to a common dimension (mean, median, or mode) or we need to zero-pad the smaller images to the maximum dimensions found among the files. In our case, this will achieve nothing. Because our 2D data will be useless in the first place because of the label issue described in the previous paragraph,.</p>
  </blockquote>
  <p>No, this is not required.</p>
  <p>However, you may use padding, otherwise your layers will shrink. More specifically, when your convolution window s WxW, your input of size NxN will produce output that is (N-W) x (N-W).</p>
  <p>Kind regards<br />
  The Supervisor</p>
  </blockquote>
  <p>Can you resolve my confusion?</p>
  <p>Must a CNN (both 1D and 2D) take input of the same size or not?</p>
  
Tags: <convolutional-neural-networks><image-recognition>
Title: Must a CNN (both 1D and 2D) take input of the same size?
UserDisplayName: user366312
ThreadText: # Must a CNN (both 1D and 2D) take input of the same size?
  
  (Asked by: username_0 on 2024-04-11)
  
  I have the notion that CNN input data must always be of the same dimensions. If we are feeding 1D tabular data, columns must be of the same numbers; if we are feeding 2D image data, all the images must be of the same dimensions. This resize may take place either in the input layer of the CNN or outside the CNN - in the data processing stage.
  
  So, I wrote my supervisor the following email:
  
  > Hello.
  >
  > CNN can process both 1D tabular data and 2D data. However, it cannot process 1D tabular data with different column lengths.
  >
  > We cannot use the following files:
  >
  > ABC.dat
  >
  > ```
  >   -------------------data--------------------      --matrix--
  >   6 LYS C  4.768   7.342 10.221 1 0 1 0  1  0        1 2 3
  >   7 TYR C  8.992   6.431  4.110 0 1 0 1  0  0        1 2 3
  >   8 SER C  2.345   8.901 12.345 0 0 1 0  1  1        1 2 3
  >
  > ```
  >
  > PQR.dat
  >
  > ```
  > -------------------data--------------------        ---matrix--
  >   0 ALA C  0.000   0.000  0.000 1 1 1 1  1  0        1 2 3 4 5 6
  >   1 GLU C  6.691   9.772  0.000 1 1 1 1  1  0        1 2 3 4 5 6
  >   2 PHE C  6.601   8.709 12.389 0 0 0 0  1  0        1 2 3 4 5 6
  >   3 ARG C  6.489   9.682 11.525 0 0 0 0  0  0        1 2 3 4 5 6
  >   4 HIS C  6.249   0.000  0.000 0 0 0 0  0  0        1 2 3 4 5 6
  >   5 ASP C  0.000   0.000  0.000 0 0 0 0  0  0        1 2 3 4 5 6
  >
  > ```
  >
  > XYZ.dat
  >
  > ```
  > -------------------data--------------------        -------matrix-------
  >   0 GLU C  0.773   6.403  9.702 1 0 1 0 1 0 0        1 2 3 4 5 6 7 8 9 10
  >   1 TYR C  1.710   3.978  3.997 1 0 1 1 1 0 1        1 2 3 4 5 6 7 8 9 10
  >   2 ASP C  2.564   9.689  4.051 1 0 1 0 0 0 1        1 2 3 4 5 6 7 8 9 10
  >   3 TYR C  4.485   4.886  8.724 1 0 0 1 1 1 0        1 2 3 4 5 6 7 8 9 10
  >   4 HIS C  6.145   7.992  9.437 1 0 0 1 0 0 1        1 2 3 4 5 6 7 8 9 10
  >   5 ALA C  5.373   3.810  6.506 1 0 0 0 0 0 1        1 2 3 4 5 6 7 8 9 10
  >   6 HIS C  6.314   3.519  1.994 0 1 0 1 1 0 0        1 2 3 4 5 6 7 8 9 10
  >   7 ASP C  8.348   3.026  9.201 0 1 1 0 1 0 1        1 2 3 4 5 6 7 8 9 10
  >   8 TYR C  5.810   2.019  9.094 0 1 0 1 1 1 0        1 2 3 4 5 6 7 8 9 10
  >   9 LYS C  7.361   1.221  2.055 0 0 0 1 0 0 0        1 2 3 4 5 6 7 8 9 10
  >
  > ```
  >
  > Can we feed these three files into CNN? Of course, we can. However, the issue will be the labels. In our data, each row has one label in the third column. If we feed these tables to a CNN as 2D data, we lose those labels. Because CNN will need one label for each 2D piece of data, which would be the protein name in our case,.
  >
  > Another issue is that a CNN must (and must) take 2D inputs of the same dimensions. In our case, each file has different dimensions. So, we must either resize the files to a common dimension (mean, median, or mode) or we need to zero-pad the smaller images to the maximum dimensions found among the files. In our case, this will achieve nothing. Because our 2D data will be useless in the first place because of the label issue described in the previous paragraph,.
  >
  > Kind regards.  
  > The Student
  
  My professor disagrees with that.
  
  He wrote:
  
  > > Another issue is that a CNN must (and must) take 2D inputs of the same dimensions.
  >
  > No, it doesn't have to. Just take our previous paper as an example: the input is of size NxK for a protein of N residues, each residue has K features. The output layer is Nx2. For every protein the input has different size and we use the same network. which uses 1D convolution.
  >
  > > In our case, each file has different dimensions. So, we must either resize the files to a common dimension (mean, median, or mode) or we need to zero-pad the smaller images to the maximum dimensions found among the files. In our case, this will achieve nothing. Because our 2D data will be useless in the first place because of the label issue described in the previous paragraph,.
  >
  > No, this is not required.
  >
  > However, you may use padding, otherwise your layers will shrink. More specifically, when your convolution window s WxW, your input of size NxN will produce output that is (N-W) x (N-W).
  >
  > Kind regards  
  > The Supervisor
  
  Can you resolve my confusion?
  
  Must a CNN (both 1D and 2D) take input of the same size or not?
  
  **Tags:** convolutional-neural-networks, image-recognition
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-04-11. Score: 2
  
  Yes padding is an option, but you can actually do better than that.
  
  Consider a convolutional layer: in order to define its parameters you only need to know the number of input channels, and the number of output channels you want. Indeed, its parameters are invariant to the height and width of the input
  
  For this reason, you can exploit the fact that all PNGs have 3 (or 4 if you also consider the alpha channel) channels, and create a model like this:
  
  1. a bunch of convolutional layer
  2. a layer that aggregate over height and width (such as a global average pooling)
  3. some fully connected layers
  4. classification head
  
  This model will indeed be able to work as expected with any image size in input
  
  Spoiler: it will be a nightmare to train, because you'll have to deal with tensors of different sizes, thus training on GPUs will be very very hard if no caution is taken
  
  
  ---
  
  