This repository contains a deep learning project for binary classification of chest X-ray images into:
- NORMAL
- PNEUMONIA
The system is built using PyTorch, includes a custom Hybrid CNN with Residual Blocks, and provides a fully interactive Streamlit web application with Grad-CAM visualization for model interpretability.
Important:
This project is for educational and research purposes only.
It is not intended for clinical or medical diagnosis.
Always consult licensed medical professionals for medical interpretation of radiological images.
Pneumonia-HybridCNN/
│
├── model.py # Hybrid CNN + Residual Blocks + GradCAM
├── train.py # Full training script
├── app.py # Streamlit web application
├── requirements.txt # Python dependencies
├── environment.yml # Conda environment (optional)
├── README.md # Documentation
│
└── data/
├── train/
│ ├── NORMAL/
│ └── PNEUMONIA/
└── test/
├── NORMAL/
└── PNEUMONIA/The model combines:
- Deep convolutional layers
- Residual skip connections
- Adaptive pooling
- A fully-connected classification head
This structure enables strong feature extraction while avoiding vanishing gradients.
The project includes a Grad-CAM implementation that generates:
- Heatmaps highlighting important regions
- Overlays on top of the original X-ray
- Layer selection for deeper inspection
The included Streamlit interface provides:
- Image upload
- Model prediction and class probabilities
- Confidence indicators
- Grad-CAM visualization
- Medical disclaimers
- Layer selection for interpretability
- Side-by-side comparison views
The model classifies:
- Normal
- Pneumonia
Works with any dataset that follows the proper folder structure.
git clone https://github.com/yourusername/Pneumonia-HybridCNN.git
cd Pneumonia-HybridCNNpip install -r requirements.txtOR
conda env create -f environment.yml
conda activate pneumonia_cnn_envThe data can be pulled from Kaggle: https://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia
Your dataset must be strucutred as following:
data/train/NORMAL/
data/train/PNEUMONIA/
data/val/NORMAL/
data/val/PNEUMONIA/
data/test/NORMAL/
data/test/PNEUMONIA/
Supported formats: (.jpg, .jpeg, .png) the training script automatically splits validation data from the training set.
Run:
python train.pyWhat happens during training:
- Data augmentation
- Class weighting for imbalance
- Learning rate scheduling
- Early stopping
- Best-model checkpointing
- Epoch-level performance reporting
- Test-set evaluation (confusion matrix + classification report)
Start the app:
streamlit rum app.pyOnce launched, the interface allows you to:
- Upload a chest X-ray
- Generate predictions
- View per-class probabilities
- Inspect Grad-CAM heatmaps
- Switch Grad-CAM layers
- See educational disclaimers on every important step
In typical training runs, the model achieves:
- About 96% overall accuracy
- Balanced performance across all three classes
- Strong recall for Pneumonia.
- Smooth generalization due to augmentation and class weighting
Performance depends on dataset composition and size.
- HybridPneumoniaCNN model class
- ResidualBlock implementation
- Grad-CAM generation utilities
- Train/validation/test loops
- Augmentation and dataset loaders
- Weighted loss
- Scheduler and early stopping
- Model saving and evaluation
- Image loading and preprocessing
- Model inference
- Bar-graph probabilities
- Gradient-based heatmaps
- Color-coded confidence warnings
- Educational disclaimers
List of packages required for training and deployment.
Contributing
Contributions are welcome.
Possible improvements:
- Faster model variants
- Additional explainability methods
- Deployment scripts
- UI enhancements
- Support for more datasets
Submit a pull request or open an issue for discussion.
This repository is intended solely for educational experimentation with deep learning, medical imaging, model interpretability, and web deployment.
It is not a clinical diagnostic tool under any circumstances.