Convolutional Neural Networks
What Are Convolutional Neural Networks?
Convolutional Neural Networks (CNNs) are deep learning models designed to process visual information. They learn to detect spatial hierarchies in images - from simple edges to complex structures like faces or traffic signs - by passing data through layers of specialised filters.
How CNNs Learn Spatial Hierarchies of Features
- Layer-by-layer learning: CNNs build understanding step-by-step, starting with basic patterns and ending with complex features.
- Early layers: Detect low-level features like edges, corners, and textures in small patches of the image.
- Mid-level layers: Combine simple features into recognisable parts, such as eyes, wheels, or letters.
- Deep layers: Form high-level concepts by linking parts into full objects like faces, animals, or vehicles.
- Adaptive learning: CNN filters adjust automatically during training to focus on features that improve performance for a given task.
- Hierarchy of features: Each layer adds a new level of detail, allowing CNNs to recognise patterns with increasing accuracy.
Basic CNN Architecture
A CNN processes image data through stages that detect and combine visual features.
Input Layer
This layer receives the raw image as pixel values. For example, a 28×28 grayscale image of a handwritten digit is a 2D array of intensities from 0–255.
Example: A scanned image of the number “3” is passed in as a 28×28 matrix.
Convolutional Layer
Small filters (kernels) slide across the image to detect visual features like vertical edges, horizontal lines, or curves. Each filter produces a feature map showing where that pattern appears.
Example: Early filters may detect the loop in a “6” or the vertical stroke of a “1”.
Activation Function
An activation (e.g. ReLU) lets the network learn more than straight-line patterns. It helps the model respond to curves, corners, and texture changes by keeping useful signals and damping unhelpful ones.
Example: ReLU helps tell a curved “3” from a straight “7” by reacting to shape and shading differences.
Pooling Layer
Pooling reduces the size of feature maps by summarising local regions (e.g. taking the maximum value). This keeps the most important signals while making the model less sensitive to small shifts.
Example: If an “8” is slightly off-centre, pooling still preserves the two loops that define it.
Fully Connected Layer
After feature extraction, the feature maps are flattened and passed to dense layers. These combine information from all detected features to form a global understanding of the image.
Example: The network uses loops, lines, and curves together to decide if the digit is most likely a “2”, “3”, or “8”.
Output Layer
The final layer produces the prediction, often with a softmax that returns a probability for each class and sums to 1.
Example: The model outputs [0.01, 0.03, 0.89, 0.02, …], meaning it’s 89% confident the digit is a “2”.
Factors that Affect CNNs
Convolutional Neural Networks (CNNs) learn visual patterns by stacking stages that detect edges, textures, shapes, and whole objects. How well they learn-and how fast they run-depends on a few design choices. Below, each factor explains what it controls, why it matters, and sensible starting points for IB-level projects.
Depth & Width (how big the network is)
What it controls: Depth is how many feature-extracting stages you stack (often called “convolutional blocks”). A convolutional block is usually one or more convolution layers followed by an activation (and sometimes batch normalisation and/or pooling). Width is how many filters (also called channels) you use in a layer-more filters means the layer can learn more kinds of patterns at that stage.
- Why it matters: Too small and the model underfits (misses real patterns). Too large and it overfits (memorises noise) and trains slowly.
- Good starting points: 3–5 convolutional blocks for small image tasks; begin with 16–64 filters and double as you go deeper (e.g. 32 → 64 → 128).
- How to read signals: Very high training accuracy but much lower validation accuracy suggests the model is too large or not regularised. Low training and validation accuracy suggests the model is too small or needs more training time.
Tip: Several small layers (e.g. stacked 3×3 convolutions) often beat one huge layer-they build a similar “view” of the image while adding extra decision steps.
Convolution Kernels (size, stride, padding)
What it controls: The kernel size sets how large a patch of the image a filter “looks at” at one time; the stride is how far the filter moves each step; padding decides whether you keep the spatial size or let it shrink.
- Kernel size: 3×3 is a strong default: it captures fine detail without being expensive. Larger (5×5, 7×7) see more context but increase compute and memory; you can often get a similar effect by stacking multiple 3×3 layers.
- Stride: A stride of 1 slides one pixel at a time and keeps detail. A stride of 2 skips every other pixel, which halves width/height (downsampling) and speeds things up, but throws away some detail-use it later in the network, not right at the start.
- Padding:
samepadding preserves the spatial size (good when stacking many layers);validpadding lets the feature map shrink gradually, which some designs prefer.
Quick rule: Start with 3×3 kernels, stride 1, same padding. Downsample only between blocks using stride-2 convolutions or a max-pool layer.
Activation Function
What it controls: The activation adds non-linearity so the network can model curved edges, corners, textures, and other complex patterns. Without activations, a deep stack of convolutions would behave like one big linear filter.
- Good defaults: ReLU is fast and works well in most hidden layers. If many neurons stop responding (stay at zero), try Leaky ReLU, which keeps a tiny slope for negative inputs.
- Alternatives: GELU can work well in advanced models but costs a bit more compute-keep it for later exploration.
Debug hint: If learning is unstable or very slow, try a lower learning rate, add batch normalisation, or switch from ReLU to Leaky ReLU.
Loss Function (the training goal)
What it controls: The loss is the number the optimiser tries to make smaller each step. It measures how far the model’s predictions are from the targets and guides every weight update.
- Single-label classification (exactly one class per image): Use cross-entropy with a softmax output. In plain words, this heavily penalises confident wrong answers and rewards confident correct ones.
- Multi-label classification (several labels can be “on”): Use binary cross-entropy with a sigmoid output-each class is treated as an independent yes/no.
- Regression (predicting a number, e.g. age): Use MSE (mean squared error) or MAE (mean absolute error). MAE is less sensitive to extreme outliers.
- Class imbalance: Consider class weights or a focal-style loss so rare classes still influence learning.
Sanity check: Match the final layer to the loss: softmax ⇔ cross-entropy (one class), sigmoid ⇔ binary cross-entropy (multi-label).
Quick Reference
| Factor | Start With | When to Change | Common Pitfall |
|---|---|---|---|
| Depth & Width | 3–5 blocks; 32→64→128 filters | Underfitting → add capacity; Overfitting → add regularisation or shrink |
Jumping to a huge model too early |
| Kernels | 3×3, stride 1, same padding |
Need more context → occasional 5×5 or stride-2 between blocks | Too much early downsampling removes detail |
| Activation | ReLU in hidden layers | Dead units → Leaky ReLU; Advanced models → try GELU |
High LR + no normalisation → unstable training |
| Loss | Cross-entropy + softmax (single-label) | Multi-label → binary CE + sigmoid; regression → MSE/MAE |
Final layer doesn’t match the loss |
Glossary: “Cost” is informal shorthand for computational cost-how much time and memory the choice uses. A “conv block” is a small stack of layers (convolution → activation → optional norm/pooling) treated as one stage.
Build → Measure → Adjust
- Start simple (3×3 kernels, ReLU, cross-entropy), and watch training and validation curves.
- If both scores are low, add capacity or train longer. If training is high but validation lags, add augmentation/regularisation or reduce size.
- Change one thing at a time and keep brief notes-small, controlled tweaks beat guesswork.
Python Project: Train a CNN to Classify Handwritten Digits
Scenario: A convolutional neural network (CNN) trained to recognise handwritten digits (0–9) from the MNIST database. Each image is 28×28 pixels and grayscale.
CNN vs ANN: Same dataset, different approach
Both projects classify MNIST digits, but they learn in different ways. An ANN (dense MLP) ignores image layout by flattening pixels; a CNN keeps the 2D structure and learns small visual patterns that repeat across the image.
| Aspect | ANN (Dense) | CNN |
|---|---|---|
| Input handling | Flattens 28×28 → 784 (loses 2D layout) | Keeps 28×28×1 grid (preserves layout) |
| First layer params (example) | Dense 64: 784×64 + 64 ≈ 50,240 | Conv 32×(3×3): 3×3×1×32 + 32 ≈ 320 |
| What’s learned | Global combinations of all pixels | Local edges/textures → shapes → digits |
| Invariance | None built-in | More tolerant to small shifts/positions |
| Typical outcome | Good on MNIST | Often better accuracy with fewer weights |
Bottom line: A CNN is better matched to images because it exploits the 2D structure.
Training Your CNN (MNIST)
Place all files in the same folder.
from tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Densefrom tensorflow.keras.datasets import mnistfrom tensorflow.keras.utils import to_categorical# Step 1: Load and prepare data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1).astype("float32") / 255
x_test = x_test.reshape(-1, 28, 28, 1).astype("float32") / 255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# Step 2: Build the CNN
model =
Sequential([# Convolutional layer: learns edges/patterns
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),# Pooling layer: reduces spatial size
MaxPooling2D(pool_size=(2, 2)),# Flatten before dense layers
Flatten(),# Fully connected layer: combines features
Dense(128, activation='relu'),# Output: 10 digits
Dense(10, activation='softmax')])
# Step 3: Compile the model
model.
compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])# Step 4: Train the model
model.
fit(x_train, y_train, epochs=5, batch_size=32, validation_split=0.2)# Step 5: Evaluate on test set
loss, accuracy = model.
evaluate(x_test, y_test)print("Test Accuracy:", accuracy)# Save the trained CNN
model.
save("my_cnn_digit_model.keras")
Testing Your CNN (Your Own Image)
Draw a digit, save as a 28×28 grayscale PNG with a dark digit on a white background (e.g. digit.png), then run:
from tensorflow.keras.models import load_modelfrom PIL import Imageimport numpy as np# Load the trained CNN model
model =
load_model("my_cnn_digit_model.keras")# Load and preprocess your custom image
img = Image.open("digit.png").convert("L") # Grayscale
img = img.resize((28, 28)) # 28×28 pixels
img_array = np.array(img)
img_array = 255 - img_array # Invert: white bg, dark digit
img_array = img_array / 255.0 # Normalize to [0,1]
img_array = img_array.reshape(1, 28, 28, 1) # For CNN input
prediction = model.predict(img_array)
predicted_digit = prediction.argmax()
print("Predicted digit:", predicted_digit)
Tip: Center the digit; use a thick dark stroke on a clean white background.
Sample Images (28×28)
Place these in the same folder as your scripts.
If your own image fails, compare brightness/contrast and size with these samples.
Real-World Applications of Convolutional Neural Networks (CNNs)
Facial Recognition
Used in identity verification systems (e.g. phone unlocking or airport security).
- Input Layer: Pixel values from a face image, often 96×96 or 224×224 RGB.
- Hidden Layers:
- Early convolutional layers identify patterns like edges, eyes, or jawlines.
- Deeper layers combine features into a unique facial signature or embedding.
- Output Layer: A match score or predicted identity from a database of known faces.
Medical Imaging
Used to detect and diagnose conditions such as pneumonia, tumors, or retinal diseases.
- Input Layer: Medical scans like X-rays, MRIs, or retinal images.
- Hidden Layers:
- Convolutional layers extract anatomical features from tissues and organs.
- Deeper layers highlight abnormal shapes or densities for classification.
- Output Layer: Probability score or label (e.g. “tumor present”, “normal”).
Autonomous Vehicles
CNNs process camera feeds in self-driving systems to understand surroundings.
- Input Layer: Real-time camera input from front, side, and rear views.
- Hidden Layers:
- Early layers detect lanes, obstacles, and road edges.
- Mid-to-deep layers recognise traffic signs, pedestrians, and other cars.
- Output Layer: Object classification and bounding box predictions for decision making.
Satellite Imaging
CNNs are used in environmental monitoring, disaster response, and land-use analysis.
- Input Layer: High-resolution satellite images in RGB or multispectral format.
- Hidden Layers:
- Convolutional layers detect land features such as forests, water bodies, or buildings.
- Deeper layers classify regions (e.g. urban, agricultural, flood-affected).
- Output Layer: A segmentation map or class prediction for each region.
Challenges in Training CNNs
- High Computational Cost: Training large models often requires GPUs.
- Data Requirements: CNNs need many labelled images for good generalisation.
- Overfitting: Without regularisation, models may perform well on training data but poorly on new data.
Key Takeaways
- CNNs are powerful models for image processing tasks like classification and detection.
- They learn features at multiple levels using convolution, activation, pooling, and dense layers.
- Hyperparameters such as kernel size and number of layers affect accuracy and efficiency.
- They power applications from medicine to mobility to environmental monitoring.