Skip to content
BIASED

Convolution

Cross-correlation, filters, padding and stride, receptive field, weight sharing, shapes and FLOPs of a conv layer — the layer that made vision workable.

Why convolution

The topic Optimizing deep networks closed the training loop: a stable loss, normalization, and an optimizer that takes useful steps. Every network there was fully connected and read an image as a flat vector of pixels. That works for tiny digits and collapses for photographs. This topic introduces the layer that replaced it in vision: the convolution, which looks at small neighbourhoods and uses the same weights at every position.

A fully connected layer on a 224×224224 \times 224 colour image that maps to a 224×224×64224 \times 224 \times 64 feature map needs about 4.8⋅10114.8 \cdot 10^{11} weights — roughly two terabytes in float32. It also has a separate weight for every pixel, so an edge detector learned in one corner says nothing about an edge in the centre, and permuting all pixels the same way would leave training equally easy. Images have the opposite structure: what matters is local, and a cat is a cat wherever it stands.

A convolution is that dense layer with two constraints. Each output looks only at a small window (locality), and every window is processed with the same weights (weight sharing). Locality cuts the parameters from the size of the image to the size of the window; sharing makes the layer translation-equivariant: a pattern is detected wherever it appears.

Convolution as cross-correlation

For one input channel, one output channel and stride 1, a layer with kernel K∈Rk×kK \in \mathbb R^{k \times k} and bias bb computes

Yi,j=b+∑m=0k−1∑n=0k−1Km,n Xi+m, j+nY_{i,j} = b + \sum_{m=0}^{k-1} \sum_{n=0}^{k-1} K_{m,n}\, X_{i+m,\, j+n}

wherever the window fits, so the output is (H−k+1)×(W−k+1)(H - k + 1) \times (W - k + 1). Strictly this is a cross-correlation: a true convolution flips the kernel. The two differ only by that flip, and since the kernel is learned, libraries compute the cross-correlation and call it a convolution.

Worked example. A 4×44 \times 4 input whose right half is brighter, and a 3×33 \times 3 kernel with columns −1,0,1-1, 0, 1:

X=(1023013210230123),K=(−101−101−101),b=0.X = \begin{pmatrix} 1 & 0 & 2 & 3 \\ 0 & 1 & 3 & 2 \\ 1 & 0 & 2 & 3 \\ 0 & 1 & 2 & 3 \end{pmatrix}, \qquad K = \begin{pmatrix} -1 & 0 & 1 \\ -1 & 0 & 1 \\ -1 & 0 & 1 \end{pmatrix}, \qquad b = 0.

The top-left output is Y0,0=5Y_{0,0} = 5; sliding gives 77, then 66 and 66. All four are positive: every window is darker on the left than on the right. Flattened, X↦YX \mapsto Y is still a linear map — a sparse 4×164 \times 16 matrix whose rows share the same nine entries of KK. Everything said about linear layers in Backpropagation still applies; the gradient w.r.t. KK sums the contributions of all positions that share it.

Channels, parameters and FLOPs

A colour image has three channels; a hidden map has dozens or hundreds. Output channel oo uses a Cin×k×kC_{\mathrm{in}} \times k \times k filter and adds a bias bob_o:

Yo,i,j=bo+∑c=0Cin−1∑m,nWo,c,m,n Xc, i+m, j+n.Y_{o,i,j} = b_o + \sum_{c=0}^{C_{\mathrm{in}}-1} \sum_{m,n} W_{o,c,m,n}\, X_{c,\, i+m,\, j+n}.

The weight tensor is W∈RCout×Cin×k×kW \in \mathbb R^{C_{\mathrm{out}} \times C_{\mathrm{in}} \times k \times k}. The layer has Cout(Cink2+1)C_{\mathrm{out}}(C_{\mathrm{in}} k^2 + 1) parameters — independent of image size — and costs Cout Cin k2 H′W′C_{\mathrm{out}}\, C_{\mathrm{in}}\, k^2\, H' W' multiply-adds, which papers usually call FLOPs.

The first layer of a ResNet for 224×224224 \times 224 images is a 7×77 \times 7 convolution from 3 to 64 channels with stride 2 and padding 3, no bias (BatchNorm follows). It has 64⋅3⋅49=9,40864 \cdot 3 \cdot 49 = 9{,}408 weights and about 1.18⋅1081.18 \cdot 10^8 multiply-adds on a 112×112112 \times 112 map — roughly 3% of ResNet-50. A 3×33 \times 3 convolution from the same input to 224×224×64224 \times 224 \times 64 has 1,7921{,}792 parameters, against 4.8⋅10114.8 \cdot 10^{11} for the dense baseline.

