Computer Vision · Medical Imaging
A skin lesion classifier built to be honest about its accuracy
The Skin Lesion Classifier sorts a dermoscopic photo of a mole or spot into one of the seven HAM10000 lesion types, melanoma and basal cell carcinoma among them, with a calibrated confidence and a plain-language risk level.
I rebuilt it from a Keras notebook into three parts: a PyTorch training pipeline, a FastAPI service that runs the model with ONNX Runtime, and a React app. It is a research and teaching tool, not a diagnosis.
- lesion types, learned from 10,015 dermoscopic images
- 7
- model time per image on a CPU
- ~20 ms
- for an image it has seen before, from the cache
- ~5 ms
- train, validation and test split, made by lesion
- 70 / 15 / 15
What was wrong with the first version
The project started as a Keras notebook: a frozen MobileNetV2 with a small head on top. Going back to it, I found its numbers could not be trusted.
- Data leakage. HAM10000 has 10,015 images of about 7,470 lesions, and splitting by image put photos of the same lesion in both training and validation.
- A broken evaluation. The validation generator shuffled, so predictions were compared with labels in a different order.
- Augmented validation images, and class weights dropped from the main training run, so plain accuracy could hide poor melanoma detection.
So I treated the rebuild as a chance to get one number I could defend, rather than a bigger one.
Splitting by lesion, not by photo
The new pipeline splits with StratifiedGroupKFold on the lesion ID: 70% for training, 15% for validation and 15% for testing, stratified by diagnosis. No lesion appears in two splits, the split files are saved so any run can be reproduced, and the test set is touched once, at the end.
Training for the rare cancers
About 67% of the images are benign moles, so a model can look accurate while missing melanoma. Three choices push back on that:
- Class-weighted cross-entropy (the inverse square root of each class’s frequency) with label smoothing.
- Checkpoints chosen by balanced accuracy, the mean recall over classes, so a rare cancer counts as much as a common mole.
- Augmentation suited to dermoscopy, where a lesion has no "up": random resized crops, horizontal and vertical flips, full rotation and mild colour jitter.
The backbone is an ImageNet-pretrained EfficientNet-B2 at 256 px, fine-tuned end to end with AdamW, a warm-up then cosine decay, mixed precision, gradient clipping, an exponential moving average of the weights and early stopping. At inference, predictions are averaged over flipped views.
Confidence that means something
A medical interface should not say 95% when it means 70%. After training, temperature scaling is fitted on the validation set, so a reported 80% is right about 80% of the time.
The API adds up the probabilities of melanoma, basal cell carcinoma and actinic keratosis into one malignant probability and turns it into a risk level. When the top class is under 50%, the app says the model is unsure instead of naming a type.
Fast on an old CPU
I wanted it to run without a GPU, so I benchmarked backbones in ONNX Runtime on my development machine, a 2015 quad-core Intel Core i5-6500T throttled to about 0.8 GHz during the tests. The numbers moved with the machine’s load, so treat them as rough.
| Backbone, one 224 px image, 4 threads | Median |
|---|---|
| ConvNeXt-Tiny | 150–600 ms |
| ResNet-50 | 48 ms |
| EfficientNet-B2 (chosen) | 19 ms |
| EfficientNet-B0 | 12 ms |
Through the whole API, with decoding and preprocessing, one view takes about 55 ms and the default two flipped views about 140 ms. A repeat upload comes back from the cache in about 5 ms. ONNX Runtime also keeps the runtime near 50 MB, where PyTorch would need about 2 GB.
Built to be served
- Results are cached by model version and the SHA-256 of the image, first in memory and then in Redis, and identical uploads in flight share one inference.
- Uploads are checked for type and size (JPEG, PNG or WebP up to 10 MB), with decompression-bomb protection and EXIF orientation handled.
- Each exported model carries its version, preprocessing, class order, calibration temperature and test metrics, and a test checks that the API preprocesses images exactly as training did.
- Docker Compose runs nginx (the static site and rate limiting), the API and Redis, and CI runs the API, ML and web tests on every push.
Its limits, stated plainly
- HAM10000 is dermoscopic. Ordinary phone photos look different, and accuracy on them will be lower.
- There is no out-of-distribution check yet: a photo of a cat still gets a lesion label, though low confidence is flagged as uncertain.
- The dataset skews toward lighter skin and a European population, and performance on other groups has not been measured.
- HAM10000 is licensed CC BY-NC-SA 4.0, so models trained on it are for non-commercial use.
Hiring for work like this?
I'm open to entry-level software engineering and AI/ML roles, in India or abroad, and I can join immediately. Email is the quickest way to reach me.