Grad-CAM (Gradient-Weighted Class Activation Mapping) is a technique used to produce visual explanations for decisions made by Convolutional Neural Networks (CNNs).
It answers the question:
Which regions of the image were most important for this specific class prediction?
Grad-CAM works by computing gradients of a target class score with respect to the feature maps of the final convolutional layer and producing a heatmap highlighting important regions.
-
$y^c$ — score (logit) for class ($c$ ) -
$A^k$ — ($k$ )-th feature map of the selected convolutional layer -
$A_{ij}^k$ — activation at spatial location ($(i, j)$ ) in feature map ($k$ ) -
$Z$ — total number of spatial locations in a feature map
Compute the gradient of the class score with respect to each feature map activation:
This tells us how sensitive the class score is to each spatial location.
Compute importance weights for each feature map:
α_k^crepresents how important feature mapkis for classc.
Combine feature maps using the computed weights:
ReLU keeps only positive contributions (regions that support the class prediction).
- A coarse localization heatmap
- Upsampled to input image size
- Overlaid on the original image for visualization
Grad-CAM:
- Uses gradients flowing into the last convolutional layer
- Produces class-discriminative localization maps
- Works with most CNN-based architectures
- Does not require architectural modifications
Cloning of repository
git clone https://github.com/kja8586/Grad-CAM.git
Changing directory
cd Grad-CAM
Installing dependencies Using requirements.txt
pip install -r requirements.txt
Execute the code
python main.py