File size: 5,235 Bytes
43b61ba d6bbbab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
---
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](https://www.kaggle.com/datasets/newra008/lung-mask-image-dataset)
- For binary EfficientNet-B3 classifiers [Pneumonia Multi-Class Dataset ](https://www.kaggle.com/datasets/kmljts/pneumonia-multiclass)
## Interactive Gradio Web App
- check out the live web app click [here](https://huggingface.co/spaces/Clocksp/classification_with_segmentation)
- For Models click [here](https://huggingface.co/spaces/Clocksp/classification_with_segmentation/tree/main)
### 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:**
<img src="Assests/c1.png">
**ROC Curve:**
<img src="Assests/r1.png">
### 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:**
<img src="Assests/c2.png">
**ROC Curve:**
<img src="Assests/r2.png">
---
## ⚠️ 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
|