A clean, modular PyTorch implementation of You Only Look Once (YOLO) v1, the pioneering single-shot object detection model. This implementation features a flexible architecture with support for multiple backbones, distributed training, and comprehensive evaluation metrics.
YOLO v1 revolutionized object detection by framing it as a single regression problem, directly predicting bounding boxes and class probabilities from full images in one evaluation. This implementation stays true to the original paper while incorporating modern best practices and engineering improvements.
- ποΈ Modular Architecture: Clean separation of concerns with pluggable backbone networks
- π Transfer Learning: Support for pretrained ResNet50 backbone with configurable freezing
- βοΈ Distributed Training: Seamless integration with Modal for cloud GPU training
- π― Auto Device Detection: Automatically selects optimal device (MPS for Apple Silicon, CUDA for NVIDIA, CPU fallback)
- π Comprehensive Metrics: Full mAP@50:95 evaluation with per-class and size-based analysis
- π§ͺ Production Ready: Extensive test coverage and type hints throughout
- π Training Monitoring: TensorBoard integration for real-time training visualization
Evaluated on PASCAL VOC 2007 test set (4,952 images):
| Metric | Score |
|---|---|
| mAP@50:95 | 21.20% |
| mAP@0.5 | 43.87% |
| mAP@0.75 | 14.33% |
| Precision | 17.47% |
| Recall | 52.27% |
| Class | AP@0.5 | Class | AP@0.5 |
|---|---|---|---|
| Cat | 69.54% | Dog | 67.83% |
| Train | 60.29% | Horse | 61.97% |
| Bus | 50.29% | Bicycle | 54.35% |
| Size | mAP@50:95 | mAP@0.5 | Count |
|---|---|---|---|
| Large (β₯96Γ96) | 26.19% | 56.39% | 8,322 |
| Medium (32Γ32-96Γ96) | 9.25% | 9.94% | 4,126 |
| Small (<32Γ32) | 8.18% | 8.18% | 875 |
Pretrained weights available: Model checkpoint with ResNet50 backbone trained on PASCAL VOC is available for download on π€ Hugging Face.
# Clone the repository
git clone https://github.com/mattiaskvist/yolo-v1.git
cd yolo-v1
# Install dependencies with uv
uv sync# Authenticate (follow prompts)
uv run modal setupMake sure you add KAGGLE_USERNAME and KAGGLE_KEY as secrets in your Modal project for dataset downloads.
# Train with auto device detection (MPS/CUDA/CPU)
uv run src/train.py --epochs 135
# Train on specific device
uv run src/train.py --epochs 135 --device mps # Apple Silicon
uv run src/train.py --epochs 135 --device cuda # NVIDIA GPU
uv run src/train.py --epochs 135 --device cpu # CPU only# Train on Modal with GPU (L4)
uv run modal run -d src/train.py --epochs 135 --remote
# Resume from checkpoint (if interrupted)
uv run modal run -d src/train.py --resume true --epochs 135 --remote# Evaluate model on VOC2007 test set
uv run src/evaluate.py --checkpoint checkpoints/yolo_best.pth
# Evaluate with specific device
uv run src/evaluate.py --checkpoint checkpoints/yolo_best.pth --device mpsNote: if trained on Modal, download the checkpoint from the checkpoints/ directory in your Modal project.
Pretrained weights: Download the ResNet50 checkpoint from π€ Hugging Face and place it in checkpoints/.
# Run inference on images
uv run src/predict.py --checkpoint checkpoints/yolo_best.pth --image-dir path/to/images/
# Adjust confidence and NMS thresholds
uv run src/predict.py \
--checkpoint checkpoints/yolo_best.pth \
--image-dir path/to/images/ \
--conf-threshold 0.3 \
--nms-threshold 0.4- Grid Size (S): 7Γ7
- Bounding Boxes per Cell (B): 2
- Classes (C): 20 (PASCAL VOC)
- Input Size: 448Γ448 RGB
- Output: 7Γ7Γ30 tensor (per cell: 2 boxes Γ 5 predictions + 20 class probabilities)
-
YOLOv1Backbone: Original architecture from the paper
- 24 convolutional layers
- Alternating 1Γ1 and 3Γ3 convolutions
- Leaky ReLU activation (Ξ±=0.1)
-
ResNetBackbone: Transfer learning with ResNet50
- Pretrained on ImageNet
- Configurable layer freezing
- Faster convergence
- Weights available on π€ Hugging Face
Training and evaluation use PASCAL VOC 2007 and 2012 datasets:
- Training: VOC 2007 trainval + VOC 2012 trainval (~16,551 images)
- Validation: VOC 2007 test (4,952 images)
- Classes: 20 object categories (person, car, dog, etc.)
Datasets are automatically downloaded from Kaggle during first training.
This project is licensed under the MIT License - see the LICENSE file for details.
If you use this project in your research, please cite the original YOLO paper:
@article{Redmon_2016_CVPR,
author = {Redmon, Joseph and Divvala, Santosh and Girshick, Ross and Farhadi, Ali},
title = {You Only Look Once: Unified, Real-Time Object Detection},
booktitle = {Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
month = {June},
year = {2016}
}