An end-to-end deep learning system for detecting pneumonia from pediatric chest X-rays, achieving 92.31% accuracy with Vision Transformer architecture.
This project implements a comprehensive medical AI pipeline for pneumonia classification using state-of-the-art deep learning models. The system includes data preprocessing, multiple model architectures, explainability features (Grad-CAM), and a production-ready Streamlit web application.
- π€ Multiple Model Architectures: Baseline CNN, DenseNet121, EfficientNet-B0, Vision Transformer (ViT-B/16)
- π Comprehensive Evaluation: ROC curves, confusion matrices, sensitivity/specificity analysis
- π Model Explainability: Grad-CAM heatmap visualizations for clinical validation
- π Production Deployment: ONNX export, Streamlit web interface
- βοΈ Handles Class Imbalance: Weighted loss functions and proper evaluation metrics
- π Excellent Performance: 92.31% test accuracy, 98.21% sensitivity, 0.978 AUC
| Model | Test Accuracy | Sensitivity | Specificity | AUC | Parameters |
|---|---|---|---|---|---|
| Baseline CNN | 84.94% | 94.87% | 68.38% | 0.9355 | 26.1M |
| DenseNet121 | 84.13% | 99.74% | 58.12% | 0.9649 | 7.0M |
| EfficientNet-B0 | 90.87% | 97.95% | 79.06% | 0.9580 | 4.0M |
| ViT-B/16 | 92.31% | 98.21% | 82.48% | 0.9780 | 85.8M |
pneumonia-xray-classification/
βββ notebooks/ # Jupyter notebooks (development pipeline)
βββ src/ # Source code modules
βββ models/ # Trained model checkpoints
βββ deployment/ # Production deployment files
β βββ streamlit_app.py # Streamlit web application
β βββ *.onnx # ONNX exported models
β βββ requirements.txt # Deployment dependencies
βββ results/ # Evaluation results and visualizations
βββ data/ # Dataset (not included in repo)
βββ docs/ # Documentation
βββ README.md
- Python 3.8+
- CUDA-compatible GPU (optional, for training)
- 8GB+ RAM
- Clone the repository
git clone https://github.com/yourusername/pneumonia-xray-classification.git
cd pneumonia-xray-classification- Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -r requirements.txt- Download dataset
Download the Pediatric Chest X-Ray Dataset and place it in:
data/chest_xray/
βββ train/
βββ val/
βββ test/
Run the Jupyter notebooks in sequence:
jupyter notebook notebooks/01_environment_setup.ipynbOr train directly using Python:
python src/train.py --model efficientnet --epochs 10cd deployment
streamlit run streamlit_app.pyThen open your browser to http://localhost:8501
from src.models import load_model
from src.utils import preprocess_image
# Load model
model = load_model('models/efficientnet_best.pth')
# Predict
image = preprocess_image('path/to/xray.jpg')
prediction, confidence = model.predict(image)
print(f"Prediction: {prediction} ({confidence:.2%})")- Run all notebooks sequentially (01-10)
- Models are saved in
models/directory - Results visualizations in
results/directory - Evaluation metrics in notebook 08
Expected training time:
- Baseline CNN: ~30 min (CPU)
- EfficientNet: ~45 min (CPU)
- ViT-B/16: ~60 min (CPU)
- All models: ~10-15 min (GPU)
- Push code to GitHub
- Go to share.streamlit.io
- Deploy from
deployment/streamlit_app.py - Ensure
requirements.txtis in deployment folder
docker build -t pneumonia-classifier .
docker run -p 8501:8501 pneumonia-classifierUse exported ONNX models for production inference:
import onnxruntime as ort
session = ort.InferenceSession('deployment/pneumonia_classifier_efficientnet.onnx')
outputs = session.run(None, {'input': preprocessed_image})The system includes Grad-CAM (Gradient-weighted Class Activation Mapping) to visualize which regions of X-rays the model focuses on:
This helps clinicians:
- Validate model decisions
- Identify potential biases
- Build trust in AI predictions
This is a research project and educational tool.
- β Not FDA approved for clinical use
- β Not a substitute for professional medical diagnosis
- β Intended for research and educational purposes only
- β Always consult qualified healthcare professionals
Source: Kaggle - Chest X-Ray Images (Pneumonia)
- Training: 5,216 images (1,341 Normal, 3,875 Pneumonia)
- Validation: 47 images
- Test: 624 images (234 Normal, 390 Pneumonia)
- Population: Pediatric patients (1-5 years old)
Citation:
Kermany, Daniel; Zhang, Kang; Goldbaum, Michael (2018),
"Labeled Optical Coherence Tomography (OCT) and Chest X-Ray Images for Classification",
Mendeley Data, V2, doi: 10.17632/rscbjbr9sj.2
- Deep Learning: PyTorch, Torchvision
- Data Processing: NumPy, OpenCV, Albumentations
- Visualization: Matplotlib, Seaborn
- Explainability: Pytorch-Grad-CAM
- Web App: Streamlit
- Deployment: ONNX Runtime
- Evaluation: scikit-learn
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit changes (
git commit -m 'Add AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Your Name
- GitHub: @yourusername
- LinkedIn: Your LinkedIn
- Email: your.email@example.com
- Dataset provided by Kermany et al.
- Pretrained models from PyTorch/Torchvision
- Streamlit for the amazing web framework
- Medical imaging community for guidance
- Kermany et al. (2018). "Identifying Medical Diagnoses and Treatable Diseases by Image-Based Deep Learning"
- Dosovitskiy et al. (2021). "An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale"
- Tan & Le (2019). "EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks"
β If you find this project helpful, please consider giving it a star!
Model files are hosted in GitHub Releases due to their size.
For Streamlit deployment:
python download_models.py
# Select option 1 (EfficientNet only)For all models:
python download_models.py
# Select option 4 (Everything)Download from Releases page:
| File | Size | Purpose | Download |
|---|---|---|---|
efficientnet_best.pth |
46 MB | Recommended for Streamlit | Download |
pneumonia_classifier_efficientnet.onnx |
15 MB | ONNX (production) | Download |
vit_best.pth |
978 MB | Best accuracy (92.31%) | Download |
densenet121_best.pth |
80 MB | High sensitivity (99.74%) | Download |
baseline_cnn_best.pth |
299 MB | Baseline model | Download |
Place downloaded files:
.pthfiles βmodels/directory.onnxfiles βdeployment/directory
Download the Chest X-Ray Images dataset from Kaggle.
Extract to: data/chest_xray/

