File size: 8,469 Bytes
2842a14
7b2ba8f
 
 
 
2842a14
7b2ba8f
 
cf8e5ec
 
 
 
58aa02e
1b5ea11
 
 
 
 
e8d3934
1c740da
2842a14
7b2ba8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd7e1e7
7b2ba8f
bd7e1e7
7b2ba8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2842a14
7b2ba8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2842a14
7b2ba8f
2842a14
7b2ba8f
 
 
 
 
 
 
 
2842a14
7b2ba8f
2842a14
7b2ba8f
2842a14
bd7e1e7
2842a14
7b2ba8f
2842a14
7b2ba8f
2842a14
3801f04
2842a14
3801f04
 
 
 
 
 
 
 
 
cf8e5ec
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
---
language:
- en
metrics:
- accuracy
library_name: keras
tags:
- medical
- bio
- biology
- Pneumonia_detection
- X-Ray_Classfication
- image-classification
widget:
- src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/tiger.jpg
  example_title: Tiger
- src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/teapot.jpg
  example_title: Teapot
base_model: microsoft/resnet-50
inference: True
---
# ryefoxlime/PneumoniaDetection

## Model Details
I have developed a robust model that utilizes transfer learning and the powerful ResNet50V2 architecture to effectively classify chest X-ray images into two categories: pneumonia and normal. This model demonstrates high accuracy and generalizability, making it a promising tool for assisting in pneumonia diagnosis.

### Model Description
The ResNet50V2:
ResNet50V2 is a deep convolutional neural network (CNN) architecture, part of the ResNet (Residual Networks) family. It's known for its depth, utilizing residual blocks that help address the vanishing gradient problem during training. The "V2" signifies an improvement over the original ResNet50, incorporating tweaks to enhance performance.

Transfer Learning:
Transfer learning involves leveraging a pre-trained model's knowledge on a large dataset and applying it to a different but related task. For our use case, ResNet50V2, which has been trained on a diverse dataset, is adapted to classify pneumonia-related images.

Image Classification:
The core task of the model is to categorize images into two classes: "affected by pneumonia" and "normal." This binary classification is crucial for diagnosing medical conditions based on visual information in images.

Model Training:
During training, the pre-trained ResNet50V2 model is used as a feature extractor. The existing weights are frozen, preventing further training on the original dataset, and a new classifier tailored to this specific task is added. This new classifier is trained using the labeled dataset of pneumonia and normal images.

Loss Function and Optimization:
To guide the training process, a loss function is employed to measure the difference between predicted and actual labels. Common choices for image classification tasks include categorical cross-entropy. An optimizer, such as stochastic gradient descent (SGD) or Adam. In our case we have used Adam as our optimier of choice, which is used to adjust the model's weights based on the calculated loss.

Evaluation:
The model's performance is assessed using a separate dataset not seen during training. Metrics like accuracy, precision, recall, and F1-score are often used to gauge how well the model generalizes to new, unseen data.

Deployment:
Once the model demonstrates satisfactory performance, it can be deployed for real-world use. This involves integrating it into a system or application where it can receive new images, make predictions, and aid in the diagnosis of pneumonia.

