-
Notifications
You must be signed in to change notification settings - Fork 14
/
train_val_test.py
275 lines (239 loc) · 9.72 KB
/
train_val_test.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
import logging
import numpy, random, time
import torch
import torch.nn.functional as F
import torch.optim as optim
from ssl_lib.algs.builder import gen_ssl_alg
from ssl_lib.algs import utils as alg_utils
from ssl_lib.models import utils as model_utils
from ssl_lib.consistency.builder import gen_consistency
from ssl_lib.models.builder import gen_model
from ssl_lib.datasets.builder import gen_dataloader
from ssl_lib.param_scheduler import scheduler
from ssl_lib.misc.meter import Meter
def evaluation(raw_model, eval_model, loader, device):
raw_model.eval()
eval_model.eval()
sum_raw_acc = sum_acc = sum_loss = 0
with torch.no_grad():
for (data, labels) in loader:
data, labels = data.to(device), labels.to(device)
preds = eval_model(data)
raw_preds = raw_model(data)
loss = F.cross_entropy(preds, labels)
sum_loss += loss.item()
acc = (preds.max(1)[1] == labels).float().mean()
raw_acc = (raw_preds.max(1)[1] == labels).float().mean()
sum_acc += acc.item()
sum_raw_acc += raw_acc.item()
mean_raw_acc = sum_raw_acc / len(loader)
mean_acc = sum_acc / len(loader)
mean_loss = sum_loss / len(loader)
raw_model.train()
eval_model.train()
return mean_raw_acc, mean_acc, mean_loss
def param_update(
cfg,
cur_iteration,
model,
teacher_model,
optimizer,
ssl_alg,
consistency,
labeled_data,
ul_weak_data,
ul_strong_data,
labels,
average_model
):
start_time = time.time()
all_data = torch.cat([labeled_data, ul_weak_data, ul_strong_data], 0)
forward_func = model.forward
stu_logits = forward_func(all_data)
labeled_preds = stu_logits[:labeled_data.shape[0]]
stu_unlabeled_weak_logits, stu_unlabeled_strong_logits = torch.chunk(stu_logits[labels.shape[0]:], 2, dim=0)
if cfg.tsa:
none_reduced_loss = F.cross_entropy(labeled_preds, labels, reduction="none")
L_supervised = alg_utils.anneal_loss(
labeled_preds, labels, none_reduced_loss, cur_iteration+1,
cfg.iteration, labeled_preds.shape[1], cfg.tsa_schedule)
else:
L_supervised = F.cross_entropy(labeled_preds, labels)
if cfg.coef > 0:
# get target values
if teacher_model is not None: # get target values from teacher model
t_forward_func = teacher_model.forward
tea_logits = t_forward_func(all_data)
tea_unlabeled_weak_logits, _ = torch.chunk(tea_logits[labels.shape[0]:], 2, dim=0)
else:
t_forward_func = forward_func
tea_unlabeled_weak_logits = stu_unlabeled_weak_logits
# calc consistency loss
model.update_batch_stats(False)
y, targets, mask = ssl_alg(
stu_preds = stu_unlabeled_strong_logits,
tea_logits = tea_unlabeled_weak_logits.detach(),
w_data = ul_weak_data,
s_data = ul_strong_data,
stu_forward = forward_func,
tea_forward = t_forward_func
)
model.update_batch_stats(True)
L_consistency = consistency(y, targets, mask, weak_prediction=tea_unlabeled_weak_logits.softmax(1))
else:
L_consistency = torch.zeros_like(L_supervised)
mask = None
# calc total loss
coef = scheduler.exp_warmup(cfg.coef, cfg.warmup_iter, cur_iteration+1)
loss = L_supervised + coef * L_consistency
if cfg.entropy_minimization > 0:
loss -= cfg.entropy_minimization * \
(stu_unlabeled_weak_logits.softmax(1) * F.log_softmax(stu_unlabeled_weak_logits, 1)).sum(1).mean()
# update parameters
cur_lr = optimizer.param_groups[0]["lr"]
optimizer.zero_grad()
loss.backward()
if cfg.weight_decay > 0:
decay_coeff = cfg.weight_decay * cur_lr
model_utils.apply_weight_decay(model.modules(), decay_coeff)
optimizer.step()
# update teacher parameters by exponential moving average
if cfg.ema_teacher:
model_utils.ema_update(
teacher_model, model, cfg.ema_teacher_factor,
cfg.weight_decay * cur_lr if cfg.ema_apply_wd else None,
cur_iteration if cfg.ema_teacher_warmup else None)
# update evaluation model's parameters by exponential moving average
if cfg.weight_average:
model_utils.ema_update(
average_model, model, cfg.wa_ema_factor,
cfg.weight_decay * cur_lr if cfg.wa_apply_wd else None)
# calculate accuracy for labeled data
acc = (labeled_preds.max(1)[1] == labels).float().mean()
return {
"acc": acc,
"loss": loss.item(),
"sup loss": L_supervised.item(),
"ssl loss": L_consistency.item(),
"mask": mask.float().mean().item() if mask is not None else 1,
"coef": coef,
"sec/iter": (time.time() - start_time)
}
def main(cfg, logger):
# set seed
torch.manual_seed(cfg.seed)
numpy.random.seed(cfg.seed)
random.seed(cfg.seed)
# select device
if torch.cuda.is_available():
device = "cuda"
torch.backends.cudnn.benchmark = True
else:
logger.info("CUDA is NOT available")
device = "cpu"
# build data loader
logger.info("load dataset")
lt_loader, ult_loader, val_loader, test_loader, num_classes, img_size = gen_dataloader(cfg.root, cfg.dataset, True, cfg, logger)
# set consistency type
consistency = gen_consistency(cfg.consistency, cfg)
# set ssl algorithm
ssl_alg = gen_ssl_alg(cfg.alg, cfg)
# build student model
model = gen_model(cfg.model, num_classes, img_size).to(device)
# build teacher model
if cfg.ema_teacher:
teacher_model = gen_model(cfg.model, num_classes, img_size).to(device)
teacher_model.load_state_dict(model.state_dict())
else:
teacher_model = None
# for evaluation
if cfg.weight_average:
average_model = gen_model(cfg.model, num_classes, img_size).to(device)
average_model.load_state_dict(model.state_dict())
else:
average_model = None
model.train()
logger.info(model)
# build optimizer
if cfg.optimizer == "sgd":
optimizer = optim.SGD(
model.parameters(), cfg.lr, cfg.momentum, weight_decay=0, nesterov=True
)
elif cfg.optimizer == "adam":
optimizer = optim.AdamW(
model.parameters(), cfg.lr, (cfg.momentum, 0.999), weight_decay=0
)
else:
raise NotImplementedError
# set lr scheduler
if cfg.lr_decay == "cos":
lr_scheduler = scheduler.CosineAnnealingLR(optimizer, cfg.iteration)
elif cfg.lr_decay == "step":
lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [400000, ], cfg.lr_decay_rate)
else:
raise NotImplementedError
# init meter
metric_meter = Meter()
maximum_val_acc = 0
logger.info("training")
for i, (l_data, ul_data) in enumerate(zip(lt_loader, ult_loader)):
l_aug, labels = l_data
ul_w_aug, ul_s_aug, _ = ul_data
params = param_update(
cfg, i, model, teacher_model, optimizer, ssl_alg,
consistency, l_aug.to(device), ul_w_aug.to(device),
ul_s_aug.to(device), labels.to(device),
average_model
)
# moving average for reporting losses and accuracy
metric_meter.add(params, ignores=["coef"])
# display losses every cfg.disp iterations
if ((i+1) % cfg.disp) == 0:
state = metric_meter.state(
header = f'[{i+1}/{cfg.iteration}]',
footer = f'ssl coef {params["coef"]:.4g} | lr {optimizer.param_groups[0]["lr"]:.4g}'
)
logger.info(state)
lr_scheduler.step()
# validation
if ((i + 1) % cfg.checkpoint) == 0 or (i+1) == cfg.iteration:
with torch.no_grad():
if cfg.weight_average:
eval_model = average_model
else:
eval_model = model
logger.info("validation")
mean_raw_acc, mean_val_acc, mean_val_loss = evaluation(model, eval_model, val_loader, device)
logger.info("validation loss %f | validation acc. %f | raw acc. %f", mean_val_loss, mean_val_acc, mean_raw_acc)
# test
if not cfg.only_validation and mean_val_acc > maximum_val_acc:
torch.save(eval_model.state_dict(), os.path.join(cfg.out_dir, "best_model.pth"))
maximum_val_acc = mean_val_acc
logger.info("test")
mean_raw_acc, mean_test_acc, mean_test_loss = evaluation(model, eval_model, test_loader, device)
logger.info("test loss %f | test acc. %f | raw acc. %f", mean_test_loss, mean_test_acc, mean_raw_acc)
torch.save(model.state_dict(), os.path.join(cfg.out_dir, "model_checkpoint.pth"))
torch.save(optimizer.state_dict(), os.path.join(cfg.out_dir, "optimizer_checkpoint.pth"))
logger.info("test accuracy %f", mean_test_acc)
if __name__ == "__main__":
import os, sys
from parser import get_args
args = get_args()
os.makedirs(args.out_dir, exist_ok=True)
# setup logger
plain_formatter = logging.Formatter(
"[%(asctime)s] %(name)s %(levelname)s: %(message)s", datefmt="%m/%d %H:%M:%S"
)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
s_handler = logging.StreamHandler(stream=sys.stdout)
s_handler.setFormatter(plain_formatter)
s_handler.setLevel(logging.DEBUG)
logger.addHandler(s_handler)
f_handler = logging.FileHandler(os.path.join(args.out_dir, "console.log"))
f_handler.setFormatter(plain_formatter)
f_handler.setLevel(logging.DEBUG)
logger.addHandler(f_handler)
logger.propagate = False
logger.info(args)
main(args, logger)