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 12 |
|
| 13 |
+
# Classification With Segmentation
|
| 14 |
+
|
| 15 |
+
This project implements a complete chest X-ray analysis pipeline using:
|
| 16 |
+
|
| 17 |
+
**lung segmentation using UNet (Unet.ipynb)**
|
| 18 |
+
|
| 19 |
+
**Two binary EfficientNet-B3 classifiers (pneumonia classification.ipynb):**
|
| 20 |
+
|
| 21 |
+
- Normal vs Bacterial Pneumonia
|
| 22 |
+
- Normal vs Viral Pneumonia
|
| 23 |
+
|
| 24 |
+
**Intelligent decision fusion**
|
| 25 |
+
|
| 26 |
+
- If both classifiers fire, the model selects the dominant probability and returns a final diagnosis.
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
## Dataset
|
| 30 |
+
- For Unet [Lung Mask Image Dataset](https://www.kaggle.com/datasets/newra008/lung-mask-image-dataset)
|
| 31 |
+
- For binary EfficientNet-B3 classifiers [Pneumonia Multi-Class Dataset ](https://www.kaggle.com/datasets/kmljts/pneumonia-multiclass)
|
| 32 |
+
## Interactive Gradio Web App
|
| 33 |
+
- check out the live web app click [here](https://huggingface.co/spaces/Clocksp/classification_with_segmentation)
|
| 34 |
+
- For Models click [here](https://huggingface.co/spaces/Clocksp/classification_with_segmentation/tree/main)
|
| 35 |
+
### Includes
|
| 36 |
+
- Lung segmentation mask
|
| 37 |
+
- Masked classifier input
|
| 38 |
+
- Bacterial & viral probability scores
|
| 39 |
+
- Final diagnosis
|
| 40 |
+
- Segmentation overlay
|
| 41 |
+
- Static Test Samples Table at the bottom
|
| 42 |
+
|
| 43 |
+
---
|
| 44 |
+
## Features
|
| 45 |
+
|
| 46 |
+
### 1. Lung Segmentation (UNet)
|
| 47 |
+
The UNet model extracts lung regions from chest X-rays, removing background and irrelevant anatomical structures.
|
| 48 |
+
|
| 49 |
+
### 2. Masked Image Classification
|
| 50 |
+
Two EfficientNet-B3 classifiers operate only on segmented lung regions, leading to improved diagnostic accuracy.
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
### 3. Dual-Model Decision Logic
|
| 54 |
+
|
| 55 |
+
| Condition | Final Output |
|
| 56 |
+
|------------------------------|--------------------------|
|
| 57 |
+
| Both bacterial & viral probabilities are low | **NORMAL** |
|
| 58 |
+
| Bacterial probability high only | **BACTERIAL PNEUMONIA** |
|
| 59 |
+
| Viral probability high only | **VIRAL PNEUMONIA** |
|
| 60 |
+
| Both probabilities high | Chooses the **higher** probability |
|
| 61 |
+
|
| 62 |
+
### 4. Important Note on Viral vs Bacterial Overlap
|
| 63 |
+
|
| 64 |
+
Chest X-ray images of viral and bacterial pneumonia often look very similar, with overlapping radiological patterns such as:
|
| 65 |
+
|
| 66 |
+
- Diffuse opacities
|
| 67 |
+
- Patchy consolidations
|
| 68 |
+
- Similar lung distribution
|
| 69 |
+
|
| 70 |
+
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.
|
| 71 |
+
|
| 72 |
+
---
|
| 73 |
+
|
| 74 |
+
## Model Details
|
| 75 |
+
|
| 76 |
+
### UNet
|
| 77 |
+
- **Input:** 300 × 300
|
| 78 |
+
- **Output:** Lung segmentation mask
|
| 79 |
+
- **Purpose:** Improves classifier performance by isolating lung regions and removing irrelevant anatomy.
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
### EfficientNet-B3 Classifiers
|
| 83 |
+
Two separate binary classifiers are used:
|
| 84 |
+
|
| 85 |
+
- **Normal vs Bacterial**
|
| 86 |
+
- **Normal vs Viral**
|
| 87 |
+
|
| 88 |
+
This modular setup allows each pneumonia type to be diagnosed independently and increases robustness.
|
| 89 |
+
|
| 90 |
+
---
|
| 91 |
+
|
| 92 |
+
## Performance Metrics
|
| 93 |
+
|
| 94 |
+
### 1. UNet Lung Segmentation
|
| 95 |
+
The segmentation model achieves high spatial accuracy, ensuring downstream classifiers receive clean, lung-focused inputs.
|
| 96 |
+
|
| 97 |
+
| Metric | Score |
|
| 98 |
+
|------------------------------|---------|
|
| 99 |
+
| Accuracy | 0.9899 |
|
| 100 |
+
| Precision | 0.9828 |
|
| 101 |
+
| Recall | 0.9749 |
|
| 102 |
+
| F1 Score | 0.9788 |
|
| 103 |
+
| IoU (Intersection over Union) | 0.9585 |
|
| 104 |
+
| Dice Coefficient | 0.9788 |
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
### 2. Normal vs Bacterial Pneumonia Classifier (EfficientNet-B3)
|
| 108 |
+
|
| 109 |
+
#### Class-wise Performance
|
| 110 |
+
| Class | Precision | Recall | F1-Score | Support |
|
| 111 |
+
|---------------|-----------|--------|----------|---------|
|
| 112 |
+
| Normal (0) | 0.98 | 0.97 | 0.97 | 475 |
|
| 113 |
+
| Bacterial (1) | 0.98 | 0.99 | 0.99 | 834 |
|
| 114 |
+
|
| 115 |
+
#### Overall Metrics
|
| 116 |
+
| Metric | Score |
|
| 117 |
+
|---------------|-------|
|
| 118 |
+
| Accuracy | 0.98 |
|
| 119 |
+
| Macro Avg F1 | 0.98 |
|
| 120 |
+
| Weighted Avg F1 | 0.98 |
|
| 121 |
+
|
| 122 |
+
**Confusion Matrix:**
|
| 123 |
+
|
| 124 |
+
<img src="Assests/c1.png">
|
| 125 |
+
|
| 126 |
+
**ROC Curve:**
|
| 127 |
+
<img src="Assests/r1.png">
|
| 128 |
+
|
| 129 |
+
### 3. Normal vs Viral Pneumonia Classifier (EfficientNet-B3)
|
| 130 |
+
|
| 131 |
+
#### Class-wise Performance
|
| 132 |
+
| Class | Precision | Recall | F1-Score | Support |
|
| 133 |
+
|---------------|-----------|--------|----------|---------|
|
| 134 |
+
| Normal (0) | 0.99 | 0.99 | 0.99 | 475 |
|
| 135 |
+
| Viral (1) | 0.99 | 0.99 | 0.99 | 448 |
|
| 136 |
+
|
| 137 |
+
#### Overall Metrics
|
| 138 |
+
| Metric | Score |
|
| 139 |
+
|---------------|-------|
|
| 140 |
+
| Accuracy | 0.99 |
|
| 141 |
+
| Macro Avg F1 | 0.99 |
|
| 142 |
+
| Weighted Avg F1 | 0.99 |
|
| 143 |
+
|
| 144 |
+
**Confusion Matrix:**
|
| 145 |
+
<img src="Assests/c2.png">
|
| 146 |
+
|
| 147 |
+
**ROC Curve:**
|
| 148 |
+
<img src="Assests/r2.png">
|
| 149 |
+
|
| 150 |
+
---
|
| 151 |
+
## ⚠️ Important Interpretation Note
|
| 152 |
+
|
| 153 |
+
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.
|
| 154 |
+
|
| 155 |
+
**Therefore:**
|
| 156 |
+
|
| 157 |
+
- Some misclassification between **Viral ↔ Bacterial** is expected.
|
| 158 |
+
- The system applies a **fallback decision rule** when both probabilities are high.
|
| 159 |
+
- Outputs should **not** be used for any form of clinical diagnosis or medical decision-making.
|
| 160 |
+
|
| 161 |
+
---
|
| 162 |
+
|
| 163 |
+
## License
|
| 164 |
+
MIT License
|