Computing IG for Image Models
You'll learn: How to implement Integrated Gradients step-by-step for image classification models by interpolating pixels, batching gradients, and aggregating attributions.
The Core Workflow
When applying Integrated Gradients to images, you transform the abstract formula into concrete computations. You're essentially tracing a path through pixel space from your baseline (often a black image) to your actual input image, computing gradients at multiple points along this path.
Step-by-Step Implementation
1. Interpolating Pixels
Create intermediate images along the straight-line path from baseline to input. If your baseline is all zeros and you want m=50 steps:
# baseline shape: (C, H, W), input_image shape: (C, H, W)
alphas = torch.linspace(0, 1, steps=m)
interpolated_images = baseline + alphas.view(-1, 1, 1, 1) * (input_image - baseline)
# Result: (m, C, H, W) tensor of intermediate images
Each interpolated image is a weighted blend: at α=0 you get the baseline, at α=1 you get the original input.
2. Batching Gradients
Feed all m interpolated images through your model in batches to compute gradients efficiently:
interpolated_images.requires_grad = True
outputs = model(interpolated_images)
target_output = outputs[:, target_class]
gradients = torch.autograd.grad(target_output.sum(), interpolated_images)[0]
# Result: (m, C, H, W) gradients
3. Aggregating Attributions
Average the gradients (Riemann approximation) and scale by the input-baseline difference:
avg_gradients = gradients.mean(dim=0) # Average over m steps
attributions = (input_image - baseline) * avg_gradients
# Result: (C, H, W) attribution map
Key Takeaway: Computing IG for images requires interpolating pixel values along the baseline-to-input path, efficiently batching gradient computations across all interpolation steps, and averaging those gradients to produce per-pixel attribution scores.