Grad-CAM (Gradient-weighted Class Activation Mapping) produces visual explanations for decisions made by CNN-based models. This project replicates the original paper by Selvaraju et al. (ICCV 2017) using a pretrained VGG16 on the ImageNet dataset.
Grad-CAM highlights which regions of an image a CNN focused on when making a prediction. It works by:
- Running a forward pass to get the prediction.
- Computing gradients of the target class score with respect to the last convolutional layer.
- Averaging those gradients (Global Average Pooling) to get importance weights α.
- Multiplying the weights by the feature maps and applying ReLU.
- Upsampling the resulting heatmap to the original image size.
L_Grad-CAM = ReLU( Σ_k α_k · A^k )
The heatmap is then overlaid on the original image — hot colours = important regions.
Grad_CAM_VGG16_ImageNet/
├── GRAD_CAM.ipynb # 📓 Main notebook (run this in Colab)
├── grad_cam.py # 🐍 Python script version of the notebook
├── requirements.txt # 📦 Python dependencies
├── .env.example # 🔐 Secret variables template (copy → .env)
└── README.md # 📄 This file
- Click the "Open in Colab" badge at the top of this README.
- Go to Runtime → Change runtime type and select GPU (T4 recommended).
- Set up your Kaggle credentials (see 🔐 Secrets Setup below).
- Click Runtime → Run all (or press
Ctrl+F9).
That's it — the notebook downloads the dataset, runs Grad-CAM on sample images, and displays the heatmap visualizations automatically.
Prerequisites: Python ≥ 3.8, pip
# 1. Clone the repo
git clone https://github.com/Aditri-web/ML-CaPsule.git
cd ML-CaPsule/Grad_CAM_VGG16_ImageNet
# 2. Create and activate a virtual environment (recommended)
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# 3. Install dependencies
pip install -r requirements.txt
pip install python-dotenv # for loading .env secrets
# 4. Set up your secrets (see Secrets Setup below)
cp .env.example .env
# → open .env and fill in your KAGGLE_USERNAME and KAGGLE_KEY
# 5. Run the script
python grad_cam.pyThis project needs a Kaggle API key to download the ImageNet Mini dataset.
- Log in to kaggle.com.
- Go to Account Settings → scroll to API → click Create New Token.
- This downloads a
kaggle.jsonfile containing yourusernameandkey.
- Click the 🔑 key icon in the left sidebar ("Secrets").
- Add two secrets:
- Name:
KAGGLE_USERNAME→ Value: your Kaggle username - Name:
KAGGLE_KEY→ Value: the key string fromkaggle.json
- Name:
- Toggle "Notebook access" ON for both.
⚠️ Never paste your API key directly into the notebook cell or commit it to GitHub.
cp .env.example .envOpen .env and fill in:
KAGGLE_USERNAME=your_username
KAGGLE_KEY=your_api_key_here
The .env file is listed in .gitignore and will never be committed.
The notebook uses the ImageNet Mini dataset (ifigotin/imagenetmini-1000 on Kaggle). After downloading, it picks sample validation images automatically.
Three representative classes used for demonstration:
| Class | ImageNet ID | What Grad-CAM shows |
|---|---|---|
| 🐕 Labrador Retriever | n02099712 |
Face and fur texture highlighted |
| 🐱 Siamese Cat | n02123597 |
Eyes and face region highlighted |
| 🐘 African Elephant | n02504458 |
Trunk and tusks highlighted |
See test_images_explanation.md for a detailed description of each test image class, what features the model focuses on, and what the Grad-CAM heatmap reveals.
| Library | Purpose |
|---|---|
torch / torchvision |
VGG16 model + transforms |
opencv-python-headless |
Heatmap generation & image manipulation |
matplotlib |
3-panel visualization |
Pillow |
Image loading |
requests |
Fetching ImageNet labels |
kaggle |
Dataset download via CLI |
python-dotenv |
Loading secrets from .env (local only) |
Install everything with:
pip install -r requirements.txtrequirements.txt contents:
torch
torchvision
opencv-python-headless
matplotlib
Pillow
requests
kaggle
python-dotenv
pytorch-grad-cam
!pip install torch torchvision pytorch-grad-cam opencv-python-headless
import torch, torchvision.models as models, cv2, numpy as npInstalls all required libraries and imports them.
model = models.vgg16(weights=models.VGG16_Weights.IMAGENET1K_V1)
model.eval()Downloads the pretrained VGG16 weights. .eval() disables dropout and batch norm training behaviour.
The GradCAM class attaches hooks to model.features[28] — VGG16's last convolutional layer. Hooks capture:
- Forward activations (feature map values)
- Backward gradients (how each neuron influenced the prediction)
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])Standard ImageNet preprocessing — same values used during VGG16 training.
cam, target_class = gradcam.generate_cam(input_tensor)Runs forward + backward passes, pools the gradients, and returns a normalised 224×224 heatmap.
visualize_gradcam(original_img, cam, target_class, confidence)Produces a 3-panel plot: Original | Heatmap | Overlay.
Selvaraju, R. R., Cogswell, M., Das, A., Vedantam, R., Parikh, D., & Batra, D. (2017).
Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization.
ICCV 2017. Paper link
Pull requests and issues are welcome! Please read CONTRIBUTING.md before submitting.