conv · K * I + b
pooling · max/avg
output = (W-K+2P)/S+1
Computer Vision
CNN
MASTER GUIDE

Convolutional Neural Networks (CNN)

Inspired by the visual cortex, CNNs revolutionized computer vision through local feature detection, hierarchical representations, and transfer learning. A comprehensive, visually interactive deep dive from first principles to state-of-the-art architectures.

11 Sections
50 min read
Beginner to Advanced
Interactive Visuals
Begin Learning
Contents
Historical Intuition Core Intuition Convolution Operation Padding and Stride Pooling Layers CNN Architecture Famous Architectures Transfer Learning Hyperparameters Applications Python Code
01

Historical Intuition

From neuroscience experiments on cat brains to the deep learning revolution that conquered computer vision.

Hubel & Wiesel: The Neuroscience Foundation

The story of Convolutional Neural Networks begins not in a computer lab, but in a neurophysiology laboratory. In 1962, David Hubel and Torsten Wiesel conducted their Nobel Prize-winning experiments on the visual cortex of cats. By inserting microelectrodes into the primary visual cortex and presenting the cats with various visual stimuli, they made a groundbreaking discovery: individual neurons in the visual cortex respond to specific patterns in specific regions of the visual field.

They identified two types of cells: simple cells, which respond to edges at particular orientations and positions, and complex cells, which respond to edges at particular orientations regardless of their exact position. This hierarchy -- from position-specific to position-invariant responses -- is the biological blueprint that inspired the architecture of modern CNNs.

The concept of a receptive field emerged from this work: each neuron processes information from only a small region of the visual field, not the entire image. This local connectivity principle became the cornerstone of convolutional layers.

From Biology to Computation

Inspired by Hubel and Wiesel's findings, Kunihiko Fukushima proposed the Neocognitron in 1980, the first computational model that implemented a hierarchy of simple and complex cell layers for pattern recognition. The Neocognitron introduced the ideas of local connectivity and hierarchical feature extraction, but it used unsupervised learning and was difficult to train.

The real breakthrough came in 1998 when Yann LeCun and colleagues introduced LeNet-5, a convolutional neural network trained with backpropagation for handwritten digit recognition. LeNet-5 combined convolutional layers, pooling layers, and fully connected layers into a trainable end-to-end system. It was deployed by the US Postal Service to read zip codes on mail, becoming one of the first successful commercial applications of neural networks.

The Deep Learning Revolution

For over a decade after LeNet, CNNs remained a niche technology. Then came AlexNet in 2012. Designed by Alex Krizhevsky, Ilya Sutskever, and Geoffrey Hinton, AlexNet won the ImageNet Large Scale Visual Recognition Challenge (ILSVRC) by a staggering margin, reducing the top-5 error rate from 26% to 16%. This single result ignited the modern deep learning revolution.

1962
Hubel & Wiesel discover simple and complex cells in the cat visual cortex, establishing the neuroscience foundation for CNNs
1980
Fukushima proposes the Neocognitron, the first neural network architecture with local connectivity and hierarchical feature extraction
1998
LeCun introduces LeNet-5, the first practical CNN trained with backpropagation, deployed for zip code recognition
2012
AlexNet wins ImageNet with a massive margin, launching the deep learning era. GPUs and ReLU activation make deep CNNs feasible
2014-15
VGG, GoogLeNet, and ResNet push accuracy further, introducing deeper architectures with skip connections
Today
CNNs power everything from self-driving cars to medical diagnosis, with EfficientNet, Vision Transformers, and hybrid models leading the frontier
02

Core Intuition

Why local connectivity, weight sharing, and translation invariance make CNNs so powerful for visual data.

Local Connectivity

In a traditional fully connected (dense) neural network, every neuron in one layer is connected to every neuron in the previous layer. For an image of size \( 224 \times 224 \times 3 \) (RGB), a single neuron in the first hidden layer would need \( 224 \times 224 \times 3 = 150{,}528 \) weights. With just 1,000 neurons in the first layer, that is over 150 million parameters -- and this is just the first layer.

