LambdaLabTM
Artificial Intelligence · Class 10 · Unit 5 · Computer Vision
class 10 aipractical unit⏱️ 9 min read

Layers of a CNN

You met the CNN in Unit 2 as one of the two deep learning models. Here is what is actually inside it — four layers, each doing one job to the picture on its way to becoming a label.

Convolutional Neural Network
A deep learning algorithm that can take in an input image, assign importance — learnable weights and biases — to various aspects and objects in the image, and be able to differentiate one from the other.

1The four layers

In order, and each in one line
#LayerWhat it does
1Convolution LayerExtracts features such as edges, using kernels. Its output is the feature map.
2Rectified Linear Unit (ReLU)Removes every negative number from the feature map; positives stay as they are.
3Pooling LayerReduces the size of the feature map while keeping the important features.
4Fully Connected LayerFlattens everything and turns it into a label.

21. Convolution layer

The first layer of a CNN. Its objective is to extract the high-level features such as edges from the input image, using the convolution operation from the previous lesson.

More than one of them
A CNN need not be limited to a single convolutional layer. Conventionally the first one captures low-level features — edges, colour, gradient orientation. With added layers the network adapts to high-level features too, giving a whole understanding of the images in the dataset.
Feature map
Several kernels are used in this layer to produce several features. The output of the layer is called the feature map, also called the activation map— the two terms are interchangeable.

Two things the feature map buys you:

  • The image size is reduced, so it can be processed more efficiently.
  • Only the useful features are kept. You might only need someone’s eyes, nose and mouth to recognise them — not the whole face.

32. Rectified Linear Unit (ReLU)

ReLU
After the feature map is produced it is passed to the ReLU layer, which simply gets rid of all the negative numbers in the feature map and lets the positive numbers stay as they are.

Passing the feature map through ReLU introduces non-linearity. The practical effect is that a smooth grey gradient from black to white becomes an abrupt colour change, which makes the edges more obvious and gives the later layers a better feature to work with.

43. Pooling layer

Pooling
Responsible for reducing the spatial size of the convolved feature while still retaining the important features. Two types can be performed:
Max Pooling returns the maximum value from the portion of the image covered by the kernel.
Average Pooling returns the average value from that portion.
A misprint in the textbook
The Class X handbook defines average pooling as “Max Pooling returns the maximum value…” — the same sentence as max pooling, copied by mistake. Average pooling returns the average, which is what its name says and what the widget below computes.
ReLU and pooling, by hand

A four-by-four patch of a feature map. Step through and check the arithmetic yourself.

12
-40
8
3
-7
96
-15
22
30
4
-60
18
-2
41
9
-25
The raw feature map that came out of the convolution layer. Some values are negative.

ReLU introduces non-linearity and makes the colour change more abrupt, which sharpens the edges for the layers that come after it.

The pooling layer earns its place by doing two things:

  1. Making the image smaller and more manageable.
  2. Making the image more resistant to small transformations, distortions and translations in the input — a small difference in the input image gives a very similar pooled image.

54. Fully connected layer

Fully Connected (FC) layer
The final layer. Its objective is to take the results of the convolution and pooling process and use them to classify the image into a label.

The output of convolution and pooling is flattened into a single vector of values, each representing a probability that a certain feature belongs to a label. If the image is of a cat, the features representing whiskers or fur should have high probabilities for the label “cat”.

Evidence, then verdict

The first three layers collect evidence: here is an edge, here is a corner, here is a whisker-shaped thing, and the picture is now small enough to hold in your head. The fully connected layer is the moment somebody looks at all that evidence together and says the word: cat.

6Practice

1
Name the four layers of a CNN and state what each one does. (Practice)
4 marks
▶Show the answer

1. Convolution layer — the first layer. It extracts high-level features such as edges from the input image using kernels; its output is the feature map.
2. ReLU layer — removes all the negative numbers from the feature map and lets the positive numbers stay, introducing non-linearity and making edges more obvious.
3. Pooling layer — reduces the spatial size of the convolved feature while retaining the important features, using max or average pooling. It makes the image smaller and more resistant to small distortions.
4. Fully connected layer — the final layer. It flattens the output into a single vector and uses it to classify the image into a label.

2
What is the primary objective of the Convolution Layer in a CNN?
A) To flatten the input image B) To assign importance to various aspects of the image C) To reduce the spatial size of the input image D) To perform element-wise multiplication of image arraysClass X handbook, Test Yourself
1 mark
▶Show the answer

(B) To assign importance to various aspects/objects in the image. Careful with (D): element-wise multiplication is how the layer works, not its objective. (A) is the fully connected layer and (C) is pooling.

7Check yourself

Quick Check

What does the ReLU layer do?

Quick Check

Which layer reduces the spatial size of the convolved feature?

Quick Check

Max pooling returns which value from the portion covered by the kernel?

Quick Check

What is the output of the convolution layer called?