-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.py
488 lines (385 loc) · 14.6 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
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
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].reshape(-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
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
'''Some helper functions for PyTorch, including:
- get_mean_and_std: calculate the mean and std value of dataset.
- msr_init: net parameter initialization.
- progress_bar: progress bar mimic xlua.progress.
'''
import os
import sys
import time
import math
import torch.nn as nn
import torch.nn.init as init
def get_mean_and_std(dataset):
'''Compute the mean and std value of dataset.'''
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True, num_workers=2)
mean = torch.zeros(3)
std = torch.zeros(3)
print('==> Computing mean and std..')
for inputs, targets in dataloader:
for i in range(3):
mean[i] += inputs[:,i,:,:].mean()
std[i] += inputs[:,i,:,:].std()
mean.div_(len(dataset))
std.div_(len(dataset))
return mean, std
def init_params(net):
'''Init layer parameters.'''
for m in net.modules():
if isinstance(m, nn.Conv2d):
init.kaiming_normal(m.weight, mode='fan_out')
if m.bias:
init.constant(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant(m.weight, 1)
init.constant(m.bias, 0)
elif isinstance(m, nn.Linear):
init.normal(m.weight, std=1e-3)
if m.bias:
init.constant(m.bias, 0)
# _, term_width = os.popen('stty size', 'r').read().split()
# term_width = int(term_width)
TOTAL_BAR_LENGTH = 65.
last_time = time.time()
begin_time = last_time
def progress_bar(current, total, msg=None):
global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.
cur_len = int(TOTAL_BAR_LENGTH*current/total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1
sys.stdout.write(' [')
for i in range(cur_len):
sys.stdout.write('=')
sys.stdout.write('>')
for i in range(rest_len):
sys.stdout.write('.')
sys.stdout.write(']')
cur_time = time.time()
step_time = cur_time - last_time
last_time = cur_time
tot_time = cur_time - begin_time
L = []
L.append(' Step: %s' % format_time(step_time))
L.append(' | Tot: %s' % format_time(tot_time))
if msg:
L.append(' | ' + msg)
msg = ''.join(L)
sys.stdout.write(msg)
for i in range(term_width-int(TOTAL_BAR_LENGTH)-len(msg)-3):
sys.stdout.write(' ')
# Go back to the center of the bar.
for i in range(term_width-int(TOTAL_BAR_LENGTH/2)+2):
sys.stdout.write('\b')
sys.stdout.write(' %d/%d ' % (current+1, total))
if current < total-1:
sys.stdout.write('\r')
else:
sys.stdout.write('\n')
sys.stdout.flush()
def format_time(seconds):
days = int(seconds / 3600/24)
seconds = seconds - days*3600*24
hours = int(seconds / 3600)
seconds = seconds - hours*3600
minutes = int(seconds / 60)
seconds = seconds - minutes*60
secondsf = int(seconds)
seconds = seconds - secondsf
millis = int(seconds*1000)
f = ''
i = 1
if days > 0:
f += str(days) + 'D'
i += 1
if hours > 0 and i <= 2:
f += str(hours) + 'h'
i += 1
if minutes > 0 and i <= 2:
f += str(minutes) + 'm'
i += 1
if secondsf > 0 and i <= 2:
f += str(secondsf) + 's'
i += 1
if millis > 0 and i <= 2:
f += str(millis) + 'ms'
i += 1
if f == '':
f = '0ms'
return f
import torch
import torchvision
import numpy as np
from PIL import Image
class ImageDataset(torch.utils.data.Dataset):
def __init__(self, data, target, transform):
'''data format: np.ndarray, float32 range from 0 to 1, H x W x C'''
self.data = data
self.target = target
self.transform = transform
def __len__(self):
return len(self.target)
def __getitem__(self, index: int):
img, target = self.data[index], self.target[index]
img = np.uint8(img * 255)
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
return img, target
class Perturbed_Dataset(torch.utils.data.Dataset):
def __init__(self, data, perturbation, target, transform, pert=1) -> None:
super().__init__()
'''Clean Examples if 'pert' is False'''
'''data format: np.ndarray, float32 range from 0 to 1, H x W x C'''
self.data = data
self.perturbation = perturbation
self.target = target
self.transform = transform
self.pert = pert
'''Perturbation mode: S for sample-wise, C for class-wise, U for universal'''
if len(self.perturbation.shape) == 4:
if self.perturbation.shape[0] == len(self.target):
self.mode = 'S'
else:
self.mode = 'C'
else:
self.mode = 'U'
def __len__(self):
return len(self.target)
def __getitem__(self, index:int):
if self.pert == 1:
if self.mode == 'S':
img_p, target = self.data[index] + self.perturbation[index], self.target[index]
elif self.mode == 'C':
img_p, target = self.data[index] + self.perturbation[self.target[index]], self.target[index]
else:
img_p, target = self.data[index] + self.perturbation, self.target[index]
elif self.pert == 2:
img_p, target = self.perturbation[index], self.target[index]
else:
img_p, target = self.data[index], self.target[index]
img_p = np.clip(img_p, 0, 1)
img_p = np.uint8(img_p * 255)
img_p = Image.fromarray(img_p)
if self.transform is not None:
img_p = self.transform(img_p)
return img_p, target
def net_param_diff_norm(model:torch.nn.Module, state_dict_init, p='fro'):
diff_norm_list = []
for name, parameter in model.named_parameters():
diff_norm_list.append(torch.norm(parameter.data - state_dict_init[name], p=p).cpu().numpy())
diff_norm = np.linalg.norm(np.array(diff_norm_list))
return diff_norm
def load_cifar10_data(path,download=True,transform_train=None,transform_test=None):
'''return torchvision.datasets.CIFAR10'''
traindata = torchvision.datasets.CIFAR10(root=path,train=True,download=download,transform=transform_train)
testdata = torchvision.datasets.CIFAR10(root=path,train=False,download=download,transform=transform_test)
traindata.targets = np.array(traindata.targets)
testdata.targets = np.array(testdata.targets)
return traindata, testdata
def load_cifar100_data(path,download=True,transform_train=None,transform_test=None):
'''return torchvision.datasets.CIFAR10'''
traindata = torchvision.datasets.CIFAR100(root=path,train=True,download=download,transform=transform_train)
testdata = torchvision.datasets.CIFAR100(root=path,train=False,download=download,transform=transform_test)
traindata.targets = np.array(traindata.targets)
testdata.targets = np.array(testdata.targets)
return traindata, testdata
def save_img(imgs:list,save_path:str,nrow=8):
img_save = torchvision.utils.make_grid(torch.cat(imgs,dim=0),nrow,pad_value=1)
img_save = img_save.permute(1,2,0) * 255
img_save = img_save.cpu().numpy()
img_save = Image.fromarray(img_save.astype('uint8'))
img_save.save(save_path)
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.utils import _pair, _quadruple
class MedianPool2d(nn.Module):
""" Median pool (usable as median filter when stride=1) module.
Args:
kernel_size: size of pooling kernel, int or 2-tuple
stride: pool stride, int or 2-tuple
padding: pool padding, int or 4-tuple (l, r, t, b) as in pytorch F.pad
same: override padding and enforce same padding, boolean
"""
def __init__(self, kernel_size=3, stride=1, padding=0, same=False):
super(MedianPool2d, self).__init__()
self.k = _pair(kernel_size)
self.stride = _pair(stride)
self.padding = _quadruple(padding) # convert to l, r, t, b
self.same = same
def _padding(self, x):
if self.same:
ih, iw = x.size()[2:]
if ih % self.stride[0] == 0:
ph = max(self.k[0] - self.stride[0], 0)
else:
ph = max(self.k[0] - (ih % self.stride[0]), 0)
if iw % self.stride[1] == 0:
pw = max(self.k[1] - self.stride[1], 0)
else:
pw = max(self.k[1] - (iw % self.stride[1]), 0)
pl = pw // 2
pr = pw - pl
pt = ph // 2
pb = ph - pt
padding = (pl, pr, pt, pb)
else:
padding = self.padding
return padding
def forward(self, x):
# using existing pytorch functions and tensor ops so that we get autograd,
# would likely be more efficient to implement from scratch at C/Cuda level
x = F.pad(x, self._padding(x), mode='reflect')
x = x.unfold(2, self.k[0], self.stride[0]).unfold(3, self.k[1], self.stride[1])
x = x.contiguous().view(x.size()[:4] + (-1,)).median(dim=-1)[0]
return x
weights = torch.tensor([[[[1,1,1],[1,1,1],[1,1,1]], [[1,1,1],[1,1,1],[1,1,1]], [[1,1,1],[1,1,1],[1,1,1]]]], dtype=torch.float)
mean_conv = nn.Conv2d(3, 3, 3, bias=False, stride=1, padding=1)
with torch.no_grad():
mean_conv.weight = nn.Parameter(weights)