MaxPooling2d() turns odd input into even numbers, is there anyway to go around this?
See original GitHub issueDuring MaxPooling2d()
the row with odd number [ 45 ]
becomes even [ 22 ]
, this becomes an issue during Deconv or let’s say UpSampling2D()
, it doesn’t count up to the same sizes as it started.
Here is a gist of the full model, but what I’m pointing to is the maxpooling2d_59
block4_conv3 (Convolution2D) (None, 512, 45, 60) 2359808 activation_299[0][0]
____________________________________________________________________________________________________
batchnormalization_312 (BatchNorm(None, 512, 45, 60) 120 block4_conv3[0][0]
____________________________________________________________________________________________________
activation_300 (Activation) (None, 512, 45, 60) 0 batchnormalization_312[0][0]
____________________________________________________________________________________________________
maxpooling2d_59 (MaxPooling2D) (None, 512, 22, 30) 0 activation_300[0][0]
____________________________________________________________________________________________________
block5_conv1 (Convolution2D) (None, 512, 22, 30) 2359808 maxpooling2d_59[0][0]
____________________________________________________________________________________________________
batchnormalization_313 (BatchNorm(None, 512, 22, 30) 60 block5_conv1[0][0]
____________________________________________________________________________________________________
activation_301 (Activation) (None, 512, 22, 30) 0 batchnormalization_313[0][0]
The issue is here, at size 22
to 44
, rather 45
convolution2d_42 (Convolution2D) (None, 512, 22, 30) 2359808 activation_305[0][0]
____________________________________________________________________________________________________
batchnormalization_318 (BatchNorm(None, 512, 22, 30) 60 convolution2d_42[0][0]
____________________________________________________________________________________________________
activation_306 (Activation) (None, 512, 22, 30) 0 batchnormalization_318[0][0]
____________________________________________________________________________________________________
upsampling2d_52 (UpSampling2D) (None, 512, 44, 60) 0 activation_306[0][0]
____________________________________________________________________________________________________
convolution2d_43 (Convolution2D) (None, 512, 44, 60) 2359808 upsampling2d_52[0][0]
____________________________________________________________________________________________________
batchnormalization_319 (BatchNorm(None, 512, 44, 60) 120 convolution2d_43[0][0]
____________________________________________________________________________________________________
activation_307 (Activation) (None, 512, 44, 60) 0 batchnormalization_319[0][0]
____________________________________________________________________________________________________
It causes this error otherwise continuing to a Reshape()
.
ValueError: total size of new array must be unchanged
Relevant issues and each seem to solve it a different way:
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
MaxPooling2d() turns odd input into even numbers, is there anyway ...
During MaxPooling2d() the row with odd number [ 45 ] becomes even [ 22 ] , this becomes an issue during Deconv or...
Read more >Is maxpooling on odd number possible? - Stack Overflow
Regarding your first question: You can read in the documentation how the output shape of max-pooling is computed.
Read more >MaxPooling2D layer - Keras
Max pooling operation for 2D spatial data. Downsamples the input along its spatial dimensions (height and width) by taking the maximum value over...
Read more >How does Max Pooling handle Odd Image Dimensions? [closed]
For the even image dimension case, max pooling is simple to understand - it simply performs convolution over the image with the max...
Read more >A guide to an efficient way to build neural network ...
Here we will speak about the additional parameters present in CNNs, ... It is the equal to the number of color channels for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Indeed,
UpSampling2D()
only supports integer factors (2x, 3x etc). You can either add the necessaryZeroPadding2D()
, or, alternatively, pad your input before you feed it to the network. For example, instead of an image of(h, w) = (360, 480)
, resize (or pad) your input to the nearest factor that can be evenly divided by 2 until the bottom of your network.For example, if your network has 7x
MaxPooling2D
operations, you need to resize (or pad) your input so that it’s divisible by2**7 == 128
, i.e.(h, w) = (384, 512)
. You can do this by adding a singleZeroPadding2D
andCropping2D
statement. Conceptually, it looks like this:You can use
MaxPooling2D(..., border_mode="same")
to create the correct output shape.