forked from Rodger-Huang/SYSU-HCP-at-ImageCLEF-VQA-Med-2021
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
617 lines (504 loc) · 22.2 KB
/
utils.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim.optimizer import Optimizer
from torch.optim.lr_scheduler import _LRScheduler
from torch.autograd import Variable
from torch.nn import Parameter
import math
from scipy.special import lambertw
from scipy.special import binom
class SuperLoss(nn.Module):
def __init__(self, C=330, lam=0.25, rank=None):
super(SuperLoss, self).__init__()
self.tau = torch.log(torch.FloatTensor([C]).to(rank))
self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100
self.rank = rank
def forward(self, l_i):
l_i_detach = l_i.detach()
# self.tau = 0.9 * self.tau + 0.1 * l_i_detach
sigma = self.sigma(l_i_detach)
loss = (l_i - self.tau) * sigma + self.lam * torch.log(sigma)**2
loss = loss.mean()
return loss
def sigma(self, l_i):
x = -2 / torch.exp(torch.ones_like(l_i)).to(self.rank)
y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam)
y = y.cpu().numpy()
sigma = np.exp(-lambertw(y))
sigma = sigma.real.astype(np.float32)
sigma = torch.from_numpy(sigma).to(self.rank)
return sigma
def load_pretrain_model(net, weights):
net_keys = list(net.state_dict().keys())
weights_keys = list(weights.keys())
# assert(len(net_keys) <= len(weights_keys))
i = 0
j = 0
while i < len(net_keys) and j < len(weights_keys):
name_i = net_keys[i]
name_j = weights_keys[j]
if net.state_dict()[name_i].shape == weights[name_j].shape:
net.state_dict()[name_i].copy_(weights[name_j].cpu())
i += 1
j += 1
else:
i += 1
return net
class WarmUpLR(_LRScheduler):
"""warmup_training learning rate scheduler
Args:
optimizer: optimzier(e.g. SGD)
total_iters: totoal_iters of warmup phase
"""
def __init__(self, optimizer, total_iters, last_epoch=-1):
self.total_iters = total_iters
super().__init__(optimizer, last_epoch)
def get_lr(self):
"""we will use the first m batches, and set the learning
rate to base_lr * m / total_iters
"""
return [base_lr * self.last_epoch / (self.total_iters + 1e-8) for base_lr in self.base_lrs]
def mixup_data(x, y, alpha=1.0, use_cuda=True, rank=None):
'''Compute the mixup data. Return mixed inputs, pairs of targets, and lambda'''
if alpha > 0.:
lam = np.random.beta(alpha, alpha)
else:
lam = 1.
batch_size = x.size()[0]
if use_cuda:
index = torch.randperm(batch_size).to(rank)
else:
index = torch.randperm(batch_size)
mixed_x = lam * x + (1 - lam) * x[index, :]
y_a, y_b = y, y[index]
return mixed_x, y_a, y_b, lam
def mixup_criterion(y_a, y_b, lam):
return lambda criterion, pred: lam * criterion(pred, y_a) + (1 - lam) * criterion(pred, y_b)
def smooth(label, classes, eta=0.1):
smoothed = []
for l in label:
res = l.one_hot(classes, on_value=1 - eta + eta / classes, off_value=eta / classes)
smoothed.append(res)
return smoothed
def CELoss(logit, target, reduction='mean'):
criterion = nn.CrossEntropyLoss(reduction=reduction)
return criterion(logit, target)
class SGD_GC(Optimizer):
def __init__(self, params, lr=1e-4, momentum=0, dampening=0,
weight_decay=0, nesterov=False):
if lr < 0.0:
raise ValueError("Invalid learning rate: {}".format(lr))
if momentum < 0.0:
raise ValueError("Invalid momentum value: {}".format(momentum))
if weight_decay < 0.0:
raise ValueError("Invalid weight_decay value: {}".format(weight_decay))
defaults = dict(lr=lr, momentum=momentum, dampening=dampening,
weight_decay=weight_decay, nesterov=nesterov)
if nesterov and (momentum <= 0 or dampening != 0):
raise ValueError("Nesterov momentum requires a momentum and zero dampening")
super(SGD_GC, self).__init__(params, defaults)
def __setstate__(self, state):
super(SGD_GC, self).__setstate__(state)
for group in self.param_groups:
group.setdefault('nesterov', False)
def step(self, closure=None):
"""Performs a single optimization step.
Arguments:
closure (callable, optional): A closure that reevaluates the model
and returns the loss.
"""
loss = None
if closure is not None:
loss = closure()
for group in self.param_groups:
weight_decay = group['weight_decay']
momentum = group['momentum']
dampening = group['dampening']
nesterov = group['nesterov']
for p in group['params']:
if p.grad is None:
continue
d_p = p.grad.data
if weight_decay != 0:
d_p.add_(weight_decay, p.data)
# GC operation for Conv layers and FC layers
if len(list(d_p.size())) > 1:
d_p.add_(-d_p.mean(dim=tuple(range(1, len(list(d_p.size())))), keepdim=True))
if momentum != 0:
param_state = self.state[p]
if 'momentum_buffer' not in param_state:
buf = param_state['momentum_buffer'] = torch.clone(d_p).detach()
else:
buf = param_state['momentum_buffer']
buf.mul_(momentum).add_(1 - dampening, d_p)
if nesterov:
d_p = d_p.add(momentum, buf)
else:
d_p = buf
p.data.add_(-group['lr'], d_p)
return loss
class FocalLossV1(nn.Module):
def __init__(self,
alpha=0.25,
gamma=2,
reduction='mean', ):
super(FocalLossV1, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.reduction = reduction
self.crit = nn.BCEWithLogitsLoss(reduction='none')
def forward(self, logits, label):
'''
args:
logits: tensor of shape (N, ...)
label: tensor of shape(N, ...)
'''
# compute loss
logits = logits.float() # use fp32 if logits is fp16
with torch.no_grad():
alpha = torch.empty_like(logits).fill_(1 - self.alpha)
alpha[label == 1] = self.alpha
probs = torch.sigmoid(logits)
pt = torch.where(label == 1, probs, 1 - probs)
ce_loss = self.crit(logits, label.double())
loss = (alpha * torch.pow(1 - pt, self.gamma) * ce_loss)
if self.reduction == 'mean':
loss = loss.mean()
if self.reduction == 'sum':
loss = loss.sum()
return loss
class MultiFocalLoss(nn.Module):
"""
This is a implementation of Focal Loss with smooth label cross entropy supported which is proposed in
'Focal Loss for Dense Object Detection. (https://arxiv.org/abs/1708.02002)'
Focal_Loss= -1*alpha*(1-pt)^gamma*log(pt)
:param num_class:
:param alpha: (tensor) 3D or 4D the scalar factor for this criterion
:param gamma: (float,double) gamma > 0 reduces the relative loss for well-classified examples (p>0.5) putting more
focus on hard misclassified example
:param smooth: (float,double) smooth value when cross entropy
:param balance_index: (int) balance class index, should be specific when alpha is float
:param size_average: (bool, optional) By default, the losses are averaged over each loss element in the batch.
"""
def __init__(self, num_class=330, alpha=None, gamma=2, balance_index=-1, smooth=None, size_average=True):
super(MultiFocalLoss, self).__init__()
self.num_class = num_class
self.alpha = alpha
self.gamma = gamma
self.smooth = smooth
self.size_average = size_average
if self.alpha is None:
self.alpha = torch.ones(self.num_class, 1)
elif isinstance(self.alpha, (list, np.ndarray)):
assert len(self.alpha) == self.num_class
self.alpha = torch.FloatTensor(alpha).view(self.num_class, 1)
self.alpha = self.alpha / self.alpha.sum()
elif isinstance(self.alpha, float):
alpha = torch.ones(self.num_class, 1)
alpha = alpha * (1 - self.alpha)
alpha[balance_index] = self.alpha
self.alpha = alpha
else:
raise TypeError('Not support alpha type')
if self.smooth is not None:
if self.smooth < 0 or self.smooth > 1.0:
raise ValueError('smooth value should be in [0,1]')
def forward(self, input, target):
logit = F.softmax(input, dim=1)
if logit.dim() > 2:
# N,C,d1,d2 -> N,C,m (m=d1*d2*...)
logit = logit.view(logit.size(0), logit.size(1), -1)
logit = logit.permute(0, 2, 1).contiguous()
logit = logit.view(-1, logit.size(-1))
target = target.view(-1, 1)
# N = input.size(0)
# alpha = torch.ones(N, self.num_class)
# alpha = alpha * (1 - self.alpha)
# alpha = alpha.scatter_(1, target.long(), self.alpha)
epsilon = 1e-10
alpha = self.alpha
if alpha.device != input.device:
alpha = alpha.to(input.device)
idx = target.cpu().long()
one_hot_key = torch.FloatTensor(target.size(0), self.num_class).zero_()
one_hot_key = one_hot_key.scatter_(1, idx, 1)
if one_hot_key.device != logit.device:
one_hot_key = one_hot_key.to(logit.device)
if self.smooth:
one_hot_key = torch.clamp(
one_hot_key, self.smooth, 1.0 - self.smooth)
pt = (one_hot_key * logit).sum(1) + epsilon
logpt = pt.log()
gamma = self.gamma
alpha = alpha[idx]
loss = -1 * alpha * torch.pow((1 - pt), gamma) * logpt
if self.size_average:
loss = loss.mean()
else:
loss = loss.sum()
return loss
def cal_acc(y_pred, y):
y_pred = torch.argmax(y_pred, dim=1, keepdim=False)
return torch.sum(y_pred == y).float() / y.shape[0]
def lr_poly(base_lr, iter_, max_iter=100, power=0.9):
return base_lr * ((1 - float(iter_) / max_iter) ** power)
def split_weights(net):
"""split network weights into to categlories,
one are weights in conv layer and linear layer,
others are other learnable paramters(conv bias,
bn weights, bn bias, linear bias)
Args:
net: network architecture
Returns:
a dictionary of params splite into to categlories
"""
decay = []
no_decay = []
for m in net.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
decay.append(m.weight)
if m.bias is not None:
no_decay.append(m.bias)
else:
if hasattr(m, 'weight'):
no_decay.append(m.weight)
if hasattr(m, 'bias'):
no_decay.append(m.bias)
assert len(list(net.parameters())) == len(decay) + len(no_decay)
return [dict(params=decay), dict(params=no_decay, weight_decay=0)]
class LargeMarginInSoftmaxLoss(nn.CrossEntropyLoss):
r"""
This combines the Softmax Cross-Entropy Loss (nn.CrossEntropyLoss) and the large-margin inducing
regularization proposed in
T. Kobayashi, "Large-Margin In Softmax Cross-Entropy Loss." In BMVC2019.
This loss function inherits the parameters from nn.CrossEntropyLoss except for `reg_lambda` and `deg_logit`.
Args:
reg_lambda (float, optional): a regularization parameter. (default: 0.3)
deg_logit (bool, optional): underestimate (degrade) the target logit by -1 or not. (default: False)
If True, it realizes the method that incorporates the modified loss into ours
as described in the above paper (Table 4).
"""
def __init__(self, reg_lambda=0.3, deg_logit=None,
weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean'):
super(LargeMarginInSoftmaxLoss, self).__init__(weight=weight, size_average=size_average,
ignore_index=ignore_index, reduce=reduce, reduction=reduction)
self.reg_lambda = reg_lambda
self.deg_logit = deg_logit
def forward(self, input, target):
N = input.size(0) # number of samples
C = input.size(1) # number of classes
Mask = torch.zeros_like(input, requires_grad=False)
Mask[range(N), target] = 1
if self.deg_logit is not None:
input = input - self.deg_logit * Mask
loss = F.cross_entropy(input, target, weight=self.weight,
ignore_index=self.ignore_index, reduction=self.reduction)
X = input - 1.e6 * Mask # [N x C], excluding the target class
reg = 0.5 * ((F.softmax(X, dim=1) - 1.0 / (C - 1)) * F.log_softmax(X, dim=1) * (1.0 - Mask)).sum(dim=1)
if self.reduction == 'sum':
reg = reg.sum()
elif self.reduction == 'mean':
reg = reg.mean()
elif self.reduction == 'none':
reg = reg
return loss + self.reg_lambda * reg
class LabelSmoothingCrossEntropy(nn.Module):
def __init__(self, eps=0.1, reduction='mean', classes=330):
super(LabelSmoothingCrossEntropy, self).__init__()
self.eps = eps
self.reduction = reduction
def forward(self, output, target):
B, c = output.size()
log_preds = F.log_softmax(output, dim=-1)
if self.reduction == 'sum':
loss = -log_preds.sum()
else:
loss = -log_preds.sum(dim=-1)
if self.reduction == 'mean':
loss = loss.mean()
return loss * self.eps / c + (1 - self.eps) * F.nll_loss(log_preds, target, reduction=self.reduction)
class LabelSmoothingCrossEntropyWithSuperLoss(nn.Module):
def __init__(self, eps=0.1, reduction='mean', classes=330, rank=None):
super(LabelSmoothingCrossEntropyWithSuperLoss, self).__init__()
self.eps = eps
self.reduction = reduction
self.super_loss = SuperLoss(C=classes, rank=rank)
self.rank = rank
def forward(self, output, target):
B, c = output.size()
log_preds = F.log_softmax(output, dim=-1)
if self.reduction == 'sum':
loss = -log_preds.sum()
else:
loss = -log_preds.sum(dim=-1)
if self.reduction == 'mean':
loss = loss.mean()
# l_i = (-log_preds.sum(dim=-1)) * self.eps / c + (1 - self.eps) * F.nll_loss(log_preds, target, reduction='none')
# return self.super_loss(l_i)
loss_cls = loss * self.eps / c + (1 - self.eps) * self.super_loss(F.nll_loss(log_preds, target, reduction='none'))
return loss_cls
# index = torch.randperm(B).to(self.rank)
# same_index = target==target[index]
# dif_index = target!=target[index]
# same_loss = torch.pow(output[same_index] - output[index][same_index, :], 2)
# loss_com = torch.log(1 + torch.exp(-torch.abs(output[dif_index] - output[index][dif_index]).mean()))
# if same_loss.size()[0] != 0:
# loss_com += same_loss.mean()
# return loss_cls + 0.01 * loss_com
class LabelSmoothingCrossEntropyWithLMSCE(nn.Module):
def __init__(self, eps=0.1, reduction='mean'):
super(LabelSmoothingCrossEntropyWithLMSCE, self).__init__()
self.eps = eps
self.reduction = reduction
self.loss_F = LargeMarginInSoftmaxLoss(reduction=self.reduction)
def forward(self, output, target):
c = output.size()[-1]
log_preds = F.log_softmax(output, dim=-1)
if self.reduction == 'sum':
loss = -log_preds.sum()
else:
loss = -log_preds.sum(dim=-1)
if self.reduction == 'mean':
loss = loss.mean()
return loss * self.eps / c + (1 - self.eps) * self.loss_F(log_preds, target)
def flip_lr(image):
assert image.dim() == 4, 'You need to provide a [B,C,H,W] image to flip'
return torch.flip(image, [3])
class LSoftmaxLinear(nn.Module):
def __init__(self, input_features, output_features, margin, device):
super().__init__()
self.input_dim = input_features # number of input feature i.e. output of the last fc layer
self.output_dim = output_features # number of output = class numbers
self.margin = margin # m
self.beta = 100
self.beta_min = 0
self.scale = 0.99
self.device = device # gpu or cpu
# Initialize L-Softmax parameters
self.weight = nn.Parameter(torch.FloatTensor(input_features, output_features), requires_grad=True)
self.divisor = math.pi / self.margin # pi/m
self.C_m_2n = torch.Tensor(binom(margin, range(0, margin + 1, 2))).to(device) # C_m{2n}
self.cos_powers = torch.Tensor(range(self.margin, -1, -2)).to(device) # m - 2n
self.sin2_powers = torch.Tensor(range(len(self.cos_powers))).to(device) # n
self.signs = torch.ones(margin // 2 + 1).to(device) # 1, -1, 1, -1, ...
self.signs[1::2] = -1
self.reset_parameters()
def calculate_cos_m_theta(self, cos_theta):
sin2_theta = 1 - cos_theta**2
cos_terms = cos_theta.unsqueeze(1) ** self.cos_powers.unsqueeze(0) # cos^{m - 2n}
sin2_terms = (sin2_theta.unsqueeze(1) # sin2^{n}
** self.sin2_powers.unsqueeze(0))
cos_m_theta = (self.signs.unsqueeze(0) * # -1^{n} * C_m{2n} * cos^{m - 2n} * sin2^{n}
self.C_m_2n.unsqueeze(0) *
cos_terms *
sin2_terms).sum(1) # summation of all terms
return cos_m_theta
def reset_parameters(self):
nn.init.kaiming_normal_(self.weight.data.t())
def find_k(self, cos):
# to account for acos numerical errors
eps = 1e-7
cos = torch.clamp(cos, -1 + eps, 1 - eps)
acos = cos.acos()
k = (acos / self.divisor).floor().detach()
return k
def forward(self, input, target=None):
if self.training:
assert target is not None
x, w = input, self.weight
beta = max(self.beta, self.beta_min)
logit = x.mm(w)
indexes = range(logit.size(0))
logit_target = logit[indexes, target]
# cos(theta) = w * x / ||w||*||x||
w_target_norm = w[:, target].norm(p=2, dim=0)
x_norm = x.norm(p=2, dim=1)
cos_theta_target = logit_target / (w_target_norm * x_norm + 1e-10)
# equation 7
cos_m_theta_target = self.calculate_cos_m_theta(cos_theta_target)
# find k in equation 6
k = self.find_k(cos_theta_target)
# f_y_i
logit_target_updated = (w_target_norm *
x_norm *
(((-1) ** k * cos_m_theta_target) - 2 * k))
logit_target_updated_beta = (logit_target_updated + beta * logit[indexes, target]) / (1 + beta)
logit[indexes, target] = logit_target_updated_beta
self.beta *= self.scale
return logit
else:
assert target is None
return input.mm(self.weight)
def myphi(x,m):
x = x * m
return 1-x**2/math.factorial(2)+x**4/math.factorial(4)-x**6/math.factorial(6) + \
x**8/math.factorial(8) - x**9/math.factorial(9)
class AngleLinear(nn.Module):
def __init__(self, in_features, out_features, m=4, phiflag=True):
super(AngleLinear, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.Tensor(in_features,out_features))
self.weight.data.uniform_(-1, 1).renorm_(2,1,1e-5).mul_(1e5)
self.phiflag = phiflag
self.m = m
self.mlambda = [
lambda x: x**0,
lambda x: x**1,
lambda x: 2*x**2-1,
lambda x: 4*x**3-3*x,
lambda x: 8*x**4-8*x**2+1,
lambda x: 16*x**5-20*x**3+5*x
]
def forward(self, input):
x = input # size=(B,F) F is feature len
w = self.weight # size=(F,Classnum) F=in_features Classnum=out_features
ww = w.renorm(2, 1, 1e-5).mul(1e5)
xlen = x.pow(2).sum(1).pow(0.5) # size=B
wlen = ww.pow(2).sum(0).pow(0.5) # size=Classnum
cos_theta = x.mm(ww) # size=(B,Classnum)
cos_theta = cos_theta / xlen.view(-1, 1) / wlen.view(1, -1)
cos_theta = cos_theta.clamp(-1, 1)
if self.phiflag:
cos_m_theta = self.mlambda[self.m](cos_theta)
theta = Variable(cos_theta.data.acos())
k = (self.m*theta/3.14159265).floor()
n_one = k*0.0 - 1
phi_theta = (n_one**k) * cos_m_theta - 2*k
else:
theta = cos_theta.acos()
phi_theta = myphi(theta,self.m)
phi_theta = phi_theta.clamp(-1*self.m,1)
cos_theta = cos_theta * xlen.view(-1, 1)
phi_theta = phi_theta * xlen.view(-1, 1)
output = (cos_theta, phi_theta)
return output # size=(B, Classnum, 2)
class AngleLoss(nn.Module):
def __init__(self, gamma=0):
super(AngleLoss, self).__init__()
self.gamma = gamma
self.it = 0
self.LambdaMin = 5.0
self.LambdaMax = 1500.0
self.lamb = 1500.0
def forward(self, input, target):
self.it += 1
cos_theta, phi_theta = input
target = target.view(-1, 1) # size=(B, 1)
index = cos_theta.data * 0.0 # size=(B, Classnum)
index.scatter_(1, target.data.view(-1, 1), 1)
index = index.bool()
index = Variable(index)
self.lamb = max(self.LambdaMin, self.LambdaMax/(1 + 0.1*self.it))
output = cos_theta * 1.0 # size=(B, Classnum)
output[index] -= cos_theta[index] * (1.0+0) / (1 + self.lamb)
output[index] += phi_theta[index] * (1.0+0) / (1 + self.lamb)
logpt = F.log_softmax(output, dim=-1)
logpt = logpt.gather(1, target)
logpt = logpt.view(-1)
pt = Variable(logpt.data.exp())
loss = -1 * (1 - pt)**self.gamma * logpt
loss = loss.mean()
return loss