Computer Vision Lab Lessons Convolution step by step

Convolution step by step

The exact computation of convolution: element-wise multiplication, summation, padding, stride and output size.

Beginner35 minConvolutional neural networksConvolution and neural networks

This content was prepared automatically from documented sources and its human scientific review is not yet complete. If you find an error, let us know.

Introduction

Convolution is the heart of convolutional neural networks, and understanding it by hand removes the mystery from everything that follows.

Learning objectives

  • Compute an output cell by hand.
  • Understand padding and stride.
  • Compute the output dimensions.

Prerequisites

Spatial filtering.

Scientific explanation

The kernel slides over the input, and at each position we multiply each kernel element by the corresponding pixel, sum the products and then add the bias. Zero padding (same) keeps the dimensions, and no padding (valid) shrinks the output. A stride s moves the window s pixels at a time, which shrinks the output. Convolutional layers actually compute cross-correlation without flipping the kernel.

In-depth explanation

A complete numerical example

A 4×4 input and a 3×3 kernel that detects vertical edges, with no padding and stride 1, so the output is 2×2:

InputKernel
[[1,1,0,0],[1,1,0,0],[1,1,0,0],[1,1,0,0]][[1,0,−1],[1,0,−1],[1,0,−1]]

The output at (0,0): (1+1+1)\cdot1 + (1+1+1)\cdot0 + (0+0+0)\cdot(-1) = 3, and at (0,1): (1+1+1) - (0+0+0) = 3... and the result [[3,3],[3,3]]: a strong response because every window covers the transition from 1 to 0. (Try computing the second row yourself.)

Multi-channel convolution

In a network, the kernel is not just 3×3 but C_{in}\times3\times3: it extends across all input channels, and the result at each position is a single number. Each output channel has its own kernel. So a layer of 64\to128 with a 3×3 kernel has 128\times64\times9 + 128 = 73{,}856 parameters.

Convolution as matrix multiplication

In practice, libraries implement convolution by turning it into a large matrix multiplication (im2col, or the Winograd and FFT algorithms), because matrix multiplication is the most optimised operation on a GPU. This explains why the time of a convolution depends on the shape of the tensors, not only on the number of operations.

Translation equivariance

(\tau_{\delta}f) * w = \tau_{\delta}(f * w)

Shifting the input shifts the output by the same amount. This property is why convolutional networks are efficient: the same pattern is detected wherever it appears. But it does not cover rotation or scale, which is why we need augmentation. Strides and pooling also partly break it.

Receptive field

r_\ell = r_{\ell-1} + (k_\ell - 1)\prod_{i<\ell} s_i

Example: three 3×3 layers with stride 1 → 1+2+2+2 = 7. And if the second layer has stride 2: 1+2+2\cdot1+2\cdot2 = 9. The "effective" field is smaller than the theoretical one because the contribution of the edges is weaker, and it is roughly Gaussian.

Review questions

  1. Compute the number of parameters of a 3→16 layer with a 5×5 kernel.
  2. Why is a 5×5 kernel replaced by two 3×3 layers?
  3. What is the receptive field of four 3×3 layers with stride 1?

Mathematical equations

Output size = ⌊(n + 2p − k)/s⌋ + 1. Example: n = 7, k = 3, p = 0, s = 1 → a 5×5 output.

Practical example

The horizontal Sobel kernel on a matrix with a vertical edge (zeros then nines) gives large values (36) at the edge and zeros in uniform regions.

Implementation steps

  1. Open the convolution lab and choose "Sobel X".
  2. Press "Next" and follow the computation in each cell.
  3. Change the padding and stride and note the output size.

Expected results

The output contains large values only in the edge columns, and its size matches the equation.

Common mistakes

Confusing mathematical convolution (with kernel flipping) with cross-correlation, forgetting the bias, and miscomputing the dimensions with a stride.

Practical applications

Every convolutional layer in a CNN, and classical filters.

References

  1. V. Dumoulin, F. Visin (2016).A guide to convolution arithmetic for deep learning. arXiv:1603.07285. doi:10.48550/arXiv.1603.07285
  2. I. Goodfellow, Y. Bengio, A. Courville (2016).Deep Learning. MIT Press. www.deeplearningbook.org

Related experiments

Related algorithms

Try in convolution and neural networks