← Praruj Thapa
/
Project · Medical imaging · 2026

Classifying blood cells with a Vision Transformer

Many blood tests still include a person sorting white cells by eye under a microscope. I fine-tuned a pretrained Vision Transformer to sort them into eight types from 46,232 microscope images, on a free Colab GPU in 40 minutes, then checked where it goes wrong, not just how often it is right.

98.4%
test accuracy: 4,551 of 4,624 held-out images
98.1%
macro F1, which weights the rare cell types as much as the common ones
73
errors in 4,624 test images, and just two mix-ups account for 31 of them
Where it goes wrong

Rows are the true cell type, columns what the model said. Everything on the diagonal is right. The orange cells are the mistakes, and they are not spread evenly: 17 neutrophils called plasma cells and 14 monocytes called lymphocytes are 31 of the 73 errors.

Swipe sideways to see every column →
BasEosEryLymMonNeuPlaPltBasophil1511Eosinophil172119Erythroblast155Lymphocyte278911Monocyte1451322Neutrophil16221,70917Plasma cell5122279Platelet1234PREDICTED →TRUE ↓

Monocytes and lymphocytes are both round white cells with a single nucleus, a pair people mix up too. Basophils are the rarest type here (152 test images) and still reach 0.971 F1.

Cell typeTest imagesPrecisionRecallF1
Basophil1520.9500.9930.971
Eosinophil7320.9920.9850.988
Erythroblast1550.9811.0000.990
Lymphocyte7930.9780.9950.986
Monocyte5310.9900.9660.978
Neutrophil1,7370.9920.9840.988
Plasma cell2890.9360.9650.951
Platelet2351.0000.9960.998

Plasma cells have the lowest precision (0.936): when the model says “plasma cell”, about 1 in 16 times it is something else, usually a neutrophil.

Why 98.4% is probably optimistic
The test set is not independent enough. The dataset merges several public collections, and one of them (Kaggle Blood Cell Images) was published as about 12,500 augmented images: altered copies of a much smaller set of originals. A random 80/10/10 split can put one copy of a cell in training and another in test, which flatters the score.

The honest next test is a split by source: train on some collections and test on one the model has never seen, which is also closer to what a new hospital's microscope would look like. That number will be lower, and it is the one that matters.

This is a learning project, not a diagnostic tool. Clinical use would need clinically validated data and regulatory review.

How it was trained

Data

46,232 images of 8 cell types (Hugging Face: ehottl/blood_dataset), split 80/10/10 with class proportions kept in every split, seed 42.

Model

google/vit-base-patch16-224, pretrained on ImageNet, with its 1,000-class head replaced by an 8-class one.

Training

Hugging Face Trainer on a free Colab T4: 4 epochs, batch 32, learning rate 2e-5 with 10% warm-up, mixed precision. Random crops, flips and small rotations for training images only.

Choosing the model

The checkpoint kept is the one with the best validation macro F1, not accuracy, so a model that ignores rare cell types cannot win. All settings live in one config file, so the run can be repeated.

Most of the learning happens early

Validation accuracy jumps from 95.2% to 98.1% between epochs 1 and 2, then gains only 0.4 points over the next two. More epochs would add little; better data (the split above) would matter more.

EpochTrain lossValidation lossValidation accuracyValidation macro F1
10.20830.130795.2%95.9%
20.14220.050598.1%98.2%
30.10850.048098.2%98.2%
40.10960.037098.5%98.5%
What I learned
  • Accuracy hides the rare classes. With 1,737 neutrophils and 152 basophils in the test set, a model can look great while failing the rare cells. Macro F1 and a per-class table keep them visible.
  • Read the confusion matrix, not just the score. The errors cluster in two pairs; that tells you what to fix, where a single number does not.
  • Ask how the data was built. Reading the dataset card is what showed the pre-augmented images, and with them the reason to distrust a random split.
Stack
PyTorchHugging Face TransformersdatasetsevaluatetorchvisionGradioGoogle Colab (T4)