-
Notifications
You must be signed in to change notification settings - Fork 21
/
tools.py
401 lines (349 loc) · 19.4 KB
/
tools.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
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import Dataset
import os
from PIL import Image
from torchvision import transforms, datasets
from torch.utils.data import DataLoader
import random
import numpy as np
from torchvision.utils import save_image
from utils import supervisor
from utils.tools import IMG_Dataset
import config
class AverageMeter:
"""Computes and stores the average and current value"""
def __init__(self, name: str = None, fmt: str = ':f'):
self.name: str = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0.
self.avg = 0.
self.sum = 0.
self.count = 0
def update(self, val: float, n: int = 1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
def to_numpy(x, **kwargs) -> np.ndarray:
if isinstance(x, torch.Tensor):
x = x.detach().cpu().numpy()
return np.array(x, **kwargs)
# Project function
def tanh_func(x: torch.Tensor) -> torch.Tensor:
return (x.tanh() + 1) * 0.5
def generate_dataloader(dataset='cifar10', dataset_path='./data/', batch_size=128, split='train', shuffle=True, drop_last=False, data_transform=None):
if dataset == 'cifar10':
if data_transform is None:
data_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.247, 0.243, 0.261]),
])
dataset_path = os.path.join(dataset_path, 'cifar10')
if split == 'train':
train_data = datasets.CIFAR10(root=dataset_path, train=True, download=False, transform=data_transform)
train_data_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=4, pin_memory=True)
return train_data_loader
elif split == 'std_test' or split == 'full_test':
test_data = datasets.CIFAR10(root=dataset_path, train=False, download=False, transform=data_transform)
test_data_loader = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=4, pin_memory=True)
return test_data_loader
elif split == 'valid' or split == 'val':
val_set_dir = os.path.join('clean_set', 'cifar10', 'clean_split')
val_set_img_dir = os.path.join(val_set_dir, 'data')
val_set_label_path = os.path.join(val_set_dir, 'clean_labels')
val_set = IMG_Dataset(data_dir=val_set_img_dir, label_path=val_set_label_path, transforms=data_transform)
val_loader = torch.utils.data.DataLoader(val_set, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=4, pin_memory=True)
return val_loader
elif split == 'test':
test_set_dir = os.path.join('clean_set', 'cifar10', 'test_split')
test_set_img_dir = os.path.join(test_set_dir, 'data')
test_set_label_path = os.path.join(test_set_dir, 'labels')
test_set = IMG_Dataset(data_dir=test_set_img_dir, label_path=test_set_label_path, transforms=data_transform)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=True, drop_last=drop_last, num_workers=4, pin_memory=True)
return test_loader
elif dataset == 'gtsrb':
if data_transform is None:
data_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.3337, 0.3064, 0.3171], [0.2672, 0.2564, 0.2629]),
])
dataset_path = os.path.join(dataset_path, 'gtsrb')
if split == 'train':
train_data = datasets.GTSRB(root=dataset_path, split='train', download=False, transform=data_transform)
train_data_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=4, pin_memory=True)
return train_data_loader
elif split == 'std_test' or split == 'full_test':
test_data = datasets.GTSRB(root=dataset_path, split='test', download=False, transform=data_transform)
test_data_loader = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=4, pin_memory=True)
return test_data_loader
elif split == 'valid' or split == 'val':
val_set_dir = os.path.join('clean_set', 'gtsrb', 'clean_split')
val_set_img_dir = os.path.join(val_set_dir, 'data')
val_set_label_path = os.path.join(val_set_dir, 'clean_labels')
val_set = IMG_Dataset(data_dir=val_set_img_dir, label_path=val_set_label_path, transforms=data_transform)
val_loader = torch.utils.data.DataLoader(val_set, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=4, pin_memory=True)
return val_loader
elif split == 'test':
test_set_dir = os.path.join('clean_set', 'gtsrb', 'test_split')
test_set_img_dir = os.path.join(test_set_dir, 'data')
test_set_label_path = os.path.join(test_set_dir, 'labels')
test_set = IMG_Dataset(data_dir=test_set_img_dir, label_path=test_set_label_path, transforms=data_transform)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=True, drop_last=drop_last, num_workers=4, pin_memory=True)
return test_loader
elif dataset == 'imagenette':
if data_transform is None:
data_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
dataset_path = os.path.join(dataset_path, 'imagenette2')
if split == 'train':
train_data = datasets.ImageFolder(os.path.join(os.path.join(data_dir, 'imagenette2'), 'train'), data_transform)
train_data_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=4, pin_memory=True)
return train_data_loader
elif split == 'std_test' or split == 'full_test':
test_data = datasets.ImageFolder(os.path.join(os.path.join(data_dir, 'imagenette2'), 'val'), data_transform)
test_data_loader = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=4, pin_memory=True)
return test_data_loader
elif split == 'valid' or split == 'val':
val_set_dir = os.path.join('clean_set', 'imagenette', 'clean_split')
val_set_img_dir = os.path.join(val_set_dir, 'data')
val_set_label_path = os.path.join(val_set_dir, 'clean_labels')
val_set = IMG_Dataset(data_dir=val_set_img_dir, label_path=val_set_label_path, transforms=data_transform)
val_loader = torch.utils.data.DataLoader(val_set, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=4, pin_memory=True)
return val_loader
elif split == 'test':
test_set_dir = os.path.join('clean_set', 'imagenette', 'test_split')
test_set_img_dir = os.path.join(test_set_dir, 'data')
test_set_label_path = os.path.join(test_set_dir, 'labels')
test_set = IMG_Dataset(data_dir=test_set_img_dir, label_path=test_set_label_path, transforms=data_transform)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=True, drop_last=drop_last, num_workers=4, pin_memory=True)
return test_loader
else:
print('<To Be Implemented> Dataset = %s' % dataset)
exit(0)
def unpack_poisoned_train_set(args, batch_size=128, shuffle=False, data_transform=None):
"""
Return with `poison_set_dir`, `poisoned_set_loader`, `poison_indices`, and `cover_indices` if available
"""
if data_transform is None:
data_transform_aug, data_transform, trigger_transform, normalizer, denormalizer = supervisor.get_transforms(args)
poison_set_dir = supervisor.get_poison_set_dir(args)
if os.path.exists(os.path.join(poison_set_dir, 'data')): # if old version
poisoned_set_img_dir = os.path.join(poison_set_dir, 'data')
if os.path.exists(os.path.join(poison_set_dir, 'imgs')): # if new version
poisoned_set_img_dir = os.path.join(poison_set_dir, 'imgs')
poisoned_set_label_path = os.path.join(poison_set_dir, 'labels')
poison_indices_path = os.path.join(poison_set_dir, 'poison_indices')
cover_indices_path = os.path.join(poison_set_dir, 'cover_indices') # for adaptive attacks
poisoned_set = IMG_Dataset(data_dir=poisoned_set_img_dir,
label_path=poisoned_set_label_path, transforms=data_transform)
poisoned_set_loader = torch.utils.data.DataLoader(poisoned_set, batch_size=batch_size, shuffle=shuffle, num_workers=4, pin_memory=True)
poison_indices = torch.load(poison_indices_path)
if ('adaptive' in args.poison_type) or args.poison_type == 'TaCT':
cover_indices = torch.load(cover_indices_path)
return poison_set_dir, poisoned_set_loader, poison_indices, cover_indices
return poison_set_dir, poisoned_set_loader, poison_indices, []
def jaccard_idx(mask: torch.Tensor, real_mask: torch.Tensor, select_num: int = 9) -> float:
if select_num <= 0: return 0
mask = mask.to(dtype=torch.float)
real_mask = real_mask.to(dtype=torch.float)
detect_mask = mask > mask.flatten().topk(select_num)[0][-1]
sum_temp = detect_mask.int() + real_mask.int()
overlap = (sum_temp == 2).sum().float() / (sum_temp >= 1).sum().float()
return float(overlap)
def normalize_mad(values: torch.Tensor, side: str = None) -> torch.Tensor:
if not isinstance(values, torch.Tensor):
values = torch.tensor(values, dtype=torch.float)
median = values.median()
abs_dev = (values - median).abs()
mad = abs_dev.median()
measures = abs_dev / mad / 1.4826
if side == 'double': # TODO: use a loop to optimie code
dev_list = []
for i in range(len(values)):
if values[i] <= median:
dev_list.append(float(median - values[i]))
mad = torch.tensor(dev_list).median()
for i in range(len(values)):
if values[i] <= median:
measures[i] = abs_dev[i] / mad / 1.4826
dev_list = []
for i in range(len(values)):
if values[i] >= median:
dev_list.append(float(values[i] - median))
mad = torch.tensor(dev_list).median()
for i in range(len(values)):
if values[i] >= median:
measures[i] = abs_dev[i] / mad / 1.4826
return measures
def to_list(x) -> list:
if isinstance(x, (torch.Tensor, np.ndarray)):
return x.tolist()
return list(x)
def val_atk(args, model, split='test', batch_size=100):
"""
Validate the attack (described in `args`) on `model`
"""
model.eval()
data_transform_aug, data_transform, trigger_transform, normalizer, denormalizer = supervisor.get_transforms(args)
poison_transform = supervisor.get_poison_transform(poison_type=args.poison_type, dataset_name=args.dataset,
target_class=config.target_class[args.dataset],
trigger_transform=data_transform,
is_normalized_input=(not args.no_normalize),
alpha=args.alpha if args.test_alpha is None else args.test_alpha,
trigger_name=args.trigger, args=args)
test_loader = generate_dataloader(dataset=args.dataset, dataset_path=config.data_dir, batch_size=batch_size, split=split, shuffle=False, drop_last=False, data_transform=data_transform)
if args.poison_type == 'none':
num = 0
num_non_target = 0
num_clean_correct = 0
acr = 0 # attack correct rate
with torch.no_grad():
for batch_idx, (data, label) in enumerate(test_loader):
data, label = data.cuda(), label.cuda() # train set batch
output = model(data)
pred = output.argmax(dim=1) # get the index of the max log-probability
num_clean_correct += pred.eq(label).sum().item()
num += len(label)
clean_acc = num_clean_correct / num
print('Accuracy: %d/%d = %f' % (num_clean_correct, num, clean_acc))
return clean_acc, 0, clean_acc
if args.poison_type == 'TaCT':
num = 0
num_source = 0
num_non_source = 0
num_clean_correct = 0
num_poison_eq_clean_label = 0
num_poison_eq_poison_label_source = 0
num_poison_eq_poison_label_non_source = 0
acr = 0 # attack correct rate
with torch.no_grad():
for batch_idx, (data, label) in enumerate(test_loader):
data, label = data.cuda(), label.cuda() # train set batch
output = model(data)
pred = output.argmax(dim=1) # get the index of the max log-probability
num_clean_correct += pred.eq(label).sum().item()
num += len(label)
# filter out target inputs (FIXME: target now fixed to 0)
data = data[label != 0]
label = label[label != 0]
# source inputs (FIXME: source now fixed to 1)
source_data = data[label == 1]
source_label = label[label == 1]
# non-source inputs
non_source_data = data[label != 1]
non_source_label = label[label != 1]
num_source += len(source_label)
num_non_source += len(non_source_label)
# poison!
if len(source_label) > 0: poison_source_data, poison_source_label = poison_transform.transform(source_data, source_label)
if len(non_source_label) > 0: poison_non_source_data, poison_non_source_label = poison_transform.transform(non_source_data, non_source_label)
# forward
if len(source_label) > 0: poison_source_output = model(poison_source_data)
if len(non_source_label) > 0: poison_non_source_output = model(poison_non_source_data)
if len(source_label) > 0: poison_source_pred = poison_source_output.argmax(dim=1) # get the index of the max log-probability
if len(non_source_label) > 0: poison_non_source_pred = poison_non_source_output.argmax(dim=1) # get the index of the max log-probability
for bid in range(len(source_label)):
if poison_source_pred[bid] == poison_source_label[bid]:
num_poison_eq_poison_label_source+=1
if poison_source_pred[bid] == source_label[bid]:
num_poison_eq_clean_label+=1
for bid in range(len(non_source_label)):
if poison_non_source_pred[bid] == poison_non_source_label[bid]:
num_poison_eq_poison_label_non_source+=1
if poison_non_source_pred[bid] == non_source_label[bid]:
num_poison_eq_clean_label+=1
clean_acc = num_clean_correct / num
asr_source = num_poison_eq_poison_label_source/num_source
asr_non_source = num_poison_eq_poison_label_non_source/num_non_source
acr = num_poison_eq_clean_label / len(test_loader.dataset)
print('Accuracy : %d/%d = %f' % (num_clean_correct, num, clean_acc))
print('ASR (source) : %d/%d = %f' % (num_poison_eq_poison_label_source, num_source, asr_source))
print('ASR (non-source) : %d/%d = %f' % (num_poison_eq_poison_label_non_source, num_non_source, asr_non_source))
print('ACR (Attack Correct Rate) : %d/%d = %f' % (num_poison_eq_clean_label, len(test_loader.dataset), acr))
return clean_acc, asr_source, asr_non_source, acr
else:
num = 0
num_non_target = 0
num_clean_correct = 0
num_poison_eq_poison_label = 0
num_poison_eq_clean_label = 0
acr = 0 # attack correct rate
with torch.no_grad():
for batch_idx, (data, label) in enumerate(test_loader):
data, label = data.cuda(), label.cuda() # train set batch
output = model(data)
pred = output.argmax(dim=1) # get the index of the max log-probability
num_clean_correct += pred.eq(label).sum().item()
num += len(label)
data, poison_label = poison_transform.transform(data, label)
poison_output = model(data)
poison_pred = poison_output.argmax(dim=1) # get the index of the max log-probability
this_batch_size = len(poison_label)
for bid in range(this_batch_size):
if label[bid] != poison_label[bid]: # samples of non-target classes
num_non_target += 1
if poison_pred[bid] == poison_label[bid]:
num_poison_eq_poison_label+=1
if poison_pred[bid] == label[bid]:
num_poison_eq_clean_label+=1
else:
if poison_pred[bid] == label[bid]:
num_poison_eq_clean_label+=1
clean_acc = num_clean_correct / num
asr = num_poison_eq_poison_label / num_non_target
acr = num_poison_eq_clean_label / len(test_loader.dataset)
print('Accuracy: %d/%d = %f' % (num_clean_correct, num, clean_acc))
print('ASR: %d/%d = %f' % (num_poison_eq_poison_label, num_non_target, asr))
print('ACR (Attack Correct Rate): %d/%d = %f' % (num_poison_eq_clean_label, len(test_loader.dataset), acr))
return clean_acc, asr, acr
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.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_(100.0 / batch_size))
return res
class Cutout(object):
"""Randomly mask out one or more patches from an image.
Args:
n_holes (int): Number of patches to cut out of each image.
length (int): The length (in pixels) of each square patch.
"""
def __init__(self, n_holes, length):
self.n_holes = n_holes
self.length = length
def __call__(self, img):
"""
Args:
img (Tensor): Tensor image of size (C, H, W).
Returns:
Tensor: Image with n_holes of dimension length x length cut out of it.
"""
h = img.size(1)
w = img.size(2)
mask = torch.ones((h, w))
for n in range(self.n_holes):
y = torch.randint(high=h, size=(1, 1))
x = torch.randint(high=w, size=(1, 1))
y1 = torch.clip(y - self.length // 2, 0, h)
y2 = torch.clip(y + self.length // 2, 0, h)
x1 = torch.clip(x - self.length // 2, 0, w)
x2 = torch.clip(x + self.length // 2, 0, w)
mask[y1: y2, x1: x2] = 0.
mask = mask.expand_as(img)
img = img * mask
return img