6.2. Explainability in CNNs

6 min read 1068 words

🪄 Step 1: Intuition & Motivation

  • Core Idea (short): CNNs are powerful but opaque — they can achieve 99% accuracy, yet we often don’t know why. Explainability (or interpretability) bridges this gap by showing which parts of an image influence the model’s decision.

Without interpretability, CNNs are black boxes. With interpretability, they become glass boxes — you can see what they focus on and why.

  • Simple Analogy: Imagine asking a student why they think a picture shows a “cat.” A good student points to the ears and whiskers — that’s explainability. A bad student might point at the background sofa — that’s bias.

CNN explainability tells us where the model is looking — and whether it’s for the right reasons.


🌱 Step 2: Core Concept — How CNN Interpretability Works

CNNs make decisions layer by layer — from low-level edges to high-level object features. Interpretability methods trace these internal activations back to the input, highlighting which pixels influenced the final prediction most.


🧩 1. Saliency Maps — “Where the Model is Sensitive”

What’s Happening Under the Hood?

A saliency map shows which pixels most affect the output score for a given class.

We compute the gradient of the class score $S_c$ with respect to each pixel in the input image $I$:

$$ M_{saliency} = \left| \frac{\partial S_c}{\partial I} \right| $$
  • High gradient = changing that pixel would strongly affect the prediction.
  • Low gradient = that pixel doesn’t matter much.

This highlights regions that contribute most to the model’s confidence.

Interpretation: Brighter areas in the saliency map = regions the model finds important.

It’s like shining a flashlight backward through the network to see which pixels “light up” the decision.

🧩 2. Class Activation Maps (CAM) — “Which Regions Triggered the Neuron”

What’s Happening Under the Hood?

CAMs connect final convolutional features to the class score. They visualize which parts of the image activate the class neuron.

Formula (simplified):

$$ M_c(x, y) = \sum_k w_k^c f_k(x, y) $$

Where:

  • $f_k(x, y)$ = activation of feature map $k$ at location $(x, y)$
  • $w_k^c$ = weight connecting feature $k$ to class $c$

Result → heatmap highlighting where the CNN looked to identify class $c$.

🧠 Example: For “dog” class, the CAM might glow around the face and paws, but not the background.

Limitation: Works only when the final layer is global average pooling (GAP). To fix that → enter Grad-CAM.


🧩 3. Grad-CAM — “The Gold Standard for CNN Explainability”

What’s Happening Under the Hood?

Grad-CAM (Gradient-weighted Class Activation Mapping) generalizes CAMs to any CNN architecture.

It uses gradients flowing into the last convolutional layer to identify important regions for a target class.

Steps:

  1. Compute gradient of class score $S_c$ w.r.t. feature maps $A^k$.
  2. Compute average gradient per channel: $$ \alpha_k^c = \frac{1}{Z} \sum_i \sum_j \frac{\partial S_c}{\partial A_{ij}^k} $$
  3. Weight feature maps by importance: $$ L_{Grad-CAM}^c = ReLU\left(\sum_k \alpha_k^c A^k\right) $$

The result: A heatmap highlighting areas that influenced the class prediction.

Grad-CAM tells you, “Here’s what your model was looking at when it said ‘cat.’” It’s like replaying the model’s mental focus frame by frame.

🧩 4. Feature Attribution — “Who Deserves the Credit?”

What’s Happening Under the Hood?

Feature attribution methods assign credit or blame to each input pixel or feature for the model’s output.

Popular techniques:

  • Integrated Gradients: Average gradients as input scales from 0 → original image.
  • SHAP (SHapley Additive exPlanations): Based on cooperative game theory — measures how each feature changes model output.
  • Occlusion Sensitivity: Mask out image regions to see how predictions change.

Each method asks: “If I remove this pixel/feature, how much does the prediction change?”

💡 Insight: Attribution methods help you quantify feature importance, not just visualize it.


⚙️ Step 3: Why Interpretability Matters in Practice

Interpretability isn’t just academic — it’s essential for trust, safety, and compliance.

PurposeHow Explainability Helps
DebuggingFind when CNNs learn wrong features (e.g., focusing on watermarks or backgrounds).
Bias DetectionReveal spurious correlations (e.g., model identifies “doctor” only when it sees a man).
Regulatory ComplianceLegal frameworks (like EU’s AI Act) demand model transparency and interpretability.
Safety & ReliabilityEnsure the model bases decisions on relevant visual cues (e.g., pedestrian detection focusing on the person, not shadows).

🧠 Strong candidates emphasize that interpretability isn’t just about pretty heatmaps — it’s a tool for trust and accountability.


🧩 Step 4: Example Insight — “A Real Debugging Scenario”

Suppose a CNN for “cat vs. dog” performs perfectly in validation. But when Grad-CAM visualizations are examined:

  • Dog images → model focuses on collars.
  • Cat images → model focuses on background carpet.

It wasn’t learning dogs and cats — it was learning collars and carpets. Such insight can only be caught via explainability, not raw accuracy metrics.


🧮 Step 5: Mathematical Foundation (Conceptual)

Grad-CAM Formula Recap
$$ L_{Grad-CAM}^c = ReLU\left(\sum_k \alpha_k^c A^k\right) $$
  • $A^k$ — feature map activations of convolution layer
  • $\alpha_k^c$ — importance weights (averaged gradients for class $c$)
  • ReLU ensures only positive influences are visualized (helps interpret focus regions)

Interpretation: The heatmap $L_{Grad-CAM}^c$ shows spatial locations that positively influence the prediction of class $c$.

Think of it as reverse engineering attention: tracing how much each region “contributed” to the final answer.

⚖️ Step 6: Strengths, Limitations & Trade-offs

Strengths

  • Improves model trust and transparency.
  • Useful for debugging, fairness, and compliance.
  • Techniques like Grad-CAM are intuitive and easy to visualize.

⚠️ Limitations

  • Interpretations are approximations — not always perfect truths.
  • Saliency maps can be noisy or inconsistent.
  • Grad-CAM highlights what influenced the output, not why the model decided so.

⚖️ Trade-offs

  • Simple methods (like saliency maps) are fast but coarse.
  • Advanced methods (Integrated Gradients, SHAP) are more accurate but slower.
  • A balance between faithfulness and computational cost is key for production interpretability.

🚧 Step 7: Common Misunderstandings

🚨 Common Misunderstandings (Click to Expand)
  • “Explainability guarantees correctness.” No — it only shows what influenced the decision, not if it was right.
  • “Grad-CAM is always positive.” ReLU filters only positive influence; negative evidence is ignored.
  • “Visualization = understanding.” Interpretability aids human reasoning but doesn’t replace rigorous validation.

🧩 Step 8: Mini Summary

🧠 What You Learned: Explainability in CNNs uncovers how models “see” images, through methods like Grad-CAM, CAM, and saliency maps.

⚙️ How It Works: Gradients and activations are traced back to highlight influential image regions.

🎯 Why It Matters: Interpretability builds trust, diagnoses bias, and supports safe deployment in critical real-world systems.

Any doubt in content? Ask me anything?
Chat
🤖 👋 Hi there! I'm your learning assistant. If you have any questions about this page or need clarification, feel free to ask!