-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_wsol.py
executable file
·131 lines (90 loc) · 3.82 KB
/
main_wsol.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
import datetime as dt
import sys
from copy import deepcopy
from dlib.process.parseit import parse_input
from dlib.process.instantiators import get_model
from dlib.process.instantiators import get_optimizer
from dlib.utils.tools import log_device
from dlib.utils.tools import bye
from dlib.configure import constants
from dlib.learning.train_wsol import Trainer
from dlib.process.instantiators import get_loss
from dlib.process.instantiators import get_pretrainde_classifier
from dlib.utils.shared import fmsg
from dlib.utils.shared import is_cc
import dlib.dllogger as DLLogger
def main():
args, args_dict = parse_input(eval=False)
log_device(args)
model = get_model(args)
model.cuda(args.c_cudaid)
best_state_dict = deepcopy(model.state_dict())
optimizer, lr_scheduler = get_optimizer(args, model)
loss = get_loss(args)
inter_classifier = None
if args.task in [constants.F_CL, constants.NEGEV]:
inter_classifier = get_pretrainde_classifier(args)
inter_classifier.cuda(args.c_cudaid)
trainer: Trainer = Trainer(
args=args, model=model, optimizer=optimizer,
lr_scheduler=lr_scheduler, loss=loss, classifier=inter_classifier)
DLLogger.log(fmsg("Start epoch 0 ..."))
trainer.evaluate(epoch=0, split=constants.VALIDSET)
trainer.model_selection(epoch=0)
trainer.print_performances()
trainer.report(epoch=0, split=constants.VALIDSET)
DLLogger.log(fmsg("Epoch 0 done."))
for epoch in range(trainer.args.max_epochs):
zepoch = epoch + 1
DLLogger.log(fmsg(("Start epoch {} ...".format(zepoch))))
train_performance = trainer.train(split=constants.TRAINSET,
epoch=zepoch)
trainer.evaluate(zepoch, split=constants.VALIDSET)
trainer.model_selection(epoch=zepoch)
trainer.report_train(train_performance, zepoch)
trainer.print_performances()
trainer.report(zepoch, split=constants.VALIDSET)
DLLogger.log(fmsg(("Epoch {} done.".format(zepoch))))
trainer.adjust_learning_rate()
DLLogger.flush()
trainer.save_checkpoints()
trainer.save_best_epoch()
trainer.capture_perf_meters()
DLLogger.log(fmsg("Final epoch evaluation on test set ..."))
if args.task != constants.SEG:
chpts = [constants.BEST_CL]
if args.localization_avail:
chpts = [constants.BEST_LOC] + chpts
else:
chpts = [constants.BEST_LOC]
use_argmax = False
for eval_checkpoint_type in chpts:
t0 = dt.datetime.now()
if eval_checkpoint_type == constants.BEST_LOC:
epoch = trainer.args.best_loc_epoch
elif eval_checkpoint_type == constants.BEST_CL:
epoch = trainer.args.best_cl_epoch
else:
raise NotImplementedError
DLLogger.log(
fmsg('EVAL TEST SET. CHECKPOINT: {}. ARGMAX: {}'.format(
eval_checkpoint_type, use_argmax)))
trainer.load_checkpoint(checkpoint_type=eval_checkpoint_type)
trainer.evaluate(epoch, split=constants.TESTSET,
checkpoint_type=eval_checkpoint_type,
fcam_argmax=use_argmax)
trainer.print_performances(checkpoint_type=eval_checkpoint_type)
trainer.report(epoch, split=constants.TESTSET,
checkpoint_type=eval_checkpoint_type)
trainer.save_performances(
epoch=epoch, checkpoint_type=eval_checkpoint_type)
trainer.switch_perf_meter_to_captured()
tagargmax = f'Argmax: {use_argmax}'
DLLogger.log("EVAL time TESTSET - CHECKPOINT {} {}: {}".format(
eval_checkpoint_type, tagargmax, dt.datetime.now() - t0))
DLLogger.flush()
trainer.save_args()
trainer.plot_perfs_meter()
bye(trainer.args)
if __name__ == '__main__':
main()