CNNs solve this with local connectivity: each neuron connects to only a small local region of the input, called its receptive field. A neuron with a \( 3 \times 3 \) receptive field on a 3-channel image needs only \( 3 \times 3 \times 3 = 27 \) weights plus a bias. This mirrors how neurons in the visual cortex each process a small patch of the visual field.

Local connectivity exploits the fundamental structure of images: nearby pixels are strongly correlated and form local patterns (edges, corners, textures), while distant pixels are largely independent. A small filter is sufficient to detect these local features.

Weight Sharing & Translation Invariance

Weight sharing takes local connectivity one step further: the same set of weights (the filter/kernel) is applied at every spatial position in the input. This means a filter that detects a vertical edge at position (10, 10) will also detect vertical edges at position (100, 200) -- the network does not need to learn separate detectors for each location.

This provides translation invariance (more precisely, translation equivariance): if the input shifts, the output shifts by the same amount. An edge-detecting filter produces the same response regardless of where the edge appears in the image.

Weight sharing also dramatically reduces the number of parameters. A convolutional layer with 64 filters of size \( 3 \times 3 \) on a 3-channel input has only \( 64 \times (3 \times 3 \times 3 + 1) = 1{,}792 \) parameters, regardless of the input image size. Compare this to 150 million for a fully connected approach.

Parameter Comparison: FC vs CNN

To appreciate the efficiency of CNNs, consider an input image of size \( 224 \times 224 \times 3 \):

\[ \text{FC layer (1000 neurons)} = 224 \times 224 \times 3 \times 1000 = 150{,}528{,}000 \text{ params} \]
\[ \text{Conv layer (64 filters, 3x3)} = 64 \times (3 \times 3 \times 3 + 1) = 1{,}792 \text{ params} \]

That is a reduction factor of over 80,000x. This massive parameter reduction prevents overfitting, speeds up training, and enables CNNs to scale to large images that would be impossible for fully connected networks.

Local Connectivity

Each neuron sees only a small patch of the input, exploiting the spatial locality of visual features like edges and textures

Weight Sharing

The same filter slides across the entire image, detecting the same feature everywhere with a single set of learned weights

Translation Equivariance

If the input shifts, the feature map shifts by the same amount, making CNNs naturally robust to object position changes

03

Convolution Operation

The fundamental operation: sliding a kernel over an input to produce feature maps through element-wise multiplication and summation.

How Convolution Works

The discrete convolution (technically cross-correlation in deep learning) operates by sliding a small matrix called a kernel (or filter) over the input. At each position, the kernel is element-wise multiplied with the overlapping input patch, and all products are summed to produce a single output value. This output forms the feature map.

For a 2D input \( I \) and kernel \( K \) of size \( k \times k \), the output at position \( (i, j) \) is:

\[ (I * K)(i, j) = \sum_{m=0}^{k-1} \sum_{n=0}^{k-1} I(i+m, j+n) \cdot K(m, n) + b \]

Where \( b \) is a bias term. The output feature map size depends on the input size \( W \), kernel size \( K \), padding \( P \), and stride \( S \):

\[ \text{output size} = \left\lfloor \frac{W - K + 2P}{S} \right\rfloor + 1 \]

For example, with a \( 32 \times 32 \) input, a \( 5 \times 5 \) kernel, no padding (\( P=0 \)), and stride 1 (\( S=1 \)): \( \lfloor(32 - 5 + 0)/1\rfloor + 1 = 28 \). The output is \( 28 \times 28 \).

Multiple Filters and Channels

A single filter produces a single 2D feature map. To detect multiple types of features (horizontal edges, vertical edges, corners, etc.), we use multiple filters. If we apply \( F \) filters, we get \( F \) feature maps stacked into a 3D output volume.

For multi-channel inputs (like RGB images with 3 channels), each filter has the same depth as the input. A filter on an RGB image has shape \( k \times k \times 3 \), performing a 3D convolution that sums across all channels:

\[ \text{Output}(i, j, f) = \sum_{c=0}^{C-1} \sum_{m=0}^{k-1} \sum_{n=0}^{k-1} I(i+m, j+n, c) \cdot K_f(m, n, c) + b_f \]

Where \( C \) is the number of input channels and \( f \) indexes the filter. The total parameters for this layer is \( F \times (k \times k \times C + 1) \).

