🌾
Real-world scenario
Samuel at AgriSense (Kisumu, Kenya): building a crop disease classifier from 2,000 phone photos. Small dataset + augmentation = production model.

Samuel is an agricultural data scientist at AgriSense in Kisumu, Kenya. His task: classify crop diseases (3 types) from low-resolution phone photos taken by farmers in the field. Challenge: only 2,000 labeled images total (6 months of farmer uploads). Using batch size 32, his accuracy reaches 87.24%. Without augmentation, the model overfits—training loss 0.2542, validation loss explodes to 0.6+ (poor generalization). Adding augmentation (horizontal flip, rotation, crop) increases training loss to 0.4068 (learning slower) but validation loss drops to 0.48 (robust to unseen variations). Batching and augmentation are the lifeline for real-world small datasets.

2,000
Images (small)
KSh 450K
Monthly budget
87.24%
Accuracy (batch 32)

PyTorch Dataset & DataLoader: the training pipeline foundation

Why batching? Training on one sample at a time is slow. Training on all 2,000 at once requires huge memory. Batching (e.g., 32 samples per step) balances speed and stability. Gradients average across the batch, reducing noise and improving convergence.

Dataset class: Wraps your data (images, labels). Implements __len__ (total samples) and __getitem__ (fetch one sample + apply transforms).

DataLoader: Iterator that combines Dataset with batching, shuffling, and multi-worker loading. Transparently feeds batches to your training loop.

Transforms: Preprocessing applied per-sample. Training transforms include augmentation (RandomFlip, Rotate); validation transforms only normalize (no augmentation).

Creating a custom Dataset class

Start with a simple Dataset wrapping a CSV (image paths + labels). Fashion-MNIST example (60K training, 10K test, 28x28 grayscale, 10 classes):

custom_dataset.py Custom Dataset class for Fashion-MNIST