A newer version of the Gradio SDK is available:
6.5.1
title: Classification With Segmentation
emoji: 🏢
colorFrom: red
colorTo: red
sdk: gradio
sdk_version: 5.49.1
app_file: app.py
pinned: false
license: mit
Classification With Segmentation
This project implements a complete chest X-ray analysis pipeline using:
lung segmentation using UNet (Unet.ipynb)
Two binary EfficientNet-B3 classifiers (pneumonia classification.ipynb):
- Normal vs Bacterial Pneumonia
- Normal vs Viral Pneumonia
Intelligent decision fusion
- If both classifiers fire, the model selects the dominant probability and returns a final diagnosis.
Dataset
- For Unet Lung Mask Image Dataset
- For binary EfficientNet-B3 classifiers Pneumonia Multi-Class Dataset
Interactive Gradio Web App
Includes
- Lung segmentation mask
- Masked classifier input
- Bacterial & viral probability scores
- Final diagnosis
- Segmentation overlay
- Static Test Samples Table at the bottom
Features
1. Lung Segmentation (UNet)
The UNet model extracts lung regions from chest X-rays, removing background and irrelevant anatomical structures.
2. Masked Image Classification
Two EfficientNet-B3 classifiers operate only on segmented lung regions, leading to improved diagnostic accuracy.
3. Dual-Model Decision Logic
| Condition | Final Output |
|---|---|
| Both bacterial & viral probabilities are low | NORMAL |
| Bacterial probability high only | BACTERIAL PNEUMONIA |
| Viral probability high only | VIRAL PNEUMONIA |
| Both probabilities high | Chooses the higher probability |
4. Important Note on Viral vs Bacterial Overlap
Chest X-ray images of viral and bacterial pneumonia often look very similar, with overlapping radiological patterns such as:
- Diffuse opacities
- Patchy consolidations
- Similar lung distribution
Because of this natural similarity, perfect separation between viral and bacterial pneumonia is not guaranteed, and classifiers may show overlapping confidence values. A fallback rule chooses the class with the higher probability when both are high.
Model Details
UNet
- Input: 300 × 300
- Output: Lung segmentation mask
- Purpose: Improves classifier performance by isolating lung regions and removing irrelevant anatomy.
EfficientNet-B3 Classifiers
Two separate binary classifiers are used:
- Normal vs Bacterial
- Normal vs Viral
This modular setup allows each pneumonia type to be diagnosed independently and increases robustness.
Performance Metrics
1. UNet Lung Segmentation
The segmentation model achieves high spatial accuracy, ensuring downstream classifiers receive clean, lung-focused inputs.
| Metric | Score |
|---|---|
| Accuracy | 0.9899 |
| Precision | 0.9828 |
| Recall | 0.9749 |
| F1 Score | 0.9788 |
| IoU (Intersection over Union) | 0.9585 |
| Dice Coefficient | 0.9788 |
2. Normal vs Bacterial Pneumonia Classifier (EfficientNet-B3)
Class-wise Performance
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Normal (0) | 0.98 | 0.97 | 0.97 | 475 |
| Bacterial (1) | 0.98 | 0.99 | 0.99 | 834 |
Overall Metrics
| Metric | Score |
|---|---|
| Accuracy | 0.98 |
| Macro Avg F1 | 0.98 |
| Weighted Avg F1 | 0.98 |
Confusion Matrix:
ROC Curve:

3. Normal vs Viral Pneumonia Classifier (EfficientNet-B3)
Class-wise Performance
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Normal (0) | 0.99 | 0.99 | 0.99 | 475 |
| Viral (1) | 0.99 | 0.99 | 0.99 | 448 |
Overall Metrics
| Metric | Score |
|---|---|
| Accuracy | 0.99 |
| Macro Avg F1 | 0.99 |
| Weighted Avg F1 | 0.99 |
Confusion Matrix:

ROC Curve:

⚠️ Important Interpretation Note
Despite strong quantitative performance, viral and bacterial pneumonia can present with highly overlapping radiological features, making them difficult to distinguish purely from chest X-ray images — even for trained radiologists.
Therefore:
- Some misclassification between Viral ↔ Bacterial is expected.
- The system applies a fallback decision rule when both probabilities are high.
- Outputs should not be used for any form of clinical diagnosis or medical decision-making.
License
MIT License