In this lesson, we will see in more detail what feature maps are, how to take them, and how to display them with Keras.

Before going on I suggest you see the previous lesson Conv2D Layer | Computer Vision with Keras p.3 because what we get as a result of the convolutional layer is the extraction of the features and the feature map is the result of all the features we have extracted.

Display feature map

As we saw in the previous lesson we take the feature map which includes 64 images

feature_map = model.predict(np.array([img]))

but this time we use the matplotlib library instead of OpenCV. If you don’t already have it you will need to install it

from matplotlib import pyplot as plt

and show the first image of the feature map

feature_img = feature_map[0, :, :, 1]

this is achieved for the first image and I remember we have 64 of these

feature map first image of array

What is a feature map

To better understand this part, let’s see all of them by looping through the filter

# Display feature Map
for i in range(64):
    feature_img = feature_map[0, :, :, i]
    ax = plt.subplot(8, 8, i+1)
    ax.set_xticks([])
    ax.set_yticks([])
    plt.imshow(feature_img, cmap="gray")
plt.show()

as you can see from the image below there are different versions of the same dog image. Each image represents a feature that allows us to make the model recognize if a dog is present. the purpose of the feature map is to extract features and the farther we go with de model less information we have.

display of image of dog feature map

For example, if we want to reconstitute the face it has many features: lips, nose, etc. ideally each image of these represents a feature of the face and this at the end of the process allows us to say whether this is a face or not a face.

Each image extracts something different.

In the next lessons, we will see how to apply these concepts to make everything even clearer.