Interactive: Convolution Visualization

Watch the kernel slide over the input grid, computing dot products at each position to build the output feature map progressively.

2D Convolution Animation

Adjust kernel size and stride. The cyan overlay shows the current kernel position. The output grid builds as the kernel slides across the input.

04

Padding and Stride

Controlling spatial dimensions through zero-padding strategies and stride configurations.

Types of Padding

Padding adds zeros (or other values) around the border of the input before convolution. Without padding, the output shrinks at every layer, which limits how deep the network can be.

  • Valid padding (no padding, P=0): The kernel is applied only where it fully overlaps the input. Output size shrinks: \( (W - K)/S + 1 \). Information at the borders is underrepresented.
  • Same padding: Enough zeros are added so that the output has the same spatial dimensions as the input (when stride = 1). For a kernel of size \( K \), the padding needed is \( P = \lfloor K/2 \rfloor \). This is the most common choice in modern architectures.
  • Full padding: Every possible partial overlap is included by padding with \( P = K - 1 \) zeros on each side. The output is larger than the input: \( W + K - 1 \). Rarely used in practice.
\[ \text{Same padding: } P = \left\lfloor \frac{K}{2} \right\rfloor, \quad \text{so } \text{out} = \frac{W - K + 2\lfloor K/2 \rfloor}{1} + 1 = W \]

Stride: Controlling Resolution

The stride determines how many pixels the kernel moves at each step. A stride of 1 means the kernel slides one pixel at a time, producing a high-resolution feature map. A stride of 2 moves two pixels at a time, halving the output dimensions and acting as a form of downsampling.

Strided convolutions are often used as an alternative to pooling layers for reducing spatial dimensions. Many modern architectures (like ResNet) use stride-2 convolutions instead of max pooling because the network can learn the optimal downsampling strategy.

Dilated (Atrous) Convolutions

Dilated convolutions insert gaps between kernel elements, expanding the receptive field without increasing the number of parameters. A dilation rate of \( d \) means there are \( d-1 \) zeros between kernel values. The effective kernel size becomes \( K + (K-1)(d-1) \). Widely used in semantic segmentation (DeepLab) and sequence modeling (WaveNet).

Dimension Calculations Summary

For an input of size \( W \times H \), kernel \( K \times K \), padding \( P \), stride \( S \), and dilation \( d \):

\[ W_{\text{out}} = \left\lfloor \frac{W + 2P - d(K-1) - 1}{S} \right\rfloor + 1 \]

For the standard case (dilation = 1), this simplifies to the familiar formula. Understanding these dimension calculations is critical for designing CNN architectures -- mismatched dimensions are one of the most common sources of errors when building networks.

1

Example: Valid Convolution

Input 32x32, Kernel 5x5, Stride 1, Pad 0: \( \lfloor(32-5+0)/1\rfloor+1 = 28 \). Output: 28x28.

2

Example: Same Convolution

Input 32x32, Kernel 5x5, Stride 1, Pad 2: \( \lfloor(32-5+4)/1\rfloor+1 = 32 \). Output: 32x32.

3

Example: Strided Convolution

Input 32x32, Kernel 3x3, Stride 2, Pad 1: \( \lfloor(32-3+2)/2\rfloor+1 = 16 \). Output: 16x16.

05

Pooling Layers

Reducing spatial dimensions and introducing invariance through downsampling operations.

Max Pooling

Max pooling slides a window over the feature map and takes the maximum value within each window. The most common configuration is a \( 2 \times 2 \) window with stride 2, which halves both spatial dimensions.

\[ \text{MaxPool}(i, j) = \max_{0 \le m < k, \; 0 \le n < k} \text{Input}(i \cdot s + m, \; j \cdot s + n) \]

Max pooling retains the strongest activation (the most prominent feature) in each local region. This provides a degree of translational invariance: small shifts in the input do not change the maximum value. It also reduces the computational cost of subsequent layers by shrinking the feature maps.

Max pooling has no learnable parameters -- it is a fixed operation. Its aggressive downsampling discards spatial information, which is both its strength (invariance) and weakness (loss of precise location information, problematic for tasks like segmentation).

Average Pooling & Global Average Pooling

