Convolution and CNNs
What a convolution kernel does, why the same operation finds edges and recognises cats, and how output size and cost are calculated.
The idea
Lay a nine-square stencil over an image. Multiply every pixel under the stencil by the number in the corresponding square, add it all up, and write the result into a new image. Then shift the stencil one pixel and repeat.
That is a convolution. Depending on the numbers in the stencil, the result traces edges, blurs, sharpens, or highlights particular patterns.
Well-known kernels
| Kernel | Effect |
|---|---|
[[0,0,0],[0,1,0],[0,0,0]] | Nothing, the image is unchanged |
1/9 · [[1,1,1],[1,1,1],[1,1,1]] | Blur |
[[0,-1,0],[-1,5,-1],[0,-1,0]] | Sharpen |
[[-1,0,1],[-2,0,2],[-1,0,1]] | Vertical edges (Sobel) |
Convolution by hand
import cv2, numpy as np
img = cv2.imread("part.jpg", cv2.IMREAD_GRAYSCALE).astype(np.float32)
sharpen = np.array([[ 0, -1, 0],
[-1, 5, -1],
[ 0, -1, 0]], dtype=np.float32)
sharp = cv2.filter2D(img, -1, sharpen)
# The sum of the kernel values sets the brightness of the result:
# sum 1 leaves it unchanged, sum 0 gives a dark edge image.
print(sharpen.sum()) # 1.0The three dials
| Dial | Effect |
|---|---|
| Kernel size | How much context a filter sees. Usually 3, rarely 5 or 7. |
| Stride | Step size. Stride 2 halves the output size. |
| Padding | Add a border so the output keeps its size. |
- For an unchanged output size with kernel 3, always set padding 1.
- Pooling reduces resolution without parameters; stride 2 does the same with them.
- With multiple channels, a kernel always spans the full input channel depth.
The formulas
Worked through
A layer with H = W = 224, k = 3, p = 1, s = 1, C_in = 64,
C_out = 128:
H_out = ⌊(224 + 2 − 3)/1⌋ + 1 = 224, resolution preserved.- Parameters:
9 × 64 × 128 + 128 = 73,856 - FLOPs:
2 × 224 × 224 × 9 × 64 × 128 ≈ 7.4e9per image
A single layer costs 7.4 GFLOPs. A fifty-layer network lands at several hundred GFLOPs per image, which gives the required compute directly. See Why GPUs.
Why separable convolutions save so much
A depthwise separable convolution splits the operation into one convolution per channel followed by a 1 by 1 convolution across channels.
For k = 3 and C_out = 128 that is 1/128 + 1/9 = 0.119: just under an eighth
of the cost. MobileNet and every architecture aimed at edge devices rests on this.
Related courses and sources
An Image is Worth 16x16 Words
Images as a sequence of patches, processed like text. The paper that brought transformers into vision.
For anyone processing image and text in one model.
Deep Learning Specialization
Five courses from the basics of neural networks to sequence models. Thorough, with programming exercises, and in places older than current practice.
For anyone who can program and wants to work through the field completely.
Deep Residual Learning
The shortcut across layers that made hundred-layer networks trainable. Present in every architecture today.
For understanding how networks were able to get deep at all.
Dive into Deep Learning
A textbook with runnable code beside every derivation. Each chapter opens as a notebook you can recompute yourself.
For anyone who wants to compute along while reading; every chapter opens as a notebook.
Hugging Face computer vision course
From image preprocessing through convolutional networks to vision transformers, with runnable examples for detection and segmentation.
For development with image data; assumes Python and delivers runnable examples in exchange.
Practical Deep Learning for Coders
Starts with a working model in the first hour and supplies the theory afterwards. The shortest route from basic Python to a model you trained yourself.
For impatient readers who know Python: your first model runs within the first hour.
U-Net
Segmentation from few examples, developed in medical imaging. Still the first choice for segmentation.
For segmentation from few examples; still the first choice.
Very Deep Convolutional Networks
The paper showing that depth with small filters wins. The architecture convolutional networks are usually explained with.
For getting into convolutional networks; the architecture they are usually explained with.