File size: 2,942 Bytes
2eb6d08
335b580
 
 
 
2eb6d08
335b580
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0e518f
335b580
 
 
 
 
b0e518f
335b580
 
 
 
 
 
 
 
 
 
 
 
b0e518f
 
 
 
335b580
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0e518f
335b580
b0e518f
335b580
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2eb6d08
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
---
license: cc-by-nc-4.0
datasets:
  - chest-xray-pneumonia
library_name: PyTorch
tags:
  - pneumonia-detection
  - cnn
  - medical-imaging
  - binary-classification
  - chest-xray
  - healthcare
  - pytorch
model-index:
  - name: ImprovedPneumoniaCNN
    results:
      - task:
          type: image-classification
          name: Pneumonia Detection
        metrics:
          - name: Accuracy
            type: accuracy
            value: 0.9676
          - name: F1 Score
            type: f1
            value: 0.9685
          - name: AUC
            type: auc
            value: 0.9959
          - name: Loss
            type: loss
            value: 0.0778
---

# ImprovedPneumoniaCNN: Pneumonia Detection from Chest X-rays

This repository hosts `ImprovedPneumoniaCNN`, a custom Convolutional Neural Network model designed to detect **Pneumonia** from chest X-ray images. It incorporates enhancements like dropout, batch normalization, SiLU activation, and Convolutional Block Attention Module (CBAM) for improved robustness and generalization.

---

## Evaluation Results

| Metric   | Score   |
|----------|---------|
| Accuracy | 96.76%  |
| F1 Score | 0.9685  |
| AUC      | 0.9959  |
| Loss     | 0.0778  |

---

### Confusion Matrix

| Normal| Pneumonia |
|-------|-----------|
| 1680  | 42        |
| 74    | 1782      |

---

### Classification Report

| Class     | Precision | Recall | F1-Score | Support |
|-----------|-----------|--------|----------|---------|
| Normal    | 0.96      | 0.98   | 0.97     | 1722    |
| Pneumonia | 0.98      | 0.96   | 0.97     | 1856    |

---

## 🏗️ Architecture Highlights

- Custom CNN with residual blocks
- Uses **CBAM** attention for spatial and channel refinement
- **SiLU** activation for better non-linearity
- **Dropout** and **BatchNorm** for regularization
- Final **Global Average Pooling** + FC layer

---

## How to Use

### Install Dependencies

```bash
pip install torch torchvision albumentations scikit-learn matplotlib seaborn
import torch
from torchvision import transforms
from PIL import Image
from model import ImprovedPneumoniaCNN  # make sure model is defined/imported

# Load model
model = ImprovedPneumoniaCNN()
model.load_state_dict(torch.load("improved_pneumonia_cnn.pth", map_location=torch.device('cpu')))
model.eval()

# Preprocess image
transform = transforms.Compose([
    transforms.Grayscale(),
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
])

img = Image.open("path_to_chest_xray.jpg")
img_tensor = transform(img).unsqueeze(0)

# Predict
with torch.no_grad():
    output = model(img_tensor)
    prediction = torch.sigmoid(output).item()
    print("Pneumonia" if prediction > 0.5 else "Normal")
```
---

## Contributors
- [Thiyaga158](https://huggingface.co/Thiyaga158)

---

## License

This model is licensed under [CC BY-NC 3.0](https://creativecommons.org/licenses/by-nc/3.0/).  
For research and educational use only.

---