Project

Visualizing CNN Feature Maps

Python 3.10+ · PyTorch · VGG19 · MIT License
Overview

A small notebook that hooks into a pretrained VGG19 and looks at what its intermediate layers actually produce when given an image - turning the usual "image in, prediction out" black box inside out.

The project uses PyTorch forward hooks to capture raw output tensors from four layers in model.features: two early layers and two deep layers. A real statue photo is resized to 224×224, normalized with ImageNet statistics, passed through the model, and then inspected as feature maps.

Layer 22 produces a 512-channel 28×28 tensor. Summing each channel's activations shows that channel 159 responds most strongly to this image, picking up the broad light/dark contrast structure of the statue's silhouette and folds.

Results
Input statue image next to the grayscale activation map of VGG19 layer 22, channel 159 The 224 by 224 input statue image used for the VGG19 forward pass Grayscale activation map for VGG19 layer 22, channel 159
How it works

A forward hook is a function PyTorch calls when a layer finishes its forward pass. It receives the layer's input and output without changing the model's behavior, so it works as an observation point rather than a modification.

The notebook registers hooks on features[3], features[4], features[21], and features[22]. The early layers preserve more spatial detail at 64 channels, while the deeper layers compress the image down to a coarser 28×28 grid with 512 learned detectors.

The whole project lives in main.ipynb: load VGG19, register hooks, run the sample image through the model, render selected feature maps, then search all 512 channels of layer 22 for the strongest total activation.

What I learned

The useful realization was that CNN features are not just a metaphor. They are literal tensors that can be captured mid-flight, reshaped, and viewed as images. Looking at them directly makes the tradeoff concrete: resolution drops from 224×224 to 28×28 while channel depth grows from 64 to 512.

It also made clear that most channels are quiet for any one image. A deep feature map is not one picture; it is hundreds of specialized detectors, with only a handful carrying strong signal for a given input. For this statue, channel 159 is the one that lights up most.