Convolutional networks
About this lesson
A convolutional network (CNN) reads an image through small filters instead of wiring every pixel to every neuron. Slide one 3×3 filter over a 6×6 image, multiply and add at each of 16 positions to build a feature map, then apply ReLU and max pooling. See why sharing nine weights beats 576, and how stacked, learned filters turn edges into objects.
An image is numbers
To a computer, an image is a grid of numbers, one per pixel (picture dot). Our tiny 6×6 image uses 1 for bright and 0 for dark. It shows a short vertical stroke, two pixels wide.
x (6x6), 1 = bright, 0 = dark
0 0 1 1 0 0
0 0 1 1 0 0
0 0 1 1 0 0
0 0 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
a real photo: 3 numbers per pixel (red, green, blue)Read the lesson step by step
An image is numbers
To a computer, an image is a grid of numbers, one per pixel (picture dot). Our tiny 6×6 image uses 1 for bright and 0 for dark. It shows a short vertical stroke, two pixels wide.
x (6x6), 1 = bright, 0 = dark 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 a real photo: 3 numbers per pixel (red, green, blue)A filter: nine weights
A convolutional network (CNN) looks at small patches, not the whole image at once. It uses a filter, also called a kernel: a 3×3 grid of weights. Ours, +1 left, 0 middle, −1 right, looks for bright-left, dark-right edges.
k (3x3 filter) 1 0 −1 1 0 −1 1 0 −1 big positive answer: bright on the left, dark on the right big negative answer: dark on the left, bright on the rightMultiply and add
Lay the filter on the top-left 3×3 patch. Multiply each pixel by the weight on it, then add the nine products. Every row gives 0 + 0 − 1, so the total is −3. That one number starts the feature map.
patch (rows 0-2, columns 0-2) times k: 0 0 −1 0 0 −1 0 0 −1 sum = (0 + 0 + −1) + (0 + 0 + −1) + (0 + 0 + −1) = −3 -> fmap[0][0]Slide it everywhere
Slide the filter one pixel right and repeat, row by row. It fits 6 − 3 + 1 = 4 ways across and 4 down: 16 positions, 16 numbers. Over the stroke's right edge it gives 3. These numbers form a 4×4 feature map.
for i in range(4): # 4 rows of positions for j in range(4): # 4 columns at (0, 2): (1 + 0 + 0) + (1 + 0 + 0) + (1 + 0 + 0) = 3 fmap −3 −3 3 3 −3 −3 3 3 −2 −2 2 2 −1 −1 1 1One filter, shared
The same nine weights were used at all 16 positions. That is weight sharing: this layer has just 9 weights plus 1 bias (here 0), whatever the image size. Linking all 36 pixels to all 16 outputs would need 576 weights.
convolution 9 weights + 1 bias = 10 numbers to learn dense 36 inputs x 16 outputs = 576 weights and the filter finds the edge wherever it is in the imageReLU drops negatives
Next, an activation function called ReLU turns every negative number into 0 and keeps the rest: max(0, v). The −3s marked the stroke's other edge, dark-to-bright, which this filter is not looking for. Now only its edge remains.
relu(v) = max(0, v) before −3 −3 3 3 −3 −3 3 3 −2 −2 2 2 −1 −1 1 1 after 0 0 3 3 0 0 3 3 0 0 2 2 0 0 1 1Pooling shrinks the map
Max pooling splits the map into 2×2 blocks and keeps the biggest number in each. The 4×4 map becomes 2×2: 0, 3, 0, 2. It is four times smaller and still says: the edge is on the right, strongest at the top.
maxpool: the largest of each 2x2 block relu(fmap) 0 0 3 3 0 0 3 3 0 0 2 2 0 0 1 1 out 0 3 0 2Many filters, many maps
A real layer has many filters, each making its own feature map: one finds vertical edges, another horizontal ones, another colour blobs. AlexNet's first layer has 96. Deeper layers filter those maps, finding corners, then textures, then parts of objects.
layer 1 edges, colour blobs layer 2 corners, simple textures deeper parts of objects: eyes, wheels, letters AlexNet (2012), first layer: 96 filters of 11x11x3From maps to an answer
At the end, the small maps are flattened into one list of numbers and fed to an ordinary dense layer. It gives a score to each class, here "vertical" or "horizontal" stroke, and the highest score is the answer.
flatten [[0, 3], [0, 2]] -> [0, 3, 0, 2] dense one score per class vertical <- highest horizontal conv -> ReLU -> pool, repeated, then denseThe filters are learned
We chose our filter by hand. In a real CNN the filter weights start random. Training compares the answer with the label, and backpropagation with gradient descent nudges every weight to lower the loss. Edge-finding filters appear on their own.
start random weights in every filter repeat predict, measure the loss, backpropagate the slopes, nudge every weight downhill nobody writes "look for edges": it is learned
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A 3×3 filter with weights +1, 0, −1 in each row sits on a patch whose rows are all 1, 1, 0. What number does it produce (bias 0)?
Each row gives 1×1 + 1×0 + 0×(−1) = 1. Three rows add up to 3: bright on the left and dark on the right, exactly the edge this filter looks for.
02A 3×3 filter slides over an 8×8 image with stride 1 and no padding. How big is the feature map?
Output size = (8 − 3 + 0) ÷ 1 + 1 = 6, so the filter fits 6 ways across and 6 down: a 6×6 map.
03Why does a convolutional layer need far fewer weights than a dense layer on the same image?
One 3×3 filter has 9 weights plus a bias wherever it is used. Wiring 36 pixels to 16 outputs densely would need 576 separate weights.
04After ReLU, a 2×2 block of the feature map holds 0, 3, 0 and 3. What does 2×2 max pooling keep?
Max pooling keeps the largest number in each block, here 3. (Average pooling would give 1.5.)
05In a real CNN, where do the filter weights come from?
Training compares the network's answer with the label and nudges every weight to lower the loss. Edge detectors appear in the first layer without anyone asking for them.
Glossary
- Convolutional neural network (CNN)
- A neural network whose main layers slide small learned filters over the input, used above all for images.
- Filter (kernel)
- A small grid of weights, such as 3×3, that is laid over each patch of the input; its products with the patch are added into one number.
- Feature map
- The grid of numbers a filter produces, one for each place it fits. Big values mark where the filter's pattern is.
- Stride and padding
- Stride is how many pixels the filter jumps each time; padding adds a border of zeros around the input. Output size = (input − filter + 2 × padding) ÷ stride + 1.
- Weight sharing
- Using the same filter weights at every position, so the layer needs few weights and finds a pattern wherever it appears.
- ReLU
- An activation function, max(0, v): negative numbers become 0, positive ones pass unchanged.
- Max pooling
- Shrinking a map by keeping only the largest number in each small block, typically 2×2 with stride 2.
- Dense (fully connected) layer
- A layer in which every input is linked to every output by its own weight; CNNs usually end with one to score the classes.
Notes and sources
- Strictly, CNN libraries compute cross-correlation: the filter is not flipped before it slides. They still call it convolution, and since the weights are learned, the difference does not matter.
- Output size = (input − filter + 2 × padding) ÷ stride + 1 = (6 − 3 + 0) ÷ 1 + 1 = 4. Padding adds a border of zeros so the map can keep the input's size; stride is how far the filter jumps each time.
- A colour image has three channels (red, green, blue), so a real first-layer filter is 3×3×3 and adds up 27 products, plus its bias.
- The final dense layer is shown, not computed: its weights would be learned too. The Python program is real; run it and it prints the feature map and the pooled 2×2 map.
- Stanford CS231n, Convolutional Neural Networks for Visual Recognition: notes on convolutional layers, pooling and architectures
- Goodfellow, Bengio & Courville, Deep Learning (MIT Press, 2016), chapter 9: Convolutional Networks
- LeCun, Bottou, Bengio & Haffner, Gradient-Based Learning Applied to Document Recognition, Proc. IEEE (1998)
- Krizhevsky, Sutskever & Hinton, ImageNet Classification with Deep Convolutional Neural Networks (NeurIPS 2012)
- Zeiler & Fergus, Visualizing and Understanding Convolutional Networks (ECCV 2014)
- Nair & Hinton, Rectified Linear Units Improve Restricted Boltzmann Machines (ICML 2010)
- PyTorch documentation, torch.nn.Conv2d (computes cross-correlation)
- R. Szeliski, Computer Vision: Algorithms and Applications, 2nd ed. (2022): image filtering and edge detection
Quiz Unlocks after the last step.