Skip to main content
Open In Colab This notebook contains code samples from Chapter 5, Section 4 of Deep Learning with Python.

Overview

We cover three visualization techniques:
  1. Intermediate activations - understand how successive layers transform input
  2. Filter visualization - see what visual patterns each filter responds to
  3. Class Activation Maps (CAM) - identify which parts of an image led to classification

Visualizing Intermediate Activations

Extract feature maps from convolution and pooling layers:
import tensorflow as tf

# Load trained model
model = tf.keras.models.load_model('cats_and_dogs_small_2.h5')

# Extract outputs of top 8 layers
layer_outputs = [layer.output for layer in model.layers[:8]]

# Create model that returns these outputs
activation_model = tf.keras.models.Model(inputs=model.input, outputs=layer_outputs)

# Get activations for an input image
activations = activation_model.predict(img_tensor)
Key observations:
  • First layers act as edge detectors, retaining most input information
  • Higher layers encode abstract concepts like “cat ear” or “cat eye”
  • Activation sparsity increases with depth

Visualizing ConvNet Filters

Use gradient ascent to find input patterns that maximally activate each filter:
def generate_pattern(layer_name, filter_index, size=150):
    layer_output = model.get_layer(layer_name).output
    loss = K.mean(layer_output[:, :, :, filter_index])
    grads = K.gradients(loss, model.input)[0]
    grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)

    iterate = K.function([model.input], [loss, grads])
    input_img_data = np.random.random((1, size, size, 3)) * 20 + 128.

    for i in range(40):
        loss_value, grads_value = iterate([input_img_data])
        input_img_data += grads_value * step

    return deprocess_image(input_img_data[0])
Filter patterns by layer:
  • block1_conv1: directional edges and colors
  • block2_conv1: simple textures from edge combinations
  • Higher layers: natural textures (feathers, eyes, leaves)

Class Activation Maps (Grad-CAM)

Visualize which image regions led to classification decisions:
# Get gradient of class output w.r.t. last conv layer
grads = K.gradients(african_elephant_output, last_conv_layer.output)[0]

# Weight channels by gradient importance
pooled_grads = K.mean(grads, axis=(0, 1, 2))

# Generate heatmap
heatmap = np.mean(conv_layer_output_value, axis=-1)
heatmap = np.maximum(heatmap, 0)
heatmap /= np.max(heatmap)
This technique helps:
  • Debug classification mistakes
  • Locate objects in images
  • Understand model decision process

Connect these docs to Claude, VSCode, and more via MCP for real-time answers.