DenseNet-121 trained on 112,120 chest X-rays to detect 14 diseases — with Grad-CAM visualization showing exactly where the model looks.
Try it here → huggingface.co/spaces/Jignesh2619/chestxray-classifier
Upload any chest X-ray and get:
- Top 3 disease predictions with confidence scores
- Grad-CAM heatmap showing which lung regions influenced the prediction
| Metric | Value |
|---|---|
| Mean AUC (14 classes) | 0.7630 |
| Best class — Hernia | 0.8986 |
| Best class — Emphysema | 0.8507 |
| Best class — Cardiomegaly | 0.8244 |
| Training epochs | 10 (5 frozen + 5 fine-tune) |
| Dataset size | 112,120 images |
- Backbone: DenseNet-121 pretrained on ImageNet
- Classifier: Custom linear head → 14 disease classes
- Loss: Weighted Binary Cross-Entropy (handles class imbalance)
- Evaluation: Per-class AUROC
- Visualization: Grad-CAM on
features.denseblock4
Phase 1: Frozen backbone → train classifier head (5 epochs, lr=1e-3)
Loss: 1.1192 → 1.0378
Phase 2: Full fine-tune (5 epochs, lr=1e-5)
Loss: 0.9640 → 0.7391
Atelectasis · Cardiomegaly · Consolidation · Edema · Effusion · Emphysema · Fibrosis · Hernia · Infiltration · Mass · Nodule · Pleural Thickening · Pneumonia · Pneumothorax
| Feature | CheXNet (Stanford 2017) | This Project |
|---|---|---|
| Architecture | DenseNet-121 | DenseNet-121 |
| Dataset | NIH ChestX-ray14 | NIH ChestX-ray14 |
| Class imbalance handling | Standard BCE | ✅ Weighted BCE |
| Grad-CAM visualization | ❌ | ✅ |
| Live deployment | ❌ | ✅ HuggingFace Spaces |
| Patient-level data split | ✅ | ✅ No data leakage |
chest-x-ray.ipynb — Full training pipeline (data → model → eval → Grad-CAM)
app.py — Gradio demo (HuggingFace Spaces)
requirements.txt — Dependencies
Class Imbalance: NIH dataset has severe imbalance (e.g. Hernia: 227 samples vs Infiltration: 19,894). Computed per-class weights inversely proportional to frequency and applied to BCE loss.
No Data Leakage: Used official NIH patient-level train/test split. Images from the same patient never appear in both train and test sets.
Grad-CAM: Hooked into features.denseblock4 — the last dense block before Global Average Pooling. Gradients of the target class score with respect to feature maps are used to weight the spatial activations, producing a heatmap of the most influential regions.
pip install torch torchvision gradio opencv-python matplotlib
python app.py- CheXNet: Radiologist-Level Pneumonia Detection (Stanford, 2017)
- Densely Connected Convolutional Networks (Huang et al., 2017)
- NIH ChestX-ray14 Dataset
Built as part of a 30-day ML sprint — Day 12/13