Class imbalance wrecks model performance in ways that standard accuracy metrics hide. A model trained on a dataset with 95% background tiles and 5% defect images will learn to predict "no defect" almost every time, score 95% accuracy, and still be useless in production. This tutorial breaks down why that happens and what to do about it.
What This Tutorial Covers
- How imbalanced class distributions distort training and inflate accuracy scores
- Oversampling minority classes to rebalance the dataset
- Undersampling majority classes to reduce dominance
- Applying weighted loss functions to penalize misclassification of rare classes
- Using data augmentation to generate synthetic minority samples
- How focal loss redirects training signal toward hard-to-classify examples
When Class Imbalance Bites
The problem shows up anywhere one class vastly outnumbers the others. Rare defect detection in manufacturing (1 defect per 500 parts). Uncommon species identification in wildlife surveys. Unusual event detection in security footage. Medical imaging where pathologies appear in a small fraction of scans. If your model performs well on aggregate metrics but fails on the classes that actually matter, the training data distribution is the first place to look.
What Makes Focal Loss Different
Standard cross-entropy loss treats all correctly classified samples equally. Focal loss adds a modulating factor that down-weights easy examples and focuses the training signal on hard negatives. This works especially well in object detection, where background patches outnumber foreground objects by orders of magnitude.

