Convolutional Neural Networks
Convolutional Neural Networks (CNNs) for Image Classification
Limitations of MLPs for Image Data
Multilayer Perceptrons (MLPs) are not well-suited for processing image data due to two fundamental issues:
Lack of Translation Invariance: MLPs react differently depending on where an object appears within the image frame. For example, in digit classification, centered digits yield good predictions, but off‑center digits produce unreliable results. This is because MLPs have no built-in mechanism to recognize patterns regardless of position.
Excessive Number of Parameters: Image data typically has a large number of input features. In a fully connected network, every neuron in one layer connects to every neuron in the next layer. For a color image of size 224×224×3 (the size used by VGG-16), flattening produces over 150,000 raw pixel values. Adding three fully connected layers with 128 neurons each results in over 300 billion parameters. Such a network is:
Extremely time-consuming to train.
Requires vast amounts of data.
Highly prone to overfitting on the training set.
Key Takeaway: MLPs are inefficient and ineffective for image classification because they ignore spatial structure and explode in parameter count.
Introduction to CNNs and the VGG-16 Architecture
Convolutional Neural Networks (CNNs) were developed specifically to process image data more effectively and efficiently. They exploit spatial structure by using convolutional layers that preserve local relationships and drastically reduce the number of parameters.
High‑Level CNN Structure
A CNN contains two main components:
Feature Extractor (Backbone / Body): An upstream segment that extracts meaningful features from the input image.
Classifier (Head): A downstream segment (usually fully connected layers) that transforms the extracted features into class predictions.
The VGG-16 network, developed in 2014, is a classic example. Its architecture comprises:
Five convolutional blocks – together forming the feature extractor.
Three fully connected layers – forming the classifier, ending with a softmax output layer for classification.
The data shape evolves as it flows through the network:
Spatial dimensions (height × width) are intentionally reduced.
Depth dimension (number of channels / activation maps) is increased.
The input to VGG-16 is a color image of shape 224 × 224 × 3 (RGB channels).
Convolutional Blocks
A convolutional block is a repeated sequence of layers, typically containing:
One or more 2D convolutional layers.
A pooling layer (usually after the convolutional layers).
Other layers (e.g., batch normalization, dropout) are sometimes added, but the core is convolution + pooling.
Convolutional Layers
Convolutional layers can be thought of as the eyes of a CNN. Neurons in these layers look for specific features in the input.
The Convolution Operation
A small filter (also called a kernel) – often 3×3 or 5×5 – slides across the input data. At each position, the convolution operation is performed:
Place the filter over a region of the input (the receptive field).
Multiply each filter element by the corresponding input element.
Sum all the products to produce a single number.
This sum (plus a bias) is then passed through an activation function (e.g., ReLU). The result populates one entry of the activation map.
Example: Sobel Kernel
A classic edge‑detection kernel (the Sobel kernel) has fixed values. In a CNN, the kernel weights are learned during training. For a 6×6 input convolved with a 3×3 kernel using a stride of 1 (the filter moves one pixel at a time), the output spatial size becomes 4×4 (because the filter cannot extend beyond the input boundary unless padding is used).
Convolution Operation Formula (for one filter location):
(I∗K)(i,j)=∑m=0M−1∑n=0N−1I(i+m,j+n)⋅K(m,n)where I is the input slice, K is the kernel of size M×N, and (i,j) is the top‑left corner of the receptive field.
Padding can be added to keep the output spatial size the same as the input. Stride values greater than 1 reduce the output size.
Filters, Kernels, and Depth
A filter in a convolutional layer consists of multiple kernels – one kernel per input channel. The number of channels in a filter must match the number of channels in the input. The number of filters in a layer is a design choice; each filter produces one activation map.
Example 1: Single Filter with 3‑Channel Input
Input shape: 224 × 224 × 3 (e.g., color image).
Filter size: 3 × 3 × 3 (3 kernels, each 3×3) → 27 trainable weights + 1 bias = 28 parameters.
Output: one activation map of shape 224 × 224 × 1 (assuming padding keeps spatial size).
The convolution is performed separately for each channel; the weighted sum across channels plus bias is passed through the activation function to produce a single number in the output.
Example 2: Multiple Filters with Multi‑Channel Input
Input has depth 3 (three channels – could be color images or activation maps from a previous layer).
Two filters are used. Each filter has depth 3 to match the input.
Each filter: 3 × 3 × 3 = 27 weights → two filters = 54 weights.
One bias per filter → total 56 parameters.
Output: two activation maps (depth 2), each with the same spatial size as the input (if padding and stride are appropriate).
Key Point: The output depth of a convolutional layer equals the number of filters used.
Activation Maps
The output of a convolutional layer (after activation) is called an activation map. Each map contains a summary of features detected by its corresponding filter. The activation function introduces nonlinearity, allowing the network to learn complex patterns.
Intuition Behind Filters
Each filter learns to detect something unique about the input data. Because weights are randomly initialized and refined during training, different filters will specialize in different features. For example:
One filter might learn to detect vertical edges.
Another might detect horizontal edges.
A third might detect diagonal lines or color blobs.
The network automatically discovers the most useful features for the given task.
Hierarchical Feature Learning
CNNs are capable of learning hierarchical features – building complex representations from simpler ones.
Early layers (close to the input) detect basic structures: edges, corners, arcs, color blobs.
Later layers combine these low‑level features to recognize more complex compositions, such as textures, parts of objects, and eventually whole objects (e.g., the wheels of a car).
This depth of representation is what gives CNNs their power in image classification and other vision tasks.
Pooling Layers
Pooling layers are used after a series of convolutional layers to reduce the spatial dimensions (height and width) of the activation maps. This serves several purposes:
Downsizes the data, reducing computational load.
Reduces the number of parameters, helping to mitigate overfitting.
Provides a form of translation invariance by summarizing local regions.
How Pooling Works
A 2D sliding filter (e.g., 2×2) passes over the input slice with a defined stride (e.g., 2). For each filter position, a pooling operation is performed:
Max Pooling: Takes the maximum value from the region.
Average Pooling: Takes the average value.
Max pooling is the most common. The pooling filter has no trainable parameters – it only defines the window and stride.
Example: Max Pooling with 2×2 Filter, Stride 2
Input: 4×4 slice.
Output: 2×2 downsampled representation, containing the maximum value from each 2×2 block.
Effect: The spatial size is halved (4×4 → 2×2).
Convolutional Blocks in Detail
A basic convolutional block at the start of a CNN might look like:
Convolutional layer with one or more filters (e.g., 32 filters of size 3×3, padding to preserve size).
Activation function (e.g., ReLU) applied to each activation map.
Max pooling layer (e.g., 2×2, stride 2) to reduce spatial dimensions.
For VGG-16, each convolutional block contains two or three consecutive convolutional layers (each with ReLU) followed by a max‑pooling layer. The number of filters increases with depth: from 64 in the first block to 512 in the last block.
Transition from Feature Extractor to Classifier
The output of the last convolutional block (Conv‑5 in VGG‑16) consists of activation maps of shape 7 × 7 × 512.
The data must be flattened into a one‑dimensional vector before entering the fully connected classifier.
Flattening reshapes the 7×7×512 tensor into a vector of length 7 × 7 × 512 = 25,088.
This flattened vector is then fully connected to the first dense layer (FC‑6) of the classifier.
The fully connected layers transform the extracted spatial features into probabilities for each class. They consider information from the entire image because every element of the flattened vector connects to every neuron in the next layer.
Why Fully Connected Layers Are Needed
The activation maps from the final convolutional layer contain meaningful, spatially structured information about the image content.
The fully connected classifier can combine this information globally – it does not rely only on local regions.
The flattening operation is a repackaging of the data; it does not change the underlying information.
How a Trained CNN Maps Features to Class Probabilities
Consider a trained model (all weights fixed). When an input image (e.g., a sports car) is passed through the network:
The feature extractor produces activation maps that highlight features characteristic of a car (wheels, contours, etc.).
These maps are flattened and fed into the classifier.
The learned weights in the fully connected layers, combined with the activations, cause the output neuron corresponding to "sports car" to fire most strongly.
For a different input (e.g., an elephant), the activation maps will differ, leading to a different set of output neuron activations.
In a trained network, the weights in the classifier have been optimized to maximize the correct class output. There are over 100 million connections between the flattened maps and the first fully connected layer in VGG‑16. The network learns to map feature patterns to class probabilities through minimization of a loss function during training.
Summary of Key Concepts
CNNs are designed for image data using convolutional layers with local connectivity and weight sharing.
Translation invariance is improved through convolution and pooling, making the network robust to object location.
Convolutional layers use learned filters to detect features; each filter produces an activation map.
Pooling layers reduce spatial size and parameter count, helping prevent overfitting.
Hierarchical features are learned: low-level edges → mid-level shapes → high-level object parts.
Fully connected layers act as classifiers, mapping the final feature maps to class scores.

Ask anything
We've read this whole study set, so questions about it get the best answers.