ryefoxlime commited on
Commit
f061fc3
1 Parent(s): ed42291

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +208 -8
README.md CHANGED
@@ -1,22 +1,222 @@
1
  ---
 
 
 
 
2
  library_name: keras
 
 
 
3
  ---
 
4
 
5
- ## Model description
 
6
 
7
- More information needed
 
 
8
 
9
- ## Intended uses & limitations
 
10
 
11
- More information needed
 
12
 
13
- ## Training and evaluation data
 
14
 
15
- More information needed
 
16
 
17
- ## Training procedure
 
18
 
19
- ### Training hyperparameters
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  The following hyperparameters were used during training:
22
 
 
1
  ---
2
+ language:
3
+ - en
4
+ metrics:
5
+ - accuracy
6
  library_name: keras
7
+ tags:
8
+ - medical
9
+ pipeline_tag: image-classification
10
  ---
11
+ # ryefoxlime/PneumoniaDetection
12
 
13
+ ## Model Details
14
+ 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.
15
 
16
+ ### Model Description
17
+ The ResNet50V2:
18
+ 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.
19
 
20
+ Transfer Learning:
21
+ 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.
22
 
23
+ Image Classification:
24
+ 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.
25
 
26
+ Model Training:
27
+ 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.
28
 
29
+ Loss Function and Optimization:
30
+ 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.
31
 
32
+ Evaluation:
33
+ 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.
34
 
