Skip to content

Grad-CAM explainability for image classification#4

Open
Alvin-Nahabwe wants to merge 1 commit into
ci/pipeline-checksfrom
feat/gradcam
Open

Grad-CAM explainability for image classification#4
Alvin-Nahabwe wants to merge 1 commit into
ci/pipeline-checksfrom
feat/gradcam

Conversation

@Alvin-Nahabwe

Copy link
Copy Markdown
Collaborator

Stacked on #3 (ci/pipeline-checks, which carries #1 + #2 + CI). Its diff collapses to just the Grad-CAM files once #3 merges.

What

Users can now see why a classifier predicted what it did, not just the label.

  • New image_classification_explain.py computes Grad-CAM and saves a heatmap overlay.
  • New endpoint POST /explain/image-classification returns the predicted class, confidence, and the overlay URL.
  • grad-cam==1.5.5 pinned; the smoke test now also runs an explanation on the classifier it trains.

How it handles the varied registry

Grad-CAM needs a target layer, which differs by architecture. Auto-selection:

  • CNNs (ResNet, ConvNeXt, EfficientNet) → the last Conv2d.
  • Transformers (ViT, BEiT, Swin, DINOv2) → the final LayerNorm, with the token sequence reshaped back to a 2D grid (CLS token dropped).

Verified (CPU)

Ran end-to-end against both a CNN (efficientnet-b0) and a transformer (ViT), and against a freshly fine-tuned checkpoint (correct id2label + confidence, valid overlay). Companion UI PR on no-code-app adds the "Explain" checkbox to the inference tab.

Users can now see *why* a classifier predicted what it did, not just the
label. New image_classification_explain.py computes Grad-CAM and saves a
heatmap overlay; a /explain/image-classification endpoint returns the
predicted class, confidence, and the overlay URL.

Works across both model families in the registry, with automatic target-
layer selection:
- CNNs (ResNet, ConvNeXt, EfficientNet): the last Conv2d layer.
- Transformers (ViT, BEiT, Swin, DINOv2): the final LayerNorm, with the
  token sequence reshaped back to a 2D grid.

Verified end-to-end on CPU against both a CNN (efficientnet-b0) and a
transformer (ViT), and against a freshly fine-tuned checkpoint (correct
id2label + confidence). The smoke test now also runs an explanation on
the classifier it trains. grad-cam==1.5.5 pinned.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant