# ๐ฉบ Chest X-Ray Pneumonia Detection using Deep Learning








---
# ๐ Project Overview
This project is an **AI-powered Chest X-Ray Pneumonia Detection System** that automatically classifies chest X-ray images into:
- โ
NORMAL
- ๐ฆ PNEUMONIA
The project demonstrates multiple Deep Learning approaches and compares their performance using:
- Custom CNN (10 Epochs)
- Improved CNN (20 Epochs)
- Transfer Learning (VGG16)
The trained models are deployed using **Flask**, while the frontend is developed using **Streamlit**, creating a complete end-to-end AI medical imaging application.
---
# ๐ Features
โ Binary Classification
โ Three Deep Learning Models
โ Custom CNN Architecture
โ Improved CNN Architecture
โ Transfer Learning using VGG16
โ Real-time Image Prediction
โ Flask REST Backend
โ Streamlit Interactive UI
โ Hugging Face Model Hosting
โ GPU Training Support
โ Model Comparison
โ Confidence Score Prediction
โ Production Ready
---
# ๐ง Problem Statement
Pneumonia is one of the leading causes of death worldwide.
Radiologists inspect Chest X-rays manually which is:
- Time Consuming
- Error Prone
- Expensive
This project automates the diagnosis process using Deep Learning.
---
# ๐ Dataset
## Chest X-Ray Dataset
```
Dataset
โ
โโโ train
โ โโโ NORMAL
โ โโโ PNEUMONIA
โ
โโโ validation
โ โโโ NORMAL
โ โโโ PNEUMONIA
โ
โโโ test
โโโ NORMAL
โโโ PNEUMONIA
```
Image Format
- JPG
- JPEG
- PNG
Classes
| Label | Description |
|---------|-------------|
| NORMAL | Healthy Lung |
| PNEUMONIA | Infected Lung |
---
# โ๏ธ Tech Stack
## Programming
- Python
---
## Deep Learning
- TensorFlow
- Keras
---
## Computer Vision
- OpenCV
- NumPy
- Matplotlib
---
## Backend
- Flask
---
## Frontend
- Streamlit
---
## Deployment
- Hugging Face
- Render
---
## Version Control
- Git
- GitHub
---
# ๐ Complete Project Architecture
```
Chest X-Ray Image
โ
โผ
Upload Image (UI)
โ
โผ
Streamlit Frontend
โ
โผ
Flask REST API
โ
โผ
Image Preprocessing
โ
โโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
CNN Model CNN 20 Model VGG16 Model
โ โ โ
โโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโ
โ
โผ
Prediction Probability
โ
โผ
NORMAL / PNEUMONIA Result
โ
โผ
Display Prediction
```
---
# ๐ End-to-End Workflow
```
Dataset
โ
โผ
Image Loading
โ
โผ
Image Preprocessing
โ
โผ
Resize Images (100x100)
โ
โผ
Convert to Array
โ
โผ
Normalize Images
โ
โผ
Train / Validation Split
โ
โผ
Model Training
โ
โผ
Model Evaluation
โ
โผ
Save Best Model
โ
โผ
Deploy Model
โ
โผ
User Upload Image
โ
โผ
Prediction
```
---
# ๐งน Image Preprocessing Pipeline
Each X-Ray undergoes the following preprocessing steps:
### Step 1
Load Image
โ
### Step 2
Convert to Grayscale (CNN Models)
โ
### Step 3
Convert to RGB (VGG16)
โ
### Step 4
Resize
```
100 ร 100
```
โ
### Step 5
Convert to NumPy Array
โ
### Step 6
Normalize Pixel Values
```
0 โ 255
โ
0 โ 1
```
โ
### Step 7
Feed into Model
---
# ๐ค Model 1
## Custom CNN (10 Epochs)
Architecture
```
Input (100ร100ร1)
โ
Conv2D (64)
โ
MaxPooling
โ
Dropout
โ
Conv2D (128)
โ
MaxPooling
โ
Dropout
โ
Conv2D (256)
โ
MaxPooling
โ
Dropout
โ
Flatten
โ
Dense (64)
โ
Dropout
โ
Dense (1)
โ
Sigmoid
```
Loss
```
Binary Crossentropy
```
Optimizer
```
Adam
```
Epochs
```
10
```
---
# ๐ค Model 2
## Improved CNN (20 Epochs)
Architecture
```
Input
โ
Conv2D (64)
โ
ReLU
โ
MaxPooling
โ
Dropout
โ
Conv2D (128)
โ
ReLU
โ
MaxPooling
โ
Dropout
โ
Conv2D (256)
โ
ReLU
โ
MaxPooling
โ
Dropout
โ
Flatten
โ
Dense (64)
โ
Dropout
โ
Dense (1)
โ
Sigmoid
```
Epochs
```
20
```
Optimizer
```
Adam
```
Loss
```
Binary Crossentropy
```
---
# ๐ค Model 3
## Transfer Learning (VGG16)
Pretrained
```
ImageNet
```
Frozen Layers
```
All VGG16 Convolution Layers
```
Custom Head
```
Flatten
โ
Dense (256)
โ
Dense (128)
โ
Dense (64)
โ
Dense (1)
โ
Sigmoid
```
Callbacks
- ModelCheckpoint
- EarlyStopping
---
# ๐ง Why VGG16?
Instead of training from scratch,
VGG16 already knows how to detect
- Edges
- Shapes
- Textures
- Patterns
Only the classifier is trained on Chest X-rays.
This greatly improves performance while reducing training time.
---
# ๐ Saved Models
```
model_xray.h5
```
Custom CNN
---
```
model_pre.h5
```
Improved CNN
---
```
best_model.keras
```
Best Transfer Learning Model
---
# ๐ Training Strategy
- GPU Training
- Batch Size = 4 (CNN)
- Batch Size = 32 (VGG16)
- Validation Dataset
- Binary Crossentropy
- Adam Optimizer
- Early Stopping
- Model Checkpoint
---
# ๐ Prediction Pipeline
```
Upload Image
โ
Read Image
โ
Resize
โ
Preprocess
โ
Load Model
โ
Predict Probability
โ
Threshold = 0.5
โ
NORMAL
or
PNEUMONIA
```
---
# ๐ Deployment Architecture
```
User
โ
โผ
Streamlit Frontend
โ
โผ
Flask Backend
โ
โผ
Load Selected Model
โ
โโโโโโโโโโโโโผโโโโโโโโโโโโโ
โผ โผ โผ
CNN10 CNN20 VGG16
โ โ โ
โโโโโโโโโโโโโผโโโโโโโโโโโโโ
โผ
Prediction Engine
โ
โผ
Display Result
```
---
# ๐ Project Structure
```
Chest-XRay-Pneumonia-Detection
โ
โโโ app.py
โโโ config.py
โโโ predictor.py
โโโ utils.py
โโโ requirements.txt
โโโ README.md
โ
โโโ models
โ โโโ model_xray.h5
โ โโโ model_pre.h5
โ โโโ best_model.keras
โ
โโโ static
โ
โโโ templates
โ
โโโ css
โ
โโโ dataset
โ
โโโ screenshots
```
---
# โถ๏ธ Installation
Clone Repository
```bash
git clone https://github.com/yourusername/Chest-XRay-Pneumonia-Detection.git
```
Install Dependencies
```bash
pip install -r requirements.txt
```
Run Flask
```bash
python app.py
```
Run Streamlit
```bash
streamlit run app.py
```
---
# ๐ฅ Example Prediction
Input
```
Chest X-Ray Image
```
โ
Model Prediction
```
Probability : 0.9821
Prediction :
๐ฆ PNEUMONIA
```
---
# ๐ Future Improvements
- EfficientNet
- ResNet50
- DenseNet121
- Grad-CAM Heatmaps
- Multi-Class Disease Detection
- DICOM Support
- Cloud Deployment
- Docker
- CI/CD Pipeline
- REST API Authentication
---
# ๐จโ๐ป Author
**Sudheer Muthyala**
B.Tech โ Electronics and Communication Engineering
Machine Learning | Deep Learning | Computer Vision | Python | Flask | Streamlit
GitHub:
https://github.com/M-Sudheer18
---
# โญ If you found this project helpful
Please consider giving this repository a โญ on GitHub.
It motivates future improvements and helps others discover the project.