Content
Summary
A 2D convolution slides a small grid of weights, called a kernel, across an image and computes a weighted sum at each position. The resulting grid of numbers is a feature map that shows where a particular pattern, such as an edge, appears. Stacking many kernels yields many feature maps, which a convolutional network learns from data.
Intuition
Imagine a small stamp with a pattern of positive and negative numbers. You press it on every position of the image and ask how well the local patch matches the stamp. A large output means a strong match; a value near zero means no match. Because the same stamp is used everywhere, the network detects a pattern wherever it occurs (weight sharing) and uses far fewer parameters than a fully connected layer. Each kernel produces one feature map, so a layer with 16 kernels produces 16 maps, each highlighting a different learned pattern. Deep layers combine earlier maps to detect more complex shapes.
Key equations
Output at position (i,j): weighted sum of the k-by-k input patch with kernel w plus bias b (cross-correlation, as used in deep learning libraries).
Output height given input height H, padding p, kernel size k and stride s; width is analogous.
Number of learnable parameters in a convolutional layer.
Derivation
- Start with an input image x of size H by W and a kernel w of size k by k.
- Place the kernel's top-left corner at position (i,j) and multiply each kernel weight by the pixel beneath it.
- Sum all k squared products and add bias b to get one output value y(i,j).
- Move the kernel by stride s and repeat across rows and columns to fill the feature map.
- With no padding and stride 1, the kernel fits in H-k+1 vertical positions, so output height is H-k+1.
- Adding p pixels of padding on each side makes the effective height H+2p, and stride s divides the number of steps, giving the floor formula above.
- For multiple input channels, the kernel has depth C_in and the sum also runs over channels; using C_out kernels gives C_out feature maps.
Worked example
Input x is 4x4: rows [1,2,0,1],[3,1,2,0],[0,1,3,1],[2,0,1,2]. Kernel w is 2x2: [1,0],[0,-1]... use rows [1,0] and [0,-1]. Bias 0, stride 1, no padding. Compute the feature map.
- Output size is (4-2)/1+1 = 3, so a 3x3 map.
- The kernel computes x[i,j] - x[i+1,j+1] for each position.
- Row 0: (1-1)=0; (2-2)=0; (0-0)=0.
- Row 1: (3-1)=2; (1-3)=-2; (2-1)=1.
- Row 2: (0-0)=0; (1-1)=0; (3-2)=1.
Answer Feature map = [[0,0,0],[2,-2,1],[0,0,1]]
Common mistakes
- Calling it convolution without noting that deep learning libraries actually compute cross-correlation (no kernel flip).
- Forgetting that the kernel spans all input channels, so a 3x3 kernel on RGB has 27 weights plus a bias.
- Miscounting output size by ignoring padding or stride.
- Thinking each kernel has different weights at each position; the weights are shared across the image.
- Confusing the number of kernels with the number of input channels; output maps equal the number of kernels.