-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
114 lines (91 loc) · 3.15 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
import os
from mmaction.apis import init_recognizer
import torch
import numpy as np
import torch.nn.functional as F
import torch.nn as nn
def get_parameters(model):
group_no_weight_decay = []
group_weight_decay = []
for pname, p in model.named_parameters():
if pname.find("weight") >= 0 and len(p.size()) > 1:
# print('include ', pname, p.size())
group_weight_decay.append(p)
else:
# print('not include ', pname, p.size())
group_no_weight_decay.append(p)
assert len(list(model.parameters())) == len(group_weight_decay) + len(
group_no_weight_decay
)
groups = [
dict(params=group_weight_decay),
dict(params=group_no_weight_decay, weight_decay=0.0),
]
return groups
def keep_top_k(p, k, n_classes=1000): # p is the softmax on label output
if k == n_classes:
return p
values, indices = p.topk(k, dim=1)
mask_topk = torch.zeros_like(p)
mask_topk.scatter_(-1, indices, 1.0)
top_p = mask_topk * p
minor_value = (1 - torch.sum(values, dim=1)) / (n_classes - k)
minor_value = minor_value.unsqueeze(1).expand(p.shape)
mask_smooth = torch.ones_like(p)
mask_smooth.scatter_(-1, indices, 0)
smooth_p = mask_smooth * minor_value
topk_smooth_p = top_p + smooth_p
assert np.isclose(
topk_smooth_p.sum().item(), p.shape[0]
), f"{topk_smooth_p.sum().item()} not close to {p.shape[0]}"
return topk_smooth_p
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.avg = 0
self.sum = 0
self.cnt = 0
self.val = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.cnt += n
self.avg = self.sum / self.cnt
def accuracy(output, target, topk=(1,)):
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.reshape(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def rand_bbox(size, lam):
W = size[-2]
H = size[-1]
cut_rat = np.sqrt(1.0 - lam)
cut_w = int(W * cut_rat)
cut_h = int(H * cut_rat)
# uniform
cx = np.random.randint(W)
cy = np.random.randint(H)
bbx1 = np.clip(cx - cut_w // 2, 0, W)
bby1 = np.clip(cy - cut_h // 2, 0, H)
bbx2 = np.clip(cx + cut_w // 2, 0, W)
bby2 = np.clip(cy + cut_h // 2, 0, H)
return bbx1, bby1, bbx2, bby2
def cutmix(images, lam):
rand_index = torch.randperm(images.size()[0]).cuda()
lam = np.random.beta(lam, lam)
bbx1, bby1, bbx2, bby2 = rand_bbox(images.size(), lam)
images[:, :, :, bbx1:bbx2, bby1:bby2] = images[rand_index, :, :, bbx1:bbx2, bby1:bby2]
lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (input.size()[-1] * input.size()[-2]))
return images, lam, rand_index
def mixup(images, lam):
rand_index = torch.randperm(images.size()[0]).cuda()
lam = np.random.beta(lam, lam)
mixed_images = lam * images + (1 - lam) * images[rand_index]
return mixed_images