-
Notifications
You must be signed in to change notification settings - Fork 2
/
poison.py
322 lines (240 loc) · 11.8 KB
/
poison.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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torchvision import datasets
from torchvision.utils import make_grid
import argparse
import os
from tqdm import tqdm
from pprint import pprint
from utils import set_seed, CIFAR10Poisoned, AverageMeter, accuracy_top1, transform_test, make_and_restore_model
from attacks.step import LinfStep, L2Step
from utils import show_image_row
STEPS = {
'Linf': LinfStep,
'L2': L2Step,
}
def batch_poison(model, x, target, args, adv_or_hyp):
orig_x = x.clone().detach()
step = STEPS[args.constraint](orig_x, args.eps, args.step_size)
if adv_or_hyp == 'adv':
target = (target + 1) % 10 # Using a fixed permutation of labels
elif adv_or_hyp == 'hyp':
target = target # Maximize accuracy
for _ in range(args.num_steps):
x = x.clone().detach().requires_grad_(True)
logits = model(x)
loss = nn.CrossEntropyLoss()(logits, target)
grad = torch.autograd.grad(loss, [x])[0]
with torch.no_grad():
x = step.step(x, grad)
x = step.project(x)
x = torch.clamp(x, 0, 1)
return x.clone().detach().requires_grad_(False)
def poison_p1p2(args, loader, model, writer, adv_or_hyp):
poisoned_input = []
clean_target = []
loss_logger = AverageMeter()
acc_logger = AverageMeter()
iterator = tqdm(enumerate(loader), total=len(loader))
for i, (inp, target) in iterator:
inp = inp.cuda(non_blocking=True)
target = target.cuda(non_blocking=True)
inp_p = batch_poison(model, inp, target, args, adv_or_hyp)
poisoned_input.append(inp_p.detach().cpu())
clean_target.append(target.detach().cpu())
with torch.no_grad():
logits = model(inp_p)
loss = nn.CrossEntropyLoss()(logits, target)
acc = accuracy_top1(logits, target)
loss_logger.update(loss.item(), inp.size(0))
acc_logger.update(acc, inp.size(0))
desc = ('[{} {}] | Loss {:.4f} | Accuracy {:.3f} ||'
.format(args.poison_type, args.eps, loss_logger.avg, acc_logger.avg))
iterator.set_description(desc)
poisoned_input = torch.cat(poisoned_input, dim=0)
clean_target = torch.cat(clean_target, dim=0)
return poisoned_input, clean_target
def poison_p1(args, loader, model, writer):
poisoned_data = poison_p1p2(args, loader, model, writer, adv_or_hyp='adv')
return poisoned_data
def poison_p2(args, loader, model, writer):
poisoned_data = poison_p1p2(args, loader, model, writer, adv_or_hyp='hyp')
return poisoned_data
def poison_p3p4p5(args, loader, model, writer, poisons):
poisoned_input = []
clean_target = []
loss_logger = AverageMeter()
acc_logger = AverageMeter()
iterator = tqdm(enumerate(loader), total=len(loader))
for i, (inp, target) in iterator:
inp = inp.cuda(non_blocking=True)
target = target.cuda(non_blocking=True)
# Add the same perturbation to examples from the same class
index = target.unsqueeze(dim=1).unsqueeze(dim=2).unsqueeze(dim=3).expand(-1, 3, 32, 32)
delta = torch.gather(poisons, dim=0, index=index)
inp_p = inp + delta
inp_p = torch.clamp(inp_p, 0, 1)
poisoned_input.append(inp_p.detach().cpu())
clean_target.append(target.detach().cpu())
with torch.no_grad():
logits = model(inp_p)
loss = nn.CrossEntropyLoss()(logits, target)
acc = accuracy_top1(logits, target)
loss_logger.update(loss.item(), inp.size(0))
acc_logger.update(acc, inp.size(0))
desc = ('[{} {}] | Loss {:.4f} | Accuracy {:.3f} ||'
.format(args.poison_type, args.eps, loss_logger.avg, acc_logger.avg))
iterator.set_description(desc)
poisoned_input = torch.cat(poisoned_input, dim=0)
clean_target = torch.cat(clean_target, dim=0)
return poisoned_input, clean_target
def universal_target_attack(model, loader, target_class, writer, args):
delta = torch.zeros(1, *args.data_shape).cuda(non_blocking=True)
orig_delta = delta.clone().detach()
step = STEPS[args.constraint](orig_delta, args.eps, args.step_size)
tag = 'universal_perturbation/{}-{}'.format(target_class, loader.dataset.classes[target_class])
vis = make_grid(delta, nrow=1, normalize=True)
writer.add_image(tag, vis, global_step=0)
data_loader = DataLoader(loader.dataset, batch_size=args.batch_size, shuffle=True)
data_iter = iter(data_loader)
iterator = tqdm(range(args.num_steps * 5), total=args.num_steps * 5)
for i in iterator:
try:
inp, target = next(data_iter)
except StopIteration:
data_iter = iter(data_loader)
inp, target = next(data_iter)
inp = inp.cuda(non_blocking=True)
target = target.cuda(non_blocking=True)
target_ori = target.clone()
target.fill_(target_class)
delta = delta.clone().detach().requires_grad_(True)
inp_adv = inp + delta
inp_adv = torch.clamp(inp_adv, 0, 1)
logits = model(inp_adv)
loss = nn.CrossEntropyLoss()(logits, target)
grad = torch.autograd.grad(loss, [delta])[0]
with torch.no_grad():
delta = step.step(delta, grad)
delta = step.project(delta)
acc = accuracy_top1(logits, target_ori)
if writer is not None and i % 10 == 0:
# Visualization
tag = 'universal_perturbation/{}-{}'.format(target_class, loader.dataset.classes[target_class])
vis = make_grid(delta, nrow=1, normalize=True)
writer.add_image(tag, vis, global_step=i+1)
desc = ('[ Target class {}] | Loss {:.4f} | Accuracy {:.3f} ||'
.format(target_class, loss.item(), acc))
iterator.set_description(desc)
return delta.clone().detach().requires_grad_(False)
def poison_p3(args, loader, model, writer):
# Generate universal perturbations for each class
poisons = []
for i in range(args.num_classes):
poison = universal_target_attack(model, loader, i, writer, args)
poisons.append(poison.squeeze())
poisons = torch.stack(poisons)
poisons = poisons[list(range(1, 10))+[0]] # using a fixed permutation of labels
vis = make_grid(poisons, nrow=5, normalize=True, scale_each=True)
writer.add_image('universal_perturbation', vis, global_step=1)
poisoned_data = poison_p3p4p5(args, loader, model, writer, poisons)
return poisoned_data
def poison_p4(args, loader, model, writer):
# Generate universal perturbations for each class
poisons = []
for i in range(args.num_classes):
poison = universal_target_attack(model, loader, i, writer, args)
poisons.append(poison.squeeze())
poisons = torch.stack(poisons)
vis = make_grid(poisons, nrow=5, normalize=True, scale_each=True)
writer.add_image('universal_perturbation', vis, global_step=0)
poisoned_data = poison_p3p4p5(args, loader, model, writer, poisons)
return poisoned_data
def poison_p5(args, loader, model, writer):
# Generate random perturbations for each class
poisons = torch.zeros(args.num_classes, *args.data_shape).cuda(non_blocking=True)
step = STEPS[args.constraint](None, args.eps, None)
poisons = step.random_perturb(poisons)
vis = make_grid(poisons, nrow=5, normalize=True, scale_each=True)
writer.add_image('random_perturbation', vis, global_step=0, dataformats='CHW')
poisoned_data = poison_p3p4p5(args, loader, model, writer, poisons)
return poisoned_data
def poisoning(args, loader, model, writer):
set_seed(args.seed)
if args.poison_type == 'P1':
poisoned_data = poison_p1(args, loader, model, writer)
elif args.poison_type == 'P2':
poisoned_data = poison_p2(args, loader, model, writer)
elif args.poison_type == 'P3':
poisoned_data = poison_p3(args, loader, model, writer)
elif args.poison_type == 'P4':
poisoned_data = poison_p4(args, loader, model, writer)
elif args.poison_type == 'P5':
poisoned_data = poison_p5(args, loader, model, writer)
torch.save(poisoned_data, args.poison_file_path)
def visualization(args, writer):
clean_set = datasets.CIFAR10(args.clean_data_path, train=True, transform=transform_test)
poison_set = CIFAR10Poisoned(args.poison_data_path, args.constraint, args.poison_type, transform=transform_test)
clean_loader = DataLoader(clean_set, batch_size=5, shuffle=False, num_workers=8)
poison_loader = DataLoader(poison_set, batch_size=5, shuffle=False, num_workers=8)
clean_iterator = iter(clean_loader)
poison_iterator = iter(poison_loader)
for i in range(3):
clean_inp, label = next(clean_iterator)
poison_inp, label = next(poison_iterator)
imgs = torch.cat([clean_inp, poison_inp], dim=0)
vis = make_grid(imgs, nrow=5, normalize=False, scale_each=False)
writer.add_image('poisoned_examples', vis, global_step=i, dataformats='CHW')
ylist = None
# ylist = ['$\mathcal{D}$', '$\widehat{\mathcal{D}}_{\mathsf{P5}}$']
# clean_set.classes[1] = 'car'
show_image_row([clean_inp, poison_inp],
ylist=ylist,
tlist=[[clean_set.classes[int(t)] for t in l] for l in [label, label]],
fontsize=20,
filename=os.path.join(os.path.join(args.out_dir, args.exp_name), 'poisoned_examples_{}.png'.format(i)))
# def main(args):
# visualization(args, None)
def main(args):
if os.path.isfile(args.poison_file_path):
print('Poison [{}] already exists.'.format(args.poison_file_path))
return
data_set = datasets.CIFAR10(args.clean_data_path, train=True, download=True, transform=transform_test)
data_loader = DataLoader(data_set, batch_size=args.batch_size, shuffle=False)
model = make_and_restore_model(args.arch, resume_path=args.model_path)
model.eval()
writer = SummaryWriter(args.tensorboard_path)
poisoning(args, data_loader, model, writer)
visualization(args, writer)
if __name__ == "__main__":
parser = argparse.ArgumentParser('Generate poisoned dataset for CIFAR10')
parser.add_argument('--seed', default=0, type=int)
parser.add_argument('--eps', default=0.5, type=float)
parser.add_argument('--constraint', default='L2', choices=['Linf', 'L2'], type=str)
parser.add_argument('--arch', default='VGG16', type=str, choices=['VGG16', ])
parser.add_argument('--model_path', default='results/VGG16-STonC-lr0.1-bs128-wd0.0005-seed0/checkpoint.pth', type=str)
parser.add_argument('--dataset', default='cifar10', type=str)
parser.add_argument('--out_dir', default='results/', type=str)
parser.add_argument('--clean_data_path', default='./datasets/CIFAR10', type=str)
parser.add_argument('--poison_data_path', default='./datasets/CIFAR10Poison', type=str)
parser.add_argument('--poison_type', default='C', choices=['P1', 'P2', 'P3', 'P4', 'P5'])
parser.add_argument('--gpuid', default=0, type=int)
args = parser.parse_args()
args.exp_name = '{}-{}-{}-eps{:.5f}'.format(args.arch, args.poison_type, args.constraint, args.eps)
args.tensorboard_path = os.path.join(os.path.join(args.out_dir, args.exp_name), 'tensorboard')
args.batch_size = 256
args.num_classes = 10
args.data_shape = (3, 32, 32)
args.num_steps = 100
args.step_size = args.eps / 5
args.poison_data_path = os.path.expanduser(args.poison_data_path)
if not os.path.exists(args.poison_data_path):
os.makedirs(args.poison_data_path)
args.poison_file_path = os.path.join(args.poison_data_path, '{}.{}'.format(args.constraint, args.poison_type.lower()))
pprint(vars(args))
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpuid)
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = True
main(args)