- **Developed by:** [Nitin Kausik Remella](https://github.com/OkabeRintaro10)
- **Model type:** Sequential 
- **Language(s):** Python
- **Finetuned from model:** ResNet50V2

### Model Sources

- **Paper:** [A modified deep convolutional neural network for detecting COVID-19 and pneumonia from chest X-ray images based on the concatenation of Xception and ResNet50V2
](https://pubmed.ncbi.nlm.nih.gov/32501424/)

## Uses
This tool is used to assist medical professional in cross-validation of the diagnosis 

### Out-of-Scope Use

This model is in no form or way to replace an actual medical professional but only in assist them


## Bias, Risks, and Limitations

The model cant handle 4d images such as CT scans

## How to Get Started with the Model

```
import tensorflow as tf
from tensorflow import keras
from keras import models
model = load_model('/path/to/model')
model.evaluate('/path/to/image')
```

## Training Details

### Training Data

Downloading the dataset from [kaggle](https://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia)
split the data into 3 parts
- train
- test
- val

code to split into 25% 75% split of training data
```
# Creating Val folder
os.chdir('datasets/chest_xray/chest_xray/')
if os.path.isdir('val/NORMAL') is False:
  os.makedirs('val/NORMAL')
  os.makedirs('val/PNEUMONIA')

# Moving Images from train folder to val folder
source = 'chest_xray/train/PNEUMONIA/'
dest = 'datasets/chest_xray/chest_xray/val/PNEUMONIA'
files = os.listdir(source)
np_of_files = len(files) // 25
for file_name in random.sample(files, np_of_files):
        shutil.move(os.path.join(source, file_name), dest)

# Moving Normal Images from train folder to val folder
source = 'datasets/chest_xray/chest_xray/train/NORMAL/'
dest = 'datasets/chest_xray/chest_xray/val/NORMAL'
files = os.listdir(source)
np_of_files = len(files) // 25
for file_name in random.sample(files, np_of_files):
        shutil.move(os.path.join(source, file_name), dest)
```

### Training Procedure 
The training of the data requires ResNet50V2 to start as the base model and then using further layers to extract more information and to help in classification

#### Building the model
```
from keras.applications import VGG16, ResNet50V2

base_model = ResNet50V2(
    include_top=False, input_shape=(224, 224, 3), weights="imagenet"
)
base_model.trainable = False

def CreateModel():
    model = Sequential()
    model.add(base_model)
    # model.add(Conv2D(filters=32, kernel_size=3, strides=(2, 2)))
    model.add(AveragePooling2D(pool_size=(2, 2), strides=2))
    model.add(Flatten())
    model.add(Dense(256, activation="relu"))
    model.add(Dense(128, activation="relu"))
    model.add(Dense(2, activation="softmax"))
    model.compile(
        loss="sparse_categorical_crossentropy",
        optimizer=Adam(learning_rate=0.000035),
        metrics=["sparse_categorical_accuracy"],
    )
    return model
```
#### Fitting the model
```
%%time
history = model.fit(
    train_datagen,
    steps_per_epoch = train_datagen.n//train_datagen.batch_size,
    epochs = 10,
    validation_data= val_datagen,
    validation_steps= val_datagen.n//val_datagen.batch_size,
    callbacks=[callback, reduceLR, checkpoint],
    verbose = 1
)
```
#### Preprocessing

```
train_image_generator = ImageDataGenerator(
    rotation_range= 0.5,
    horizontal_flip=True,
    vertical_flip=True,
    zoom_range=0.5,
    rescale= 1./255
)

train_datagen = train_image_generator.flow_from_directory(
    train_dir,
    target_size= (IMG_HEIGHT,IMG_WIDTH),
    color_mode='rgb',
    batch_size= batch_size,
    class_mode= 'binary',
    classes=['NORMAL','PNEUMONIA'],
    shuffle= True,
    seed= 42
    )
```
set the value `shuffle=False` for val_datagen and test_datagen and change the value of `train_dir` to `val_dir` and 'test_dir' respectively

#### Training Hyperparameters

- **Training regime:**
- Using keras callbacks to reduce the load on the gpu/cpu by checking the model growth and early stopping or reducing the learning rate accordingly.
- Saving the best accuracy as a checkpoint to resume the training from
```
  from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint

callback = EarlyStopping(
    monitor="val_loss", patience=6, restore_best_weights=True, min_delta=0.03, verbose=2
)
reduceLR = ReduceLROnPlateau(
    monitor="val_loss",
    factor=0.01,
    patience=2,
    min_lr=0.000035,
    min_delta=0.01,
    verbose=2,
)
checkpoint = ModelCheckpoint(
    filepath=f"../Checkpoints/{{val_sparse_categorical_accuracy:.2f}}",
    save_weights_only=True,
    monitor="val_sparse_categorical_accuracy",
    mode="max",
    save_best_only=True,
    verbose=2,
    initial_value_threshold= baseline
)
  ```

#### Define Defaults

Batch_size = 32 *smaller batch size for weaker systems*
IMG_HEIGHTS = 224
IMG_WEIGHTS = 224
epochs = 10
train_dir = path/to/chest_xray/train
val_dir = path/to/chest_xray/val
test_dir = path/to/chest_xray/test
#### Metrics

Evaluation metrics used are recall and pricision

### Results

![image/png](https://cdn-uploads.huggingface.co/production/uploads/652917fd8297110ffe4e04ba/XEy2GE9mMMjZRoHT-pp_D.png)

#### Summary

The model is capable of detecting pneumonia with an accuracy of 91%

The following hyperparameters were used during training:

| Hyperparameters | Value |
| :-- | :-- |
| name | Adam |
| learning_rate | 3.5e-05 |
| decay | 0.0 |
| beta_1 | 0.9 |
| beta_2 | 0.999 |
| epsilon | 1e-07 |
| amsgrad | False |
| training_precision | float32 |