Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

CNN Filter Visualization using TensorFlow (VGG16)

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.

Objectives

  • 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

Key Concepts

Convolutional Neural Networks (CNNs)

CNNs learn spatial hierarchies of features:

  • Early layers → edges, textures
  • Middle layers → patterns, shapes
  • Deeper layers → object parts and semantics

Filter Visualization via Activation Maximization

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

Gradient Ascent

Given a target filter activation:

  • Compute gradient of activation w.r.t input image
  • Update image in direction of increasing activation

Mathematically:

$$ x_{new} = x + \eta \cdot \frac{\partial \text{activation}}{\partial x} $$

Project Structure

The notebook is organized into the following stages:

1. Introduction

  • Overview of CNN interpretability
  • Motivation for filter visualization
  • Explanation of activation maximization

2. Model Loading

  • Load pretrained VGG16 model (without top classification layers)
  • Use pretrained weights to extract learned representations
  • Prepare model for inference

3. Submodel Construction

  • Extract outputs of intermediate convolutional layers
  • Build submodels targeting specific layers
  • Enable access to feature maps at different depths

4. Image Initialization

  • Start from a random noise image
  • Normalize input for stable optimization
  • Ensure compatibility with VGG16 preprocessing

5. Gradient Ascent Optimization Loop

Core pipeline:

  1. Select a layer and filter index
  2. Forward pass → compute filter activation
  3. Compute gradients w.r.t input image
  4. Normalize gradients (e.g., using standard deviation) to stabilize updates
  5. Update image iteratively

Additional details:

  • Uses TensorFlow’s GradientTape
  • Applies controlled step size
  • Runs for multiple iterations to refine patterns

6. Image Post-processing

  • Convert optimized tensor into displayable image
  • Normalize pixel values
  • Rescale and format pixel values for visualization
  • Improve visual clarity

7. Visualization of Filters

  • Generate images for multiple filters
  • Compare patterns across layers
  • Observe the progression of feature abstraction with network depth

Observations & Insights

  • 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

Technical Details

Model

  • Architecture: VGG16
  • Weights: ImageNet pretrained
  • Framework: TensorFlow / Keras

Optimization Strategy

  • Objective: Maximize mean activation of a selected filter
  • Method: Gradient ascent in input space
  • Stabilization:
    • Gradient normalization
    • Controlled learning rate

Input Handling

  • Random noise initialization
  • Preprocessing aligned with VGG16 expectations
  • Continuous updates via differentiable pipeline

Output

The project produces:

  • Synthetic images representing maximally activating patterns
  • Layer-wise visualization of learned features
  • Insight into hierarchical feature extraction

Tech Stack

  • Python
  • TensorFlow / Keras
  • Matplotlib

Conclusion

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.

Releases

Packages

Contributors

Languages