A complete, from-scratch implementation of YOLOv1 (You Only Look Once: Unified, Real-Time Object Detection) in PyTorch — engineered to train on a single consumer-grade GPU.
The original YOLOv1 paper pre-trains its backbone on ImageNet-1K before fine-tuning on detection tasks, requiring significant compute resources. This implementation takes a different approach:
| Design Decision | Original Paper | This Implementation |
|---|---|---|
| Backbone pre-training | ImageNet-1K (1000 classes) | None — trained end-to-end from scratch |
| FC layer width | 4096 neurons | 496 neurons (≈ 8× reduction) |
| Target hardware | Multi-GPU cluster | Single low-end GPU |
| Dataset | VOC + ImageNet | PASCAL VOC only |
The fully connected layer was intentionally reduced from 4096 → 496 neurons to dramatically lower VRAM usage and parameter count, enabling training on hardware with a single GPU.
The model follows the Darknet-inspired convolutional backbone described in the YOLOv1 paper, with modifications to the detection head:
Input Image (448×448×3)
│
▼
┌─────────────────────┐
│ 24 Conv Layers │ Backbone: Conv → BatchNorm → LeakyReLU(0.1)
│ (Darknet-style) │ with MaxPool downsampling
└─────────────────────┘
│
▼
┌─────────────────────┐
│ Fully Connected │ Flatten → Linear(1024·S·S, 496) → Dropout(0.5)
│ Detection Head │ → LeakyReLU(0.1) → Linear(496, S·S·30)
└─────────────────────┘
│
▼
Output: 7×7×30 tensor
(class probabilities + bounding box predictions)
Grid configuration: S=7, B=2 boxes per cell, C=20 PASCAL VOC classes.
├── model.py # YOLOv1 architecture (Darknet backbone + detection head)
├── train.py # Training loop with mAP evaluation per epoch
├── loss.py # Multi-part YOLO loss (coord + object + no-object + class)
├── dataset.py # Custom PyTorch Dataset for PASCAL VOC format
├── utils.py # IoU, NMS, mAP, bbox conversion, and visualization
└── README.md
model.py— DefinesYOLOV1andCNNBlockmodules. The architecture is driven by a declarative config list (architecture_config), making the layer structure easy to read and modify.train.py— Orchestrates end-to-end training: data loading, forward/backward pass with Adam optimizer, and per-epoch mAP tracking on the training set.loss.py— Implements the multi-part YOLOv1 loss function with λ-weighted terms for coordinate regression, objectness confidence, no-object penalty, and class prediction (MSE-based).dataset.py— Loads PASCAL VOC-format annotations and encodes ground-truth bounding boxes into theS×S×30label matrix required by the network.utils.py— A collection of core object detection utilities:intersection_over_union()— IoU computation (midpoint and corner formats)non_max_suppression()— Post-processing to filter redundant detectionsmean_average_precision()— mAP evaluation across all 20 classesconvert_cellboxes()/cellboxes_to_boxes()— Grid-relative → image-relative coordinate conversionplot_image()— Bounding box visualization with Matplotlibsave_checkpoint()/load_checkpoint()— Model persistence
- Python 3.8+
- PyTorch ≥ 1.9 with CUDA support
- torchvision, pandas, numpy, matplotlib, tqdm
pip install torch torchvision pandas numpy matplotlib tqdm- Download the PASCAL VOC dataset (images + label
.txtfiles in YOLO format). - Prepare CSV annotation files mapping image filenames to label filenames.
- Update
IMG_DIRandLABEL_DIRintrain.pyto point to your dataset paths.
python train.pyAll training hyperparameters are defined at the top of train.py for easy tuning:
| Parameter | Default | Description |
|---|---|---|
LEARNING_RATE |
2e-5 |
Adam optimizer learning rate |
BATCH_SIZE |
8 |
Samples per batch (lower if GPU OOM) |
EPOCH |
100 |
Total training epochs |
NUM_WORKERS |
2 |
DataLoader parallel workers |
WEIGHT_DECAY |
0 |
L2 regularization factor |
The loss in loss.py follows the original YOLOv1 formulation with four components:
Loss = λ_coord · (box coordinate loss)
+ (object confidence loss)
+ λ_noobj · (no-object confidence loss)
+ (class prediction loss)
Where λ_coord = 5 and λ_noobj = 0.5, as specified in the paper. Square root is applied to width/height predictions for scale normalization.
- YOLOv1 Paper: You Only Look Once: Unified, Real-Time Object Detection — J. Redmon et al., CVPR 2016.
- Utility functions in
utils.pywere adapted from various open-source PyTorch object detection resources.
Built with PyTorch 🔥