Average pooling computes the mean of all values in the window instead of the maximum:

\[ \text{AvgPool}(i, j) = \frac{1}{k^2} \sum_{m=0}^{k-1} \sum_{n=0}^{k-1} \text{Input}(i \cdot s + m, \; j \cdot s + n) \]

Average pooling produces smoother outputs and retains more background information, but max pooling generally performs better for classification because it preserves the strongest activations.

Global Average Pooling (GAP) takes the average over the entire spatial extent of each feature map, reducing each \( H \times W \) feature map to a single number. For \( F \) feature maps, GAP produces an \( F \)-dimensional vector. It was introduced in the Network in Network paper (2013) and is now used in most modern architectures as a replacement for fully connected layers before the final classifier, dramatically reducing parameters and overfitting.

Interactive: Pooling Visualization

Watch how max pooling and average pooling downsample a feature map differently. Toggle between the two methods.

Pooling Operation Visualization

Toggle between Max (cyan) and Average (orange) pooling. The pooling window slides across the input, producing the downsampled output on the right.

06

CNN Architecture

How convolutional, pooling, and fully connected layers stack together to form a complete image classifier.

The Standard CNN Pipeline

A typical CNN for image classification follows a consistent pattern: alternating convolutional and pooling layers that progressively extract higher-level features, followed by fully connected layers that perform the final classification.

1

Input Layer

The raw image, typically resized to a fixed size (e.g., \( 224 \times 224 \times 3 \) for RGB). Pixel values are normalized to [0, 1] or standardized.

2

Conv + ReLU

Each convolutional layer applies multiple filters to detect features (edges, textures, patterns). ReLU activation \( f(x) = \max(0, x) \) introduces nonlinearity. Early layers detect simple features; deeper layers detect complex compositions.

3

Pooling

Max pooling or stride-2 convolution reduces spatial dimensions by 2x, lowering computation and providing translational invariance. The feature maps get smaller spatially but deeper in channels.

4

Repeat

Stack multiple Conv-ReLU-Pool blocks. As spatial dimensions shrink, the number of filters typically doubles: 64 -> 128 -> 256 -> 512. This creates a pyramid of increasingly abstract features.

5

Flatten / Global Average Pool

Convert the 3D feature volume into a 1D vector. Flattening concatenates all values; GAP averages each channel to a single value (preferred in modern nets).

6

Fully Connected + Softmax

One or more dense layers map features to class scores. Softmax produces a probability distribution over \( C \) classes: \( P(y=c) = \frac{e^{z_c}}{\sum_{j=1}^{C} e^{z_j}} \).

Hierarchical Feature Learning

One of the most remarkable properties of CNNs is that they automatically learn a hierarchy of features from data:

  • Layer 1: Edges and color gradients (Gabor-like filters)
  • Layer 2: Corners, junctions, and simple textures
  • Layer 3: Parts of objects (eyes, wheels, windows)
  • Layer 4-5: Whole objects and complex patterns (faces, cars, animals)

This hierarchy emerges naturally from training with backpropagation -- the network learns what features are useful for the task without any manual feature engineering. This is why CNNs are so powerful: they replace decades of hand-crafted feature design with automatic, data-driven feature learning.

Interactive: CNN Architecture Explorer

Step through the layers of a CNN and see how the spatial dimensions and depth change at each stage.

CNN Layer-by-Layer Explorer

Use the slider to highlight each layer. The info box shows the output dimensions and parameter count at that stage.

07

Famous Architectures

The landmark CNN architectures that pushed the boundaries of computer vision accuracy and efficiency.

LeNet-5 (1998)

The pioneer. Designed by Yann LeCun for handwritten digit recognition (MNIST). Architecture: 2 convolutional layers, 2 average pooling layers, 3 fully connected layers. Only 60,000 parameters. Proved that end-to-end trainable CNNs could solve practical vision problems.

AlexNet (2012)

The game changer. Won ImageNet 2012 by a huge margin. Architecture: 5 convolutional layers, 3 fully connected layers. 61 million parameters. Key innovations: ReLU activation (much faster than sigmoid/tanh), dropout regularization, data augmentation, training on two GPUs.

VGG-16 (2014)

