← All answersEvaluation & deployment

How do you detect and handle severe class imbalance in a custom image dataset?

Short answer

First, detect it: count labelled instances per class and look at per-class recall - a rare class with low recall is the tell. Then handle it with a mix of collecting more examples of the rare class, oversampling those images (or undersampling common ones), targeted augmentation, and weighting the loss toward rare classes. Finally, judge the model with per-class metrics and F1, not overall accuracy, which the majority class will flatter.

Detecting it

  • Count instances per class - not images, instances. A 100:1 ratio is a red flag.
  • Read per-class recall/AP after a baseline run; the starved classes stand out.
  • Watch the confusion matrix for a rare class being swallowed by a common one.

Handling it

  • More data: the most reliable fix is simply collecting more of the rare class.
  • Resampling: oversample images containing rare classes, or undersample the dominant ones.
  • Class weights / focal-style losses: make each rare-class mistake cost more.
  • Targeted augmentation: augment or even copy-paste rare objects into more scenes.

Whatever you do, evaluate on a validation set that reflects reality and report per-class numbers. A model at 98% overall accuracy that never once detects your critical rare class is a failure dressed up as a success.

Stuck on this in a real project?

Book a free call and I’ll give you a straight answer on your specific case.

Book a consultation