Skip to content

Latest commit

 

History

12 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌸 Grad-CAM Visualization Demo (TF-Flowers CNN)

🔍 Overview

This project shows how to use Grad-CAM (Gradient-weighted Class Activation Mapping) to visualise where a CNN looks when it makes a prediction. The demo trains a small CNN on the TensorFlow Flowers dataset (5 flower classes) and overlays Grad-CAM heatmaps on top of the input images.

Grad-CAM helps explain why the model classified an image the way it did — a key tool in Explainable AI (XAI).

🎓 Originally built for the CS42 Explainable AI unit. Designed to give a hands-on intuition for what convolutional layers actually attend to.


📦 Features

  • Loads and preprocesses the TF-Flowers dataset (5 classes: daisy, dandelion, roses, sunflowers, tulips)
  • Builds a CNN for image classification
  • Computes Grad-CAM heatmaps for any prediction
  • Visualises original image + heatmap overlay side-by-side
  • Self-contained Jupyter / Colab notebook

🌼 Dataset — TensorFlow Flowers

Property Details
Source tensorflow_datasetstf_flowers
Classes daisy, dandelion, roses, sunflowers, tulips
Samples ~3,670
Task Multi-class image classification

🖼 Sample Output

Sample of the dataset: Sample images

Grad-CAM heatmap output: Output


🧰 Requirements

Install the dependencies:

pip install -r requirements.txt

Pinned versions:

  • tensorflow>=2.12
  • tensorflow-datasets
  • numpy
  • matplotlib
  • scikit-learn

⚙️ How to Run

Option 1 — Jupyter (local)

jupyter notebook Gradcam_tf_Flowers.ipynb

Option 2 — Google Colab

  1. Open Colab → File → Upload notebook
  2. Pick Gradcam_tf_Flowers.ipynb
  3. Run all cells (GPU recommended: Runtime → Change runtime type → GPU)

📁 Project Structure

Grad-CAM-Visualization-Demo-TF-Flowers-CNN-/
├── Gradcam_tf_Flowers.ipynb   # Main notebook (training + Grad-CAM)
├── Output.png                 # Saved Grad-CAM example
├── Sample images.png          # Dataset preview
├── requirements.txt           # Python dependencies
├── LICENSE
└── README.md

🧠 How Grad-CAM Works (in 30 seconds)

  1. Pick a target class (e.g., the predicted class).
  2. Compute the gradient of the target class score with respect to the activations of the last convolutional layer.
  3. Average those gradients spatially → these are the importance weights for each feature map.
  4. Multiply each feature map by its weight, sum them all → you get a 2-D heatmap.
  5. Upsample the heatmap to the input image size and overlay it.

The brighter the region, the more it influenced the prediction.


🎓 Learning Objectives

  1. Understand what a CNN's last convolutional layer "looks at"
  2. Compute gradients with tf.GradientTape
  3. Implement and apply Grad-CAM end-to-end
  4. Critically evaluate model decisions (XAI mindset)

🔗 Related


📄 License

MIT — Credits to CS42.org.

About

Explainable AI demo: apply Grad-CAM to a CNN trained on the TensorFlow Flowers dataset to visualize which image regions drive each prediction. Jupyter notebook with sample outputs.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages