This project explores the internal representations learned by a Convolutional Neural Network (CNN) by visualizing the patterns that maximally activate its filters. Using the pretrained VGG16 architecture in TensorFlow, the project applies gradient ascent to generate synthetic input images that strongly activate specific convolutional filters.
The goal is to provide interpretability into CNN feature hierarchies by transforming abstract activations into human-interpretable visual patterns.
- Implement gradient ascent in input space to maximize filter activations
- Extract and analyze intermediate representations from a pretrained CNN
- Visualize feature maps learned at different convolutional layers
- Understand how hierarchical features evolve across network depth
CNNs learn spatial hierarchies of features:
- Early layers → edges, textures
- Middle layers → patterns, shapes
- Deeper layers → object parts and semantics
Instead of passing real images through the network, we optimize the input image itself to maximize the activation of a specific filter.
This is achieved by:
- Initializing a random image
- Iteratively updating it using gradients
- Maximizing the response of a selected filter
Given a target filter activation:
- Compute gradient of activation w.r.t input image
- Update image in direction of increasing activation
Mathematically:
The notebook is organized into the following stages:
- Overview of CNN interpretability
- Motivation for filter visualization
- Explanation of activation maximization
- Load pretrained VGG16 model (without top classification layers)
- Use pretrained weights to extract learned representations
- Prepare model for inference
- Extract outputs of intermediate convolutional layers
- Build submodels targeting specific layers
- Enable access to feature maps at different depths
- Start from a random noise image
- Normalize input for stable optimization
- Ensure compatibility with VGG16 preprocessing
Core pipeline:
- Select a layer and filter index
- Forward pass → compute filter activation
- Compute gradients w.r.t input image
- Normalize gradients (e.g., using standard deviation) to stabilize updates
- Update image iteratively
Additional details:
- Uses TensorFlow’s
GradientTape - Applies controlled step size
- Runs for multiple iterations to refine patterns
- Convert optimized tensor into displayable image
- Normalize pixel values
- Rescale and format pixel values for visualization
- Improve visual clarity
- Generate images for multiple filters
- Compare patterns across layers
- Observe the progression of feature abstraction with network depth
-
Early Layers
Produce simple patterns such as:- Edges
- Color gradients
- Basic textures
-
Intermediate Layers
Capture:- Repeated motifs
- Geometric structures
-
Deep Layers
Represent:- Complex shapes
- Object-like structures
- Semantic patterns
- Architecture: VGG16
- Weights: ImageNet pretrained
- Framework: TensorFlow / Keras
- Objective: Maximize mean activation of a selected filter
- Method: Gradient ascent in input space
- Stabilization:
- Gradient normalization
- Controlled learning rate
- Random noise initialization
- Preprocessing aligned with VGG16 expectations
- Continuous updates via differentiable pipeline
The project produces:
- Synthetic images representing maximally activating patterns
- Layer-wise visualization of learned features
- Insight into hierarchical feature extraction
- Python
- TensorFlow / Keras
- Matplotlib
This project demonstrates how neural networks internally encode visual information and how these representations can be reverse-engineered into interpretable patterns. By leveraging gradient ascent, we gain direct insight into what each convolutional filter is "looking for," providing a powerful tool for understanding deep learning models beyond black-box behavior.