forked from fcdl94/WILSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
453 lines (365 loc) · 20 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
import torch
import numpy as np
from torch import distributed
import torch.nn as nn
import torch.nn.functional as F
from functools import reduce
import tqdm
from utils.loss import KnowledgeDistillationLoss, BCEWithLogitsLossWithIgnoreIndex, \
UnbiasedKnowledgeDistillationLoss, UnbiasedCrossEntropy, IcarlLoss
from torch.cuda import amp
from segmentation_module import make_model, TestAugmentation
import tasks
from torch.nn.parallel import DistributedDataParallel
import os.path as osp
from wss.modules import PAMR, ASPP
from utils.utils import denorm, label_to_one_hot
from wss.single_stage import pseudo_gtmask, balanced_mask_loss_ce, balanced_mask_loss_unce
from utils.wss_loss import bce_loss, ngwp_focal, binarize
from segmentation_module import get_norm
from utils.scheduler import get_scheduler
class Trainer:
def __init__(self, logger, device, opts):
self.logger = logger
self.device = device
self.opts = opts
self.scaler = amp.GradScaler()
self.classes = classes = tasks.get_per_task_classes(opts.dataset, opts.task, opts.step)
if classes is not None:
new_classes = classes[-1]
self.tot_classes = reduce(lambda a, b: a + b, classes)
self.old_classes = self.tot_classes - new_classes
else:
self.old_classes = 0
self.model = make_model(opts, classes=classes)
if opts.step == 0: # if step 0, we don't need to instance the model_old
self.model_old = None
else: # instance model_old
self.model_old = make_model(opts, classes=tasks.get_per_task_classes(opts.dataset, opts.task, opts.step - 1))
self.model_old.to(self.device)
# freeze old model and set eval mode
for par in self.model_old.parameters():
par.requires_grad = False
self.model_old.eval()
self.weakly = opts.weakly and opts.step > 0
self.pos_w = opts.pos_w
self.use_aff = opts.affinity
self.weak_single_stage_dist = opts.ss_dist
self.pseudo_epoch = opts.pseudo_ep
cls_classes = self.tot_classes
self.pseudolabeler = None
if self.weakly:
self.affinity = PAMR(num_iter=10, dilations=[1, 2, 4, 8, 12]).to(device)
for p in self.affinity.parameters():
p.requires_grad = False
norm = get_norm(opts)
channels = 4096 if "wide" in opts.backbone else 2048
self.pseudolabeler = nn.Sequential(nn.Conv2d(channels, 256, kernel_size=3, stride=1, padding=1, bias=False),
norm(256),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=False),
norm(256),
nn.Conv2d(256, cls_classes, kernel_size=1, stride=1))
self.icarl = opts.icarl
self.optimizer, self.scheduler = self.get_optimizer(opts)
self.distribute(opts)
# Select the Loss Type
reduction = 'none'
self.bce = opts.bce or opts.icarl
if self.bce:
self.criterion = BCEWithLogitsLossWithIgnoreIndex(reduction=reduction)
elif opts.unce and self.old_classes != 0:
self.criterion = UnbiasedCrossEntropy(old_cl=self.old_classes, ignore_index=255, reduction=reduction)
else:
self.criterion = nn.CrossEntropyLoss(ignore_index=255, reduction=reduction)
# ILTSS
self.lde = opts.loss_de
self.lde_flag = self.lde > 0. and self.model_old is not None
self.lde_loss = nn.MSELoss()
self.lkd = opts.loss_kd
self.lkd_flag = self.lkd > 0. and self.model_old is not None
if opts.unkd:
self.lkd_loss = UnbiasedKnowledgeDistillationLoss(alpha=opts.alpha)
else:
self.lkd_loss = KnowledgeDistillationLoss(alpha=opts.alpha)
# ICARL
self.icarl_combined = False
self.icarl_only_dist = False
if opts.icarl:
self.icarl_combined = not opts.icarl_disjoint and self.model_old is not None
self.icarl_only_dist = opts.icarl_disjoint and self.model_old is not None
if self.icarl_combined:
self.licarl = nn.BCEWithLogitsLoss(reduction='mean')
self.icarl = opts.icarl_importance
elif self.icarl_only_dist:
self.licarl = IcarlLoss(reduction='mean', bkg=opts.icarl_bkg)
self.icarl_dist_flag = self.icarl_only_dist or self.icarl_combined
def get_optimizer(self, opts):
params = []
if not opts.freeze:
params.append({"params": filter(lambda p: p.requires_grad, self.model.body.parameters()),
'weight_decay': opts.weight_decay})
params.append({"params": filter(lambda p: p.requires_grad, self.model.head.parameters()),
'weight_decay': opts.weight_decay, 'lr': opts.lr*opts.lr_head})
params.append({"params": filter(lambda p: p.requires_grad, self.model.cls.parameters()),
'weight_decay': opts.weight_decay, 'lr': opts.lr*opts.lr_head})
if self.weakly:
params.append({"params": filter(lambda p: p.requires_grad, self.pseudolabeler.parameters()),
'weight_decay': opts.weight_decay, 'lr': opts.lr_pseudo})
optimizer = torch.optim.SGD(params, lr=opts.lr, momentum=0.9, nesterov=True)
scheduler = get_scheduler(opts, optimizer)
return optimizer, scheduler
def distribute(self, opts):
self.model = DistributedDataParallel(self.model.to(self.device), device_ids=[opts.device_id],
output_device=opts.device_id, find_unused_parameters=False)
if self.weakly:
self.pseudolabeler = DistributedDataParallel(self.pseudolabeler.to(self.device), device_ids=[opts.device_id],
output_device=opts.device_id, find_unused_parameters=False)
def train(self, cur_epoch, train_loader, print_int=10):
"""Train and return epoch loss"""
optim = self.optimizer
scheduler = self.scheduler
device = self.device
model = self.model
criterion = self.criterion
logger = self.logger
logger.info("Epoch %d, lr = %f" % (cur_epoch, optim.param_groups[0]['lr']))
epoch_loss = 0.0
reg_loss = 0.0
l_cam_out = 0.0
l_cam_int = 0.0
l_seg = 0.0
l_cls = 0.0
interval_loss = 0.0
lkd = torch.tensor(0.)
lde = torch.tensor(0.)
l_icarl = torch.tensor(0.)
l_reg = torch.tensor(0.)
train_loader.sampler.set_epoch(cur_epoch)
if distributed.get_rank() == 0:
tq = tqdm.tqdm(total=len(train_loader))
tq.set_description("Epoch %d, lr = %f" % (cur_epoch, optim.param_groups[0]['lr']))
else:
tq = None
model.train()
for cur_step, (images, labels, l1h) in enumerate(train_loader):
images = images.to(device, dtype=torch.float)
l1h = l1h.to(device, dtype=torch.float) # this are one_hot
labels = labels.to(device, dtype=torch.long)
with amp.autocast():
if (self.lde_flag or self.lkd_flag or self.icarl_dist_flag or self.weakly) and self.model_old is not None:
with torch.no_grad():
outputs_old, features_old = self.model_old(images, interpolate=False)
optim.zero_grad()
outputs, features = model(images, interpolate=False)
# xxx BCE / Cross Entropy Loss
if not self.weakly:
outputs = F.interpolate(outputs, size=images.shape[-2:], mode="bilinear", align_corners=False)
if not self.icarl_only_dist:
loss = criterion(outputs, labels) # B x H x W
else:
# ICaRL loss -- unique CE+KD
outputs_old = F.interpolate(outputs_old, size=images.shape[-2:], mode="bilinear",
align_corners=False)
loss = self.licarl(outputs, labels, torch.sigmoid(outputs_old))
loss = loss.mean() # scalar
# xxx ICARL DISTILLATION
if self.icarl_combined:
# tensor.narrow( dim, start, end) -> slice tensor from start to end in the specified dim
n_cl_old = outputs_old.shape[1]
outputs_old = F.interpolate(outputs_old, size=images.shape[-2:], mode="bilinear",
align_corners=False)
# use n_cl_old to sum the contribution of each class, and not to average them (as done in our BCE).
l_icarl = self.icarl * n_cl_old * self.licarl(outputs.narrow(1, 0, n_cl_old),
torch.sigmoid(outputs_old))
# xxx ILTSS (distillation on features or logits)
if self.lde_flag:
lde = self.lde * self.lde_loss(features['body'], features_old['body'])
if self.lkd_flag:
outputs_old = F.interpolate(outputs_old, size=images.shape[-2:], mode="bilinear",
align_corners=False)
# resize new output to remove new logits and keep only the old ones
lkd = self.lkd * self.lkd_loss(outputs, outputs_old)
else:
bs = images.shape[0]
self.pseudolabeler.eval()
int_masks = self.pseudolabeler(features['body']).detach()
self.pseudolabeler.train()
int_masks_raw = self.pseudolabeler(features['body'])
if self.opts.no_mask:
l_cam_new = bce_loss(int_masks_raw, l1h, mode=self.opts.cam, reduction='mean')
else:
l_cam_new = bce_loss(int_masks_raw, l1h[:, self.old_classes - 1:],
mode=self.opts.cam, reduction='mean')
l_loc = F.binary_cross_entropy_with_logits(int_masks_raw[:, :self.old_classes],
torch.sigmoid(outputs_old.detach()),
reduction='mean')
l_cam_int = l_cam_new + l_loc
if self.lde_flag:
lde = self.lde * self.lde_loss(features['body'], features_old['body'])
l_cam_out = 0 * outputs[0, 0].mean() # avoid errors due to DDP
if cur_epoch >= self.pseudo_epoch:
int_masks_orig = int_masks.softmax(dim=1)
int_masks_soft = int_masks.softmax(dim=1)
if self.use_aff:
image_raw = denorm(images)
im = F.interpolate(image_raw, int_masks.shape[-2:], mode="bilinear",
align_corners=True)
int_masks_soft = self.affinity(im, int_masks_soft.detach())
int_masks_orig[:, 1:] *= l1h[:, :, None, None]
int_masks_soft[:, 1:] *= l1h[:, :, None, None]
pseudo_gt_seg = pseudo_gtmask(int_masks_soft, ambiguous=True, cutoff_top=0.6,
cutoff_bkg=0.7, cutoff_low=0.2).detach() # B x C x HW
pseudo_gt_seg_lx = binarize(int_masks_orig)
pseudo_gt_seg_lx = (self.opts.alpha * pseudo_gt_seg_lx) + \
((1-self.opts.alpha) * int_masks_orig)
# ignore_mask = (pseudo_gt_seg.sum(1) > 0)
px_cls_per_image = pseudo_gt_seg_lx.view(bs, self.tot_classes, -1).sum(dim=-1)
batch_weight = torch.eq((px_cls_per_image[:, self.old_classes:] > 0),
l1h[:, self.old_classes - 1:].bool())
batch_weight = (
batch_weight.sum(dim=1) == (self.tot_classes - self.old_classes)).float()
target_old = torch.sigmoid(outputs_old.detach())
target = torch.cat((target_old, pseudo_gt_seg_lx[:, self.old_classes:]), dim=1)
if self.opts.icarl_bkg == -1:
target[:, 0] = torch.min(target[:, 0], pseudo_gt_seg_lx[:, 0])
else:
target[:, 0] = (1-self.opts.icarl_bkg) * target[:, 0] + \
self.opts.icarl_bkg * pseudo_gt_seg_lx[:, 0]
l_seg = F.binary_cross_entropy_with_logits(outputs, target, reduction='none').sum(dim=1)
l_seg = l_seg.view(bs, -1).mean(dim=-1)
l_seg = self.opts.l_seg * (batch_weight * l_seg).sum() / (batch_weight.sum() + 1e-5)
l_cls = balanced_mask_loss_ce(int_masks_raw, pseudo_gt_seg, l1h)
loss = l_seg + l_cam_out
l_reg = l_cls + l_cam_int
# xxx first backprop of previous loss (compute the gradients for regularization methods)
loss_tot = loss + lkd + lde + l_icarl + l_reg
self.scaler.scale(loss_tot).backward()
self.scaler.step(optim)
if scheduler is not None:
scheduler.step()
self.scaler.update()
epoch_loss += loss.item()
reg_loss += l_reg.item() if l_reg != 0. else 0.
reg_loss += lkd.item() + lde.item() + l_icarl.item()
interval_loss += loss.item() + lkd.item() + lde.item() + l_icarl.item()
interval_loss += l_reg.item() if l_reg != 0. else 0.
if tq is not None:
tq.update(1)
tq.set_postfix(loss='%.6f' % loss)
if (cur_step + 1) % print_int == 0:
interval_loss = interval_loss / print_int
logger.debug(f"Epoch {cur_epoch}, Batch {cur_step + 1}/{len(train_loader)},"
f" Loss={interval_loss}")
logger.debug(f"Loss made of: CE {loss}, LKD {lkd}, LDE {lde}, LReg {l_reg}")
# visualization
if logger is not None:
x = cur_epoch * len(train_loader) + cur_step + 1
logger.add_scalar('Loss/tot', interval_loss, x, intermediate=True)
logger.add_scalar('Loss/CAM_int', l_cam_int, x, intermediate=True)
logger.add_scalar('Loss/CAM_out', l_cam_out, x, intermediate=True)
logger.add_scalar('Loss/SEG_int', l_cls, x, intermediate=True)
logger.add_scalar('Loss/SEG_out', l_seg, x, intermediate=True)
logger.commit(intermediate=True)
interval_loss = 0.0
if tq is not None:
tq.close()
# collect statistics from multiple processes
epoch_loss = torch.tensor(epoch_loss).to(self.device)
reg_loss = torch.tensor(reg_loss).to(self.device)
torch.distributed.reduce(epoch_loss, dst=0)
torch.distributed.reduce(reg_loss, dst=0)
if distributed.get_rank() == 0:
epoch_loss = epoch_loss / distributed.get_world_size() / len(train_loader)
reg_loss = reg_loss / distributed.get_world_size() / len(train_loader)
logger.info(f"Epoch {cur_epoch}, Class Loss={epoch_loss}, Reg Loss={reg_loss}")
return (epoch_loss, reg_loss)
def validate(self, loader, metrics):
"""Do validation and return specified samples"""
metrics.reset()
model = self.model
device = self.device
model.eval()
with torch.no_grad():
for i, x in enumerate(loader):
images = x[0].to(device, dtype=torch.float32)
labels = x[1].to(device, dtype=torch.long)
# if self.weakly:
# l1h = x[2]
with amp.autocast():
outputs, features = model(images)
_, prediction = outputs.max(dim=1)
labels = labels.cpu().numpy()
prediction = prediction.cpu().numpy()
metrics.update(labels, prediction)
# collect statistics from multiple processes
metrics.synch(device)
score = metrics.get_results()
return score
def validate_CAM(self, loader, metrics):
"""Do validation and return specified samples"""
metrics.reset()
model = self.model
device = self.device
self.pseudolabeler.eval()
model.eval()
def classify(images):
masks = self.pseudolabeler(model(images, as_feature_extractor=True)['body'])
masks = F.interpolate(masks, size=images.shape[-2:], mode="bilinear", align_corners=False)
masks = masks.softmax(dim=1)
return masks
i = -1
with torch.no_grad():
for x in tqdm.tqdm(loader):
i = i+1
images = x[0].to(device, dtype=torch.float32)
labels = x[1].to(device, dtype=torch.long)
l1h = x[2].to(device, dtype=torch.bool)
with amp.autocast():
masks = classify(images)
_, prediction = masks.max(dim=1)
labels[labels < self.old_classes] = 0
labels = labels.cpu().numpy()
prediction = prediction.cpu().numpy()
metrics.update(labels, prediction)
# collect statistics from multiple processes
metrics.synch(device)
score = metrics.get_results()
return score
def load_step_ckpt(self, path):
# generate model from path
if osp.exists(path):
step_checkpoint = torch.load(path, map_location="cpu")
self.model.load_state_dict(step_checkpoint['model_state'], strict=False) # False for incr. classifiers
if self.opts.init_balanced:
# implement the balanced initialization (new cls has weight of background and bias = bias_bkg - log(N+1)
self.model.module.init_new_classifier(self.device)
# Load state dict from the model state dict, that contains the old model parameters
new_state = {}
for k, v in step_checkpoint['model_state'].items():
new_state[k[7:]] = v
self.model_old.load_state_dict(new_state, strict=True) # Load also here old parameters
self.logger.info(f"[!] Previous model loaded from {path}")
# clean memory
del step_checkpoint['model_state']
elif self.opts.debug:
self.logger.info(f"[!] WARNING: Unable to find of step {self.opts.step - 1}! "
f"Do you really want to do from scratch?")
else:
raise FileNotFoundError(path)
def load_ckpt(self, path):
opts = self.opts
assert osp.isfile(path), f"Error, ckpt not found in {path}"
checkpoint = torch.load(opts.ckpt, map_location="cpu")
self.model.load_state_dict(checkpoint["model_state"], strict=True)
self.optimizer.load_state_dict(checkpoint["optimizer_state"])
self.scheduler.load_state_dict(checkpoint["scheduler_state"])
if "scaler" in checkpoint:
self.scaler.load_state_dict(checkpoint["scaler"])
if self.weakly:
self.pseudolabeler.load_state_dict(checkpoint["pseudolabeler"])
cur_epoch = checkpoint["epoch"] + 1
best_score = checkpoint['best_score']
self.logger.info("[!] Model restored from %s" % opts.ckpt)
# if we want to resume training, resume trainer from checkpoint
del checkpoint
return cur_epoch, best_score