Convolutional Neural Networks (CNNs): Architecture and Mechanics
Understand Convolutional Neural Networks (CNNs). Learn about local receptive fields, parameter sharing, feature maps, stride, and padding.
Introduction
If you connect a $1000 \times 1000$ pixel RGB image directly to a standard fully connected hidden layer with just 100 neurons, you will end up with:
$$3,000,000 \text{ inputs} \times 100 \text{ neurons} = 300,000,000 \text{ weights}$$
This is an astronomical number of parameters for a single layer, leading to catastrophic overfitting and slow training.
To solve this, researchers designed Convolutional Neural Networks (CNNs). Inspired by the structure of the biological visual cortex (discovered by Hubel and Wiesel in 1959), CNNs use spatial local connections and weight sharing to build highly efficient, translation-invariant models that excel at spatial classification.
What You Will Learn
- Why CNNs are superior to MLPs for spatial data.
- The two core principles: Local Receptive Fields and Parameter Sharing.
- How Stride and Padding govern the shape of output feature maps.
- The formula for calculating output sizes of convolutional layers.
- Implementing a Convolutional Layer in PyTorch.
Why This Topic Matters
CNNs are the gold standard for image classification, object detection, and segmentation. They power the facial unlocking systems on smartphones, tumor boundary detectors in healthcare, and optical character recognition (OCR) systems in document processors.
Prerequisites
Detailed Explanation
CNNs achieve their efficiency through three architectural concepts:
1. Local Receptive Fields
Instead of connecting every hidden neuron to every single input pixel, a hidden neuron in a CNN is only connected to a small local patch of pixels (e.g., a $3 \times 3$ area). This patch is called the neuron's Local Receptive Field. It allows the network to learn local spatial structures (like lines or corners) before combining them into complex shapes.
2. Parameter Sharing (Weight Sharing)
Instead of assigning a unique weight to every connection, a CNN uses the same weights (the kernel coefficients) for all neurons in a layer. As the kernel slides across the image, it searches for a specific pattern (like a vertical edge) everywhere.
- If a kernel detects an edge in the top-left corner, it will also detect that same edge in the bottom-right corner. This is called Translation Invariance.
3. Stride and Padding
The shape of the output Feature Map depends on two configurations:
A. Stride ($S$)
Stride is the step size by which the kernel shifts at each step.
- Stride = 1: The kernel shifts by 1 pixel (outputs are large and overlapping).
- Stride = 2: The kernel shifts by 2 pixels (outputs are halved in size).
B. Padding ($P$)
When convolving, the border pixels are only visited once, whereas center pixels are visited multiple times. Additionally, the output image shrinks. To prevent this, we pad the borders of the image with zeros (Zero Padding).
- Valid Padding ($P=0$): No padding. The output size shrinks.
- Same Padding: Pad borders such that output height/width equals input height/width.
Zero Padding (P=1):
[ 0 0 0 0 0 ]
[ 0 255 255 255 0 ] <- Original image inside
[ 0 255 255 255 0 ]
[ 0 0 0 0 0 ]
The Output Size Formula
For an input of height/width $W$, a kernel size $F$, padding $P$, and stride $S$, the output dimension $O$ is calculated as:
$$O = \left\lfloor \frac{W - F + 2P}{S} \right\rfloor + 1$$
Example Calculation:
If input $W = 32$, kernel $F = 5$, padding $P = 2$, and stride $S = 1$: $$O = \frac{32 - 5 + 4}{1} + 1 = 31 + 1 = 32$$ (The output dimension is exactly 32, representing Same Padding).
Visual Diagram (Mermaid)
graph TD
A[Input Image: 32x32x3] -->|Convolve with 32 filters of 5x5| B[Feature Maps: 28x28x32]
B -->|Downsample| C[Pooling Layer]
C -->|Flatten to 1D| D[Fully Connected Classifier]
style B fill:#3B82F6,stroke:#fff,color:#fff
style D fill:#10B981,stroke:#fff,color:#fff
Python Code Examples
We will build a simple convolutional block in PyTorch and verify the change in output shape.
import torch
import torch.nn as nn
# 1. Simulate a batch of images
# Batch size=4, Channels=3 (RGB), Height=32, Width=32
input_batch = torch.randn(4, 3, 32, 32)
print("Input Batch Shape:", input_batch.shape)
# 2. Define a Convolutional Layer
# in_channels=3, out_channels=16 (number of filters), kernel_size=3, stride=1, padding=1
conv_layer = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
# Pass input through layer
output_feature_maps = conv_layer(input_batch)
print("\nOutput Shape (with Same Padding):", output_feature_maps.shape)
# Expected: [4, 16, 32, 32] (channels increased, spatial dimensions preserved)
# 3. Define a Conv Layer that reduces spatial dimensions
# stride=2, padding=0, kernel=3
conv_strided = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=2, padding=0)
output_strided = conv_strided(input_batch)
print("\nOutput Shape (Strided, no Padding):", output_strided.shape)
# Calculation: floor((32 - 3 + 0)/2) + 1 = floor(14.5) + 1 = 14 + 1 = 15
# Expected: [4, 16, 15, 15]
Industry Use Cases
- Security Surveillance: Deploying real-time face detection models that locate facial regions in video streams.
- Biomedical Image Classification: Scanning mammogram results to classify cells as benign or malignant.
- Retail Visual Search: Matching photos of outfits taken by users to specific catalog models.
Summary
CNNs solve the parameter explosion of traditional dense layers through local receptive fields (connecting to small patches) and parameter sharing (sliding a single kernel globally). Using stride and padding configurations, engineers can customize how spatial layers extract, highlight, and downsample geometric shapes.
Next Topic
Once features are extracted by convolutional layers, how do we reduce their spatial sizes to make them invariant to minor shifts? Let's check: Pooling Layers and Classic CNN Architectures.