Max pooling layer | Computer Vision with Keras p.5

In this tutorial, we will see what the Max pooling layer on a convolutional neural network is, what parameters to set and how to use it. Before going on with the lesson I suggest you, if you haven’t already done so, see the previous episode Feature map | Computer Vision with Keras p.4

What is the Max pooling layer?

The operation with the Max pooling layer consists of taking an image’s pixel square and extracting that square’s highest value.

For example, the image below the feature map is divided into many groups of 4 pixels and the highest value is extracted from each group. You will understand the concept better in the next steps

feature map print first image dog

Prepare the code for the Max pooling layer

Before proceeding with the demonstration it is necessary to make some changes to the code to print the values of our interest. The code is practically identical to that of the previous lesson so I will focus only on the explanation of the points of interest, in any case, you can download the complete code from the download at the bottom of the page.

Set to 0 random seed

When you go to show the array with the value of the pixels relative to the image, Keras generates random numbers for each extraction and this becomes a problem when we have to make a comparison.

keras pixel image conv2D layer

but setting the random seeds to zero we solved

import tensorflow as tf

# Use random seed

Now let’s prepare the Keras model and print the image pixels with the conv2D layer and max pooling layer

# CNN Model
model = keras.Sequential()
model.add(layers.Conv2D(input_shape=(32, 32, 3), kernel_size=(3, 3), filters=64))
model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

as seen in the code above in the max pooling parameters we consider the square of 2 x 2 pixels, this means that we must take the highest value in each group of 4.

Comparison between Conv2D layer and max pooling layer

As you can see in the image below I have highlighted the first group of 2×2 pixels of the conv2D layer of which only the highest value is recovered to compose the max pooling layer

conv2D layer to max pooling layer

Obviously, in the example, I have only circled the first group but the same thing is repeated every 2×2 square, you just need to compare the values to verify the theory.


Learn to build Computer Vision Software easily and efficiently.

This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.

Sign UP for FREE