Blame
|
1 | # 10. Convolutional networks |
||||||
| 2 | ||||||||
| 3 | A dense layer treats an image as a flat vector, so it must learn a separate weight for every pixel and forgets that nearby pixels belong together. Convolutional networks replace that dense connectivity with a small filter that slides across the grid, reusing the same weights everywhere. This module introduces the convolution as a structured layer for grid data, then builds up stride, padding, channels, and pooling. |
|||||||
| 4 | ||||||||
| 5 | **Objectives** |
|||||||
| 6 | - Motivate convolution from locality, translation equivariance, and parameter sharing. |
|||||||
| 7 | - Define the 2D convolution (cross-correlation) used in deep learning. |
|||||||
| 8 | - Compute the output size from input size, kernel, padding, and stride. |
|||||||
| 9 | - Extend a filter to multiple input and output channels (feature maps). |
|||||||
| 10 | - Use max and average pooling to downsample and add small translation invariance. |
|||||||
| 11 | - Compare the parameter count of a convolution against an equivalent dense layer. |
|||||||
| 12 | ||||||||
| 13 | ## 10.1 Why not a dense layer |
|||||||
| 14 | ||||||||
| 15 | Consider a modest $224 \times 224$ RGB image. Flattened it has $224 \times 224 \times 3 \approx 150{,}000$ inputs, so a single dense layer with even $1{,}000$ units carries about $150$ million weights. Three facts about images make almost all of them wasteful. |
|||||||
| 16 | ||||||||
| 17 | - **Locality**: a pixel is explained by its neighbours (an edge, a corner, a texture), not by pixels on the far side of the image. |
|||||||
| 18 | - **Translation equivariance**: an edge is an edge wherever it appears, so the same detector should apply at every position. Shifting the input shifts the response by the same amount. |
|||||||
| 19 | - **Parameter sharing**: because the detector is position independent, one small set of weights can be reused across the whole image instead of learning fresh weights per pixel. |
|||||||
| 20 | ||||||||
| 21 | A convolutional layer bakes all three in. It uses a small filter (the shared weights) applied at every location (locality and equivariance), which is why it needs orders of magnitude fewer parameters than the dense layer above. |
|||||||
| 22 | ||||||||
| 23 | *Remark:* recall the notation from the Introduction. A layer $l$ computes $z^{[l]} = W^{[l]} a^{[l-1]} + b^{[l]}$ and $a^{[l]} = g^{[l]}(z^{[l]})$, with explicit bias $b^{[l]}$. A convolution is just a structured $W^{[l]}$ whose entries are tied together and mostly zero, so the same layer equation still holds. |
|||||||
| 24 | ||||||||
| 25 | ## 10.2 The 2D convolution |
|||||||
| 26 | ||||||||
| 27 | ### 10.2.1 Cross-correlation |
|||||||
| 28 | ||||||||
| 29 | Let $I$ be a 2D input (one channel of an image) and $K$ a kernel of size $k \times k$. The operation used in deep learning slides $K$ over $I$ and takes, at each position $(i, j)$, the sum of elementwise products between the kernel and the patch it covers: |
|||||||
| 30 | ||||||||
| 31 | $$\boxed{ (I * K)_{i,j} = \sum_{m}\sum_{n} I_{i+m,\, j+n}\, K_{m,n} }$$ |
|||||||
| 32 | ||||||||
| 33 | Each output value is one dot product between the kernel and a local window of the input, so a small $3 \times 3$ kernel looks at nine pixels regardless of image size. |
|||||||
| 34 | ||||||||
| 35 |  |
|||||||
| 36 | ||||||||
| 37 | *A convolution slides a small kernel across the input, and each position produces one cell of the output feature map.* |
|||||||
| 38 | ||||||||
| 39 | *Remark:* this is technically cross-correlation. The mathematical convolution flips the kernel first, but deep learning libraries do not flip and still call it convolution, because the learned kernel simply absorbs the flip. We follow that convention throughout. |
|||||||
| 40 | ||||||||
| 41 | ### 10.2.2 The layer output |
|||||||
| 42 | ||||||||
| 43 | A convolutional layer applies this operation, adds the explicit bias $b$, and passes the result through the activation $g$: |
|||||||
| 44 | ||||||||
| 45 | $$\boxed{ a^{[l]}_{i,j} = g\!\left( (a^{[l-1]} * K)_{i,j} + b \right) }$$ |
|||||||
| 46 | ||||||||
| 47 | The bias is a single scalar shared across every position of the output, exactly one more instance of parameter sharing. |
|||||||
| 48 | ||||||||
| 49 | ## 10.3 Stride, padding, and output size |
|||||||
| 50 | ||||||||
| 51 | Two hyperparameters control how the kernel sweeps the input. |
|||||||
| 52 | ||||||||
| 53 | - **Stride** $s$: the step in pixels between successive kernel positions. A larger stride skips positions and shrinks the output. |
|||||||
| 54 | - **Padding** $p$: a border of $p$ zeros added around the input. It lets the kernel reach the edges and controls the output size. |
|||||||
| 55 | ||||||||
| 56 | For a 1D input of size $n$ (the same formula applies per axis for 2D), the output size is: |
|||||||
| 57 | ||||||||
| 58 | $$\boxed{ o = \left\lfloor \frac{n + 2p - k}{s} \right\rfloor + 1 }$$ |
|||||||
| 59 | ||||||||
| 60 | *Remark:* two common choices have names. "Valid" padding uses $p = 0$, so the output shrinks by $k - 1$ at stride $1$. "Same" padding picks $p$ so that $o = n$ at stride $1$, which for an odd kernel means $p = (k - 1)/2$. |
|||||||
| 61 | ||||||||
| 62 | For example, with $n = 32$, $k = 5$, $p = 0$, $s = 1$ the output is $\lfloor (32 - 5)/1 \rfloor + 1 = 28$. Adding $p = 2$ ("same") gives $\lfloor (32 + 4 - 5)/1 \rfloor + 1 = 32$. |
|||||||
| 63 | ||||||||
| 64 | ## 10.4 Channels and feature maps |
|||||||
| 65 | ||||||||
| 66 | Real images have channels (three for RGB), and a kernel spans all of them. A filter for an input with $C_\text{in}$ channels has shape $k \times k \times C_\text{in}$, and its convolution sums over spatial positions and channels to produce one 2D output, called a **feature map**. |
|||||||
| 67 | ||||||||
| 68 | To detect many patterns a layer stacks $C_\text{out}$ such filters, so the layer has $C_\text{out}$ feature maps and its output is a volume of shape $o \times o \times C_\text{out}$. Each feature map responds to one learned pattern (an edge orientation, a colour blob, later a texture) at every position. |
|||||||
| 69 | ||||||||
| 70 | $$\boxed{ W^{[l]} \in \mathbb{R}^{\,k \times k \times C_\text{in} \times C_\text{out}}, \qquad b^{[l]} \in \mathbb{R}^{\,C_\text{out}} }$$ |
|||||||
| 71 | ||||||||
| 72 | *Remark:* the output channel count $C_\text{out}$ of one layer becomes the input channel count $C_\text{in}$ of the next, so depth grows as spatial size shrinks. There is one bias per output channel, which is why $b^{[l]}$ has $C_\text{out}$ entries. |
|||||||
| 73 | ||||||||
| 74 | ## 10.5 Pooling |
|||||||
| 75 | ||||||||
| 76 | Pooling downsamples a feature map by summarising each small window with a single number, using a fixed rule and no learned weights. The two common rules are the maximum and the average over each $k \times k$ window: |
|||||||
| 77 | ||||||||
| 78 | $$\boxed{ \text{max}: \max_{m,n} a_{i+m,\, j+n} \qquad \text{avg}: \frac{1}{k^2}\sum_{m,n} a_{i+m,\, j+n} }$$ |
|||||||
| 79 | ||||||||
| 80 | Pooling with stride $s = k$ (non-overlapping windows) shrinks each spatial dimension by a factor of $k$, which cuts computation for later layers. It also grants small **translation invariance**: a max over a window returns the same value if the strong response shifts within that window. |
|||||||
| 81 | ||||||||
| 82 |  |
|||||||
| 83 | ||||||||
| 84 | *Max pooling downsamples each region to its largest value, shrinking the feature map and adding small translation invariance.* |
|||||||
| 85 | ||||||||
| 86 | *Remark:* pooling has no parameters and reduces resolution, which is why modern architectures often replace it with strided convolutions instead. Convolution is equivariant to translation (the response moves with the input), whereas pooling adds a little invariance (the response ignores small moves). |
|||||||
| 87 | ||||||||
| 88 | ## 10.6 The parameter payoff |
|||||||
| 89 | ||||||||
| 90 | The point of parameter sharing is size. Take an input of $32 \times 32 \times 3$ and a layer producing a $32 \times 32 \times 16$ output with a $3 \times 3$ kernel ("same" padding). The convolution shares one small filter bank across all positions, while a dense layer connecting every input to every output does not. |
|||||||
| 91 | ||||||||
| 92 | | Layer | Weights | Biases | Total parameters | |
|||||||
| 93 | | --- | --- | --- | --- | |
|||||||
| 94 | | Convolution ($3\times3$, $16$ filters) | $3 \cdot 3 \cdot 3 \cdot 16 = 432$ | $16$ | $448$ | |
|||||||
| 95 | | Equivalent dense layer | $(32\cdot32\cdot3)\cdot(32\cdot32\cdot16) \approx 5.0\times10^{10}$ | $16{,}384$ | $\approx 5.0\times10^{10}$ | |
|||||||
| 96 | ||||||||
| 97 | The convolution uses a few hundred parameters against about fifty billion for the dense layer, and it generalizes better because the same feature detector is reused everywhere rather than relearned per position. |
|||||||
| 98 | ||||||||
| 99 | ## 10.7 A convolutional stage |
|||||||
| 100 | ||||||||
| 101 | A typical stage chains convolution, activation, and pooling, turning the raw image into a stack of feature maps that later stages refine. |
|||||||
| 102 | ||||||||
| 103 |  |
|||||||
| 104 | ||||||||
| 105 | *A convolutional stage: convolution, activation, then pooling, repeated to build feature maps.* |
|||||||
| 106 | ||||||||
| 107 | *Remark:* stacking such stages makes the receptive field (the input region that influences one output value) grow with depth, so early layers see edges and deep layers see whole objects, all built from the same local operation. |
|||||||
| 108 | ||||||||
| 109 | *One convolution and pooling stage is the building block. The next part assembles many of them into the classic designs, from LeNet and AlexNet to residual networks.* |
|||||||
| 110 | ||||||||
| 111 | --- |
|||||||
| 112 | Next: [CNN architectures](/en/Deep%20Learning/11%20CNN%20architectures) · [Course overview](/en/Deep%20Learning) |
|||||||
