-
Notifications
You must be signed in to change notification settings - Fork 11
/
train_cv.py
92 lines (70 loc) · 2.61 KB
/
train_cv.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
import numpy as np
import os
import sys
import tensorflow as tf
from keras import backend as K
from keras.callbacks import TensorBoard
from keras.optimizers import RMSprop
from sklearn.metrics import roc_auc_score
from data import fold_data, augment
from model import multitask_cnn, loss_dict, loss_weights_dict
checkpoints_dir = "/data/checkpoints/<FOLD>/"
logs_dir = "/data/logs/<FOLD>/"
batch_size = 128
epochs = 250
base_lr = 0.001
def train(fold):
fold_checkpoints_dir = checkpoints_dir.replace("<FOLD>", str(fold))
fold_logs_dir = logs_dir.replace("<FOLD>", str(fold))
if not os.path.exists(fold_checkpoints_dir):
os.makedirs(fold_checkpoints_dir)
if not os.path.exists(fold_logs_dir):
os.makedirs(fold_logs_dir)
x_train, y_train, x_test, y_test = fold_data(fold)
print("Training and validation data processed.")
print("Training data shape: {}".format(len(x_train)))
print("Test data shape: {}".format(len(x_test)))
model = multitask_cnn()
optimizer = RMSprop(lr=base_lr)
model.compile(
optimizer=optimizer,
loss=loss_dict,
loss_weights=loss_weights_dict,
metrics=["accuracy"],
)
training_log = TensorBoard(
log_dir=os.path.join(fold_logs_dir, "log"), write_graph=False
)
callbacks = [training_log]
y_train_cancer = y_train["out_cancer"]
y_test_cancer = y_test[0]
for e in range(epochs):
x_train_augmented = augment(x_train)
model.fit(
x={"thyroid_input": x_train_augmented},
y=y_train,
validation_data=(x_test, y_test),
batch_size=batch_size,
epochs=e + 1,
initial_epoch=e,
shuffle=True,
callbacks=callbacks,
)
if np.mod(e + 1, 10) == 0:
y_pred = model.predict(x_train, batch_size=batch_size, verbose=1)
auc_train = roc_auc_score(y_train_cancer, y_pred[0])
y_pred = model.predict(x_test, batch_size=batch_size, verbose=1)
auc_test = roc_auc_score(y_test_cancer, y_pred[0])
with open(os.path.join(fold_logs_dir, "auc.txt"), "a") as auc_file:
auc_file.write("{},{}\n".format(auc_train, auc_test))
model.save(os.path.join(fold_checkpoints_dir, "weights.h5"))
print("Training fold {} completed.".format(fold))
if __name__ == "__main__":
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.allow_soft_placement = True
sess = tf.Session(config=config)
K.set_session(sess)
device = "/gpu:" + sys.argv[1]
with tf.device(device):
train(int(sys.argv[2]))