35
+ Deployment:
36
+ 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.
37
+
38
+ - **Developed by:** [Nitin Kausik Remella](https://github.com/OkabeRintaro10)
39
+ - **Model type:** Sequential
40
+ - **Language(s):** Python
41
+ - **Finetuned from model:** ResNet50V2
42
+
43
+ ### Model Sources
44
+
45
+ - **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
46
+ ](https://pubmed.ncbi.nlm.nih.gov/32501424/)
47
+
48
+ ## Uses
49
+ This tool is used to assist medical professional in cross-validation of the diagnosis
50
+
51
+ ### Out-of-Scope Use
52
+
53
+ This model is in no form or way to replace an actual medical professional but only in assist them
54
+
55
+
56
+ ## Bias, Risks, and Limitations
57
+
58
+ The model cant handle 4d images such as CT scans
59
+
60
+ ## How to Get Started with the Model
61
+
62
+ ```
63
+ import tensorflow as tf
64
+ from tensorflow import keras
65
+ from keras import models
66
+ model = load_model('/path/to/model')
67
+ model.evaluate('/path/to/image')
68
+ ```
69
+
70
+ ## Training Details
71
+
72
+ ### Training Data
73
+
74
+ Downloading the dataset from [kaggle](https://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia)
75
+ split the data into 3 parts
76
+ - train
77
+ - test
78
+ - val
79
+
80
+ code to split into 25% 75% split of training data
81
+ ```
82
+ # Creating Val folder
83
+ os.chdir('datasets/chest_xray/chest_xray/')
84
+ if os.path.isdir('val/NORMAL') is False:
85
+ os.makedirs('val/NORMAL')
86
+ os.makedirs('val/PNEUMONIA')
87
+
88
+ # Moving Images from train folder to val folder
89
+ source = 'chest_xray/train/PNEUMONIA/'
90
+ dest = 'datasets/chest_xray/chest_xray/val/PNEUMONIA'
91
+ files = os.listdir(source)
92
+ np_of_files = len(files) // 25
93
+ for file_name in random.sample(files, np_of_files):
94
+ shutil.move(os.path.join(source, file_name), dest)
95
+
96
+ # Moving Normal Images from train folder to val folder
97
+ source = 'datasets/chest_xray/chest_xray/train/NORMAL/'
98
+ dest = 'datasets/chest_xray/chest_xray/val/NORMAL'
99
+ files = os.listdir(source)
100
+ np_of_files = len(files) // 25
101
+ for file_name in random.sample(files, np_of_files):
102
+ shutil.move(os.path.join(source, file_name), dest)
103
+ ```
104
+
105
+ ### Training Procedure
106
+ 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
107
+
108
+ #### Building the model
109
+ ```
110
+ from keras.applications import VGG16, ResNet50V2
111
+
112
+ base_model = ResNet50V2(
113
+ include_top=False, input_shape=(224, 224, 3), weights="imagenet"
114
+ )
115
+ base_model.trainable = False
116
+
117
+ def CreateModel():
118
+ model = Sequential()
119
+ model.add(base_model)
120
+ # model.add(Conv2D(filters=32, kernel_size=3, strides=(2, 2)))
121
+ model.add(AveragePooling2D(pool_size=(2, 2), strides=2))
122
+ model.add(Flatten())
123
+ model.add(Dense(256, activation="relu"))
124
+ model.add(Dense(128, activation="relu"))
125
+ model.add(Dense(2, activation="softmax"))
126
+ model.compile(
127
+ loss="sparse_categorical_crossentropy",
128
+ optimizer=Adam(learning_rate=0.000035),
129
+ metrics=["sparse_categorical_accuracy"],
130
+ )
131
+ return model
132
+ ```
133
+ #### Fitting the model
134
+ ```
135
+ %%time
136
+ history = model.fit(
137
+ train_datagen,
138
+ steps_per_epoch = train_datagen.n//train_datagen.batch_size,
139
+ epochs = 10,
140
+ validation_data= val_datagen,
141
+ validation_steps= val_datagen.n//val_datagen.batch_size,
142
+ callbacks=[callback, reduceLR, checkpoint],
143
+ verbose = 1
144
+ )
145
+ ```
146
+ #### Preprocessing
147
+
148
+ ```
149
+ train_image_generator = ImageDataGenerator(
150
+ rotation_range= 0.5,
151
+ horizontal_flip=True,
152
+ vertical_flip=True,
153
+ zoom_range=0.5,
154
+ rescale= 1./255
155
+ )
156
+
157
+ train_datagen = train_image_generator.flow_from_directory(
158
+ train_dir,
159
+ target_size= (IMG_HEIGHT,IMG_WIDTH),
160
+ color_mode='rgb',
161
+ batch_size= batch_size,
162
+ class_mode= 'binary',
163
+ classes=['NORMAL','PNEUMONIA'],
164
+ shuffle= True,
165
+ seed= 42
166
+ )
167
+ ```
168
+ 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
169
+
170
+ #### Training Hyperparameters
171
+
172
+ - **Training regime:**
173
+ - 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.
174
+ - Saving the best accuracy as a checkpoint to resume the training from
175
+ ```
176
+ from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
177
+
178
+ callback = EarlyStopping(
179
+ monitor="val_loss", patience=6, restore_best_weights=True, min_delta=0.03, verbose=2
180
+ )
181
+ reduceLR = ReduceLROnPlateau(
182
+ monitor="val_loss",
183
+ factor=0.01,
184
+ patience=2,
185
+ min_lr=0.000035,
186
+ min_delta=0.01,
187
+ verbose=2,
188
+ )
189
+ checkpoint = ModelCheckpoint(
190
+ filepath=f"../Checkpoints/{{val_sparse_categorical_accuracy:.2f}}",
191
+ save_weights_only=True,
192
+ monitor="val_sparse_categorical_accuracy",
193
+ mode="max",
194
+ save_best_only=True,
195
+ verbose=2,
196
+ initial_value_threshold= baseline
197
+ )
198
+ ```
199
+
200
+ #### Define Defaults
201
+
202
+ Batch_size = 32 *smaller batch size for weaker systems*
203
+ IMG_HEIGHTS = 224
204
+ IMG_WEIGHTS = 224
205
+ epochs = 10
206
+ train_dir = path/to/chest_xray/train
207
+ val_dir = path/to/chest_xray/val
208
+ test_dir = path/to/chest_xray/test
209
+ #### Metrics
210
+
211
+ Evaluation metrics used are recall and pricision
212
+
213
+ ### Results
214
+
215
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/652917fd8297110ffe4e04ba/XEy2GE9mMMjZRoHT-pp_D.png)
216
+
217
+ #### Summary
218
+
219
+ The model is capable of detecting pneumonia with an accuracy of 91%
220
 
221
  The following hyperparameters were used during training:
222