-
Notifications
You must be signed in to change notification settings - Fork 17
/
util.py
168 lines (133 loc) · 4.73 KB
/
util.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
import logging
import os
import numpy as np
import torch
if torch.cuda.is_available():
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = True
device = torch.device('cuda')
else:
device = torch.device('cpu')
def _patch_noise_extend_to_img(noise, image_size=[3, 32, 32], patch_location='center'):
c, h, w = image_size[0], image_size[1], image_size[2]
mask = np.zeros((c, h, w), np.float32)
x_len, y_len = noise.shape[1], noise.shape[2]
if patch_location == 'center' or (h == w == x_len == y_len):
x = h // 2
y = w // 2
elif patch_location == 'random':
x = np.random.randint(x_len // 2, w - x_len // 2)
y = np.random.randint(y_len // 2, h - y_len // 2)
else:
raise('Invalid patch location')
x1 = np.clip(x - x_len // 2, 0, h)
x2 = np.clip(x + x_len // 2, 0, h)
y1 = np.clip(y - y_len // 2, 0, w)
y2 = np.clip(y + y_len // 2, 0, w)
mask[:, x1: x2, y1: y2] = noise
return mask
def setup_logger(name, log_file, level=logging.INFO):
"""To setup as many loggers as you want"""
formatter = logging.Formatter('%(asctime)s %(message)s')
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
def log_display(epoch, global_step, time_elapse, **kwargs):
display = 'epoch=' + str(epoch) + \
'\tglobal_step=' + str(global_step)
for key, value in kwargs.items():
if type(value) == str:
display = '\t' + key + '=' + value
else:
display += '\t' + str(key) + '=%.4f' % value
display += '\ttime=%.2fit/s' % (1. / time_elapse)
return display
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.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(1/batch_size))
return res
def save_model(filename, epoch, model, optimizer, scheduler, save_best=False, **kwargs):
# Torch Save State Dict
state = {
'epoch': epoch+1,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': scheduler.state_dict() if scheduler is not None else None
}
for key, value in kwargs.items():
state[key] = value
torch.save(state, filename + '.pth')
filename += '_best.pth'
if save_best:
torch.save(state, filename)
return
def load_model(filename, model, optimizer, scheduler, **kwargs):
# Load Torch State Dict
filename = filename + '.pth'
checkpoints = torch.load(filename, map_location=device)
model.load_state_dict(checkpoints['model_state_dict'])
if optimizer is not None and checkpoints['optimizer_state_dict'] is not None:
optimizer.load_state_dict(checkpoints['optimizer_state_dict'])
if scheduler is not None and checkpoints['scheduler_state_dict'] is not None:
scheduler.load_state_dict(checkpoints['scheduler_state_dict'])
return checkpoints
def count_parameters_in_MB(model):
return sum(np.prod(v.size()) for name, v in model.named_parameters() if "auxiliary_head" not in name)/1e6
def build_dirs(path):
if not os.path.exists(path):
os.makedirs(path)
return
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
self.max = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
self.max = max(self.max, val)
def onehot(size, target):
vec = torch.zeros(size, dtype=torch.float32)
vec[target] = 1.
return vec
def rand_bbox(size, lam):
if len(size) == 4:
W = size[2]
H = size[3]
elif len(size) == 3:
W = size[1]
H = size[2]
else:
raise Exception
cut_rat = np.sqrt(1. - lam)
cut_w = np.int(W * cut_rat)
cut_h = np.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