Course project · MYZ 309EAI in Mathematical Engineering · May 2026

Loss functions and sampling for imbalanced skin-lesion classification

A controlled comparison of cross-entropy, weighted CE, focal loss, class-balanced CE and re-sampling on HAM10000, read through metrics that a clinical task needs.

Deep learningClass imbalanceFocal lossPyTorchMedical imaging
Per-class recall of the five configurations.

Overview

On the HAM10000 dermoscopic dataset the majority class, melanocytic nevi, is 67 % of the data and dermatofibroma only 1.1 %. A model trained naively with cross-entropy reaches high accuracy by mostly predicting the majority class, and misses exactly the rare classes that matter clinically.

This study holds the CNN, optimiser, split and seed fixed and changes only the loss function or the sampling strategy. It then reports macro-F1, balanced accuracy and per-class recall instead of accuracy alone.

The configuration with the best accuracy (standard CE, 73.6 %) has the worst balanced accuracy and never detects dermatofibroma. Focal loss gives the best macro-F1; re-sampling buys the highest melanoma recall at the cost of over-predicting minority classes.

What I built

  • A 106 391-parameter CNN trained from scratch on a stratified 70/15/15 split.
  • Five configurations: CE, inverse-frequency weighted CE, focal loss (γ = 2), class-balanced CE (effective number of samples) and a balanced random sampler.
  • Confusion matrices, per-class recall heat maps, error analysis and an annotated over-fitting study; report, slides and two interactive HTML briefs.

Results

ConfigurationMacro-F1Balanced acc.AccuracyRecall melRecall df
Standard CE0.3810.3520.7360.290.00
Weighted CE0.3410.5050.5210.490.06
Focal loss (γ = 2)0.4430.5380.6220.620.39
Class-balanced CE0.3950.4470.7180.320.06
CE + balanced sampler0.3910.5370.4640.780.39

Held-out test set of 1 503 images.

Figures

Run it

$ pip install -r requirements.txt
$ jupyter notebook notebook/MYZ309E_HAM10000_Project.ipynb
$ python src/train_real.py "Focal (gamma=2)" 8