Simplicity through depth. Used only \( 3 \times 3 \) convolutions stacked deeply (16-19 layers). Two stacked \( 3 \times 3 \) convolutions have the same receptive field as one \( 5 \times 5 \) but with fewer parameters and more nonlinearity. 138 million parameters, mostly in the fully connected layers.

GoogLeNet / Inception (2014)

Efficiency through width. Introduced the Inception module: parallel pathways with \( 1 \times 1 \), \( 3 \times 3 \), \( 5 \times 5 \) convolutions and max pooling, concatenated together. Used \( 1 \times 1 \) convolutions as bottlenecks to reduce computation. Only 6.8 million parameters -- 22 layers deep but far more efficient than VGG.

ResNet (2015)

The depth breakthrough. Introduced residual connections (skip connections) that allow gradients to flow directly through the network: \( \mathbf{y} = F(\mathbf{x}) + \mathbf{x} \). This solved the vanishing gradient problem and enabled training of networks with 50, 101, or even 152 layers. ResNet-50 has 25.6 million parameters and achieved superhuman performance on ImageNet.

\[ \mathbf{y} = F(\mathbf{x}, \{W_i\}) + \mathbf{x} \quad \text{(Residual Learning)} \]

EfficientNet (2019)

Optimal scaling. Introduced compound scaling that uniformly scales depth, width, and resolution using a compound coefficient \( \phi \). EfficientNet-B0 achieves better accuracy than ResNet-50 with only 5.3 million parameters. The scaling rule balances all three dimensions simultaneously for maximum efficiency.

\[ \text{depth: } d = \alpha^\phi, \quad \text{width: } w = \beta^\phi, \quad \text{resolution: } r = \gamma^\phi \]

Parameter Count Comparison

08

Transfer Learning

Leveraging pre-trained models to achieve state-of-the-art results with limited data and compute.

Why Transfer Learning Works

Training a deep CNN from scratch requires massive datasets (millions of images) and significant compute resources (days on multiple GPUs). Transfer learning sidesteps this by reusing a network that was already trained on a large dataset like ImageNet (1.2 million images, 1000 classes).

The key insight is that early layers learn universal features -- edges, textures, colors, and simple patterns that are useful for virtually any vision task. Only the later layers learn task-specific features. By reusing the early layers and only retraining the later ones, we can leverage the universal representations without needing a large dataset.

Transfer learning is now the default approach for almost all computer vision tasks. It is rare to train a CNN from scratch unless you have a truly massive and unique dataset.

Two Transfer Learning Strategies

1

Feature Extraction

Freeze all convolutional layers of a pre-trained model and replace only the final classifier (fully connected layers). The CNN acts as a fixed feature extractor. Fast to train and works well when your dataset is small and similar to ImageNet.

2

Fine-Tuning

Start with a pre-trained model, replace the classifier, and then unfreeze some or all of the convolutional layers to retrain them with a very small learning rate. This allows the network to adapt its features to the new domain. Works best when you have a moderate amount of data.

Fine-Tuning Tip

Always use a much smaller learning rate when fine-tuning (e.g., 1e-5 vs 1e-3 for training from scratch). The pre-trained weights are already good; large updates would destroy them. Also, fine-tune from the top layers down -- unfreeze gradually, not all at once.

When to Use Which Strategy

  • Small dataset, similar domain: Feature extraction. Freeze all conv layers, train only the classifier.
  • Small dataset, different domain: Feature extraction from earlier layers (retrain more of the network).
  • Large dataset, similar domain: Fine-tune the entire network with a small learning rate.
  • Large dataset, different domain: Fine-tune aggressively, or consider training from scratch.
09

Hyperparameters

The key design choices that determine CNN performance: filter sizes, architecture depth, and training strategies.

Filter Size

Modern CNNs overwhelmingly use \( 3 \times 3 \) filters, following the VGG philosophy. Two stacked \( 3 \times 3 \) convolutions have an effective receptive field of \( 5 \times 5 \) but with fewer parameters (\( 2 \times 3^2 = 18 \) vs \( 5^2 = 25 \)) and an extra nonlinearity in between.

