Class 10 · Unit 4: Computer vision · Lesson 4.5 · 40 min
Inside a CNN
Take your feature map from last lesson. Two formulas later you'll have built three layers of a real neural network.
Today you will: Apply ReLU to a feature map · Shrink it with max pooling · Trace an image through all four layers of a CNN
Story
Rahat has the apple's feature map up on his screen, all reds and greens.
Rahat: “So is this it? Is this how the phone knows it's an apple?”
Hiba: “It's the start. That's just the edges. A real network does that with dozens of kernels, then throws half the numbers away, then shrinks what's left, then does it all again.”
Rahat: “Throws numbers away? On purpose?”
Hiba: “On purpose. It keeps what matters. Starting with those.” (She points at the red band down the left side.)
Watch
Four layers, one apple — — scan code 10.4.5 in the printed book.
Learn it
A Convolutional Neural Network (CNN) is the deep-learning model that recognises images, and it is built from the operation you just learned, repeated in four kinds of layer. It finds features with many kernels, sharpens them, shrinks them, then weighs everything into a score for each label. The big difference from Lesson 4.4: nobody chooses the kernels. The network learns them from labelled images.
Words to know
- CNN a deep-learning model for images, with learnable weights and biases.
- Convolution layer many kernels, each making a feature map (also called an activation map). Early layers find edges and colours; deeper layers combine them into shapes, then objects.
- ReLU (Rectified Linear Unit) — turns every negative number into 0 and leaves positives alone, so useful features stand out.
- Pooling layer shrinks a feature map while keeping what matters. Max pooling keeps the biggest number in each patch; average pooling keeps the average.
- Flatten read a grid row by row into one long list.
- Fully connected layer gives every number in the list a weight for each label and adds them up into scores, like apple 0.93, pear 0.05.
Step by step: a 4 × 4 feature map through two layers
12 -5 0 8
-3 6 -9 2
4 0 7 -6
-1 9 3 5
- ReLU: every negative becomes 0 → rows become 12 0 0 8 · 0 6 0 2 · 4 0 7 0 · 0 9 3 5.
- Max pooling (2 × 2 blocks): top-left block {12, 0, 0, 6} → 12; top-right {0, 8, 0, 2} → 8; bottom-left {4, 0, 0, 9} → 9; bottom-right {7, 0, 3, 5} → 7.
- The pooled map is 2 × 2: 12 8 / 9 7. Sixteen numbers became four, and the strong features survived.
- Flatten: 12, 8, 9, 7.
- Fully connected: each of the four gets a weight for "apple" and for "not apple"; the sums become the two scores.
How it learns: a CNN starts with random kernels and weights, and adjusts them after every mistake (Lesson 2.4), until its kernels find the features that matter for its labels.
Careful: ReLU and pooling do not throw away what matters. ReLU drops what the next layer does not need, and pooling keeps the strongest signal in each patch, which is why the edge survives. And a CNN does not look for apples; it looks for patterns of numbers that came with the label apple.
On the laptop
You need: LibreOffice Calc · your convolution.ods from Lesson 4.4
Mission 1: ReLU (8 min, in pairs)
- ☐1
Add a sheet named ReLU. In A1:
=MAX(0;$Output.A1). Fill it across to J and down to 10. - ☐2
Predict: what happens to the red band down the left of the apple? Write it down.
- ☐3
Give ReLU the same colour scale as Output. Were you right?
Mission 2: Max pooling (10 min)
- ☐4
Add a sheet named Pool. In A1, type
=MAX(OFFSET($ReLU.$A$1;(ROW()-1)*2;(COLUMN()-1)*2;2;2))— count the brackets: every one you open, you close. It finds the biggest number in each 2 × 2 block of ReLU. Fill it across to E and down to 5. - ☐5
Predict: the pooled map is 5 × 5. Will you still be able to see the edge of the apple?
- ☐6
Colour it. Count: how many numbers did you start with, after convolution, and after pooling?
Mission 3: Fully connected, on paper (5 min)
- ☐7
Read your 5 × 5 pool row by row into one list of 25 numbers — that is flattening.
- ☐8
A fully connected layer gives every one of the 25 numbers a weight for "apple" and a weight for "not apple", and adds them up. Which positions in your list would you give a big "apple" weight to? Why?
Finished early?
Change the pooling to average pooling: replace MAX with AVERAGE.
Compare the two 5 × 5 maps. Which keeps the edge sharper?
No laptop today?
Your teacher writes a 4 × 4 feature map with some negative numbers on the board. The class does ReLU aloud (every negative → 0), then max pooling into a 2 × 2, one block per row of desks. Then flatten the 2 × 2 into a list of 4 and discuss which should count most towards "apple".
Debate it
A CNN that recognises faces learned its kernels from millions of photos. Nobody can point to the kernel that means "eyes".
- If a face-recognition system wrongly matched a person to a crime, could anyone explain why? (Lesson 2.4's Think asked the same about neural networks.)
- Should a system nobody can explain be used to make decisions about people? Where would you draw the line?
Check yourself — practice, not a test
1. Put the layers of a CNN in order:
- 1. Convolution
- 2. Pooling
- 3. Fully connected
- 4. ReLU
2. What does ReLU do to a feature map?
- ○ Makes it smaller
- ○ Turns negative numbers into 0 and keeps the positives
- ○ Adds a kernel
- ○ Gives the final label
3. Max pooling on the 2 × 2 block (3, 7, 0, 5) gives:
- ○ 3.75
- ○ 0
- ○ 7
- ○ 15
4. In a CNN, a person chooses every kernel by hand.
- ○ True
- ○ False
5. The output of a convolution layer is called a feature ______.
Remember
Find features, drop the negatives, shrink, then vote.
Typing practice
Skill: a formula inside a formula (5 min)
-
On typing.com: the symbols lessons.
-
Type three times, eyes on the screen:
=MAX(OFFSET($ReLU.$A$1;(ROW()-1)*2;(COLUMN()-1)*2;2;2)) -
Count the brackets: every one you open, you close. Goal this term: 38–44 WPM at 95%.