Convolution layers

A small kernel slides over the image. Kernel size, stride, padding and dilation decide how big the output is and how far each pixel sees.

What you'll learn
  • Compute a convolution's output size from kernel, stride and padding
  • Read the weight shape of nn.Conv2d and count its parameters
  • Explain how dilation grows the receptive field without shrinking the image

A linear layer connects every input to every output. For a 224×224 colour image that is 150,528 inputs per neuron — far too many, and blind to the fact that nearby pixels belong together. A convolution uses one small kernel, slides it across the image, and computes the same weighted sum at every position. The weights are shared, so the layer is small and finds a pattern wherever it occurs.

A kernel sliding over conv.ipynb's checkerboard, with zero padding drawn in. The readout shows the products that make each output pixel; the sliders change image size, kernel size, stride and padding, and the output shape follows the formula.

The output-size formula

Along each spatial dimension,

\[\text{out} = \left\lfloor \frac{\text{in} + 2p - d\,(k - 1) - 1}{s} \right\rfloor + 1\]

for kernel size \(k\), stride \(s\), padding \(p\) and dilation \(d\). With \(d = 1\) this is the familiar \(\lfloor (\text{in} + 2p - k)/s \rfloor + 1\).

The notebook’s layer is

m = torch.nn.Conv2d(in_channels=1, out_channels=2, kernel_size=2,
                    stride=(1, 2), padding=1)

applied to the 4×4 checkerboard of shape (1, 4, 4). Height: \(\lfloor(4 + 2 - 2)/1\rfloor + 1 = 5\). Width, with stride 2: \(\lfloor(4 + 2 - 2)/2\rfloor + 1 = 3\). Two output channels make the result (2, 5, 3) — a stride can differ per dimension, and here it does.

m.weight.shape is (2, 1, 2, 2): out-channels × in-channels × kernel height × kernel width. With one bias per output channel the layer has \(2 \cdot 1 \cdot 2 \cdot 2 + 2 = 10\) parameters, whatever the size of the image.

Padding modes

Zero padding is the default. The notebook also tries circular padding with F.pad:

padding = (2, 2, 1, 1)
padded_input = F.pad(input_data, padding, mode='circular')

F.pad reads the tuple from the last dimension backwards: (left, right, top, bottom). So a 2×2 input gains 2 columns on each side and 1 row on each side, becoming 4×6. The comments in that cell expect (1, 1, 6, 6), which would need padding 2 on every side, (2, 2, 2, 2) — a value that is in neither of the two tuples the cell assigns. Printing the shape, as the cell does, is what catches it.

Seeing further with dilation

multilayer_convnet.ipynb stacks six 3×3 convolutions on a dog photo with dilations 1, 2, 4, 4, 4, 4, each padded so the image stays 224×224. A dilated kernel spreads its nine taps \(d\) pixels apart, so each layer widens what an output pixel can see by \(2d\). The receptive field after the stack is

\[1 + 2\,(1 + 2 + 4 + 4 + 4 + 4) = 39 \text{ pixels,}\]

against 13 for six undilated layers — at the same parameter count and with no loss of resolution. Segmentation networks use exactly this trick.

Try it yourself. Before running feature_map = m(input) in conv.ipynb, predict its shape. Then change to stride=2, padding=0 and predict again. Finally, print m.weight.grad after feature_map.sum().backward() and explain why each weight’s gradient is a sum over output positions.

View source on GitHub →