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.
1The four layers
| # | Layer | What it does |
|---|---|---|
| 1 | Convolution Layer | Extracts features such as edges, using kernels. Its output is the feature map. |
| 2 | Rectified Linear Unit (ReLU) | Removes every negative number from the feature map; positives stay as they are. |
| 3 | Pooling Layer | Reduces the size of the feature map while keeping the important features. |
| 4 | Fully Connected Layer | Flattens 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.
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)
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
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 four-by-four patch of a feature map. Step through and check the arithmetic yourself.
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:
- Making the image smaller and more manageable.
- 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
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
6Practice
▶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.
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
▶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
What does the ReLU layer do?
Which layer reduces the spatial size of the convolved feature?
Max pooling returns which value from the portion covered by the kernel?
What is the output of the convolution layer called?