Occasionally \( 1 \times 1 \) convolutions are used as bottleneck layers to reduce channel dimensions (introduced by GoogLeNet), and \( 7 \times 7 \) or \( 5 \times 5 \) may appear in the very first layer to capture larger-scale patterns from raw pixels.

Number of Filters

The number of filters typically starts small and doubles as spatial dimensions halve. A common pattern is: 64 -> 128 -> 256 -> 512. This keeps the computational cost per layer roughly constant because halving spatial dimensions (4x fewer pixels) is offset by doubling filters.

More filters mean more features can be detected but also more parameters and computation. The optimal number depends on the complexity of the task -- CIFAR-10 (10 classes) needs fewer filters than ImageNet (1000 classes).

Learning Rate & Training

The learning rate is the single most important hyperparameter for training. Common strategies:

  • Initial rate: Typically \( 10^{-3} \) for Adam or \( 10^{-1} \) for SGD with momentum
  • Learning rate scheduling: Step decay (divide by 10 every 30 epochs), cosine annealing, or warmup + cosine
  • Batch size: 32-256 for standard GPUs. Larger batches enable higher learning rates but may generalize worse
  • Weight decay: L2 regularization of \( 10^{-4} \) to \( 10^{-5} \) helps prevent overfitting

Data Augmentation

Data augmentation is a critical regularization technique for CNNs. By applying random transformations to training images, we artificially increase the effective dataset size and force the network to learn invariant features:

  • Geometric: Random horizontal flip, rotation (up to 15 degrees), random crop, scaling
  • Color: Random brightness, contrast, saturation adjustments, color jittering
  • Advanced: Cutout (random erasing), Mixup (blend two images and labels), CutMix (paste patches between images)
  • AutoAugment: Use reinforcement learning or random search to find the optimal augmentation policy automatically

Batch Normalization

Batch Normalization normalizes the input to each layer to have zero mean and unit variance, then applies learnable scale and shift parameters. It stabilizes training, allows higher learning rates, and acts as a regularizer. Almost all modern CNNs use BatchNorm after every convolutional layer.

10

Applications

From image classification to medical diagnosis -- the domains where CNNs have achieved transformative results.

Where CNNs Excel

Image Classification

Categorizing images into classes: cat vs dog, tumor vs healthy tissue, defective vs normal product. CNNs achieve superhuman accuracy on benchmarks like ImageNet.

Object Detection

Locating and classifying multiple objects in an image with bounding boxes. Architectures like YOLO, Faster R-CNN, and SSD enable real-time detection for autonomous driving and surveillance.

Semantic Segmentation

Classifying every pixel in an image. Used in autonomous driving (road vs sidewalk vs car), medical imaging (organ boundaries), and satellite imagery analysis.

Medical Imaging

CNNs detect tumors in X-rays, classify skin lesions from photos, segment organs in CT scans, and screen for diabetic retinopathy. Often matching or exceeding specialist-level accuracy.

Interactive: Feature Map Filters

See how different convolution filters transform an image. Toggle between edge detection (horizontal, vertical), sharpening, and blurring filters.

Convolution Filter Effects

Left: input image. Center: kernel values. Right: convolution result. Click filter buttons to switch between different classical filters.

ImageNet Top-5 Accuracy Over Time

The chart below shows how CNN architectures improved Top-5 accuracy on the ImageNet benchmark over the years, from AlexNet's breakthrough in 2012 to near-perfect accuracy with EfficientNet.

Advantages

Automatic Feature Learning

No need for manual feature engineering. The network discovers the best features for the task directly from raw pixels.

Translation Invariance

Weight sharing and pooling make CNNs robust to the position of objects in the image.

Transfer Learning

Pre-trained models on ImageNet can be fine-tuned for new tasks with very little data, making CNNs accessible to everyone.

GPU Parallelism

Convolution operations are highly parallelizable on modern GPUs, enabling training of very deep networks in reasonable time.

Disadvantages

Data Hungry

Training from scratch requires large labeled datasets. Transfer learning mitigates but does not eliminate this issue.

Computationally Expensive

Training deep CNNs requires powerful GPUs and significant energy. Inference can also be slow for real-time applications on edge devices.

Limited Interpretability

Understanding why a CNN made a specific prediction is difficult. Grad-CAM and other visualization tools help but do not fully solve this.

