# ๐Ÿฉบ Chest X-Ray Pneumonia Detection using Deep Learning
![Python](https://img.shields.io/badge/Python-3.10-blue?style=for-the-badge&logo=python) ![TensorFlow](https://img.shields.io/badge/TensorFlow-2.x-orange?style=for-the-badge&logo=tensorflow) ![Keras](https://img.shields.io/badge/Keras-Deep%20Learning-red?style=for-the-badge&logo=keras) ![Flask](https://img.shields.io/badge/Flask-Backend-black?style=for-the-badge&logo=flask) ![Streamlit](https://img.shields.io/badge/Streamlit-Web_App-ff4b4b?style=for-the-badge&logo=streamlit) ![OpenCV](https://img.shields.io/badge/OpenCV-Image_Processing-green?style=for-the-badge&logo=opencv) ![CNN](https://img.shields.io/badge/CNN-Custom_Model-success?style=for-the-badge) ![VGG16](https://img.shields.io/badge/VGG16-Transfer_Learning-blueviolet?style=for-the-badge)
--- # ๐Ÿ“Œ 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.