Skip to content

Implementation of small object detection using Detectron2 with a Faster R-CNN (ResNet-50 FPN) backbone. The project fine-tunes a COCO-pretrained model on a custom vehicle dataset.

mbsoroush/SmallObjectDetection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“˜ Small Object Detection with Detectron2

Python Detectron2 DeepLearning


πŸ”° Overview

This project focuses on detecting small objects in images using deep learning techniques with Detectron2.
Small object detection remains a challenging task due to scale variation, occlusion, and limited pixel information β€” yet it’s essential for:

  • πŸš— Traffic and surveillance analysis
  • πŸ›°οΈ Aerial and satellite imaging
  • 🩺 Medical image interpretation
  • πŸ€– Autonomous vehicles and robotics

The notebook walks through all key stages: dataset preparation, model configuration, training, and evaluation.


πŸ“¦ Dataset

This project is dataset-agnostic β€” any COCO-style annotated dataset with small object instances can be used.

Example dataset structure:

dataset/
β”œβ”€β”€ train/
β”‚   β”œβ”€β”€ images/
β”‚   └── annotations.json
β”œβ”€β”€ val/
β”‚   β”œβ”€β”€ images/
β”‚   └── annotations.json

Each annotation file follows the COCO format, including bounding boxes, segmentation masks, and class labels.


βš™οΈ Installation

Clone the repository and install the required dependencies:

git clone https://github.com/your-username/small-object-detection.git
cd small-object-detection
pip install -r requirements.txt

Or install manually:

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
pip install detectron2 opencv-python matplotlib tqdm

πŸš€ Usage

To train and evaluate the model, open the notebook:

jupyter notebook small-object-detection.ipynb

Or, if converted to a Python script:

python train_detectron2.py

🧹 Data Preprocessing

Before training, the input data is preprocessed to ensure quality and consistency:

βœ… Dataset Validation

  • Verify image–annotation alignment
  • Remove empty or corrupted samples

βœ… Resizing and Normalization

  • Scale images while maintaining aspect ratio
  • Normalize pixel intensity for Detectron2’s input format

βœ… Annotation Conversion

  • Convert to COCO JSON format
  • Verify bounding box and category consistency

🧠 Model Configuration & Training

Training is performed using Detectron2, a Facebook AI Research (FAIR) framework.

🧩 Model Selection

  • Base architecture: faster_rcnn_R_50_FPN_3x (ResNet-50 backbone with FPN)
  • Pretrained weights from COCO used for transfer learning

βš™οΈ Training Parameters

  • Learning rate: 0.00025
  • Batch size: 4
  • Iterations: ~5000
  • Augmentation: random flips and resizing

πŸ“ Evaluation Metrics

  • mAP (mean Average Precision) across IoU thresholds
  • Separate evaluation for small, medium, and large objects

πŸ’Ύ Output Checkpoints

  • Model checkpoints saved in /output after every epoch
  • Final model stored as model_final.pth

πŸ“Š Results & Visualization

After training, performance is analyzed both quantitatively and visually.

πŸ“ˆ Evaluation Metrics

  • Example mAP (small objects): ~0.35
  • Example mAP (medium/large): ~0.60

πŸ–ΌοΈ Detection Visualization

Bounding boxes and labels plotted over sample images.
Model predictions compared to ground truth for validation.

Example output visualization:

from detectron2.utils.visualizer import Visualizer
visualizer = Visualizer(image[:, :, ::-1], metadata=metadata)
out = visualizer.draw_instance_predictions(outputs["instances"].to("cpu"))
plt.imshow(out.get_image()[:, :, ::-1])

πŸ§ͺ Troubleshooting

Common issues and fixes:

❌ CUDA Out of Memory

  • Reduce batch size or image resolution

❌ Dataset Registration Error

  • Ensure correct paths and annotation file format in DatasetCatalog.register()

❌ Low Detection on Small Objects

  • Use Feature Pyramid Network (FPN)
  • Apply higher input resolution
  • Consider multi-scale training

πŸš€ Deployment Options

You can deploy the trained model using:

βœ… Local Inference Script

Run inference on an image locally:

python inference.py --image path/to/image.jpg --model output/model_final.pth

βœ… Web or API Deployment

  • Use Flask or FastAPI for REST inference
  • Convert to ONNX or TorchScript for optimized runtime

πŸ“¦ Deliverables

This repository includes:

  • βœ… Jupyter notebook β†’ small-object-detection.ipynb
  • βœ… Model checkpoints β†’ /output/model_final.pth
  • βœ… Visualization samples β†’ /visuals/
  • βœ… Config and training logs β†’ /configs/
  • βœ… Requirements file β†’ requirements.txt

🏁 Conclusion

βœ… Objective Achieved

  • Successfully trained a small-object detector using Detectron2

βœ… Key Features

  • Adaptable to any COCO-style dataset
  • Visual evaluation and mAP metrics integrated

βœ… Impact

  • Enhances object detection for low-visibility and small-scale targets
  • Useful for applications in surveillance, remote sensing, and medical imaging

βœ… Future Work

  • Experiment with custom backbones (e.g., Swin Transformer, ConvNeXt)
  • Explore attention-based architectures for improved small object recall
  • Optimize inference speed with quantization or pruning

βš–οΈ License

This work is licensed under a Creative Commons Attribution–NonCommercial–NoDerivatives 4.0 International License.
Use in CVs, portfolios, or derivative works is not permitted without explicit permission from the author.

About

Implementation of small object detection using Detectron2 with a Faster R-CNN (ResNet-50 FPN) backbone. The project fine-tunes a COCO-pretrained model on a custom vehicle dataset.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published