Grad-CAM explainability for image classification#4
Open
Alvin-Nahabwe wants to merge 1 commit into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Users can now see why a classifier predicted what it did, not just the label.
image_classification_explain.pycomputes Grad-CAM and saves a heatmap overlay.POST /explain/image-classificationreturns the predicted class, confidence, and the overlay URL.grad-cam==1.5.5pinned; 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:
Conv2d.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.