A4.3.9 · Machine learning approaches
Convolutional neural networks
By the end you can
Describe how filters, pooling and layers let a convolutional network learn a hierarchy of features in images, and what affects how it processes them.
A small photo of 224 × 224 pixels in colour has about 150 000 numbers. If every one were connected to every neuron in a first layer of 1000 neurons, that layer alone would need 150 million weights, and it would treat a cat in the top left and the same cat in the bottom right as completely different problems. A (CNN) avoids both problems with one idea: look at the image through a small window, and reuse the same window everywhere.
Convolution: a filter slides over the image#
A (or kernel) is a small grid of numbers, typically 3 × 3. To apply it, place it over a patch of the image, multiply each pair of overlapping numbers, and add up the results. That sum is one cell of the output. Then slide the filter along by the (1 cell, or 2, or more) and repeat. The output grid is a : it shows where in the image the filter's pattern was found. The size is (n − k) ÷ stride + 1 for an n × n image and a k × k filter, with no padding.
A filter with columns −1, 0, 1 gives a large positive number where the image goes from dark (left) to bright (right), and a large negative number where it goes the other way: it detects vertical edges. In a CNN the filter values are not typed in; they are learned during training, so the network discovers which patterns are useful.
Image 6 × 6
Filter 3 × 3
Feature map 4 × 4
After ReLU
After 2 × 2 max pooling
Feature map cell (row 1, column 1), the amber window on the image:
0 × -1 + 0 × 0 + 0 × 1 + 0 × -1 + 0 × 0 + 0 × 1 + 0 × -1 + 0 × 0 + 9 × 1 = 9
Large where dark meets light going left to right. Click a cell of the feature map to see its arithmetic. The output size is (6 − 3) ÷ 1 + 1 = 4 (rounded down) in each direction. Dark cells are large positive values, red cells are negative, and ReLU turns every negative value into 0.
The layers of a CNN#
| Layer | What it does | Output |
|---|---|---|
| Input | Holds the image as a grid of numbers (one grid per colour channel). | The pixels |
| Convolution + activation | Slides several learned filters over the input. ReLU then removes negative values. Each filter gives its own feature map. | A stack of feature maps |
| Summarises each small window by one value, usually the maximum. Shrinks the maps and keeps the strongest responses. | Smaller feature maps | |
| Repeat | More convolution and pooling layers combine simple features into complicated ones. | Higher-level feature maps |
| Flatten | Unrolls the final maps into one long list of numbers. | A vector |
| Ordinary neural network layers that combine all the features. The last one has an output for each class. | Class scores |
The result is a hierarchy of features. The first layers respond to edges and corners. The next combine edges into curves, textures and simple shapes. Deeper layers combine those into parts of objects (an eye, a wheel), and the final layers into whole objects. This mirrors the structure of images, where nearby pixels belong together, and it is why CNNs are used for object detection, face recognition and medical imaging (A4.1.1).
What changes the way a CNN works#
- Number of layers. More layers can learn more abstract features, but need more data, time and computing power, and are more likely to overfit.
- Kernel size and stride. Small kernels see fine detail; larger ones see more context. A larger stride gives smaller feature maps and quicker processing, but loses detail.
- Activation function. ReLU is the usual choice inside the convolutional layers, and a sigmoid or similar function turns the last layer into class probabilities.
- Loss function. The measure of error that training tries to reduce; a different task (classification or regression) needs a different loss.
Check yourself
Convolutional neural networks
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
6 questions · 22 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Explain why a convolutional neural network is better suited to image recognition than a network in which every pixel is connected to every neuron.
[3]Explain: Give reasons: say what happens AND why, or how one thing causes another.
2.Describe the roles of the convolutional layer, the pooling layer and the fully connected layer in a CNN.
[4]Describe: A detailed account: say what happens, step by step if it is a process.
3.A 28 × 28 image is convolved with a 5 × 5 filter using a stride of 1 and no padding. The resulting feature map is then max-pooled with 2 × 2 windows. Calculate the size of the pooled output.
[3]Calculate: Work out the number, showing the working.
4.The 3 × 3 image patch with rows 1 2 0 / 3 1 0 / 4 1 2 is multiplied cell by cell with the filter rows −1 0 1 / −1 0 1 / −1 0 1. Calculate the value of the feature map cell.
[2]Calculate: Work out the number, showing the working.
5.Outline four factors that affect how a CNN processes its input.
[4]Outline: A brief account: the main points, without much detail.
6.Explain how a convolutional neural network can learn to recognise a cat in a photograph, referring to the hierarchy of features it learns.
[6]Explain: Give reasons: say what happens AND why, or how one thing causes another.
In a nutshell
- A CNN is a neural network for grid-like data, mainly images. It uses small learned filters that slide over the input, so the same weights detect a pattern wherever it appears.
- Convolution: multiply the filter with the image patch under it and add: one feature map cell. Output size = (n − k) ÷ stride + 1.
- Then ReLU, then pooling (for example 2 × 2 max pooling halves each side); repeat; then flatten and use fully connected layers to classify.
- Layers learn a hierarchy of features: edges, then shapes and textures, then whole objects.
- Factors: number of layers, filter size, stride, activation function and loss function. CNNs need lots of labelled data and computing power (A4.1.2), but fewer weights than a fully connected network.
