UAV-GradCAM-Lite
Spatial attention and Grad-CAM heat maps showing exactly where a model looks in an aerial frame.
WHAT IT DOES
- PyTorch proof-of-concept demonstrating Spatial Attention mechanisms and Grad-CAM interpretability on UAV aerial datasets
- Implemented CBAM-style attention blocks for small object detection from aerial perspectives
- Generated heat-map visualizations showing model focus on target clusters vs background noise
- Built as open-source proxy to demonstrate architectural competencies for research applications
- Designed for integration into Feature Pyramid Networks (FPN) of YOLO/Faster R-CNN architectures
Python
Language
1
Stars
0
Forks
Apr 2026
Last push
FROM THE REPOSITORY
README
UAV-Attention-GradCAM-Lite
A lightweight PyTorch implementation demonstrating Spatial Attention mechanisms and Grad-CAM interpretability for UAV imagery analysis. Built as an open-source demonstration of architectural competencies used in commercial computer vision projects.
Visual Results
Grad-CAM Heatmap Visualization
| Original Image | Grad-CAM Heatmap |
![]() |
![]() |
![]() |
![]() |
Additional Examples
| Original Image | Grad-CAM Heatmap |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
The heatmaps highlight regions of the image that the model focuses on during inference, providing interpretability for the attention mechanism.
Overview
This project implements:
- CBAM-style Spatial Attention: Integrated after layer4 in a pre-trained ResNet18 backbone
- Grad-CAM Visualization: Generates class activation maps from attention feature maps
- UAV Image Processing: Optimized for aerial imagery analysis
Repository Structure
UAV-Attention-GradCAM-Lite/
├── assets/ # Input images and generated heatmaps
├── spatial_attention.py # Spatial attention module implementation
├── gradcam_engine.py # Grad-CAM computation and visualization
├── requirements.txt # Python dependencies
└── README.md # Documentation
Installation
Prerequisites
- Python 3.7+
- pip or conda package manager
Setup
- Clone the repository:
git clone https://github.com/HoneyBadger-010/UAV-GradCAM-Lite.git
cd UAV-GradCAM-Lite
- Create a virtual environment (recommended):
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
- Install dependencies:
pip install --extra-index-url https://download.pytorch.org/whl/cpu torch torchvision opencv-python numpy matplotlib
Or use the requirements file:
pip install -r requirements.txt
Usage
Run the Grad-CAM engine on images in the assets/ directory:
python gradcam_engine.py
The script will:
- Load a pre-trained ResNet18 model with spatial attention
- Process all
.jpgand.pngimages in./assets/ - Generate Grad-CAM heatmap overlays
- Save results with
_heatmapsuffix
Example Output
For an input file assets/example.jpg, the script generates:
assets/example_heatmap.jpg
How It Works
Spatial Attention Module
The spatial_attention.py module implements a CBAM-inspired spatial attention mechanism:
- Applies max and average pooling across the channel dimension
- Generates spatial attention maps through a convolutional layer
- Multiplies attention weights with input features
Grad-CAM Engine
The gradcam_engine.py script:
- Preprocesses images (resize, normalize with ImageNet statistics)
- Performs forward pass through the attention-enhanced ResNet
- Computes gradients with respect to the predicted class
- Generates class activation maps from attention features
- Overlays heatmaps on original images using OpenCV
Technical Details
- Model: ResNet18 with spatial attention after layer4
- Input Size: 224x224 pixels
- Normalization: ImageNet mean and standard deviation
- Heatmap: ReLU-activated weighted combination of feature maps
- Overlay: 50% alpha blending with JET colormap
Applications
- UAV/drone imagery analysis
- Object detection interpretability
- Attention mechanism visualization
- Model debugging and validation
License
This project is open-source and available for educational and research purposes.
Acknowledgments
- PyTorch team for the deep learning framework
- ResNet architecture from "Deep Residual Learning for Image Recognition"
- CBAM attention mechanism from "Convolutional Block Attention Module"
- Grad-CAM from "Grad-CAM: Visual Explanations from Deep Networks"













