Basic ConvNet
A comprehensive introduction to Convolutional Neural Networks (CNNs) and training them with DeepSpeed for image classification.
Introduction to Convolutional Neural Networks
Convolutional Neural Networks (CNNs) are a specialized class of neural networks designed specifically for processing structured grid data, such as images. They have revolutionized computer vision, achieving superhuman performance in tasks like image classification, object detection, and segmentation.
Why CNNs for Images?
Traditional fully-connected neural networks have significant limitations when applied to images:
-
Parameter explosion: A 224×224 RGB image has 150,528 input features. A fully-connected layer with 1000 neurons would require over 150 million parameters!
-
No spatial awareness: Fully-connected networks treat each pixel independently, ignoring the spatial structure and local patterns in images.
-
No translation invariance: A cat in the top-left corner looks completely different to a fully-connected network than the same cat in the bottom-right corner.
CNNs address these issues through three key architectural innovations:
- Local connectivity (receptive fields)
- Parameter sharing (convolution)
- Spatial hierarchies (pooling)
The Convolution Operation
Mathematical Origins
The convolution operation has deep roots in mathematics, signal processing, and statistics. It describes how one function modifies another through a "sliding" operation.
Continuous Convolution (Integral Form)
For two continuous functions and , their convolution is defined as:
This integral computes a weighted average of at each point , where the weights are given by "flipped" and "shifted" to position .
Intuition: Imagine as a "template" that slides across , computing an overlap at each position.
Discrete Convolution (Summation Form)
For discrete signals (like digital images), the convolution becomes a summation:
For finite signals of length and :
2D Convolution for Images
For images, we extend to two dimensions. Given an input image and a kernel (filter) :
Where:
- is the input image of size
- is the kernel of size
- is the output position
- indexes the kernel elements
True convolution flips the kernel before sliding it:
What nn.Conv2d computes is cross-correlation, with no flip:
The distinction is immaterial for learning — since is learned, the network simply learns the flipped kernel, and the two hypothesis classes are identical. It matters in exactly two places: when you port hand-designed kernels from a signal-processing reference (a Sobel operator must be flipped to behave as documented), and when you invoke the convolution theorem, , which holds for true convolution and underlies FFT-based conv implementations. Cross-correlation is associative-unfriendly and non-commutative; convolution is both.
Why convolution and not some other local operator?
The choice is not heuristic. It is forced by a symmetry requirement.
Let denote translation of an image by vector . An operator is translation-equivariant if
— shifting the input shifts the output identically. The relevant theorem: a linear operator is translation-equivariant if and only if it is a convolution. Convolution is not a way to build a shift-equivariant linear layer; it is the only way.
This is why CNNs work on images. The statistics of natural images are approximately stationary — an edge is an edge wherever it appears — so equivariance is the correct inductive bias, and imposing it as a hard architectural constraint is far more sample-efficient than hoping a fully-connected layer learns it from data.
The convolution layer is equivariant: move the cat, the cat's feature map moves. The classifier needs invariance: move the cat, the label does not change. Invariance is manufactured downstream — by pooling, by strided subsampling, and ultimately by global average pooling, which sums over all spatial positions and so discards location entirely. Conflating the two is the most common conceptual error about CNNs.
Note also that this invariance is only approximate. Azulay & Weiss (2019) and Zhang (2019) showed that strided downsampling violates the Nyquist criterion, so modern CNNs are not shift-invariant in practice: a one-pixel translation can change the predicted class. Anti-aliased (blur-pooled) downsampling substantially repairs this.
What parameter sharing actually buys
Return to the 224×224×3 image and a hypothetical first layer producing 64 feature maps.
| Layer type | Parameter count | |
|---|---|---|
| Fully connected to | ||
| Conv2d, , kernel |
Eight orders of magnitude, from two constraints: local connectivity (each output depends on a patch, not all pixels) and parameter sharing (the same kernel is reused at every spatial position). Crucially, the parameter count is independent of and — the same layer processes any resolution. A fully-connected layer cannot.
The Sliding Window Mechanism
The convolution operation works by sliding a small window (the kernel) across the input image:
Step-by-step example:
Consider a 5×5 input and a 3×3 kernel:
For position :