-
Notifications
You must be signed in to change notification settings - Fork 5
/
trainer.py
483 lines (402 loc) · 23.1 KB
/
trainer.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
481
482
483
import os
import numpy as np
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.utils as nn_utils
import torch.backends.cudnn as cudnn
from torch.nn import SyncBatchNorm
import torch.optim.lr_scheduler as lr_scheduler
from torch.nn.parallel import DistributedDataParallel
import utils
from utils import CONFIG
import networks
from rmi import RMILoss
class Trainer(object):
def __init__(self,
train_dataloader,
test_dataloader,
logger,
tb_logger):
# Save GPU memory.
cudnn.benchmark = False
self.train_dataloader = train_dataloader
self.test_dataloader = test_dataloader
self.logger = logger
self.tb_logger = tb_logger
self.model_config = CONFIG.model
self.train_config = CONFIG.train
self.log_config = CONFIG.log
self.loss_dict = {'rec': None,
'comp': None,
'smooth_l1':None,
'grad':None,
'gabor':None,
'rmi':None}
self.test_loss_dict = {'rec': None,
'smooth_l1':None,
'mse':None,
'sad':None,
'grad':None,
'gabor':None,
'rmi':None}
self.grad_filter = torch.tensor(utils.get_gradfilter()).cuda()
self.gabor_filter = torch.tensor(utils.get_gaborfilter(16)).cuda()
self.build_model()
self.resume_step = None
self.best_loss = 1e+8
utils.print_network(self.G, CONFIG.version)
if self.train_config.resume_checkpoint:
self.logger.info('Resume checkpoint: {}'.format(self.train_config.resume_checkpoint))
self.restore_model(self.train_config.resume_checkpoint)
if self.model_config.imagenet_pretrain and self.train_config.resume_checkpoint is None:
self.logger.info('Load Imagenet Pretrained: {}'.format(self.model_config.imagenet_pretrain_path))
if self.model_config.arch.encoder == "vgg_encoder":
utils.load_VGG_pretrain(self.G, self.model_config.imagenet_pretrain_path)
else:
utils.load_imagenet_pretrain(self.G, self.model_config.imagenet_pretrain_path)
def build_model(self):
self.G = networks.get_generator(encoder=self.model_config.arch.encoder, decoder=self.model_config.arch.decoder)
self.G.cuda()
if CONFIG.dist:
self.logger.info("Using pytorch synced BN")
self.G = SyncBatchNorm.convert_sync_batchnorm(self.G)
self.G_optimizer = torch.optim.Adam(self.G.parameters(),
lr = self.train_config.G_lr,
betas = [self.train_config.beta1, self.train_config.beta2])
if CONFIG.dist:
# SyncBatchNorm only supports DistributedDataParallel with single GPU per process
self.G = DistributedDataParallel(self.G, device_ids=[CONFIG.local_rank], output_device=CONFIG.local_rank)
else:
self.G = nn.DataParallel(self.G)
self.build_lr_scheduler()
def build_lr_scheduler(self):
"""Build cosine learning rate scheduler."""
self.G_scheduler = lr_scheduler.CosineAnnealingLR(self.G_optimizer,
T_max=self.train_config.total_step
- self.train_config.warmup_step)
def reset_grad(self):
"""Reset the gradient buffers."""
self.G_optimizer.zero_grad()
def restore_model(self, resume_checkpoint):
"""
Restore the trained generator and discriminator.
:param resume_checkpoint: File name of checkpoint
:return:
"""
pth_path = os.path.join(self.log_config.checkpoint_path, '{}.pth'.format(resume_checkpoint))
checkpoint = torch.load(pth_path, map_location = lambda storage, loc: storage.cuda(CONFIG.gpu))
self.resume_step = checkpoint['iter']
self.logger.info('Loading the trained models from step {}...'.format(self.resume_step))
self.G.load_state_dict(checkpoint['state_dict'], strict=True)
if not self.train_config.reset_lr:
if 'opt_state_dict' in checkpoint.keys():
try:
self.G_optimizer.load_state_dict(checkpoint['opt_state_dict'])
except ValueError as ve:
self.logger.error("{}".format(ve))
else:
self.logger.info('No Optimizer State Loaded!!')
if 'lr_state_dict' in checkpoint.keys():
try:
self.G_scheduler.load_state_dict(checkpoint['lr_state_dict'])
except ValueError as ve:
self.logger.error("{}".format(ve))
else:
self.G_scheduler = lr_scheduler.CosineAnnealingLR(self.G_optimizer,
T_max=self.train_config.total_step - self.resume_step - 1)
if 'loss' in checkpoint.keys():
self.best_loss = checkpoint['loss']
def train(self):
data_iter = iter(self.train_dataloader)
if self.train_config.resume_checkpoint:
start = self.resume_step + 1
else:
start = 0
moving_max_grad = 0
moving_grad_moment = 0.999
max_grad = 0
for step in range(start, self.train_config.total_step + 1):
print('***************',step,'*******************')
try:
image_dict = next(data_iter)
except:
data_iter = iter(self.train_dataloader)
image_dict = next(data_iter)
image, alpha, trimap = image_dict['image'], image_dict['alpha'], image_dict['trimap']
image = image.cuda()
alpha = alpha.cuda()
trimap = trimap.cuda()
# train() of DistributedDataParallel has no return
self.G.train()
log_info = ""
loss = 0
"""===== Update Learning Rate ====="""
if step < self.train_config.warmup_step and self.train_config.resume_checkpoint is None:
cur_G_lr = utils.warmup_lr(self.train_config.G_lr, step + 1, self.train_config.warmup_step)
utils.update_lr(cur_G_lr, self.G_optimizer)
else:
self.G_scheduler.step()
cur_G_lr = self.G_scheduler.get_lr()[0]
"""===== Forward G ====="""
alpha_pred, info_dict = self.G(image, trimap) # info_dict: intermediate feature of networks like attention
#alpha_pred_init, info_dict_init = self.G(image, trimap)
#alpha_pred, info_dict = self.G(image, alpha_pred_init)
weight = utils.get_unknown_tensor(trimap)
#weight = None
#print("{}-------".format(torch.sum(weight)))
"""===== Calculate Loss ====="""
if self.train_config.rec_weight > 0:
self.loss_dict['rec'] = self.regression_loss(alpha_pred, alpha, loss_type='l1', weight=weight) \
* self.train_config.rec_weight
if self.train_config.smooth_l1_weight > 0:
self.loss_dict['smooth_l1'] = self.smooth_l1(alpha_pred, alpha, weight=weight) \
* self.train_config.smooth_l1_weight
if self.train_config.comp_weight > 0:
self.loss_dict['comp'] = self.composition_loss(alpha_pred, image_dict['fg'].cuda(),
image_dict['bg'].cuda(), image, weight=weight) \
* self.train_config.comp_weight
if self.train_config.grad_weight > 0:
self.loss_dict['grad'] = self.grad_loss(alpha_pred, alpha, weight=weight, grad_filter=self.grad_filter) \
* self.train_config.grad_weight
if self.train_config.gabor_weight > 0:
self.loss_dict['gabor'] = self.gabor_loss(alpha_pred, alpha, weight=weight, gabor_filter=self.gabor_filter) \
* self.train_config.gabor_weight
if self.train_config.rmi_weight > 0:
self.loss_dict['rmi'] = self.rmi_loss(alpha_pred, alpha)*self.train_config.rmi_weight
for loss_key in self.loss_dict.keys():
if self.loss_dict[loss_key] is not None and loss_key in ['rec', 'comp', 'smooth_l1', 'grad', 'gabor', 'rmi']:
loss += self.loss_dict[loss_key]
"""===== Back Propagate ====="""
self.reset_grad()
loss.backward()
"""===== Clip Large Gradient ====="""
if self.train_config.clip_grad:
if moving_max_grad == 0:
moving_max_grad = nn_utils.clip_grad_norm_(self.G.parameters(), 1e+6)
max_grad = moving_max_grad
else:
max_grad = nn_utils.clip_grad_norm_(self.G.parameters(), 2 * moving_max_grad)
moving_max_grad = moving_max_grad * moving_grad_moment + max_grad * (
1 - moving_grad_moment)
"""===== Update Parameters ====="""
self.G_optimizer.step()
"""===== Write Log and Tensorboard ====="""
# stdout log
if step % self.log_config.logging_step == 0:
# reduce losses from GPUs
if CONFIG.dist:
self.loss_dict = utils.reduce_tensor_dict(self.loss_dict, mode='mean')
loss = utils.reduce_tensor(loss)
# create logging information
for loss_key in self.loss_dict.keys():
if self.loss_dict[loss_key] is not None:
log_info += loss_key.upper() + ": {:.4f}, ".format(self.loss_dict[loss_key])
self.logger.debug("Image tensor shape: {}. Trimap tensor shape: {}".format(image.shape, trimap.shape))
log_info = "[{}/{}], ".format(step, self.train_config.total_step) + log_info
log_info += "lr: {:6f}".format(cur_G_lr)
self.logger.info(log_info)
# tensorboard
if step % self.log_config.tensorboard_step == 0 or step == start: # and step > start:
self.tb_logger.scalar_summary('Loss', loss, step)
# detailed losses
for loss_key in self.loss_dict.keys():
if self.loss_dict[loss_key] is not None:
self.tb_logger.scalar_summary('Loss_' + loss_key.upper(),
self.loss_dict[loss_key], step)
self.tb_logger.scalar_summary('LearnRate', cur_G_lr, step)
if self.train_config.clip_grad:
self.tb_logger.scalar_summary('Moving_Max_Grad', moving_max_grad, step)
self.tb_logger.scalar_summary('Max_Grad', max_grad, step)
# write images to tensorboard
if step % self.log_config.tensorboard_image_step == 0 or step == start:
if self.model_config.trimap_channel == 3:
trimap = trimap.argmax(dim=1, keepdim=True)
alpha_pred[trimap==2] = 1
alpha_pred[trimap==0] = 0
image_set = {'image': (utils.normalize_image(image[-1, ...]).data.cpu().numpy()
* 255).astype(np.uint8),
'trimap': (trimap[-1, ...].data.cpu().numpy() * 127).astype(np.uint8),
'alpha': (alpha[-1, ...].data.cpu().numpy() * 255).astype(np.uint8),
'alpha_pred': (alpha_pred[-1, ...].data.cpu().numpy() * 255).astype(np.uint8)}
if info_dict is not None:
for key in info_dict.keys():
if key.startswith('offset'):
image_set[key] = utils.flow_to_image(info_dict[key][0][-1,...].data.cpu()
.numpy()).transpose([2, 0, 1]).astype(np.uint8)
# write softmax_scale to offset image
scale = info_dict[key][1].cpu()
image_set[key] = utils.put_text(image_set[key], 'unknown: {:.2f}, known: {:.2f}'
.format(scale[-1,0].item(), scale[-1,1].item()))
else:
image_set[key] = (utils.normalize_image(info_dict[key][-1,...]).data.cpu().numpy()
* 255).astype(np.uint8)
self.tb_logger.image_summary(image_set, step)
"""===== TEST ====="""
if ((step % self.train_config.val_step) == 0 or step == self.train_config.total_step):# and step > start:
if not os.path.exists(CONFIG.root_path + 'prediction/step_'+str(step)):
os.mkdir(CONFIG.root_path + 'prediction/step_'+str(step))
self.G.eval()
test_loss = 0
log_info = ""
self.test_loss_dict['mse'] = 0
self.test_loss_dict['sad'] = 0
# self.test_loss_dict['rmi'] = 0
for loss_key in self.loss_dict.keys():
if loss_key in self.test_loss_dict and self.loss_dict[loss_key] is not None:
self.test_loss_dict[loss_key] = 0
with torch.no_grad():
for idx,image_dict in enumerate(self.test_dataloader):
image, alpha, trimap = image_dict['image'], image_dict['alpha'], image_dict['trimap']
alpha_shape = image_dict['alpha_shape']
image = image.cuda()
alpha = alpha.cuda()
trimap = trimap.cuda()
alpha_pred, info_dict = self.G(image, trimap)
cv2.imwrite(CONFIG.root_path + 'prediction/step_'+str(step)+'/'+image_dict['image_name'][0],alpha_pred.detach().cpu().numpy()[0][0]*255)
h, w = alpha_shape
alpha_pred = alpha_pred[..., :h, :w]
trimap = trimap[..., :h, :w]
weight = utils.get_unknown_tensor(trimap)
# value of MSE/SAD here is different from test.py and matlab version
self.test_loss_dict['mse'] += self.mse(alpha_pred, alpha, weight)
self.test_loss_dict['sad'] += self.sad(alpha_pred, alpha, weight)
# self.test_loss_dict['rmi'] += self.rmi_loss(alpha_pred, alpha)
if self.train_config.rec_weight > 0:
self.test_loss_dict['rec'] += self.regression_loss(alpha_pred, alpha, weight=weight) \
* self.train_config.rec_weight
if self.train_config.smooth_l1_weight > 0:
self.test_loss_dict['smooth_l1'] += self.smooth_l1(alpha_pred, alpha, weight=weight) \
* self.train_config.smooth_l1_weight
if self.train_config.grad_weight > 0:
self.test_loss_dict['grad'] = self.grad_loss(alpha_pred, alpha, weight=weight,
grad_filter=self.grad_filter) \
* self.train_config.grad_weight
if self.train_config.gabor_weight > 0:
self.test_loss_dict['gabor'] = self.gabor_loss(alpha_pred, alpha, weight=weight,
gabor_filter=self.gabor_filter) \
* self.train_config.gabor_weight
if self.train_config.rmi_weight > 0:
self.test_loss_dict['rmi'] += self.rmi_loss(alpha_pred, alpha) * self.train_config.rmi_weight
# reduce losses from GPUs
if CONFIG.dist:
self.test_loss_dict = utils.reduce_tensor_dict(self.test_loss_dict, mode='mean')
"""===== Write Log and Tensorboard ====="""
# stdout log
for loss_key in self.test_loss_dict.keys():
if self.test_loss_dict[loss_key] is not None:
self.test_loss_dict[loss_key] /= len(self.test_dataloader)
# logging
log_info += loss_key.upper()+": {:.4f} ".format(self.test_loss_dict[loss_key])
self.tb_logger.scalar_summary('Loss_'+loss_key.upper(),
self.test_loss_dict[loss_key], step, phase='test')
if loss_key in ['rec', 'smooth_l1', 'grad', 'gabor', 'rmi']:
test_loss += self.test_loss_dict[loss_key]
self.logger.info("TEST: LOSS: {:.4f} ".format(test_loss)+log_info)
self.tb_logger.scalar_summary('Loss', test_loss, step, phase='test')
if self.model_config.trimap_channel == 3:
trimap = trimap.argmax(dim=1, keepdim=True)
alpha_pred[trimap==2] = 1
alpha_pred[trimap==0] = 0
image_set = {'image': (utils.normalize_image(image[-1, ...]).data.cpu().numpy()
* 255).astype(np.uint8),
'trimap': (trimap[-1, ...].data.cpu().numpy() * 127).astype(np.uint8),
'alpha': (alpha[-1, ...].data.cpu().numpy() * 255).astype(np.uint8),
'alpha_pred': (alpha_pred[-1, ...].data.cpu().numpy() * 255).astype(np.uint8)}
if info_dict is not None:
for key in info_dict.keys():
if key.startswith('offset'):
image_set[key] = utils.flow_to_image(info_dict[key][0][-1,...].data.cpu()
.numpy()).transpose([2, 0, 1]).astype(np.uint8)
# write softmax_scale to offset image
scale = info_dict[key][1].cpu()
image_set[key] = utils.put_text(image_set[key], 'unknown: {:.2f}, known: {:.2f}'
.format(scale[-1,0].item(), scale[-1,1].item()))
else:
image_set[key] = (utils.normalize_image(info_dict[key][-1,...]).data.cpu().numpy()
* 255).astype(np.uint8)
self.tb_logger.image_summary(image_set, step, phase='test')
"""===== Save Model ====="""
if (step % self.log_config.checkpoint_step == 0 or step == self.train_config.total_step) \
and CONFIG.local_rank == 0 and (step > start):
self.logger.info('Saving the trained models from step {}...'.format(iter))
self.save_model("latest_model", step, loss)
if self.test_loss_dict['mse'] < self.best_loss:
self.best_loss = self.test_loss_dict['mse']
self.save_model("best_model", step, loss)
def save_model(self, checkpoint_name, iter, loss):
"""Restore the trained generator and discriminator."""
torch.save({
'iter': iter,
'loss': loss,
'state_dict': self.G.state_dict(),
'opt_state_dict': self.G_optimizer.state_dict(),
'lr_state_dict': self.G_scheduler.state_dict()
}, os.path.join(self.log_config.checkpoint_path, '{}.pth'.format(checkpoint_name)))
@staticmethod
def rmi_loss(logit, target):
"""
region mutual information loss
:param logit:
:param target:
:return:
"""
return RMILoss(with_logits=False, bce_weight=0, stride=1)(logit, target).float()
@staticmethod
def regression_loss(logit, target, loss_type='l1', weight=None):
"""
Alpha reconstruction loss
:param logit:
:param target:
:param loss_type: "l1" or "l2"
:param weight: tensor with shape [N,1,H,W] weights for each pixel
:return:
"""
if weight is None:
if loss_type == 'l1':
return F.l1_loss(logit, target)
elif loss_type == 'l2':
return F.mse_loss(logit, target)
else:
raise NotImplementedError("NotImplemented loss type {}".format(loss_type))
else:
if loss_type == 'l1':
return F.l1_loss(logit * weight, target * weight, reduction='sum') / (torch.sum(weight) + 1e-8)
elif loss_type == 'l2':
return F.mse_loss(logit * weight, target * weight, reduction='sum') / (torch.sum(weight) + 1e-8)
else:
raise NotImplementedError("NotImplemented loss type {}".format(loss_type))
@staticmethod
def smooth_l1(logit, target, weight):
loss = torch.sqrt((logit * weight - target * weight)**2 + 1e-6)
loss = torch.sum(loss) / (torch.sum(weight) + 1e-8)
return loss
@staticmethod
def mse(logit, target, weight):
# return F.mse_loss(logit * weight, target * weight, reduction='sum') / (torch.sum(weight) + 1e-8)
return Trainer.regression_loss(logit, target, loss_type='l2', weight=weight)
@staticmethod
def sad(logit, target, weight):
return F.l1_loss(logit * weight, target * weight, reduction='sum') / 1000
@staticmethod
def composition_loss(alpha, fg, bg, image, weight, loss_type='l1'):
"""
Alpha composition loss
"""
merged = fg * alpha + bg * (1 - alpha)
return Trainer.regression_loss(merged, image, loss_type=loss_type, weight=weight)
@staticmethod
def gabor_loss(logit, target, gabor_filter, loss_type='l2', weight=None):
""" pass """
gabor_logit = F.conv2d(logit, weight=gabor_filter, padding=2)
gabor_target = F.conv2d(target, weight=gabor_filter, padding=2)
return Trainer.regression_loss(gabor_logit, gabor_target, loss_type=loss_type, weight=weight)
@staticmethod
def grad_loss(logit, target, grad_filter, loss_type='l1', weight=None):
""" pass """
grad_logit = F.conv2d(logit, weight=grad_filter, padding=1)
grad_target = F.conv2d(target, weight=grad_filter, padding=1)
grad_logit = torch.sqrt((grad_logit * grad_logit).sum(dim=1, keepdim=True) + 1e-8)
grad_target = torch.sqrt((grad_target * grad_target).sum(dim=1, keepdim=True) + 1e-8)
return Trainer.regression_loss(grad_logit, grad_target, loss_type=loss_type, weight=weight)