Object detection model that distinguishes between cows and buffalos using Faster R-CNN (ResNet50-FPN V2) with PyTorch Lightning.
One of the learning tasks from my AI Lead as part of transitioning from QA to AI
This project trains a Faster R-CNN model to detect and classify cattle in images into two categories:
- Cow
- Buffalo
The model uses transfer learning from COCO-pretrained weights and is fine-tuned on a cattle-specific dataset.
| Component | Details |
|---|---|
| Backbone | ResNet50-FPN V2 (pretrained on COCO) |
| Detection Head | FastRCNNPredictor (3 classes: background, cow, buffalo) |
| Framework | PyTorch Lightning |
| Optimizer | AdamW (lr=1e-4, weight_decay=1e-4) |
| Scheduler | ReduceLROnPlateau (patience=3) |
| Precision | Mixed FP16 (on GPU) |
Uses the Cows and Buffalo Computer Vision Dataset from Kaggle with YOLO-format annotations.
- Split: 70% train / 15% val / 15% test
- Augmentation: 2x training set via albumentations (flips, color jitter, blur, noise, rotation, etc.)
- Image size: 512×512
- Platform: Kaggle — NVIDIA T4 16GB GPU
- Batch size: 4 (with gradient accumulation x2 -> effective BS of 8)
- Max epochs: 30
- Early stopping: patience 7
- Checkpointing: top-3 models by val_loss
The model is evaluated using:
- COCO-style mAP (IoU=0.50:0.95, IoU=0.50, IoU=0.75)
- Per-class mAP (cow, buffalo)
- Classification metrics at IoU≥0.5: Precision, Recall, F1 (macro & weighted)
- Confusion matrix
some bullshit
install requirements.txt duh...Then open cattle_det/cattle-detection.ipynb and run the cells sequentially. The model was trained on Kaggle GPU T4x2 GPU