Author: Kunal Agarwal
Course: ECE 57000 - Artificial Intelligence
Institution: Purdue University
Date: Fall 2025
Project Track: TinyReproductions
- Project Overview
- Code Attribution
- Project Structure
- Dependencies
- Dataset Setup
- Installation Instructions
- Running the Project
- Expected Results
- Troubleshooting
- Acknowledgments
This project is a TinyReproduction of the paper:
The primary goal is to experimentally verify the central claim of the DiffusionDet paper: that object detection can be successfully framed as a denoising diffusion process. We reproduce this claim in a simplified setting by comparing DiffusionDet against a Faster R-CNN baseline on the PASCAL VOC 2007 dataset.
To make this project feasible within a semester on a single GPU:
- Dataset: PASCAL VOC 2007 (instead of MS COCO)
- Backbone: ResNet-50 (instead of larger transformers)
- Training Schedule: 50 epochs (instead of 100+)
- Number of Proposals: 300 (instead of 500)
- Diffusion Steps: 1000 (same as original)
The following code was written entirely by me for this project inspired by DiffusionDet and related works:
-
models/diffusion_det.py(Lines 1-344)- Complete DiffusionDet implementation
- Forward diffusion process (q_sample)
- Reverse diffusion process (inference)
- Loss computation functions
- Helper classes: MLP, SinusoidalTimeEmbedding, PositionalEncoding2D
-
utils/voc_dataset.py(Lines 1-115)- VOCDataset class implementation
- XML annotation parsing
- Custom collate function
-
train.py(Lines 1-230)- Training loop for DiffusionDet
- Optimizer configuration
- Checkpoint saving logic
- Loss tracking and visualization
-
evaluate.py(Lines 1-240)- Complete mAP@0.5 evaluation implementation
- IoU calculation
- Precision-Recall curve computation
- Average Precision calculation
-
train_faster_rcnn.py(Lines 1-140)- Training script for baseline model
Source: https://github.com/ShoufaChen/DiffusionDet (Apache 2.0 License)
Adaptations:
-
models/diffusion_det.py, Lines 57-74: Cosine beta schedule- Original: Used in official implementation with Detectron2
- Adapted: Simplified to PyTorch-only implementation, removed Detectron2 dependencies
-
models/diffusion_det.py, Lines 133-160: Box embedding and time embedding combination- Original: Core architecture from DiffusionDet paper
- Adapted: Simplified decoder architecture, removed proposal matching
Source: "Denoising Diffusion Probabilistic Models" (arXiv:2006.11239)
Adaptations:
-
models/diffusion_det.py, Lines 91-99: Forward diffusion (q_sample)- Original: DDPM forward process for images
- Adapted: Applied to bounding box coordinates instead of pixels
-
models/diffusion_det.py, Lines 170-185: Reverse diffusion sampling- Original: DDPM reverse process
- Adapted: Simplified sampling (removed variance scheduling for faster inference)
diffusiondet_reproduction/
│
├── README.md # This file
├── requirements.txt # Python dependencies
│
├── models/
│ ├── __init__.py # Empty init file
│ └── diffusion_det.py # DiffusionDet model implementation
│ # - DiffusionDet class (main model)
│ # - MLP helper class
│ # - SinusoidalTimeEmbedding
│ # - PositionalEncoding2D
│
├── utils/
│ ├── __init__.py # Empty init file
| ├── diffusion_util.py # Diffusion utility functions
│ └── voc_dataset.py # PASCAL VOC dataset loader
│ # - VOCDataset class
│ # - get_transform function
│ # - collate_fn function
|
├── train.py # Training script for DiffusionDet
├── train_faster_rcnn.py # Training script for Faster R-CNN baseline
├── evaluate.py # Evaluation script (mAP@0.5)
│
├── download_voc.py # Dataset download script
├── verify_dataset.py # Dataset verification script
│
├── checkpoints/ # Model checkpoints (auto-created)
│ ├── best_model.pth # Best DiffusionDet model
│
└── checkpoints_frcnn/ # Faster R-CNN checkpoints (auto-created)
└── best_model.pth # Best baseline model
models/diffusion_det.py: Complete implementation of DiffusionDet, including the diffusion process, transformer decoder, and prediction heads.utils/voc_dataset.py: Custom PyTorch Dataset class for loading PASCAL VOC 2007 with proper box normalization.
train.py: Main training script for DiffusionDet with loss tracking and checkpoint saving.train_faster_rcnn.py: Training script for the Faster R-CNN baseline for comparison.evaluate.py: Evaluation script that computes mAP@0.5 metric on the test set.
download_voc.py: Automated script to download PASCAL VOC 2007 dataset.verify_dataset.py: Verification script to ensure dataset is properly downloaded and structured.
- OS: Linux (Ubuntu 20.04+), macOS, or Windows with WSL
- GPU: NVIDIA GPU with 2GB+ VRAM (recommended)
- CUDA 11.8 or 12.1
- Can run on CPU but training will be very slow (~20x slower)
- RAM: 8GB+ recommended
- Storage: ~10GB free space (dataset + checkpoints)
- Python 3.8, 3.9, or 3.10 (tested on 3.10)
All dependencies are listed in requirements.txt:
torch>=2.0.0
torchvision>=0.15.0
numpy>=1.24.0
Pillow>=9.5.0
matplotlib>=3.7.0
tqdm>=4.65.0| Library | Version | Purpose |
|---|---|---|
| torch | ≥2.0.0 | Deep learning framework |
| torchvision | ≥0.15.0 | Computer vision utilities, Faster R-CNN |
| numpy | ≥1.24.0 | Numerical operations |
| Pillow | ≥9.5.0 | Image loading and processing |
| matplotlib | ≥3.7.0 | Training curve visualization |
| tqdm | ≥4.65.0 | Progress bars |
No additional dependencies are required. The project uses only standard, well-maintained libraries.
The PASCAL VOC 2007 dataset can be downloaded automatically:
python3 download_voc.pyThis script will:
- Download
VOCtrainval_06-Nov-2007.tar(~439 MB) - Download
VOCtest_06-Nov-2007.tar(~430 MB) - Extract both archives to
data/VOCdevkit/VOC2007/ - Verify the dataset structure
Download mirrors (script tries in order):
- Official host: http://host.robots.ox.ac.uk/pascal/VOC/voc2007/
- Mirror: https://pjreddie.com/media/files/
Total download time: 5-15 minutes depending on connection speed.
If the automatic download fails:
-
Visit: https://pjreddie.com/projects/pascal-voc-dataset-mirror/
-
Download these files:
VOCtrainval_06-Nov-2007.tarVOCtest_06-Nov-2007.tar
-
Place in project directory:
mv VOCtrainval_06-Nov-2007.tar ~/path/to/diffusiondet_reproduction/data/ mv VOCtest_06-Nov-2007.tar ~/path/to/diffusiondet_reproduction/data/
-
Extract:
cd data tar -xf VOCtrainval_06-Nov-2007.tar tar -xf VOCtest_06-Nov-2007.tar
After download, verify the dataset:
python3 verify_dataset.pyExpected output:
✓ Annotations 9963 files
✓ ImageSets/Main 9963 files
✓ JPEGImages 9963 files
Image splits:
train : 2501 images
val : 2510 images
trainval : 5011 images
test : 4952 images
✓ Dataset verification PASSED!
- Dataset: PASCAL VOC 2007
- Task: Object Detection
- Classes: 20 object categories (aeroplane, bicycle, bird, boat, bottle, bus, car, cat, chair, cow, diningtable, dog, horse, motorbike, person, pottedplant, sheep, sofa, train, tvmonitor)
- Split:
- Trainval: 5,011 images
- Test: 4,952 images
- Size: ~870 MB (extracted)
git clone https://github.com/KunalA18/diffusiondet_reproduction
cd diffusiondet_reproductionOr if you have the files locally, ensure the directory structure matches the Project Structure section.
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
source venv/bin/activate # On Linux/Mac
# OR
venv\Scripts\activate # On WindowsFor CUDA 12.1 (most common):
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu121For CUDA 11.8:
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu118For CPU only (not recommended):
pip3 install torch torchvisionVerify GPU availability:
python3 -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}')"pip install -r requirements.txtpython3 download_voc.pyWait for download and extraction to complete (~10-15 minutes).
# Verify imports
python3 -c "from models.diffusion_det import DiffusionDet; print('✓ Model imports OK')"
python3 -c "from utils.voc_dataset import VOCDataset; print('✓ Dataset imports OK')"
# Verify dataset
python3 verify_dataset.pyIf all checks pass, you're ready to train! ✅
Before running the full training (which takes hours), do a quick test:
-
Edit
train.pyand change:config = { 'num_epochs': 2, # Changed from 50 'batch_size': 4, # Changed from 8 'num_proposals': 100, # Changed from 300 ... }
-
Run:
python3 train.py
-
Expected: Training should start and complete 2 epochs in ~5-10 minutes. This verifies everything works.
python3 train.pyConfiguration (in train.py):
- Epochs: 50
- Batch size: 8
- Learning rate: 1e-4
- Optimizer: AdamW
- Scheduler: Cosine annealing
Expected time:
- With GPU (RTX 3090/4090): ~4-6 hours
- With GPU (GTX 1080 Ti): ~8-10 hours
- With CPU: Not recommended (~80+ hours)
Output:
- Checkpoints saved to
checkpoints/ - Best model:
checkpoints/best_model.pth - Training curves:
checkpoints/training_curves.png
Console output:
Using device: cuda
Training samples: 5011
Creating model...
Total parameters: 45,234,567
Trainable parameters: 45,234,567
Starting training...
============================================================
Epoch 1/50: 100%|████████| 627/627 [04:32<00:00, 2.30it/s]
Epoch 1/50
Total Loss: 5.2341
BBox Loss: 1.8234
Class Loss: 2.1456
GIoU Loss: 1.2651
------------------------------------------------------------
...
python3 train_faster_rcnn.pyConfiguration:
- Epochs: 50
- Batch size: 8
- Learning rate: 5e-3
- Optimizer: SGD with momentum
Expected time: ~3-4 hours on GPU
Output:
- Checkpoints saved to
checkpoints_frcnn/ - Best model:
checkpoints_frcnn/best_model.pth
Evaluate DiffusionDet:
python3 evaluate.py \
--checkpoint checkpoints/best_model.pth \
--data-path data/VOCdevkit/VOC2007 \
--batch-size 8Evaluate Faster R-CNN:
python3 evaluate.py \
--checkpoint checkpoints_frcnn/best_model.pth \
--data-path data/VOCdevkit/VOC2007 \
--batch-size 8Expected output:
============================================================
Evaluation Results
============================================================
mAP@0.5: 45.23%
Per-class AP:
aeroplane : 52.34%
bicycle : 43.21%
bird : 38.56%
...
tvmonitor : 49.87%
============================================================
GPU Usage:
watch -n 1 nvidia-smiTraining Progress:
- Watch console for loss values
- Loss should decrease steadily
- Check
checkpoints/training_curves.pngperiodically
Checkpoints:
ls -lh checkpoints/
# Output:
# best_model.pth # Best model (lowest loss)
# checkpoint_epoch_10.pth # Saved every 10 epochs
# checkpoint_epoch_20.pth
# ...
# training_curves.png # Loss plots- Epochs 1-5: Loss ~5-8 (model learning basic features)
- Epochs 10-20: Loss ~2-4 (model converging)
- Epochs 30-50: Loss ~1-2 (fine-tuning)
- Epochs 1-5: Loss ~3-5
- Epochs 10-30: Loss ~1-2
- Epochs 30-50: Loss ~0.5-1
| Model | Backbone | mAP@0.5 | Status |
|---|---|---|---|
| Faster R-CNN (Baseline) | ResNet-50 | 45.1% | Expected range |
| DiffusionDet (Ours) | ResNet-50 | 48.2% | Expected range |
Success Criteria: DiffusionDet achieves comparable performance (within +3.1% mAP) to Faster R-CNN baseline.
---
## Troubleshooting
### Common Issues and Solutions
#### 1. CUDA Out of Memory
**Error**: `RuntimeError: CUDA out of memory`
**Solutions**:
```python
# In train.py, reduce batch size:
'batch_size': 4, # or even 2
# Or reduce number of proposals:
'num_proposals': 100, # instead of 300
Error: ModuleNotFoundError: No module named 'models'
Solution:
# Ensure __init__.py files exist:
touch models/__init__.py utils/__init__.py
# Or add to Python path:
export PYTHONPATH="${PYTHONPATH}:$(pwd)"Error: FileNotFoundError: [Errno 2] No such file or directory: 'data/VOCdevkit/VOC2007/ImageSets/Main/trainval.txt'
Solution:
# Re-run dataset download:
python3 download_voc.py
# Or verify structure:
python3 verify_dataset.pyIssue: Training taking 80+ hours
Solution:
- Use Google Colab (free GPU): https://colab.research.google.com
- Reduce dataset size for testing:
# In utils/voc_dataset.py, line 32, add: self.ids = self.ids[:100] # Use only 100 images
Issue: Loss stays high after 10 epochs
Causes and Solutions:
- Learning rate too high: Reduce to 5e-5
- Gradient explosion: Check gradient clipping (in train.py)
- Data loading issue: Verify boxes are normalized to [0,1]
Possible Causes:
- Didn't train long enough (train for full 50 epochs)
- Learning rate not optimal
- Bug in evaluation code (double-check IoU calculation)
If issues persist:
- Check error message carefully - most issues are path/import related
- Verify each component:
python3 -c "from models.diffusion_det import DiffusionDet; m = DiffusionDet(); print('Model OK')" python3 -c "from utils.voc_dataset import VOCDataset; d = VOCDataset('data/VOCdevkit/VOC2007'); print('Dataset OK')"
- Simplify to isolate problem:
- Train for 1 epoch with batch_size=1
- Test on 10 images only
- Check GPU memory:
nvidia-smi - Review logs: Check console output for warnings
-
DiffusionDet Paper:
- Chen, S., et al. (2023). "DiffusionDet: Diffusion Model for Object Detection." ICCV 2023.
- Official Implementation: https://github.com/ShoufaChen/DiffusionDet
-
DDPM Paper:
- Ho, J., et al. (2020). "Denoising Diffusion Probabilistic Models." NeurIPS 2020.
- Paper: https://arxiv.org/abs/2006.11239
-
Faster R-CNN:
- Ren, S., et al. (2015). "Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks." NeurIPS 2015.
- PASCAL VOC 2007:
- Everingham, M., et al. "The PASCAL Visual Object Classes Challenge 2007 (VOC2007) Results."
- http://host.robots.ox.ac.uk/pascal/VOC/voc2007/
- PyTorch: https://pytorch.org/
- Torchvision: https://pytorch.org/vision/
- Claude (Anthropic): AI assistant for implementation guidance
This project is for educational purposes as part of a university course project.
- Code License: MIT License (for original code written by me)
- Dataset: PASCAL VOC 2007 is released for research purposes
- Adapted Code: Retains original licenses (Apache 2.0 for DiffusionDet components, BSD for PyTorch components)