ryefoxlime commited on
Commit
9ecc680
1 Parent(s): f061fc3

Push Keras model using huggingface_hub.

Browse files
README.md CHANGED
@@ -1,222 +1,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
 
 
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
 
fingerprint.pb CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:8d69524a8f6193efcdabd099d738081c821c301f454e2c593d0ad57d78ccd229
3
- size 56
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d75565bde2e120514e0777ab11eb4dae9bc76142cdfff80c384b14d673b9afd9
3
+ size 58
keras_metadata.pb CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:b564e495de0a099eeb5a15317769a4e0c8193079643bc825b2e19e04b8e3f83c
3
- size 608692
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:40385983b225ba4b3c5b3fb9f9ee81c5a31c1c996b22cf7d1d37eb1e65b1f217
3
+ size 608703
logs/train/events.out.tfevents.1700989167.acdff595b88d.1243.3.v2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:54c0791f9274188defb66a1c88bca44df4824f4249eaf2e7d068e1d45d9c39f6
3
+ size 130259
logs/train/events.out.tfevents.1700990956.acdff595b88d.1243.5.v2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:89f28fe122141253fd86aa3d02e4c17a14e6b5f1402d6b87c5b572b674b0c9bf
3
+ size 129809
logs/train/events.out.tfevents.1700992331.acdff595b88d.1243.7.v2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0a477c6c82ef4dadc55e21fd5f347a3092f76ec9a648ae03a41e61933a03bb98
3
+ size 129809
logs/validation/events.out.tfevents.1700989262.acdff595b88d.1243.4.v2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2a0b2207fafbc82662b8eb6d4c4c481bc110b668103c1bd0fff597bba4808594
3
+ size 3652
logs/validation/events.out.tfevents.1700991055.acdff595b88d.1243.6.v2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f80171bf6d0d36e509e62f64ab8006402e5f54814b3058ff40780900482e1236
3
+ size 2936
logs/validation/events.out.tfevents.1700992428.acdff595b88d.1243.8.v2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cd6d462dec01a92de4b97377633f3f4d0b6f97c46e634b99b92deaf5894c296d
3
+ size 2936
model.png CHANGED
saved_model.pb CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:935b3fa4655c56a74072f22b0f3e6bf0828d793fe054ed38b1dba09651434bd3
3
- size 2852377
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:66e8a2095ea1e41c47f4f07cf8cbbb491b37d401e4e2903149521926f32d8637
3
+ size 2879673
variables/variables.data-00000-of-00001 CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:baa32120847c595572f9d598f913730b7c3e9e8e85ae6e7aa296d062584d320d
3
- size 113426868
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:66ee06de7b89855a8cbe49575ca626d7d46fd5f095ec580ecf99e75e7f8debc3
3
+ size 151444519
variables/variables.index CHANGED
Binary files a/variables/variables.index and b/variables/variables.index differ