question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MaxPooling2d() turns odd input into even numbers, is there anyway to go around this?

See original GitHub issue

During 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:

  1. Unpooling and deconvolution #378
  2. MaxPooling2D fails to utilize last column in odd dimensional input #2094

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

27reactions
thefiddlercommented, Jan 1, 2017

Indeed, UpSampling2D() only supports integer factors (2x, 3x etc). You can either add the necessary ZeroPadding2D(), 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 by 2**7 == 128, i.e. (h, w) = (384, 512). You can do this by adding a single ZeroPadding2D and Cropping2D statement. Conceptually, it looks like this:

input = Input(shape)
x = ZeroPadding2D((12, 12, 16, 16))(input)
# rest of your network here
x = Cropping2D(((12, 12), (16, 16))(x)
model = Model(input, x)
2reactions
thefiddlercommented, Dec 31, 2016

You can use MaxPooling2D(..., border_mode="same") to create the correct output shape.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found