-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
281 lines (243 loc) · 14.9 KB
/
train.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
import numpy as np
import datetime
from plot_metrics import plot_metrics_TrnVal
import tensorflow.keras.backend as K
from build_batches import build_training_batch_myeloid, build_validation_batch
from build_batches import build_training_batch_leukemia
from plot_metrics import plot_metrics_TrnVal, print_metrics_model, print_time_cost
def train_model_myeloid(model, trn_img, val_img, args):
# Initialize some variables to track the loss and the AUC
nMtrc = 2 # The different metrics to record
trn_met = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
val_met = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
val_metNoW = np.ndarray(shape=(nMtrc,0), dtype=np.float32) #Non-Weighted
tTemp = np.ndarray(shape=(nMtrc,0), dtype=np.float32) # Temporal variable
# Variables to keep track of time expenditure
timeIni = datetime.datetime.now().replace(microsecond=0) # Initial time
timeCur = datetime.datetime.now() # Current time (update in each iteration)
# -------------------------------------------------------------------------
# If continuing training, load the weights and variables.
if args.current_epoch > 0:
model.load_weights(args.save_folder + args.weights)
data = np.load(args.save_folder + 'epoch' + str(args.current_epoch) +
'_variables.npz', allow_pickle=True)
trn_img = data['trn_img']
val_img = data['val_img']
trn_met = data['trn_met']
val_met = data['val_met']
val_metNoW = data['val_metNoW']
# Plot the losses and accuracy for training/validation
x1 = 1 + np.arange(trn_met.shape[1])
plot_metrics_TrnVal(x1, trn_met[0], val_met[0], val_metNoW[0],
title='Loss', check_best='min',
legend=['Training', 'Validation', 'Validation NoW'],
save_folder=args.save_folder)
plot_metrics_TrnVal(x1, trn_met[1], val_met[1], val_metNoW[1],
title='Accuracy', ylabel='Batch accuracy',
ylim=[0,1], check_best='max',
legend=['Training', 'Validation', 'Validation NoW'],
save_folder=args.save_folder)
# -------------------------------------------------------------------------
for iEpoch in range(args.current_epoch, args.epochs):
# Change learning rate
lr_new = args.lr_init*(args.lr_decay ** iEpoch)
K.set_value(model.optimizer.lr, lr_new)
print('New learning rate: %.6f' % model.optimizer.lr)
for iIter in range(args.iterations):
# -----------------------------------------------------------------
# TRAINING. Prepare one batch and feed it to the CNN
batch_data, batch_labl, _ = build_training_batch_myeloid(trn_img,
args=args)
tLoss = model.train_on_batch(batch_data, batch_labl) #sample_weight=batch_wght)
tTemp = np.append(tTemp, np.array(tLoss)[:,np.newaxis], axis=1)
# -----------------------------------------------------------------
# After training iter2eval batches, average the losses
if ((iIter+1) % args.iter2eval == 0):
# Save the train metrics, plot them, and reset metrics
trn_met = np.append(trn_met, np.mean(tTemp,axis=1)[:,np.newaxis],
axis=1)
tTemp = np.ndarray(shape=(nMtrc,0), dtype=np.float32) # Reset
print_time_cost(timeCur, eCurr=iEpoch+1, strT='Training')
print_metrics_model(trn_met, iIter+1, typeSet='Training')
# -------------------------------------------------------------
# VALIDATION. Save the non-weighted (NoW) and weighted metrics
vTempNoW = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
vTempCls = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
for iCls in range(len(val_img)):
vTemp = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
for iVal in range(len(val_img[iCls])):
batch_data, batch_labl = build_validation_batch(
val_img[iCls][iVal:iVal+1],
imgLabel=iCls, args=args)
vLoss = model.test_on_batch(batch_data, batch_labl)
vTemp = np.append(vTemp, np.array(vLoss)[:,np.newaxis],
axis=1)
# Compure the averaged metrics for one class and append them
vTempCls = np.append(vTempCls,
np.mean(vTemp,axis=1)[:,np.newaxis],
axis=1)
vTempNoW = np.append(vTempNoW, vTemp, axis=1)
# Compute the weighted metrics, save them, and plot them
val_met = np.append(val_met,
np.mean(vTempCls,axis=1)[:,np.newaxis], axis=1)
val_metNoW = np.append(val_metNoW,
np.mean(vTempNoW,axis=1)[:,np.newaxis], axis=1)
print_metrics_model(val_met, iIter+1,
typeSet='Validation Negatives')
print_metrics_model(val_metNoW, iIter+1,
typeSet='Validation Negatives - NonWeighted')
# Plot the time again, including now the validation
print_time_cost(timeCur, timeIni, iCurr=iIter+1,
iNmbr=args.iterations, eCurr=iEpoch+1,
eNmbr=args.epochs, it2ev=args.iter2eval,
flagFuture=True, strT='Training + Validation')
timeCur = datetime.datetime.now() # Reset timestamp
# ---------------------------------------------------------------------
# End of an epoch
if args.save_model:
fileOut = ("epoch" + str(1+iEpoch) + "_weights.h5")
fileOut = args.save_folder + fileOut
model.save_weights(fileOut, overwrite=True)
print(" Model saved!")
# Save some variables for further analysis later
if args.save_variables:
fileOut = ("epoch" + str(1+iEpoch) + "_variables.npz")
fileOut = args.save_folder + fileOut
np.savez(fileOut,
trn_met=trn_met, trn_img=trn_img,
val_met=val_met, val_img=val_img, val_metNoW=val_metNoW)
print(" Variables saved!")
# ---------------------------------------------------------------------
# Plot the losses and accuracy for training/validation
x1 = 1 + np.arange(trn_met.shape[1])
plot_metrics_TrnVal(x1, trn_met[0], val_met[0], val_metNoW[0],
title='Loss', check_best='min',
legend=['Training', 'Validation', 'Validation NoW'],
save_folder=args.save_folder)
plot_metrics_TrnVal(x1, trn_met[1], val_met[1], val_metNoW[1],
title='Accuracy', ylabel='Batch accuracy',
check_best='max', ylim=[0,1],
legend=['Training', 'Validation', 'Validation NoW'],
save_folder=args.save_folder)
def train_model_leukemia(model, trnPos_img, trnNeg_img, valPos_img, valNeg_img,
args):
# Initialize some variables to track the loss and the AUC
nMtrc = 2 # The different metrics to record
trnAll_met = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
valPos_met = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
valNeg_met = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
tTemp = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
vTemp = np.ndarray(shape=(nMtrc,0), dtype=np.float32)
# Variables to keep track of time expenditure
timeIni = datetime.datetime.now().replace(microsecond=0) # Initial time
timeCur = datetime.datetime.now() # Current time (update in each iteration)
# -------------------------------------------------------------------------
# If continuing training, load the weights and variables.
if args.current_epoch > 0:
model.load_weights(args.save_folder + args.weights)
data = np.load(args.save_folder + 'epoch' + str(args.current_epoch) +
'_variables.npz', allow_pickle=True)
trnPos_img = data['trnPos_img']
trnNeg_img = data['trnNeg_img']
trnAll_met = data['trnAll_met']
valPos_img = data['valPos_img']
valPos_met = data['valPos_met']
valNeg_img = data['valNeg_img']
valNeg_met = data['valNeg_met']
# Plot the losses and accuracy for training/validation
x1 = 1 + np.arange(trnAll_met.shape[1])
plot_metrics_TrnVal(x1, trnAll_met[0], valPos_met[0], valNeg_met[0],
title='Loss', check_best='min',
legend=['Training', 'Validation Pos', 'Validation Neg'],
save_folder=args.save_folder)
plot_metrics_TrnVal(x1, trnAll_met[1], valPos_met[1], valNeg_met[1],
title='Accuracy', ylabel='Batch accuracy',
ylim=[0.5,1], check_best='max',
legend=['Training', 'Validation Pos', 'Validation Neg'],
save_folder=args.save_folder)
# -------------------------------------------------------------------------
nPosVal = valPos_img.shape[0]
nNegVal = valNeg_img.shape[0]
for iEpoch in range(args.current_epoch, args.epochs):
# Change learning rate
lr_new = args.lr_init*(args.lr_decay ** iEpoch)
K.set_value(model.optimizer.lr, lr_new)
print('New learning rate: %.6f' % model.optimizer.lr)
for iIter in range(args.iterations):
# -----------------------------------------------------------------
# TRAINING. Prepare one batch and feed it to the CNN
batch_data, batch_labl, _ = build_training_batch_leukemia(
trnPos_img, trnNeg_img, args=args)
tLoss = model.train_on_batch(batch_data, batch_labl) #sample_weight=batch_wght)
tTemp = np.append(tTemp, np.array(tLoss)[:,np.newaxis], axis=1)
# -----------------------------------------------------------------
# After training iter2eval batches, average the losses
if ((iIter+1) % args.iter2eval == 0):
# Save the train metrics, plot them, and reset metrics
trnAll_met = np.append(trnAll_met,
np.mean(tTemp,axis=1)[:,np.newaxis], axis=1)
tTemp = np.ndarray(shape=(nMtrc,0), dtype=np.float32) # Reset
print_time_cost(timeCur, eCurr=iEpoch+1, strT='Training')
print_metrics_model(trnAll_met, iIter+1, typeSet='Training')
# -------------------------------------------------------------
# VALIDATION. Positive images
for iVal in range(nPosVal):
batch_data, batch_labl = build_validation_batch(
valPos_img[iVal:iVal+1], imgLabel=1, args=args)
vLoss = model.test_on_batch(batch_data, batch_labl)
vTemp = np.append(vTemp, np.array(vLoss)[:,np.newaxis],
axis=1)
# Save the metrics for the Positive images, plot them, and reset
valPos_met = np.append(valPos_met,
np.mean(vTemp,axis=1)[:,np.newaxis], axis=1)
vTemp = np.ndarray(shape=(nMtrc,0), dtype=np.float32) # Reset
print_metrics_model(valPos_met, iIter+1,
typeSet='Validation Positives')
# -------------------------------------------------------------
# VALIDATION. Negative images.
for iVal in range(nNegVal):
batch_data, batch_labl = build_validation_batch(
valNeg_img[iVal:iVal+1], imgLabel=0, args=args)
vLoss = model.test_on_batch(batch_data, batch_labl)
vTemp = np.append(vTemp, np.array(vLoss)[:,np.newaxis],
axis=1)
# Save the metrics for Negative images, plot them, and reset
valNeg_met = np.append(valNeg_met,
np.mean(vTemp,axis=1)[:,np.newaxis], axis=1)
vTemp = np.ndarray(shape=(nMtrc,0), dtype=np.float32) # Reset
print_metrics_model(valNeg_met, iIter+1,
typeSet='Validation Negatives')
# Plot the time again, including now the validation
print_time_cost(timeCur, timeIni, iCurr=iIter+1,
iNmbr=args.iterations, eCurr=iEpoch+1,
eNmbr=args.epochs, it2ev=args.iter2eval,
flagFuture=True, strT='Training + Validation')
timeCur = datetime.datetime.now() # Reset timestamp
# ---------------------------------------------------------------------
# End of an epoch
if args.save_model:
fileOut = ("epoch" + str(1+iEpoch) + "_weights.h5")
fileOut = args.save_folder + fileOut
model.save_weights(fileOut, overwrite=True)
print(" Model saved!")
# Save some variables for further analysis later
if args.save_variables:
fileOut = ("epoch" + str(1+iEpoch) + "_variables.npz")
fileOut = args.save_folder + fileOut
np.savez(fileOut, trnAll_met=trnAll_met,
valPos_met=valPos_met, valNeg_met=valNeg_met,
trnPos_img=trnPos_img, trnNeg_img=trnNeg_img,
valPos_img=valPos_img, valNeg_img=valNeg_img)
print(" Variables saved!")
# ---------------------------------------------------------------------
# Plot the losses and accuracy for training/validation
x1 = 1 + np.arange(trnAll_met.shape[1])
plot_metrics_TrnVal(x1, trnAll_met[0], valPos_met[0], valNeg_met[0],
title='Loss', check_best='min',
legend=['Training', 'Validation Pos', 'Validation Neg'],
save_folder=args.save_folder)
plot_metrics_TrnVal(x1, trnAll_met[1], valPos_met[1], valNeg_met[1],
title='Accuracy', ylabel='Batch accuracy',
ylim=[0.5,1], check_best='max',
legend=['Training', 'Validation Pos', 'Validation Neg'],
save_folder=args.save_folder)