-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
356 lines (287 loc) · 13.4 KB
/
main.py
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import argparse
from argparse import RawTextHelpFormatter
from models import ModifiedSleepEEGNet, FeatureNet, SequenceResidualNet
from preprocessing import *
from metrics import *
def config_gpu():
physical_devices = tf.config.list_physical_devices('GPU')
try:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
except:
# Invalid device or cannot modify virtual devices once initialized.
print("Could not use GPU!!!")
def train_modified_deep_sleep_net(fold_ids, data_files, total_fold, output_dir):
config_gpu()
# model_name = "modified_sleep_eeg"
if len(fold_ids) == 0:
# all folds
fold_ids = range(0, total_fold)
all_fold_result = []
for fold_idx in fold_ids:
if 0 <= fold_idx < total_fold:
print("### FOLD %d ###" % fold_idx)
print_debug("1. Extract dataset, testset for fold %d" % fold_idx)
train, test = split_train_val(fold_idx, data_files, total_fold)
data_train, label_train = train
data_test, label_test = test
# Oversampling
print_debug("2. Oversampling")
os_train, os_label = get_balance_class_oversample(data_train, label_train)
# Shuffling training set
print_debug("3. Shuffle training data")
os_train, os_label = shuffle(os_train, os_label)
# debug_f(info, os_train, os_label)
print_debug("4. First phase training")
fn_fold = FeatureNet()
fn_fold.compile(
optimizer=tf.keras.optimizers.Adam(0.001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()]
)
fn_fold_cp = tf.keras.callbacks.ModelCheckpoint(
filepath="%s/1_cp_fold_%d" % (output_dir, fold_idx),
save_weights_only=True,
monitor="val_loss",
mode="min",
save_best_only=True
)
fn_fold_cbs = [
tf.keras.callbacks.TensorBoard(log_dir="%s/1_fold_%d" % (output_dir, fold_idx)),
tf.keras.callbacks.EarlyStopping(
monitor='val_loss', min_delta=0, patience=3,
mode='auto', baseline=None
),
fn_fold_cp
]
# Fit
if len(os_train.shape) > 3:
os_train = os_train.squeeze(-1)
if len(data_test.shape) > 3:
data_test = data_test.squeeze(-1)
fn_fold.fit(os_train, os_label,
epochs=30, # FIXME: change back to 30 before submit!!!
validation_split=0.4,
batch_size=128,
callbacks=fn_fold_cbs)
# Save first-phase model
first_phase_save_path = "%s/1_model_fold_%d" % (output_dir, fold_idx)
print_debug("Save first phase model to %s" % first_phase_save_path)
fn_fold.save(first_phase_save_path)
# Second-phase training
fn_fold.trainable = False
srn_fold = SequenceResidualNet(fn_fold, lstm_size=128)
srn_fold.compile(
optimizer=tf.keras.optimizers.Adam(0.001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()]
)
srn_fold_checkpoint = tf.keras.callbacks.ModelCheckpoint(
filepath="%s/2_cp_fold_%d" % (output_dir, fold_idx),
save_weights_only=True,
monitor="val_loss",
mode="min",
save_best_only=True
)
srn_fold_cbs = [
tf.keras.callbacks.TensorBoard(log_dir="%s/2_fold_%d" % (output_dir, fold_idx)),
tf.keras.callbacks.EarlyStopping(
monitor='val_loss', min_delta=0, patience=2,
mode='auto', baseline=None
),
srn_fold_checkpoint
]
print_debug("5. Second phase training")
srn_fold.fit(data_train, # train on sequential data, not the oversampled one
label_train,
epochs=3, # FIXME: change to 3 before submit
validation_split=0.4,
shuffle=False,
batch_size=128,
callbacks=srn_fold_cbs)
# Save learn model
save_path = "%s/model_fold_%d" % (output_dir, fold_idx)
print_debug("Saving model to %s" % save_path)
srn_fold.save(save_path)
# Evaluate
confusion, acc, prec, f1 = check_model(srn_fold, data_test, label_test)
kappa_score = cohen_kappa_score_from_confusion_matrix(confusion)
print_debug("6. EVALUATE FOR FOLD %d" % fold_idx)
print_single_result((confusion, acc, f1, kappa_score))
# Store result
all_fold_result += [(confusion, acc, prec, f1, kappa_score)]
try:
with open("%s/result_fold_%d" % (output_dir, fold_idx), "wb") as f:
result_tup = np.array([confusion, acc, prec, f1, kappa_score])
np.save(f, result_tup)
except:
print("Error during saving result for fold %d" % fold_idx)
# Save final result
final_result = np.array(all_fold_result)
try:
with open("%s/final_result" % output_dir, "wb") as f:
np.save(f, final_result)
except:
print("Error during saving final result")
print_summary_result(all_fold_result)
def train_modified_sleep_eeg_net(fold_ids, data_files, total_fold, output_dir):
config_gpu()
# model_name = "modified_sleep_eeg"
if len(fold_ids) == 0:
# all folds
fold_ids = range(0, total_fold)
all_fold_result = []
for fold_idx in fold_ids:
if 0 <= fold_idx < total_fold:
print("### FOLD %d ###" % fold_idx)
print_debug("1. Extract dataset, testset for fold %d" % fold_idx)
train, test = split_train_val(fold_idx, data_files, total_fold)
data_train, label_train = train
data_test, label_test = test
# Oversampling
print_debug("2. Oversampling")
os_train, os_label = get_balance_class_oversample(data_train, label_train)
# Shuffling training set
print_debug("3. Shuffle training data")
os_train, os_label = shuffle(os_train, os_label)
# debug_f(info, os_train, os_label)
if len(os_train.shape) > 3:
os_train = os_train.squeeze(-1)
if len(data_test.shape) > 3:
data_test = data_test.squeeze(-1)
debug_f(info, os_train, os_label)
# Initialize model
modifiedSleepEEGModel = ModifiedSleepEEGNet()
opt = tf.keras.optimizers.Adam(1e-3)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
metrics = [tf.keras.metrics.SparseCategoricalAccuracy()]
modifiedSleepEEGModel.compile(opt, loss, metrics)
callbacks = [
tf.keras.callbacks.TensorBoard(log_dir="%s/fold_%d" % (output_dir, fold_idx)),
tf.keras.callbacks.EarlyStopping(
monitor="val_loss", min_delta=0, patience=3,
mode="auto", baseline=None
)
]
# Training
print_debug("4. Training")
modifiedSleepEEGModel.fit(os_train,
os_label,
epochs=15, # FIXME: Change to 15 before submit
batch_size=256,
# Consider change it to lower batch_size if the GPU has litle memory
validation_split=0.4,
callbacks=callbacks)
# Save learn model for this fold
save_path = "%s/model_fold_%d" % (output_dir, fold_idx)
print_debug("Saving model to %s" % save_path)
modifiedSleepEEGModel.save(save_path)
# Evaluate
confusion, acc, prec, f1 = check_model(modifiedSleepEEGModel, data_test, label_test)
kappa_score = cohen_kappa_score_from_confusion_matrix(confusion)
print_debug("5. EVALUATION ON TESTSET FOR FOLD %d" % fold_idx)
print_single_result((confusion, acc, f1, kappa_score))
# Store result
all_fold_result += [(confusion, acc, prec, f1, kappa_score)]
try:
with open("%s/result_fold_%d" % (output_dir, fold_idx), "wb") as f:
result_tup = np.array([confusion, acc, prec, f1, kappa_score])
np.save(f, result_tup)
except:
print("Error during saving result for fold %d" % fold_idx)
# Save final result
final_result = np.array(all_fold_result)
try:
with open("%s/final_result" % output_dir, "wb") as f:
np.save(f, final_result)
except:
print("Error during saving final result")
print_summary_result(all_fold_result)
def print_summary_result(all_fold_result):
# Display overall stats
acc_all = [a[1] for a in all_fold_result]
f1_all = [a[3] for a in all_fold_result]
mean_acc = sum(acc_all) / len(acc_all)
mean_f1 = sum(f1_all) / len(f1_all)
# Confusion
all_confusion = None
for a in all_fold_result:
confusion = a[0]
if all_confusion is None:
all_confusion = confusion
else:
all_confusion += confusion
kappa_all = cohen_kappa_score_from_confusion_matrix(all_confusion)
print("####################################")
print("RESULT OVER %d FOLDS" % len(acc_all))
print_single_result((all_confusion, mean_acc, mean_f1, kappa_all))
def print_single_result(single_result):
confusion, acc, f1, kappa = single_result
print("ACCURACY: %.3f" % acc)
print("F1: %.3f" % f1)
print("COHEN'S KAPPA SCORE: %.3f" % kappa)
print("CONFUSION MATRIX")
print(confusion)
def load_and_summarize(result_dir):
result = np.load("%s/final_result" % result_dir, allow_pickle=True)
print_summary_result(result)
def training_mode(args):
all_files = list_files(args.data_dir)
fold_ids = [int(x.strip()) for x in args.fold_ids.split(",")]
fold_ids = list(set(fold_ids))
if -1 in fold_ids:
fold_ids = []
if args.model == "mod_sleep_eeg":
print("Training and evaluate result for ModifiedSleepEEG Network")
train_modified_sleep_eeg_net(fold_ids, all_files, args.total_fold, args.output_dir + "/modified_sleep_eeg")
elif args.model == "mod_deep_sleep":
print("Training and evaluate result for ModifiedDeepSleep Network")
train_modified_deep_sleep_net(fold_ids, all_files, args.total_fold, args.output_dir + "/modified_deep_sleep")
else:
print("Unknown model. Valid model is : mod_sleep_eeg / mod_deep_sleep")
def summarize_mode(args):
load_and_summarize(args.output_dir)
DEBUG = True
def debug_f(f, *args):
if DEBUG:
f(*args)
def print_debug(text):
debug_f(print, text)
if __name__ == "__main__":
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter)
parser.add_argument("--run", type=str,
default="train",
help="running mode: train / summarize."
"\n+ train: Training. Need to specfiy data_dir, output_dir, fold_ids, model and total_fold"
"\n+ summarize: View result of the last run in which the result is stored in `output_dir`"
)
parser.add_argument("--data_dir", type=str,
default="data/sc_eeg_fpz_cz",
help="directory contain extracted channels")
parser.add_argument("--output_dir", type=str,
default="output/sc_eeg_fpz_cz",
help="Directory to store model, progress, result")
parser.add_argument("--fold_ids", type=str,
default="0",
help="fold/folds to train, each valid fold from [0 to total_fold-1]."
"\n+ Can be a single fold, for example: 0 - the first fold, or 19 - the last fold in 20-fold cross validation"
"\n+ Can contain multiple folds, separate by comma `,`, for example: 0,1,2."
"\n+ -1 to train on all folds")
parser.add_argument("--model", type=str,
default="mod_sleep_eeg",
help="model to train and evaluate performance"
"\n+ mod_sleep_eeg: Modified SleepEEG - using CNNS"
"\n+ mod_deep_sleep: Modified DeepSleep - using CNNs + Bi-LSTMs")
parser.add_argument("--total_fold", type=int,
default=20,
help="Number of fold")
args = parser.parse_args()
running_mode = args.run
print("Running mode: %s" % running_mode)
if running_mode == "train":
training_mode(args)
elif running_mode == "summarize":
summarize_mode(args)
else:
parser.print_help()