| title | Pneumonia Detection |
|---|---|
| emoji | ๐ฅ |
| colorFrom | blue |
| colorTo | indigo |
| sdk | docker |
| pinned | false |
CNN-based pneumonia detection from chest X-rays using EfficientNetB0 transfer learning. Includes DICOM handling, Streamlit deployment, and full MLOps pipeline via Hugging Face Hub.
| Resource | Link |
|---|---|
| ๐ค Dataset | ananttripathiak/pneumonia-detection-dataset |
| ๐ค Model | ananttripathiak/pneumonia-detection-model |
| ๐ Live App | HF Space |
git clone https://github.com/ananttripathi/Pneumonia-Detection-Project.git
cd Pneumonia-Detection-Project
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txtRun notebooks in order (notebooks/01_data_overview.ipynb โ โฆ โ 05_transfer_learning.ipynb) or start the Streamlit app: streamlit run src/deployment/app.py. See QUICKSTART.md and Installation for data setup and usage.
Pneumonia is one of the leading causes of morbidity and mortality worldwide, particularly affecting children under five years and elderly populations. According to the World Health Organization (WHO), pneumonia accounts for a significant percentage of deaths caused by infectious diseases. Early detection and timely treatment are critical to improving patient outcomes, yet current diagnostic methods present challenges.
The most common method for diagnosing pneumonia is through clinical evaluation combined with chest X-ray imaging. However, several challenges exist:
- Limited Radiologist Availability: Accurate interpretation of X-rays requires skilled radiologists, whose availability is limited in many regions, especially in rural or resource-constrained healthcare settings
- Human Factors: Even when radiologists are available, factors such as fatigue, high patient load, and human error can affect the accuracy and consistency of diagnosis
- Healthcare Impact: These challenges may lead to:
- Delayed treatment
- Misdiagnosis
- Unnecessary use of antibiotics
- Worsening patient outcomes
- Strain on healthcare systems
With the advancement of machine learning and deep learning, automated image analysis has emerged as a promising solution to support medical imaging tasks. Leveraging large datasets of chest X-ray images, AI-driven approaches can be trained to recognize pneumonia-related abnormalities in the lungs with high accuracy and consistency. Such systems can serve as decision-support tools for healthcare professionals, reducing diagnostic workload, improving accuracy, and providing timely interventions, particularly in areas with limited medical expertise.
The main objective of this project is to develop an intelligent, automated system capable of detecting pneumonia from chest X-ray images using machine learning and deep learning techniques.
The system should aim to:
- Accurately classify chest X-ray images into pneumonia-positive and pneumonia-negative cases
- Assist healthcare professionals by providing a reliable second opinion that reduces diagnostic errors and variability
- Improve efficiency by delivering faster diagnoses, enabling timely treatment, and reducing the burden on radiologists
- Enhance accessibility by offering a scalable solution that can be deployed in hospitals, clinics, or rural healthcare centers with limited resources
- Support global health efforts by contributing to early detection, lowering pneumonia-related mortality rates, and optimizing antibiotic usage
Ultimately, the solution aims to bridge the gap between limited medical expertise and growing healthcare demands, making pneumonia diagnosis more accurate, efficient, and accessible worldwide.
The dataset contains chest X-ray images with the following characteristics:
- Classes:
- Pneumonia-positive
- Pneumonia-negative (Normal)
- Not Normal No Lung Opacity (abnormality present but not pneumonia)
In the dataset, some features are labeled "Not Normal No Lung Opacity". This extra third class indicates that while pneumonia was determined not to be present, there was nonetheless some type of abnormality on the image, and oftentimes this finding may mimic the appearance of true pneumonia.
DICOM Original Images: Medical images are stored in a special format called DICOM files (*.dcm). They contain a combination of:
- Header metadata
- Underlying raw image arrays for pixel data
| Section | Description | Points |
|---|---|---|
| Data Overview | - Import the data - Check the shape of the data |
6 |
| Exploratory Data Analysis | - Plot random images from each class and print their corresponding labels - Check for class imbalance - Key meaningful observations from EDA |
8 |
| Data Preprocessing | - Convert the RGB images to Grayscale - Plot the images before and after the pre-processing steps - Split the data into train, validation and test - Apply the normalization |
10 |
| Model Building | - Define a CNN model from scratch - Train the Model - Check and comment on the performance of the model |
10 |
| Business Report Quality | - Adhere to the business report checklist | 6 |
| Section | Description | Points |
|---|---|---|
| Data Overview | - Import the data - Check the shape of the data |
3 |
| Exploratory Data Analysis | - Plot random images from each class and print their corresponding labels - Check for class imbalance - Key meaningful observations from EDA |
3 |
| Data Preprocessing | - Convert the RGB images to Grayscale - Plot the images before and after the pre-processing steps - Split the data into train, validation and test - Apply the normalization |
4 |
| Model Building | - Define a CNN model from scratch - Train the Model - Check and comment on the performance of the model |
5 |
| Transfer Learning | - Apply transfer learning using pre-trained CNN models (at least 2) - Check and comment on the performance of the models - Create new architectures using the above pre-trained CNNs and adding additional layers - Check and comment on the performance of the models - Compare and comment on the performance of all models built - Choose the best model with a proper rationale - Serialize the best model, re-load it, and make Inferences on a few images |
30 |
| Model Deployment | - Build a Streamlit or Gradio app where users can upload an image and see predicted class + probability - Package app + model inside a Docker container for portability - Deploy to a Hugging Face platform and make an inference |
5 |
| Actionable Insights and Recommendations | - Key takeaways for the business | 4 |
| Business Report Quality | - Adhere to the business report checklist | 6 |
Pneumonia-Detection-Project/
โโโ README.md
โโโ requirements.txt
โโโ Dockerfile
โโโ .dockerignore
โโโ data/
โ โโโ raw/
โ โ โโโ train/
โ โ โโโ val/
โ โ โโโ test/
โ โโโ processed/
โโโ notebooks/
โ โโโ 01_data_overview.ipynb
โ โโโ 03_preprocessing.ipynb
โ โโโ 04_cnn_from_scratch.ipynb
โ โโโ 05_transfer_learning.ipynb
โโโ src/
โ โโโ data/
โ โ โโโ data_loader.py
โ โ โโโ preprocessing.py
โ โ โโโ augmentation.py
โ โโโ models/
โ โ โโโ cnn_scratch.py
โ โ โโโ transfer_learning.py
โ โ โโโ train.py
โ โ โโโ evaluate.py
โ โโโ utils/
โ โ โโโ visualization.py
โ โ โโโ metrics.py
โ โ โโโ config.py
โ โโโ deployment/
โ โโโ app.py
โโโ models/
โ โโโ saved_models/
โ โโโ best_model.h5
โ โโโ best_model.pkl
โโโ logs/
โ โโโ training_logs/
โโโ docs/
โโโ business_report.md
โโโ technical_documentation.md
# Clone the repository
git clone https://github.com/ananttripathi/Pneumonia-Detection-Project.git
cd Pneumonia-Detection-Project
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtfrom src.data.preprocessing import preprocess_images
from src.data.data_loader import load_dicom_images
# Load DICOM images
images, labels = load_dicom_images('data/raw/')
# Preprocess images
processed_images = preprocess_images(images, grayscale=True, normalize=True)from src.models.cnn_scratch import build_cnn_model
from src.models.train import train_model
# Build CNN model
model = build_cnn_model(input_shape=(224, 224, 1), num_classes=3)
# Train model
history = train_model(
model,
X_train, y_train,
X_val, y_val,
epochs=50,
batch_size=32
)from src.models.transfer_learning import build_transfer_model
from tensorflow.keras.applications import VGG16, ResNet50
# Build transfer learning model with VGG16
model_vgg = build_transfer_model(
base_model=VGG16,
input_shape=(224, 224, 3),
num_classes=3,
freeze_base=True
)
# Build transfer learning model with ResNet50
model_resnet = build_transfer_model(
base_model=ResNet50,
input_shape=(224, 224, 3),
num_classes=3,
freeze_base=True
)from src.models.evaluate import evaluate_model
from src.utils.metrics import plot_confusion_matrix, plot_roc_curve
# Evaluate model
metrics = evaluate_model(model, X_test, y_test)
print(f"Test Accuracy: {metrics['accuracy']:.4f}")
print(f"Precision: {metrics['precision']:.4f}")
print(f"Recall: {metrics['recall']:.4f}")
print(f"F1-Score: {metrics['f1_score']:.4f}")
# Plot confusion matrix
plot_confusion_matrix(y_test, predictions)
# Plot ROC curve
plot_roc_curve(y_test, predictions)from src.models.predict import predict_pneumonia
from PIL import Image
# Load and predict
image = Image.open('test_image.jpg')
prediction, probability = predict_pneumonia(model, image)
print(f"Prediction: {prediction}")
print(f"Confidence: {probability:.2%}")- Python 3.8+
- TensorFlow / Keras - Deep learning framework
- PyTorch (optional) - Alternative deep learning framework
- OpenCV - Image processing
- Pydicom - DICOM file handling
- NumPy & Pandas - Data manipulation
- Matplotlib & Seaborn - Data visualization
- Scikit-learn - Metrics and utilities
- Streamlit / Gradio - Web application deployment
- Docker - Containerization
- Hugging Face Spaces - Model hosting and deployment
Built from scratch with:
- Convolutional layers
- Max pooling layers
- Batch normalization
- Dropout for regularization
- Dense layers for classification
Pre-trained models implemented (minimum 2):
- VGG16 - Visual Geometry Group 16-layer network
- ResNet50 - Residual Network with 50 layers
- InceptionV3 - Inception architecture
- EfficientNet - Efficient architecture scaling
- DenseNet - Densely connected networks
- ๐ฅ Medical Image Processing: Specialized handling of DICOM files
- ๐ Advanced Preprocessing: Grayscale conversion, normalization, augmentation
- ๐ง Multiple CNN Architectures: Custom and pre-trained models
- ๐ Comprehensive Evaluation: Accuracy, precision, recall, F1-score, ROC-AUC
- ๐ฏ Class Imbalance Handling: Weighted loss functions and data augmentation
- ๐ Deployment Ready: Streamlit/Gradio app with Docker support
- ๐ฑ User-Friendly Interface: Upload image and get instant predictions
- ๐ณ Containerized: Docker support for easy deployment
- Read DICOM files with metadata
- Extract pixel arrays
- Convert RGB to Grayscale
- Resize to standard dimensions (e.g., 224x224)
- Pixel value scaling (0-1 or -1 to 1)
- Standardization (mean=0, std=1)
- Rotation
- Horizontal flip
- Zoom
- Brightness adjustment
- Contrast adjustment
- Training set (70-80%)
- Validation set (10-15%)
- Test set (10-15%)
The models are evaluated using:
- Accuracy: Overall correctness of predictions
- Precision: Proportion of correct positive predictions
- Recall (Sensitivity): Ability to identify all positive cases
- Specificity: Ability to identify negative cases
- F1-Score: Harmonic mean of precision and recall
- ROC-AUC: Area under the receiver operating characteristic curve
- Confusion Matrix: Detailed breakdown of predictions
# Run locally
streamlit run src/deployment/app.py# Run locally
python src/deployment/gradio_app.py# Build Docker image
docker build -t pneumonia-detection .
# Run container
docker run -p 8501:8501 pneumonia-detectionDeploy the Streamlit app to Hugging Face Spaces. Use the Docker/Streamlit template and upload src/deployment/app.py, requirements.txt, and your trained model. Example: https://huggingface.co/spaces/<your-username>/pneumonia-detection
- ๐ค Image Upload: Drag-and-drop or browse to upload chest X-ray
- ๐ฎ Real-time Prediction: Instant classification results
- ๐ Confidence Score: Probability distribution across classes
- ๐ผ๏ธ Image Preview: View uploaded image
- ๐ Visualization: Heatmap/Grad-CAM for interpretability
- ๐ Recommendation: Suggested next steps based on prediction
- Visualize which regions of the X-ray influenced the model's decision
- Highlight areas of interest for pneumonia detection
- Display intermediate layer activations
- Understand what features the model learns
- โก Faster Diagnosis: Reduce time from imaging to diagnosis
- ๐ฏ Higher Accuracy: Reduce false negatives and false positives
- ๐จโโ๏ธ Radiologist Support: Second opinion and decision support
- ๐ Reduced Workload: Automate preliminary screening
- ๐ Increased Access: Deploy in resource-limited settings
- โฑ๏ธ Timely Treatment: Earlier intervention improves outcomes
- ๐ Appropriate Antibiotic Use: Reduce unnecessary prescriptions
- ๐ฅ Better Outcomes: Lower mortality and morbidity rates
- ๐ฐ Cost Savings: Reduce complications and hospitalizations
(Add your model performance results here)
| Model | Accuracy | Precision | Recall | F1-Score | ROC-AUC |
|---|---|---|---|---|---|
| Custom CNN | 0.XX | 0.XX | 0.XX | 0.XX | 0.XX |
| VGG16 | 0.XX | 0.XX | 0.XX | 0.XX | 0.XX |
| ResNet50 | 0.XX | 0.XX | 0.XX | 0.XX | 0.XX |
| Best Model | 0.XX | 0.XX | 0.XX | 0.XX | 0.XX |
- ๐ฌ Multi-class Classification: Distinguish between bacterial and viral pneumonia
- ๐ Multi-modal Learning: Incorporate clinical data with imaging
- ๐ฑ Mobile Application: Deploy on smartphones for point-of-care use
- ๐ Continuous Learning: Update model with new data
- ๐ฅ Integration: Connect with hospital PACS systems
- ๐ Multilingual Support: Interface in multiple languages
- ๐ Analytics Dashboard: Track model performance over time
- โ๏ธ Not a Replacement: Tool assists but doesn't replace radiologists
- ๐ Privacy: HIPAA-compliant data handling
- โ Validation: Clinical validation required before deployment
- ๐ Bias: Monitor for demographic and technical biases
- ๐ Regulatory: Comply with medical device regulations
Contributions are welcome! Please see CONTRIBUTING.md for run instructions and how to contribute.
This project is licensed under the MIT License.
Suggested GitHub topics: machine-learning computer-vision healthcare deep-learning tensorflow medical-imaging pneumonia-detection transfer-learning
Co-author: ananttripathiak
For questions or support, please open a GitHub Issue.
- Dataset provided by [source]
- Medical experts for validation and guidance
- Open-source deep learning community
- Healthcare institutions for collaboration