-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
341 lines (262 loc) · 9.67 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
import torch
import random
import numpy as np
import os
import sys
import time
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Agg')
__all__ = ["Compose", "Lighting", "ColorJitter"]
def dist_l2(data, target):
dist = (data**2).sum(-1).unsqueeze(1) + (
target**2).sum(-1).unsqueeze(0) - 2 * torch.matmul(data, target.transpose(1, 0))
return dist
def get_time():
return str(time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime()))
class Logger(object):
def __init__(self, fpath=None):
self.console = sys.stdout
self.file = None
if fpath is not None:
self.file = open(fpath, 'w')
def __del__(self):
self.close()
def __enter__(self):
pass
def __exit__(self, *args):
self.close()
def write(self, msg):
self.console.write(msg)
if self.file is not None:
self.file.write(msg)
def flush(self):
self.console.flush()
if self.file is not None:
self.file.flush()
os.fsync(self.file.fileno())
def close(self):
self.console.close()
if self.file is not None:
self.file.close()
class TimeStamp():
def __init__(self, print_log=True):
self.prev = time.time()
self.print_log = print_log
self.times = {}
def set(self):
self.prev = time.time()
def flush(self):
if self.print_log:
print("\n=========Summary=========")
for key in self.times.keys():
times = np.array(self.times[key])
print(
f"{key}: {times.sum():.4f}s (avg {times.mean():.4f}s, std {times.std():.4f}, count {len(times)})"
)
self.times[key] = []
def stamp(self, name=''):
if self.print_log:
spent = time.time() - self.prev
# print(f"{name}: {spent:.4f}s")
if name in self.times.keys():
self.times[name].append(spent)
else:
self.times[name] = [spent]
self.set()
def accuracy(output, target, topk=(1, )):
"""Computes the precision@k for the specified values of k"""
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, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
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
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class Plotter():
def __init__(self, path, nepoch, idx=0):
self.path = path
self.data = {'epoch': [], 'acc_tr': [], 'acc_val': [], 'loss_tr': [], 'loss_val': []}
self.nepoch = nepoch
self.plot_freq = 10
self.idx = idx
def update(self, epoch, acc_tr, acc_val, loss_tr, loss_val):
self.data['epoch'].append(epoch)
self.data['acc_tr'].append(acc_tr)
self.data['acc_val'].append(acc_val)
self.data['loss_tr'].append(loss_tr)
self.data['loss_val'].append(loss_val)
if len(self.data['epoch']) % self.plot_freq == 0:
self.plot()
def plot(self, color='black'):
fig, axes = plt.subplots(1, 4, figsize=(4 * 4, 3))
fig.tight_layout(h_pad=3, w_pad=3)
fig.suptitle(f"{self.path}", size=16, y=1.1)
axes[0].plot(self.data['epoch'], self.data['acc_tr'], color, lw=0.8)
axes[0].set_xlim([0, self.nepoch])
axes[0].set_ylim([0, 100])
axes[0].set_title('acc train')
axes[1].plot(self.data['epoch'], self.data['acc_val'], color, lw=0.8)
axes[1].set_xlim([0, self.nepoch])
axes[1].set_ylim([0, 100])
axes[1].set_title('acc val')
axes[2].plot(self.data['epoch'], self.data['loss_tr'], color, lw=0.8)
axes[2].set_xlim([0, self.nepoch])
axes[2].set_ylim([0, 3])
axes[2].set_title('loss train')
axes[3].plot(self.data['epoch'], self.data['loss_val'], color, lw=0.8)
axes[3].set_xlim([0, self.nepoch])
axes[3].set_ylim([0, 3])
axes[3].set_title('loss val')
for ax in axes:
ax.set_xlabel('epochs')
plt.savefig(f'{self.path}/curve_{self.idx}.png', bbox_inches='tight')
plt.close()
def random_indices(y, nclass=10, intraclass=False, device='cuda'):
n = len(y)
if intraclass:
index = torch.arange(n).to(device)
for c in range(nclass):
index_c = index[y == c]
if len(index_c) > 0:
randidx = torch.randperm(len(index_c))
index[y == c] = index_c[randidx]
else:
index = torch.randperm(n).to(device)
return index
def rand_bbox(size, lam):
W = size[2]
H = size[3]
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
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img):
for t in self.transforms:
img = t(img)
return img
def __repr__(self):
format_string = self.__class__.__name__ + '('
for t in self.transforms:
format_string += '\n'
format_string += ' {0}'.format(t)
format_string += '\n)'
return format_string
class Lighting(object):
"""Lighting noise(AlexNet - style PCA - based noise)"""
def __init__(self, alphastd, eigval, eigvec, device='cpu'):
self.alphastd = alphastd
self.eigval = torch.tensor(eigval, device=device)
self.eigvec = torch.tensor(eigvec, device=device)
def __call__(self, img):
if self.alphastd == 0:
return img
alpha = img.new().resize_(3).normal_(0, self.alphastd)
rgb = self.eigvec.type_as(img).clone() \
.mul(alpha.view(1, 3).expand(3, 3)) \
.mul(self.eigval.view(1, 3).expand(3, 3)) \
.sum(1).squeeze()
# make differentiable
if len(img.shape) == 4:
return img + rgb.view(1, 3, 1, 1).expand_as(img)
else:
return img + rgb.view(3, 1, 1).expand_as(img)
class Grayscale(object):
def __call__(self, img):
gs = img.clone()
gs[0].mul_(0.299).add_(0.587, gs[1]).add_(0.114, gs[2])
gs[1].copy_(gs[0])
gs[2].copy_(gs[0])
return gs
class Saturation(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
gs = Grayscale()(img)
alpha = random.uniform(-self.var, self.var)
return img.lerp(gs, alpha)
class Brightness(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
gs = img.new().resize_as_(img).zero_()
alpha = random.uniform(-self.var, self.var)
return img.lerp(gs, alpha)
class Contrast(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
gs = Grayscale()(img)
gs.fill_(gs.mean())
alpha = random.uniform(-self.var, self.var)
return img.lerp(gs, alpha)
class ColorJitter(object):
def __init__(self, brightness=0.4, contrast=0.4, saturation=0.4):
self.brightness = brightness
self.contrast = contrast
self.saturation = saturation
def __call__(self, img):
self.transforms = []
if self.brightness != 0:
self.transforms.append(Brightness(self.brightness))
if self.contrast != 0:
self.transforms.append(Contrast(self.contrast))
if self.saturation != 0:
self.transforms.append(Saturation(self.saturation))
random.shuffle(self.transforms)
transform = Compose(self.transforms)
# print(transform)
return transform(img)
class CutOut():
def __init__(self, ratio, device='cpu'):
self.ratio = ratio
self.device = device
def __call__(self, x):
n, _, h, w = x.shape
cutout_size = [int(h * self.ratio + 0.5), int(w * self.ratio + 0.5)]
offset_x = torch.randint(h + (1 - cutout_size[0] % 2), size=[1], device=self.device)[0]
offset_y = torch.randint(w + (1 - cutout_size[1] % 2), size=[1], device=self.device)[0]
grid_batch, grid_x, grid_y = torch.meshgrid(
torch.arange(n, dtype=torch.long, device=self.device),
torch.arange(cutout_size[0], dtype=torch.long, device=self.device),
torch.arange(cutout_size[1], dtype=torch.long, device=self.device),
)
grid_x = torch.clamp(grid_x + offset_x - cutout_size[0] // 2, min=0, max=h - 1)
grid_y = torch.clamp(grid_y + offset_y - cutout_size[1] // 2, min=0, max=w - 1)
mask = torch.ones(n, h, w, dtype=x.dtype, device=self.device)
mask[grid_batch, grid_x, grid_y] = 0
x = x * mask.unsqueeze(1)
return x
class Normalize():
def __init__(self, mean, std, device='cpu'):
self.mean = torch.tensor(mean, device=device).reshape(1, len(mean), 1, 1)
self.std = torch.tensor(std, device=device).reshape(1, len(mean), 1, 1)
def __call__(self, x, seed=-1):
return (x - self.mean) / self.std