Why do convolutions beat dense layers for images? Because pictures have structure — neighboring pixels are related, and a cat at position (10, 10) is still a cat at position (200, 200). CNNs bake those two assumptions in: local receptive fields and shared weights. AlexNet, VGG, ResNet — all use the same idea. The same filter that detects an edge in the top-left corner detects an edge anywhere in the image, with 1000x fewer parameters than a dense net.
Learning Objectives
After this lesson, you will be able to:
See how small sliding filters detect patterns like edges, textures, and shapes in images
Understand how pooling shrinks the image while keeping the most important information
Follow the CNN's learning hierarchy: edges to textures to parts to whole objects
Calculate output spatial dimensions using the stride, padding, and kernel size formula -- and explain why skip connections in ResNet solved the degradation problem
CNNs are one of the most visual and intuitive architectures in all of deep learning. If you can understand how a filter slides across an image, you understand the core idea. Everything else is just stacking that idea in clever ways. Let yourself enjoy this one -- it is genuinely cool to see how a network learns to "see."
A grayscale image enters the network as a 28x28 grid of pixel values, each normalized to [0, 1]. For RGB images, there are three such grids (channels). The spatial structure of the image -- which pixels are neighbors -- is the key information that CNNs exploit.
Small learned filters (e.g., 3x3) slide across the image. At each position, the filter computes a dot product with the local pixel patch. A vertical edge filter produces strong responses where vertical edges exist. Multiple filters (32, 64, or more) each detect different patterns simultaneously.
Each filter produces a feature map -- a 2D grid showing where that pattern was detected. A 28x28 image convolved with a 3x3 filter (stride 1, same padding) produces a 28x28 feature map. With 32 filters, the output is a stack of 32 feature maps.
ReLU is applied element-wise to every value in every feature map: negative values become zero, positive values pass through. This introduces nonlinearity -- without it, stacking convolutions would still be a single linear operation.
Try it! Take any small image (even a 4x4 grid you draw on paper). Create a simple 3x3 filter where the middle column is [1,1,1] and the sides are [-1,-1,-1]. Slide it across your image -- wherever there is a vertical edge, the output spikes. You just performed convolution by hand!
Multiply each filter value by the corresponding pixel value, then sum all 9 products. This gives one number -- the output for this position. If the filter detects vertical edges, this number will be large where there is a vertical edge under the filter.
Move the filter one pixel to the right (stride = 1) and repeat the dot product. Continue sliding across the entire row, then move down one row and repeat. The filter visits every possible position.
The collection of all dot product values forms a feature map (also called an activation map). Each value indicates how strongly the filter's pattern is present at that spatial location. A vertical edge filter produces a feature map that is bright where vertical edges exist.
The number of pixels the filter moves at each step. Stride 1 means move one pixel at a time (output nearly same size as input). Stride 2 means skip every other position (output half the size). Larger strides reduce spatial dimensions but lose fine detail.
Adding zeros around the border of the input so the filter can be centered on edge pixels. "Same" padding ensures the output has the same spatial dimensions as the input. Without padding, each convolution layer shrinks each spatial dimension by (kernel_size - 1) in total — that is, (kernel_size - 1)/2 pixels trimmed off each edge. For a 3x3 kernel that's 1 pixel per edge, 2 pixels total: a 32x32 input becomes 30x30. This is exactly what the output-size formula below encodes.
The region of the original input that influences a particular output value. A single 3x3 convolution has a 3x3 receptive field. Two stacked 3x3 convolutions have a 5x5 receptive field. Three have 7x7. Deep CNNs have receptive fields that eventually cover the entire image, allowing them to capture both local details and global structure.
Try it: Watch the receptive field grow as you stack more layersInteractive
A 3x3 convolution layer with 64 filters applied to an RGB (3-channel) input has how many parameters?
Each filter has size 3x3x3 (height x width x input_channels) = 27 weights, plus 1 bias = 28 parameters per filter. With 64 filters: 64 * 28 = 1,792. This is dramatically fewer than the ~150 million a fully-connected layer would need. Weight sharing is the key -- each filter is reused at every spatial position.
Take the maximum value in each non-overlapping window (typically 2x2 with stride 2). This halves the spatial dimensions while keeping the strongest activations.
Takes the mean instead of the max. Less commonly used in intermediate layers but Global Average Pooling (averaging across the entire spatial dimension) is standard before the final classification layer.
Try it: Watch a convolutional filter slide across an image gridInteractive
Loading visualization...
Select different kernel presets (edge detection, blur, sharpen) and watch the filter slide across the 8x8 input grid. Observe how each kernel produces a different output feature map. Increase the stride to see how it reduces the output dimensions.
Try it: Watch filters detect edges, textures, and shapes layer by layerInteractive
Loading visualization...
Try this: Observe how a convolutional network's layers progressively transform the input. Early layers produce many feature maps with simple patterns, while deeper layers produce fewer, more abstract representations. Compare this to the fully-connected networks from earlier lessons -- notice how the convolutional structure constrains and focuses what each layer learns.
Tests · Verify vertical edge filter produces non-zero output where the edge is. Verify horizontal edge filter produces zeros on the vertical-edge image.
Convolution applies learned filters to detect local patterns. Small filters slide across the input, detecting edges, textures, and shapes through parameter sharing, which dramatically reduces the number of weights compared to fully-connected layers
CNNs learn hierarchical features automatically. Early layers detect edges and colors, middle layers combine these into textures and parts, and deep layers recognize whole objects; this hierarchy emerges from training, not from manual design
Pooling reduces spatial dimensions while preserving important features. Max pooling or average pooling compresses feature maps, making the representation more compact and translation-invariant
Parameter sharing and local connectivity are the key insights. The same filter weights are used across the entire image, encoding the assumption that a useful pattern (like an edge) is equally useful regardless of where it appears
What makes convolutional layers more efficient than fully-connected layers for image processing?
CNNs taught us how to build neural networks with spatial awareness. Next: Recurrent Neural Networks -- how to build networks that process sequences, remember context, and handle variable-length inputs.
A 2x2 max pooling window slides across each feature map, keeping only the maximum value in each window. This halves the spatial dimensions (28x28 becomes 14x14), making the representation more compact and slightly translation-invariant.
Additional convolution + ReLU + pooling blocks stack on top. Each layer detects higher-level patterns using the previous layer's features as input. Layer 2 detects textures (combinations of edges). Layer 3 detects object parts (combinations of textures). Spatial dimensions shrink while channel count grows.
After the final convolutional block, the 3D feature volume (e.g., 7x7x128) is flattened into a 1D vector (6,272 values). This converts the spatial representation into a format compatible with fully-connected layers.
One or more dense layers process the flattened features. These layers combine spatial features from across the entire image to make a holistic decision. Dropout is often applied here to prevent overfitting.
The final layer outputs one score per class, and softmax converts these to probabilities. For MNIST: 10 probabilities, one per digit. The highest probability is the predicted class. The CNN has transformed raw pixels into a class prediction through learned hierarchical feature extraction.
One filter detects one pattern. A convolutional layer uses many filters (typically 32, 64, 128, or more) -- each detecting a different pattern. Each filter produces its own feature map. The output of the layer is a stack of feature maps: one per filter. If you have 64 filters, the output is 64 feature maps.
After convolution, add a bias term (one per filter) and apply an activation function (usually ReLU). This is the same nonlinearity as in fully-connected layers -- without it, stacking convolutional layers would still be linear.