-
Notifications
You must be signed in to change notification settings - Fork 19
/
tools.py
352 lines (312 loc) · 18.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
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
import torchvision.transforms.functional as Ft
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, noisy_test=False):
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 noisy_test:
from torch.utils.data import ConcatDataset
noisy_test_set_dir = os.path.join('clean_set', 'cifar10', 'noisy_test_split')
noisy_test_set_img_dir = os.path.join(noisy_test_set_dir, 'data')
noisy_test_set_label_path = os.path.join(noisy_test_set_dir, 'labels')
noisy_test_set = IMG_Dataset(data_dir=noisy_test_set_img_dir, label_path=noisy_test_set_label_path, transforms=data_transform)
noisy_test_loader = torch.utils.data.DataLoader(noisy_test_set, batch_size=batch_size, shuffle=True, drop_last=drop_last, num_workers=4, pin_memory=True)
return noisy_test_loader
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.Resize((32, 32)),
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 noisy_test:
from torch.utils.data import ConcatDataset
noisy_test_set_dir = os.path.join('clean_set', 'gtsrb', 'noisy_test_split')
noisy_test_set_img_dir = os.path.join(noisy_test_set_dir, 'data')
noisy_test_set_label_path = os.path.join(noisy_test_set_dir, 'labels')
noisy_test_set = IMG_Dataset(data_dir=noisy_test_set_img_dir, label_path=noisy_test_set_label_path, transforms=data_transform)
noisy_test_loader = torch.utils.data.DataLoader(noisy_test_set, batch_size=batch_size, shuffle=True, drop_last=drop_last, num_workers=4, pin_memory=True)
return noisy_test_loader
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(dataset_path, 'imagenette2'), 'train'), data_transform)
train_data_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=32, 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(dataset_path, 'imagenette2'), 'val'), data_transform)
test_data_loader = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=32, 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=32, 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=32, pin_memory=True)
return test_loader
elif dataset == 'imagenet':
from utils import imagenet
if data_transform is None:
data_transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
transforms.CenterCrop(224),
])
dataset_path = config.imagenet_dir
train_set_dir = os.path.join(config.imagenet_dir, 'train')
test_set_dir = os.path.join(config.imagenet_dir, 'val')
if split == 'train':
train_data = imagenet.imagenet_dataset(directory=train_set_dir, data_transform=data_transform, poison_directory=None,
poison_indices=None, target_class=config.target_class['imagenet'], num_classes=1000)
train_data_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=32, pin_memory=True)
return train_data_loader
elif split == 'std_test' or split == 'full_test':
test_data = imagenet.imagenet_dataset(directory=test_set_dir, shift=False, data_transform=data_transform,
label_file=imagenet.test_set_labels, num_classes=1000)
train_data_loader = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=32, pin_memory=True)
return test_data_loader
elif split == 'valid' or split == 'val':
test_data = imagenet.imagenet_dataset(directory=test_set_dir, shift=False, data_transform=data_transform,
label_file=imagenet.test_set_labels, num_classes=1000)
val_split_meta_dir = os.path.join('clean_set', 'imagenet', 'clean_split')
val_split_indices = torch.load(os.path.join(val_split_meta_dir, 'clean_split_indices'))
val_set = torch.utils.data.Subset(test_data, val_split_indices)
val_loader = torch.utils.data.DataLoader(val_set, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=32, pin_memory=True)
return val_loader
elif split == 'test':
test_data = imagenet.imagenet_dataset(directory=test_set_dir, shift=False, data_transform=data_transform,
label_file=imagenet.test_set_labels, num_classes=1000)
test_split_meta_dir = os.path.join('clean_set', 'imagenet', 'test_split')
test_indices = torch.load(os.path.join(test_split_meta_dir, 'test_indices'))
test_data = torch.utils.data.Subset(test_data, test_indices)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=32, 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=32, 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=trigger_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.dataset == 'cifar10': num_classes = 10
elif args.dataset == 'gtsrb': num_classes = 43
elif args.dataset == 'imagenet': num_classes = 1000
else: num_classes = 10
if args.poison_type == 'TaCT' or args.poison_type == 'SleeperAgent':
source_classes = [config.source_class]
else:
source_classes = None
from utils.tools import test
ca, asr = test(model=model, test_loader=test_loader, poison_test=True, poison_transform=poison_transform, num_classes=num_classes, source_classes=source_classes, all_to_all=('all_to_all' in args.poison_type))
return ca, asr, 0
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