C.W.K.
Stream
Lesson 03 of 12 · published

CNN Intuition: Edges to Objects

~18 min · cnn, feature-hierarchy, interpretability

Level 0Curious
0 XP0/73 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

What CNNs actually learn

If you visualize the filters of a trained CNN, a beautiful pattern appears. Early layers learn oriented edge detectors and color blobs (much like the simple cells of mammalian visual cortex). Mid layers learn textures and shape parts (eyes, wheels, fur patterns). Late layers learn object-shaped features and category-specific concepts.

This wasn't designed in — it emerged from training. The architecture (locality + parameter sharing + non-linearity + depth) is enough; the specific feature hierarchy comes from the data and the gradient.

Tip: If you ever doubt that deep learning is doing something real, look at the visualized filters from AlexNet's first layer. Hand-designed edge detectors (Gabor filters) look almost identical to what the network learned from scratch.

Receptive field grows with depth

A neuron in layer 1 sees a small patch of the input. A neuron in layer 5 sees a larger patch (the union of all the layer-1 patches that feed into it via the conv stack). A neuron in layer 30 sees most of the image. This is how CNNs build up from local edges to global structure.

Pooling and downsampling

Most CNNs alternate convolutions with downsampling (stride-2 conv or MaxPool). Each downsample halves the spatial dimensions and (usually) doubles the channel count. The result is a pyramid: high-resolution shallow features at the start, low-resolution deep features at the end.

Visualizing what's inside

Tools: gradient-based saliency maps (which input pixels did the prediction depend on?), Grad-CAM (which spatial regions activated the predicted class?), feature inversion (what input would maximize this filter?). These are interpretability bricks worth knowing.

Principle: CNNs are not black boxes if you take 30 minutes to look inside. Filter visualizations and Grad-CAM heatmaps will teach you more about what your model is doing than any number of accuracy plots.

Code

Grad-CAM in 20 lines·python
import torch
import torch.nn.functional as F

def gradcam(model, x, target_class, target_layer):
    activations, gradients = [], []
    h1 = target_layer.register_forward_hook(lambda m, i, o: activations.append(o))
    h2 = target_layer.register_full_backward_hook(lambda m, gi, go: gradients.append(go[0]))

    model.eval()
    logits = model(x)
    model.zero_grad()
    logits[0, target_class].backward()

    act = activations[0][0]                  # [C, H, W]
    grad = gradients[0][0]                   # [C, H, W]
    weights = grad.mean(dim=(1, 2))           # [C]
    cam = (weights[:, None, None] * act).sum(dim=0)
    cam = F.relu(cam)
    cam = cam / cam.max()

    h1.remove(); h2.remove()
    return cam.cpu()

External links

Exercise

Pick any pretrained CNN. Run Grad-CAM on three different images and three different target classes. Inspect the heatmaps. They should highlight spatially sensible regions (not random noise) — that's what 'the model is looking at the right thing' means in practice.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.