Welcome to this tutorial on max pooling in Python. In this tutorial we will learn everything there is to learn about max-pooling.
After this tutorial we will be able to answer the following questions :
- What is pooling?
- Why do we need pooling?
- What is max pooling in Python?
- How to perform max-pooling?
Let’s get started.
What is pooling?
Pooling is a technique that reduces the number of features in a feature map. The operation involves applying a 2-Dimensional filter across a feature map. Doing this summarizes the features present in a region of the map.
What is a feature map?
A feature map is what we get as the output after applying the convolution layer on the input image. Convolution layers are the building blocks of Convolutional Neural Networks (CNN). CNNs are popular for their application in object detection.
Convolution layer applies a filter over the entire image repeatedly to detect features. The output of this convolution layer is a 2-Dimensional map that is known as a feature map.
But why do we need pooling? Let’s try and answer this question next.
Why do we need pooling?
One of the problems with feature maps generated from the convolution layer is that they are too sensitive to the location of the features in the image.
One solution for this is down-sampling of the feature map and that is exactly what pooling does.
By reducing the number of features we are able to avoid the problem of over-fitting to some extent. It also reduces the computational cost involved in the training of the model.
What is max pooling in Python?
There are different types of pooling techniques. Max pooling is one of them.
While applying max-pooling, the filter selects the maximum out of the pixels covered under the filter. The filter acts as a window out of which only the maximum value is selected for the output.
The operation of max pooling can be understood with the following example:
If we take the image array to be:
[[2, 3, 4, 2],
[8, 5, 5, 1],
[6, 7, 9, 4],
[3, 1, 4, 5]]
Apply a max-pooling filter with size 2X2 and a stride of 2 on this array.
We fill get the following output :
Note how every value in the output is the maximum value from a 2X2 window in the original array.
How is max pooling done in python?
Now that we have understood what is max pooling, let’s learn how to write a python code for it.
We will use the Keras to implement max pooling.
The Python code for applying max pooling on a numpy array is as follows:
import numpy as np
from keras.models import Sequential
from keras.layers import MaxPooling2D
arr = np.array([[2, 3, 4, 2],
[8, 5, 5, 1],
[6, 7, 9, 4],
[3, 1, 4, 5]])
arr = image.reshape(1, 4, 4, 1)
#define a max pooling layer
max_pool = MaxPooling2D(pool_size = 2, strides = 2)
#define a sequential model with just one pooling layer
model = Sequential(
#get the output
output = model.predict(arr)
#print the output
output = np.squeeze(output)
We get the output as :
In this example, we used a small array to explain the concept, but in reality feature maps of images can be much bigger in size.
This tutorial was about max-pooling in Python. We learned about pooling and the need for pooling. In the final section of the tutorial, we used Keras to implement max-pooling.
Another type of pooling technique that is quite popular is average-pooling. In average pooling, the average value is calculated for each window. Hope you had fun learning with us.