Not Ideal for Non-Grid Data

CNNs are designed for grid-structured data (images, audio spectrograms). For graphs, point clouds, or tabular data, other architectures are more appropriate.

11

Python Implementation

From basic CNN classification to transfer learning pipelines using Keras and TensorFlow.

Basic CNN for CIFAR-10

A simple CNN built with Keras for classifying 32x32 color images into 10 categories.

import tensorflow as tf
from tensorflow.keras import layers, models

# Load and preprocess data
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()
X_train, X_test = X_train / 255.0, X_test / 255.0

# Build CNN model
model = models.Sequential([
    # Block 1
    layers.Conv2D(32, (3, 3), activation='relu', padding='same',
                  input_shape=(32, 32, 3)),
    layers.BatchNormalization(),
    layers.Conv2D(32, (3, 3), activation='relu', padding='same'),
    layers.MaxPooling2D((2, 2)),
    layers.Dropout(0.25),

    # Block 2
    layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
    layers.BatchNormalization(),
    layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
    layers.MaxPooling2D((2, 2)),
    layers.Dropout(0.25),

    # Block 3
    layers.Conv2D(128, (3, 3), activation='relu', padding='same'),
    layers.BatchNormalization(),
    layers.Conv2D(128, (3, 3), activation='relu', padding='same'),
    layers.MaxPooling2D((2, 2)),
    layers.Dropout(0.25),

    # Classifier
    layers.Flatten(),
    layers.Dense(256, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.summary()
history = model.fit(X_train, y_train, epochs=30,
                    batch_size=64, validation_split=0.1)

Transfer Learning with ResNet50

Fine-tune a pre-trained ResNet50 for a custom classification task.

from tensorflow.keras.applications import ResNet50
from tensorflow.keras import layers, models

# Load pre-trained ResNet50 without top classifier
base_model = ResNet50(
    weights='imagenet',
    include_top=False,
    input_shape=(224, 224, 3)
)

# Freeze base model layers
base_model.trainable = False

# Build custom classifier on top
model = models.Sequential([
    base_model,
    layers.GlobalAveragePooling2D(),
    layers.Dense(256, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(10, activation='softmax')
])

model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

# Phase 1: Train only the classifier head
model.fit(X_train, y_train, epochs=10, batch_size=32,
          validation_split=0.2)

# Phase 2: Fine-tune top layers of base model
base_model.trainable = True
for layer in base_model.layers[:-20]:
    layer.trainable = False

model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

model.fit(X_train, y_train, epochs=10, batch_size=32,
          validation_split=0.2)

Data Augmentation Pipeline

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Define augmentation
datagen = ImageDataGenerator(
    rotation_range=15,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True,
    zoom_range=0.1,
    fill_mode='nearest'
)

# Train with augmented data
model.fit(
    datagen.flow(X_train, y_train, batch_size=64),
    epochs=50,
    validation_data=(X_test, y_test),
    steps_per_epoch=len(X_train) // 64
)

Visualizing Feature Maps

import numpy as np
import matplotlib.pyplot as plt

# Create a model that outputs intermediate activations
layer_outputs = [layer.output for layer in model.layers
                 if 'conv2d' in layer.name]
activation_model = models.Model(
    inputs=model.input, outputs=layer_outputs
)

# Get activations for a sample image
sample = X_test[0:1]
activations = activation_model.predict(sample)

# Plot feature maps from the first conv layer
first_layer_activation = activations[0]
fig, axes = plt.subplots(4, 8, figsize=(16, 8))
for i, ax in enumerate(axes.flat):
    if i < first_layer_activation.shape[-1]:
        ax.imshow(first_layer_activation[0, :, :, i],
                  cmap='viridis')
    ax.axis('off')
plt.suptitle('Feature Maps - First Conv Layer')
plt.tight_layout()
plt.show()

Grad-CAM for Interpretability

Gradient-weighted Class Activation Mapping (Grad-CAM) highlights which regions of the input image most influenced the prediction. It computes gradients of the target class with respect to the final convolutional layer and produces a heatmap overlay. Use the tf-keras-vis or pytorch-grad-cam library for easy implementation.

Continue Your Journey