-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
481 lines (434 loc) · 13.8 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import os
from ExplainerClassifierCNN import ExplainerClassifierCNN
from Dataset import Dataset, load_data
import numpy as np
import torch
from torch.utils.data import DataLoader
from torch import optim
import argparse
import utils
import csv
from EarlyStopping import EarlyStopping
from summary import summary
import sys
torch.manual_seed(0)
np.random.seed(0)
parser = argparse.ArgumentParser(description="Configurable parameters.")
# Processing parameters
parser.add_argument(
"--gpu", type=str, default="1", help="Which gpus to use in CUDA_VISIBLE_DEVICES."
)
parser.add_argument(
"--num_workers", type=int, default=4, help="Number of workers for dataloader."
)
# Directories and paths
parser.add_argument(
"--dataset_path",
type=str,
default="/media/TOSHIBA6T/ICRTO",
help="Folder where dataset is located.",
)
parser.add_argument(
"--folder",
type=str,
default="/media/TOSHIBA6T/ICRTO/results",
help="Directory where images and models are to be stored.",
)
# Data parameters
parser.add_argument(
"--dataset",
type=str,
default="imagenetHVZ",
choices=["synthetic", "NIH-NCI", "imagenetHVZ"],
help="Dataset to load.",
)
parser.add_argument(
"--nr_classes", type=int, default=2, help="Number of target classes."
)
parser.add_argument(
"--img_size", nargs="+", type=int, default=[224, 224], help="Input image size."
)
parser.add_argument(
"--aug_prob",
type=float,
default=0,
help="Probability of applying data augmentation to each image.",
)
# Training parameters
parser.add_argument(
"--nr_epochs",
type=str,
default="10,10,50",
help="Number of epochs for each of the 3 training phases as an array, for example: 50,100,50.",
)
parser.add_argument(
"-bs", "--batch_size", type=int, default=32, help="Training batch size."
)
parser.add_argument(
"--pretrained",
default=False,
action="store_true",
help="True if one wants to load pre trained models in training phase 1, False otherwise.",
)
parser.add_argument(
"-clf",
"--classifier",
type=str,
default="resnet50",
choices=["vgg", "resnet152", "resnet101", "resnet50", "resnet34", "resnet18"],
help="Classifier.",
)
parser.add_argument(
"--init_bias",
type=float,
default=2.0,
help="Initial bias for the batch norm layer of the Explainer. For more details see the paper.",
)
# Loss parameters
parser.add_argument(
"--loss",
type=str,
default="unsupervised",
choices=["hybrid", "unsupervised"],
help="Specifiy which loss to use. Either hybrid or unsupervised.",
)
parser.add_argument(
"--alpha",
type=str,
default="1.0,0.0,0.9",
help="Loss = alpha * Lclassif + (1-alpha) * Lexplic for each phase, for example: 1.0,0.0,0.9.",
)
parser.add_argument(
"--beta", type=float, help="Lexplic_unsup = beta * L1 + (1-beta) * Total Variation"
)
parser.add_argument(
"--gamma",
type=float,
help="Lexplic_hybrid = beta * L1 + (1-beta) * Total Variation + gamma* Weakly Loss",
)
parser.add_argument(
"--class_weights",
action="store_true",
default=False,
help="Use class weighting in loss function.",
)
# Learning parameters
parser.add_argument(
"--opt",
type=str,
default="sgd",
choices=["sgd", "adadelta"],
help="Optimiser to use. Either adadelta or sgd.",
)
parser.add_argument(
"-lr_clf",
"--learning_rate_classifier",
type=str,
default="1e-3,0,1e-4",
help="Learning rate for each training phase of the classifier, for example: 1e-3,0,1e-4.",
)
parser.add_argument(
"-lr_expl",
"--learning_rate_explainer",
type=str,
default="0,1e-4,1e-4",
help="Learning rate for each training phase of the explainer, for example: 0,1e-4,1e-4.",
)
parser.add_argument(
"--decay", type=float, default=0.0001, help="Learning rate decay to use with sgd."
)
parser.add_argument(
"-mom",
"--momentum",
type=float,
default=0.9,
help="Momentum to use with sgd optimiser.",
)
parser.add_argument(
"-min_lr",
"--min_learning_rate",
type=float,
default=1e-5,
help="Minimum learning rate to use with sgd and with ReduceLearningRateonPlateau.",
)
parser.add_argument(
"--patience",
type=int,
default=10,
help="Patience (number of epochs for a model to be considered as converged) to use with sgd and with ReduceLearningRateonPlateau.",
)
parser.add_argument(
"--factor",
type=float,
default=0.2,
help="Learning rate changing factor to use with sgd and with ReduceLearningRateonPlateau.",
)
# Early Stopping Parameters
parser.add_argument(
"--early_patience",
type=str,
default="200,200,200",
help="Number of epochs (for each phase) to consider before Early Stopping, for example: 10,20,5.",
)
parser.add_argument(
"--early_delta",
type=int,
default=1e-4,
help="Minimum change in the monitored quantity to qualify as an improvement for Early Stopping.",
)
args = parser.parse_args()
# select defined gpu
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# verify loss parameters
loss = args.loss
if args.beta is None:
print("Please define a value for beta.")
sys.exit(-1)
masks = False
if loss == "hybrid":
masks = True # ensure that the dataloader returns object detection masks
if args.gamma is None:
print("Please define a value for gamma.")
sys.exit(-1)
# split epochs' string into an array of 3 integer values, one for each training phase
eps = args.nr_epochs.split(",")
nr_epochs = np.array([int(x) for x in eps])
# split classifier learning rates' string into an array of 3 integer values, one for each training phase
lrs_clf = args.learning_rate_classifier.split(",")
lr_clf = np.array([float(x) for x in lrs_clf])
# split explainer learning rates' string into an array of 3 integer values, one for each training phase
lrs_expl = args.learning_rate_explainer.split(",")
lr_expl = np.array([float(x) for x in lrs_expl])
# split alphas' string into an array of 3 integer values, one for each training phase
alphas = args.alpha.split(",")
alpha = np.array([float(x) for x in alphas])
# split patience for early stopping string into an array of 3 integer values, one for each training phase
early_patience = args.early_patience.split(",")
early_patience = np.array([int(x) for x in early_patience])
img_size = tuple(args.img_size)
# create folder to store the results and models
folder = args.folder
timestamp, path = utils.create_folder(folder)
# save training config
with open(os.path.join(path, timestamp + "_train_parameters_summary.txt"), "w") as f:
f.write(str(args))
# instantiate model class and put model in GPU (if GPU available)
model = ExplainerClassifierCNN(
num_classes=args.nr_classes,
img_size=img_size,
clf=args.classifier,
init_bias=args.init_bias,
pretrained=args.pretrained,
)
model.to(device)
# save a summary of the model used
summary(
model.classifier,
[(3, 224, 224), (1, 224, 224)],
filename=os.path.join(path, timestamp + "_model_dec_info.txt"),
)
summary(
model.explainer,
(3, 224, 224),
filename=os.path.join(path, timestamp + "_model_exp_info.txt"),
)
# define class weights for imbalanced data
if args.class_weights:
class_weights = "balanced"
else:
class_weights = None
# load data and create training and validation loaders
tr_df, val_df, _, weights, classes = load_data(
folder=args.dataset_path,
dataset=args.dataset,
masks=masks,
class_weights=class_weights,
)
weights = torch.FloatTensor(weights).to(device)
train_dataset = Dataset(tr_df, masks=masks, img_size=img_size, aug_prob=args.aug_prob)
val_dataset = Dataset(val_df, masks=masks, img_size=img_size, aug_prob=0)
train_loader = DataLoader(
train_dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers,
pin_memory=True,
drop_last=False,
)
val_loader = DataLoader(
val_dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers,
pin_memory=True,
drop_last=False,
)
# Start training (3 phases)
for phase in range(3):
print("PHASE ", str(phase))
if nr_epochs[phase] == 0:
continue
early_stopping = EarlyStopping(
patience=early_patience[phase],
delta=args.early_delta,
verbose=True,
folder=path,
timestamp=timestamp,
)
# define history and checkpoint files for each phase
history_file = os.path.join(
path, timestamp + "_phase" + str(phase) + "_history.csv"
)
checkpoint_filename = os.path.join(path, timestamp + "_phase" + str(phase))
with open(history_file, "a+") as f:
csv_writer = csv.writer(f, delimiter=",")
csv_writer.writerow(
[
"epoch",
"train_global_loss",
"train_classifier_loss",
"train_explainer_loss",
"train_classifier_acc",
"val_global_loss",
"val_classifier_loss",
"val_explainer_loss",
"val_classifier_acc",
]
)
# select optimizer
if args.opt == "adadelta":
opt = optim.Adadelta(
[
{"params": model.classifier.parameters(), "lr": lr_clf[phase]},
{"params": model.explainer.parameters(), "lr": lr_expl[phase]},
],
weight_decay=args.decay,
)
else:
opt = optim.SGD(
[
{"params": model.classifier.parameters(), "lr": lr_clf[phase]},
{"params": model.explainer.parameters(), "lr": lr_expl[phase]},
],
weight_decay=args.decay,
momentum=args.momentum,
)
# use scheduler when the classifier is a resnet; ignore it when classifier is vgg
if "resnet" in args.classifier.lower():
scheduler = optim.lr_scheduler.ReduceLROnPlateau(
opt,
mode="min",
factor=args.factor,
patience=args.patience,
verbose=True,
min_lr=args.min_learning_rate,
)
else:
scheduler = None
for epoch in range(nr_epochs[phase]): # training loop for each phase
print("Epoch %d / %d" % (epoch + 1, nr_epochs[phase]))
print("Train")
model.train(train_loader, opt, device, args, phase, weights, alpha[phase])
print("Validation")
# pass entire training set through validation function to obtain metrics at the end of the training epoch
(
train_global_loss,
train_explainer_loss,
train_classifier_loss,
train_classifier_acc,
) = model.validation(train_loader, device, args, alpha[phase])
print(
"Train Loss %f \tTrain Exp Loss %f \tTrain Dec Loss %f \tTrain Acc %f"
% (
train_global_loss,
train_explainer_loss,
train_classifier_loss,
train_classifier_acc,
)
)
(
val_global_loss,
val_explainer_loss,
val_classifier_loss,
val_classifier_acc,
) = model.validation(val_loader, device, args, alpha[phase])
print(
"Val Loss %f \tVal Exp Loss %f \tVal Dec Loss %f \tVal Acc %f"
% (
val_global_loss,
val_explainer_loss,
val_classifier_loss,
val_classifier_acc,
)
)
print()
# save model and epoch metrics
model.checkpoint(
checkpoint_filename, epoch, val_global_loss, val_classifier_acc, opt
)
with open(history_file, "a+") as f:
csv_writer = csv.writer(f, delimiter=",")
csv_writer.writerow(
[
epoch,
train_global_loss,
train_classifier_loss,
train_explainer_loss,
train_classifier_acc,
val_global_loss,
val_classifier_loss,
val_explainer_loss,
val_classifier_acc,
]
)
# check if early stopping should be activated according to the patience given for each training phase
early_stopping(val_global_loss, model)
if early_stopping.early_stop:
print("Early stopping")
break
# apply scheduling if scheduler is defined (only for resnet)
if scheduler:
if isinstance(scheduler, optim.lr_scheduler.ReduceLROnPlateau):
scheduler.step(val_global_loss)
# at the end of each training phase, plot the evolution of several metrics and plot the resulting explanations
print()
utils.plot_metric_train_val(
epoch + 1,
history_file,
"classifier_loss",
path,
os.path.join(path, timestamp + "_phase" + str(phase) + "_classifier_loss.png"),
"Classifier Loss",
)
utils.plot_metric_train_val(
epoch + 1,
history_file,
"explainer_loss",
path,
os.path.join(path, timestamp + "_phase" + str(phase) + "_explainer_loss.png"),
"Explainer Loss",
)
utils.plot_metric_train_val(
epoch + 1,
history_file,
"global_loss",
path,
os.path.join(path, timestamp + "_phase" + str(phase) + "_global_loss.png"),
"Global Loss",
)
utils.plot_metric_train_val(
epoch + 1,
history_file,
"classifier_acc",
path,
os.path.join(path, timestamp + "_phase" + str(phase) + "_classifier_acc.png"),
"Accuracy",
)
# Load best model
ckpt = torch.load(checkpoint_filename + "_best_loss.pt", map_location=device)
model.classifier.load_state_dict(ckpt["classifier"])
model.explainer.load_state_dict(ckpt["explainer"])
model.save_explanations(
val_loader, phase, device, path, classes=classes, cmap="viridis"
)