This project integrates GradCAM (Gradient-weighted Class Activation Mapping) with YOLOv7 to better understand which features the model focuses on when making detection decisions.
- YOLO series models are widely known for their excellent performance on object detection tasks.
- But do they really "understand" the objects they detect?
- This project visualizes YOLOv7's internal attention using GradCAM to reveal which parts of the image influence detection results.
- My technical walkthrough on YOLO training and detection (iT 邦幫忙)
- One-stage detectors: YOLO, SSD, RetinaNet
- How to train YOLO models (step-by-step)
GradCAM (Gradient-weighted Class Activation Mapping) is an Explainable AI (XAI) technique to:
- Visualize important regions that contribute to a model’s prediction.
- Provide interpretability and diagnostic power during training or production.
GradCAM works by:
- Extracting activations from intermediate layers.
- Weighting these activations using the average gradient with respect to the predicted class.
- Overlaying the heatmap onto the original image to highlight influential regions.
To support GradCAM visualization, a few changes are needed in YOLOv7’s original codebase.
./model/yolo.py./model/experimental.py
./model/gradcam.py– Core GradCAM logic./model/yolov7_object_detector.py– YOLOv7 wrapper for GradCAMmain_gradcam.py– Main script to run GradCAM visualizations
Example output below shows the GradCAM visualization overlaying the detection result, helping interpret what the YOLOv7 model "sees".
- Add detailed code explanations and annotations
- Extend to other YOLO versions (e.g., v5, v8)
- Combine with other XAI tools (e.g., Score-CAM, LIME)
- YOLOv7 original repo: https://github.com/WongKinYiu/yolov7
- GradCAM reference: https://arxiv.org/abs/1610.02391


