Class imbalance

Class imbalance is a dataset in which some classes have far more examples than others, so a model trained on it favors the frequent classes and recognizes the rare ones poorly. In Supervisely, project statistics count objects per class, and the evaluation report gives per-class metrics after training.

Tasks and training

Where it hides

  • Between classes: thousands of cars, a few dozen cyclists.
  • Between object and background: most candidate boxes of a detector are empty, and most pixels of a defect image are not defect.
  • Inside one class: the class is common, but its night-time or rear-view examples are rare.

Why accuracy misleads

Take 1,000 inspection images, 50 of them with a defect. A model that always answers "no defect" is 95% accurate and finds nothing. Per-class precision and recall, or the F1 score of the rare class, expose it at once.

Ways to correct it

  • Find and label more of the rare cases, the most reliable fix. Active learning points at the images worth labeling next.
  • Oversample rare classes or undersample frequent ones while building training batches.
  • Weight the loss so mistakes on rare classes cost more, or use focal loss, which down-weights easy examples.
  • Augment or synthesize examples of the rare class.
  • Tune the confidence threshold per class instead of one threshold for all.
FL(pₜ) = −(1 − pₜ)^γ · log(pₜ)
Focal loss from the RetinaNet paper: pₜ is the predicted probability of the true class and γ ≥ 0 controls how much well-classified examples are discounted; γ = 0 gives ordinary cross-entropy.

Side effects

Repeating the same few rare images many times invites overfitting. Balancing the validation set makes its scores look better than production, where the classes stay uneven; keep validation close to the real mix.

Spotting it in Supervisely

Project statistics count objects per class, and every number opens the images behind it. AI Search finds rare scenes from a text prompt or an example. After training, the per-class metrics of the evaluation report show which classes the imbalance cost. All of this sits on the data curation side of the platform.

Frequently asked questions.

How do you fix class imbalance?

Collect or label more images of the rare classes, oversample them or undersample the common ones, weight the loss by class, or use a loss such as focal loss. Then measure per-class precision and recall, not only overall accuracy.

Does SMOTE work for images?

SMOTE creates synthetic examples by interpolating feature vectors, which suits tabular data. For images, augmentation, oversampling and class-weighted losses are the usual tools.