sandy45/ChestViT-Explainable-XRay-AI
Multi-Task Vision Transformer for Chest X-Ray Disease Classification & Explainability
<div align="center">
ViT-Base-16 ยท 14-Disease Multi-Label Classification ยท Attention Rollout XAI
Fine-tuned on NIH ChestX-ray14 (112,120 frontal chest X-rays)
</div>
๐ฏ Overview
This project implements an explainable medical AI system for automated chest X-ray analysis. Unlike standard classification models, this system simultaneously:
- Predicts 14 diseases in parallel (multi-label, not multi-class)
- Shows WHERE in the X-ray the model is looking via Attention Rollout
- Compares against published NIH baselines (AUC-ROC per class)
- Serves a live demo via a Gradio dashboard
The core insight: Vision Transformers (ViTs) divide images into 16ร16 patches and route information through 12 attention layers. Attention Rollout traces this information flow back to the input patches โ telling us exactly which lung regions drove each disease prediction.
๐ Architecture
Chest X-Ray Input (PNG, 1024ร1024)
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโ
โ CLAHE Preprocessing โ โ Contrast Limited Adaptive Histogram Equalization
โ + Albumentations โ โ Radiologically-realistic augmentation
โโโโโโโโโโโโโโโโโโโโโโโโโ
โ 224ร224ร3
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ViT-Base-16 โ
โ (google/vit-base-patch16-224-in21k) โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ 196 Patches (14ร14 grid, 16px/ea) โ โ
โ โ + [CLS] token = 197 total tokens โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ 12 Transformer Layers โ
โ (12 heads ร 64 dim = 768 hidden dim) โ
โ โ โ
โ [CLS] token โ Dropout โ Linear(768โ14) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโ Logits โ Sigmoid โ 14 disease probabilities
โ
โโโโ Attention weights (12 layers ร 12 heads)
โ
โผ
Attention Rollout Algorithm
(14ร14 patch attention map)
โ
โผ
224ร224 heatmap overlay on X-ray๐ Results
Results will populate after training. NIH baseline from Wang et al. (2017).
๐ Quick Start
1. Install Dependencies
# Create virtual environment
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # Linux/macOS
# Install dependencies
pip install -r requirements.txt2. Download Dataset
# First: set up Kaggle API credentials
# 1. Go to https://www.kaggle.com โ Account โ Settings โ Create New API Token
# 2. Place kaggle.json at: C:\Users\<YourName>\.kaggle\kaggle.json
# Then download NIH ChestX-ray14 (~42 GB)
python data/download.py3. Run Unit Tests
python -m pytest tests/ -v4. Train the Model
# Full training (5 epochs, ~8-12 hours on RTX 3050)
python training/train.py
# Quick smoke test (20% of data)
# Edit config/config.yaml โ dataset.train_fraction: 0.2
python training/train.pyMonitor training in real-time:
mlflow ui --backend-store-uri ./experiments/mlflow
# Open http://localhost:50005. Launch Demo
# With trained model
python app/gradio_app.py
# DEMO MODE (random weights, for UI preview only)
set DEMO_MODE=1 # Windows
python app/gradio_app.py๐ Project Structure
.
โโโ config/
โ โโโ config.yaml # All hyperparameters and paths
โโโ data/
โ โโโ download.py # Kaggle API dataset download
โ โโโ preprocessing.py # CLAHE + Albumentations pipeline
โ โโโ dataset.py # ChestXrayDataset + DataLoaders
โ โโโ raw/ # Downloaded dataset (not in git)
โโโ models/
โ โโโ vit_model.py # ViT-Base-16 with multi-label head
โโโ explainability/
โ โโโ attention_rollout.py # Attention Rollout algorithm
โโโ training/
โ โโโ losses.py # Weighted BCE + Focal Loss
โ โโโ train.py # Training loop (mixed precision, MLflow)
โ โโโ evaluate.py # AUC-ROC per class, ROC plots
โโโ app/
โ โโโ gradio_app.py # Gradio dashboard
โโโ tests/
โ โโโ test_modules.py # Unit tests (no dataset required)
โโโ checkpoints/ # Saved model weights (not in git)
โโโ results/ # ROC curves, metrics CSV
โโโ experiments/
โ โโโ mlflow/ # MLflow tracking database
โโโ config_loader.py # YAML config loader
โโโ requirements.txtโ๏ธ Configuration
All settings are in `config/config.yaml`. Key RTX 3050 settings:
training:
batch_size: 8 # Fits in 4 GB VRAM
gradient_accumulation_steps: 4 # Effective batch = 32
mixed_precision: true # fp16 โ mandatory for 4 GB VRAM
num_epochs: 5
model:
name: "google/vit-base-patch16-224-in21k"
gradient_checkpointing: true # Saves ~30% VRAM
dataset:
train_fraction: 1.0 # Set 0.2 for quick smoke test๐ฅ Attention Rollout: Why Not Grad-CAM?
Attention Rollout (Abnar & Zuidema, 2020) is mathematically derived from the transformer's own attention mechanism, making it the correct tool for ViT explainability.
๐ฅ Clinical Context
โ ๏ธ This is a research/educational project, NOT a medical device. Results should not be used for clinical diagnosis without radiologist review.
The NIH ChestX-ray14 dataset has known limitations (Rajpurkar et al., 2018, and others). AUC-ROC is the clinically relevant metric because:
- Accuracy is misleading with class imbalance (>53% "No Finding")
- AUC measures discriminative ability across all thresholds
- Radiologists can set their own confidence threshold per clinical context
๐ References
- Wang, X. et al. (2017). ChestX-ray8: Hospital-scale Chest X-ray Database and Benchmarks. CVPR.
- Dosovitskiy, A. et al. (2021). An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. ICLR.
- Abnar, S. & Zuidema, W. (2020). Quantifying Attention Flow in Transformers. arXiv:2005.00928.
- Rajpurkar, P. et al. (2017). CheXNet: Radiologist-Level Pneumonia Detection on Chest X-Rays with Deep Learning. arXiv:1711.05225.
