Pediatric Chest X-ray Pneumonia Classifier (ViT-base)
Educational project. NOT a medical device. Not for diagnosis or clinical decisions.
A Vision Transformer fine-tuned to classify pediatric (age 1-5) chest X-rays as NORMAL or PNEUMONIA.
Base model: google/vit-base-patch16-224-in21k (Apache-2.0).
Data
Kermany et al. (2018), "Chest X-Ray Images (Pneumonia)", CC BY 4.0, from Guangzhou Women and Children's Medical Center.
- Original train + validation were merged and re-split 80/20 (stratified, grouped by an ID guessed from file names). Train 4,182, validation 1,050.
- The official test set (624 images: 234 normal, 390 pneumonia) was never used for training or model selection.
- No byte-identical images appear across splits. Patient IDs are inferred from file names, not official. Train and test share some guessed IDs, but all 133 shared pneumonia IDs have different bacteria/virus subtypes, which suggests ID reuse rather than the same patient. For normal images this could not be checked.
Training
Two stages: (1) backbone frozen, classification head only (4 epochs, lr 3e-4); (2) full fine-tuning (6 epochs, lr 2e-5). AdamW, cosine schedule with warmup, batch size 32, fp16, class-weighted cross-entropy (NORMAL ~1.93, PNEUMONIA ~0.68), seed 42. Augmentation (train only): random resized crop (0.85-1.0), rotation up to 10 degrees, brightness/contrast jitter. No flips. Best epoch of each stage was chosen by validation AUC.
Results on the official test set (threshold 0.5, 95% bootstrap intervals)
| Model | AUC | Sensitivity | Specificity | Accuracy | F1 |
|---|---|---|---|---|---|
| Frozen backbone (baseline) | 0.919 [0.896-0.939] | 0.728 | 0.953 | 0.812 | 0.829 |
| Fine-tuned (this model) | 0.987 [0.977-0.994] | 0.992 [0.982-1.000] | 0.821 [0.769-0.868] | 0.928 | 0.945 |
The fine-tuned model missed 3 of 390 pneumonia cases and raised false alarms on 42 of 234 normal images. Validation specificity (~0.98) was much higher than test specificity (0.82), so validation scores overestimate performance. A lower threshold chosen on validation (0.09) did not transfer to the test set and is not recommended.
Limitations
- Trained on children aged 1-5 from a single hospital. Do not use on adults or other populations.
- Many images contain markers ("R"), tubes and wires. The model may use such shortcuts; this was not tested.
- Most published work uses this same dataset, so results may not generalize to other hospitals or scanners.
- High sensitivity comes with many false alarms (18% of normal test images).
Citation
Kermany, D., Zhang, K., Goldbaum, M. (2018). Large Dataset of Labeled Optical Coherence Tomography (OCT) and Chest X-Ray Images. Mendeley Data, V3. doi:10.17632/rscbjbr9sj.3