Padding, stride and dilation

Three hyperparameters change which inputs each output reads; none of them changes the number of weights.

  • Padding pp adds pp zero rows and columns on every side. For odd kk and stride 1, p=(k−1)/2p = (k-1)/2 keeps the side (“same” padding).
  • Stride ss moves the window by ss pixels. Stride 2 halves resolution and cuts cost by about 4.
  • Dilation dd spreads the taps: a 3×33 \times 3 kernel with d=2d = 2 covers 5×55 \times 5 with nine weights.

With all three,

H′=⌊H+2p−d (k−1)−1s⌋+1H' = \left\lfloor \frac{H + 2p - d\,(k - 1) - 1}{s} \right\rfloor + 1

(and the same for W′W'). The ResNet stem: ⌊(224+6−6−1)/2⌋+1=112\lfloor (224 + 6 - 6 - 1)/2 \rfloor + 1 = 112. When ss does not divide the numerator, the floor drops the last rows: they are never read.

The receptive field

The receptive field of a unit is the set of input pixels that can affect it. One 3×33 \times 3 layer sees 3×33 \times 3; a second 3×33 \times 3 on top sees 5×55 \times 5. With kernel sizes klk_l, strides sls_l and dilations dld_l,

rl=rl−1+(kl−1) dl jl−1,jl=jl−1 sl,r0=j0=1,r_l = r_{l-1} + (k_l - 1)\, d_l\, j_{l-1}, \qquad j_l = j_{l-1}\, s_l, \qquad r_0 = j_0 = 1,

where jlj_l is the jump: the distance in input pixels between neighbouring units of layer ll.

With stride 1 throughout, rL=1+2Lr_L = 1 + 2L: five 3×33 \times 3 layers see 11×1111 \times 11. Alternating stride 2 grows the field much faster. Doubling dilations 1,2,4,…1, 2, 4, \ldots at stride 1 gives rL=2L+1−1r_L = 2^{L+1} - 1 with no loss of resolution — the context module used in segmentation. The theoretical field is an upper bound: in a trained net the effective field is concentrated near the centre (Luo et al., 2016).

Pooling and equivariance

Pooling summarizes each window of every channel with no weights. Max or average pooling with k=s=2k = s = 2 halves height and width. Max pooling passes the backward gradient only to the argmax; average pooling spreads it evenly. Global average pooling turns C×H×WC \times H \times W into a length-CC vector and makes a classifier accept any input size.

Write TaT_a for a shift by aa pixels. A map ff is equivariant if f(TaX)=Taf(X)f(T_a X) = T_a f(X) and invariant if f(TaX)=f(X)f(T_a X) = f(X). A stride-1 convolution (circular padding, or away from borders with zero padding) is equivariant; a stack of such layers stays so. Stride and pooling break exact equivariance for shifts that are not multiples of the stride. Global pooling of an equivariant map is invariant. Equivariance does not extend to rotations or rescalings — those come from data and augmentation.

1×1 and transposed convolution

With k=1k = 1 the layer becomes a linear map Cin→CoutC_{\mathrm{in}} \to C_{\mathrm{out}} at every pixel: the 1×11 \times 1 (pointwise) convolution. From 256 to 64 channels it costs 16,38416{,}384 weights and as many multiply-adds per pixel — a ninth of a 3×33 \times 3 between the same channels. The next topic uses it as a bottleneck before expensive branches and after depthwise filters.

Transposed convolution implements the adjoint of a convolution: each input paints a copy of the kernel onto a larger map, copies ss pixels apart. It is not an inverse. When kk is not a multiple of ss, copies overlap unevenly and even a constant input yields a checkerboard (Odena, Dumoulin and Olah, 2016). A common remedy is fixed upsampling followed by an ordinary convolution.

One layer’s shapes, cost and field of view are now in place. What remains open is how to stack them: deeper stacks see more, but plain depth stops training well past a point. The next topic, Convolutional architectures, follows the designs that answered that — from VGG’s 3×33 \times 3 stacks through Inception and ResNet to MobileNet-style blocks.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Convolution output size and parametersoptional

    H′ formula and NumPy cross-correlation on the topic example; ResNet stem parameter count. Self-check in the notebook.

    ~20 min · easier on a computer · The Convolution topic read.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

Answers are saved on the server and tied to this browser until you sign in.