-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeep_learning_models.py
More file actions
225 lines (185 loc) · 9.98 KB
/
deep_learning_models.py
File metadata and controls
225 lines (185 loc) · 9.98 KB
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
import os
import csv
import time
import numpy as np
import pandas as pd
#Deep Learning Framework
import tensorflow as tf
#Metrics
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_auc_score
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
class batch_timer(tf.keras.callbacks.Callback):
""" Callback to save the time of training and testing
Args:
tf (_type_): _description_
"""
def __init__(self, file_train, file_test):
super(batch_timer, self).__init__()
self.file_test = file_test
self.file_train = file_train
def on_epoch_begin(self, epoch, logs=None):
self.start_time_train = time.time()
def on_epoch_end(self, epoch, logs=None):
stop_time_train = time.time()
time_train = stop_time_train-self.start_time_train
with open(file=self.file_train, mode="a", encoding="UTF8", newline="") as csvW:
writer = csv.writer(csvW)
writer.writerow([str(epoch), str(time_train)])
def on_test_begin(self, logs=None):
self.start_time_test = time.time()
def on_test_end(self, logs=None):
stop_time_test = time.time()
time_train = stop_time_test-self.start_time_test
with open(file=self.file_test, mode="a", encoding="UTF8", newline="") as csvW:
writer = csv.writer(csvW)
writer.writerow([str(time_train)])
#Metrics for Deep Learning models
METRICS = [
"accuracy",
tf.keras.metrics.TruePositives(name='tp'),
tf.keras.metrics.FalsePositives(name='fp'),
tf.keras.metrics.TrueNegatives(name='tn'),
tf.keras.metrics.FalseNegatives(name='fn'),
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall'),
tf.keras.metrics.AUC(name='auc'),
tf.keras.metrics.AUC(name='prc', curve='PR'), # precision-recall curve
]
def create_model(model_name, num_deep_radiomics, input_shape, num_classes)-> object:
""" Create a Deep Learning model with transfer learning and fine-tuning
Args:
model_name (_type_): _description_
num_deep_radiomics (_type_): _description_
input_shape (_type_): _description_
num_classes (_type_): _description_
Returns:
object: _description_
"""
base_model = eval("tf.keras.applications." + model_name + "(weights = 'imagenet', input_shape=input_shape, include_top = False)")
for layer in base_model.layers:
layer.trainable = False
x = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)
x = tf.keras.layers.Dense(2048, activation='relu')(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(1024, activation='relu')(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(512, activation='relu')(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(num_deep_radiomics, activation='relu', name='layer_deep_radiomics')(x)
x = tf.keras.layers.Dropout(0.2)(x)
preds = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
model = tf.keras.Model(inputs = base_model.input, outputs = preds)
for layer in model.layers:
if layer.trainable != False:
layer.trainable = True
opt = tf.keras.optimizers.Adam(learning_rate = 0.001)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=METRICS)
return model
def training_model(model, model_name, num_deep_radiomics, input_shape, X_train, y_train, X_test, y_test, epochs)-> None:
""" Training a Deep Learning model with transfer learning and fine-tuning
Args:
model (_type_): _description_
model_name (_type_): _description_
num_deep_radiomics (_type_): _description_
X_train (_type_): _description_
y_train (_type_): _description_
X_test (_type_): _description_
y_test (_type_): _description_
"""
lr_reduce = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', factor=0.1, min_delta=1e-5, patience=5, verbose=0)
early = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5, mode='max')
filepath = os.path.join('./', model_name, f"{num_deep_radiomics}_deepradiomics", f'DL_{model_name}_{num_deep_radiomics}_best_model.keras' )
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_accuracy', verbose=0, save_best_only=True, mode='max')
scores_results_dl = []
runtime_train = 0.0
start_time_train = 0.0
with tf.device('/GPU:0'):
csv_logger = tf.keras.callbacks.CSVLogger(os.path.join('./', model_name, f"{num_deep_radiomics}_deepradiomics", f'DL_{model_name}_{num_deep_radiomics}_metrics.csv'))
start_time_train = time.time()
history = {}
history = model.fit(X_train,
y_train,
batch_size=32,
epochs=epochs,
verbose=1,
validation_data=(X_test, y_test),
callbacks=[early,
lr_reduce,
csv_logger,
checkpoint,
batch_timer(os.path.join('./', model_name, f"{num_deep_radiomics}_deepradiomics", f'DL_{model_name}_{num_deep_radiomics}_time_train.csv'),
os.path.join('./', model_name, f"{num_deep_radiomics}_deepradiomics", f'DL_{model_name}_{num_deep_radiomics}_time_test.csv'))
]
)
#Save trained model
model.save(os.path.join('./', model_name, f"{num_deep_radiomics}_deepradiomics", f'DL_{model_name}_{num_deep_radiomics}_trained_model.keras'))
runtime_train = time.time() - start_time_train
y_test_arg = np.argmax(y_test, axis=1)
y_pred = np.argmax(model.predict(X_test), axis=1)
print(f'\n[INFO] Model {model_name} performance...\n')
#Metrics
accuracy_score_ = accuracy_score(y_test_arg, y_pred)
roc_auc_score_ = roc_auc_score(y_test_arg, y_pred)
f1_score_ = f1_score(y_test_arg, y_pred, average='macro')
precision_score_ = precision_score(y_test_arg, y_pred, average='macro')
recall_score_ = recall_score(y_test_arg, y_pred, average='macro')
classification_report_ = classification_report(y_test_arg, y_pred)
confusion_matrix_ = confusion_matrix(y_test_arg, y_pred)
print(f"Acurracy: {accuracy_score_}")
print(f"ROC AUC: {roc_auc_score_}")
print(f"F1-Score: {f1_score_}")
print(f"Precision: {precision_score_}")
print(f"Recall: {recall_score_}")
print(f"Classification Report: \n {classification_report_}")
print(f"Confusion Matrix: \n {confusion_matrix_}")
scores_results_dl.append({
'classifier': model_name,
'accuracy_score': accuracy_score_,
'roc_auc_score': roc_auc_score_,
'f1_score': f1_score_,
'precision_score': precision_score_,
'recall_score': recall_score_,
'classification_report': classification_report_,
'confusion_matrix': confusion_matrix_,
'time': runtime_train
})
df_dl_results = pd.DataFrame(scores_results_dl,
columns=['classifier',
'accuracy_score',
'roc_auc_score',
'f1_score',
'precision_score',
'recall_score',
'classification_report',
'confusion_matrix',
'time'])
df_dl_results.to_csv(os.path.join('./', model_name, f"{num_deep_radiomics}_deepradiomics",
f'DL_{model_name}_{num_deep_radiomics}_sklearn_results.csv'), index=None)
def save_deep_radiomic_features(model_name, num_deep_radiomics, dataset_images, label_ml) -> None:
""" Save deep radiomic features extracted from Deep Learning models
Args:
model_name (_type_): _description_
num_deep_radiomics (_type_): _description_
dataset_images (_type_): _description_
label_ml (_type_): _description_
"""
model = tf.keras.models.load_model(os.path.join('./', model_name, f"{num_deep_radiomics}_deepradiomics",
f'DL_{model_name}_{num_deep_radiomics}_trained_model.keras'))
layer_deep_radiomics = tf.keras.Model(inputs=model.input, outputs=model.get_layer('layer_deep_radiomics').output)
deep_radiomics_features_extracted = layer_deep_radiomics.predict(dataset_images)
deep_radiomics_features_extracted = pd.DataFrame(deep_radiomics_features_extracted)
deep_radiomics_features_extracted.to_pickle(os.path.join('./', model_name,
f"{num_deep_radiomics}_deepradiomics",
f'DL_{model_name}_{num_deep_radiomics}_extracted_deepradiomics_notlabel.pkl'))
deep_radiomics_features = pd.read_pickle(os.path.join('./', model_name,
f"{num_deep_radiomics}_deepradiomics",
f'DL_{model_name}_{num_deep_radiomics}_extracted_deepradiomics_notlabel.pkl'))
deep_radiomics_features['label'] = label_ml
deep_radiomics_features.to_csv(os.path.join('./', model_name,
f"{num_deep_radiomics}_deepradiomics",
f'DL_{model_name}_{num_deep_radiomics}_extracted_deepradiomics_withlabel.csv'), index=None)