What's the purpose of padding with Maxpooling?

As mentioned in the question, i've noticed that sometimes there are pooling layers with padding.

More specifically, I found this Keras tutorial, where there's a net which contains MaxPooling layers with padding.

If padding=same in convolutional layers, our output size (at least height and width, depth can change based on the number of filters) is the same as the input.

I expected the same with the MaxPooling layer, but Keras model.summary() (as shown in the article) shows that the output size after the pooling layers is half of the input.

What's the point of adding padding to the Pooling layer if we still get an output which is half of the input?

Topic pooling cnn keras convolution

Category Data Science


I think the reason is to make pool_size and stride arguments compatible with the output shape of previous layers. The command can be read as: in case it's not compatible, add padding all hyperparameters compatible.

As shown in the docs, the MaxPool2d() layer has padding='valid' as default. Evidently the author had a preference for 'same'.


The whole purpose of pooling layers is to reduce the spatial dimensions (height and width). Therefore, padding is not used to prevent a spatial size reduction like it is often for convolutional layers. Instead padding might be required to process inputs with a shape that does not perfectly fit kernel size and stride of the pooling layer.

This is an example where it perfectly fits and your pooling layer does not require any padding: Pooling with kernel size 2x2, stride 2, no padding

Side note: The output dimensions are calculated using the usual formula of $O=\frac{I-K+2P}{S}+1$ with $I$ as input size, $K$ as kernel size, $P$ as padding and $S$ as stride.

However, lets take another example where it does not fit as nicely:

Pooling with kernel size 2x2, stride 2 and padding

Here you need padding since your input size is not an integer multiple of your kernel size. Therefore, you need to add padding on one side in order make it work.

So padding="same" in Keras does not mean the spatial dimensions do not change. It just means that padding is added as required to make up for overlaps when the input size and kernel size do not perfectly fit.

Also this question for a discussion what the difference between same and valid padding is for pooling layers.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.