-
Notifications
You must be signed in to change notification settings - Fork 0
/
advertorch_attacks.py
371 lines (297 loc) · 14.7 KB
/
advertorch_attacks.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
import argparse
import sys
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision.utils import save_image
from resnet import build_resnet_32x32
from sdim import SDIM
import numpy as np
from advertorch.attacks import LinfPGDAttack, CarliniWagnerL2Attack, GradientSignAttack, JacobianSaliencyMapAttack
from utils import get_dataset, cal_parameters
def attack_run(model, adversary, hps):
model.eval()
dataset = get_dataset(data_name=hps.problem, train=False)
# hps.n_batch_test = 1
test_loader = DataLoader(dataset=dataset, batch_size=hps.n_batch_test, shuffle=False)
test_clnloss = 0
clncorrect = 0
test_advloss = 0
advcorrect = 0
attack_path = os.path.join(hps.attack_dir, hps.attack)
if not os.path.exists(attack_path):
os.mkdir(attack_path)
for batch_id, (clndata, target) in enumerate(test_loader):
# Note that images are scaled to [-1.0, 1.0]
clndata, target = clndata.to(hps.device), target.to(hps.device)
path = os.path.join(attack_path, 'original_{}.png'.format(batch_id))
save_image(clndata, path, normalize=True)
with torch.no_grad():
output = model(clndata)
print('original logits ', output.detach().cpu().numpy())
test_clnloss += F.cross_entropy(
output, target, reduction='sum').item()
pred = output.max(1, keepdim=True)[1]
#print('pred: ', pred)
clncorrect += pred.eq(target.view_as(pred)).sum().item()
advdata = adversary.perturb(clndata, target)
path = os.path.join(attack_path, '{}perturbed_{}.png'.format(prefix, batch_id))
save_image(advdata, path, normalize=True)
with torch.no_grad():
output = model(advdata)
print('adv logits ', output.detach().cpu().numpy())
test_advloss += F.cross_entropy(
output, target, reduction='sum').item()
pred = output.max(1, keepdim=True)[1]
#print('pred: ', pred)
advcorrect += pred.eq(target.view_as(pred)).sum().item()
#if batch_id == 2:
# exit(0)
break
test_clnloss /= len(test_loader.dataset)
print('Test set: avg cln loss: {:.4f},'
' cln acc: {}/{}'.format(
test_clnloss, clncorrect, len(test_loader.dataset)))
test_advloss /= len(test_loader.dataset)
print('Test set: avg adv loss: {:.4f},'
' adv acc: {}/{}'.format(
test_advloss, advcorrect, len(test_loader.dataset)))
cln_acc = clncorrect / len(test_loader.dataset)
adv_acc = advcorrect / len(test_loader.dataset)
return cln_acc, adv_acc
def attack_run_rejection_policy(model, adversary, hps):
"""
An attack run with rejection policy.
:param model: Pytorch model.
:param adversary: Advertorch adversary.
:param hps: hyperparameters
:return:
"""
model.eval()
# Get thresholds
threshold_list1 = []
threshold_list2 = []
for label_id in range(hps.n_classes):
# No data augmentation(crop_flip=False) when getting in-distribution thresholds
dataset = get_dataset(data_name=hps.problem, train=True, label_id=label_id, crop_flip=False)
in_test_loader = DataLoader(dataset=dataset, batch_size=hps.n_batch_test, shuffle=False)
print('Inference on {}, label_id {}'.format(hps.problem, label_id))
in_ll_list = []
for batch_id, (x, y) in enumerate(in_test_loader):
x = x.to(hps.device)
y = y.to(hps.device)
ll = model(x)
correct_idx = ll.argmax(dim=1) == y
ll_, y_ = ll[correct_idx], y[correct_idx] # choose samples are classified correctly
in_ll_list += list(ll_[:, label_id].detach().cpu().numpy())
thresh_idx = int(0.01 * len(in_ll_list))
thresh1 = sorted(in_ll_list)[thresh_idx]
thresh_idx = int(0.02 * len(in_ll_list))
thresh2 = sorted(in_ll_list)[thresh_idx]
threshold_list1.append(thresh1) # class mean as threshold
threshold_list2.append(thresh2) # class mean as threshold
print('1st & 2nd percentile thresholds: {:.3f}, {:.3f}'.format(thresh1, thresh2))
# Evaluation
dataset = get_dataset(data_name=hps.problem, train=False)
# hps.n_batch_test = 1
test_loader = DataLoader(dataset=dataset, batch_size=hps.n_batch_test, shuffle=False)
n_correct = 0 # total number of correct classified samples by clean classifier
n_successful_adv = 0 # total number of successful adversarial examples generated
n_rejected_adv1 = 0 # total number of successfully rejected (successful) adversarial examples, <= n_successful_adv
n_rejected_adv2 = 0 # total number of successfully rejected (successful) adversarial examples, <= n_successful_adv
attack_path = os.path.join(hps.attack_dir, hps.attack)
if not os.path.exists(attack_path):
os.mkdir(attack_path)
thresholds1 = torch.tensor(threshold_list1).to(hps.device)
thresholds2 = torch.tensor(threshold_list2).to(hps.device)
l2_distortion_list = []
for batch_id, (x, y) in enumerate(test_loader):
# Note that images are scaled to [0., 1.0]
x, y = x.to(hps.device), y.to(hps.device)
with torch.no_grad():
output = model(x)
pred = output.argmax(dim=1)
correct_idx = pred == y
x, y = x[correct_idx], y[correct_idx] # Only evaluate on the correct classified samples by clean classifier.
n_correct += correct_idx.sum().item()
adv_x = adversary.perturb(x, y)
with torch.no_grad():
output = model(adv_x)
diff = adv_x - x
l2_distortion = diff.norm(p=2, dim=-1).mean().item() # mean l2 distortion
pred = output.argmax(dim=1)
successful_idx = pred != y # idx of successful adversarial examples.
values, pred = output[successful_idx].max(dim=1)
# confidence_idx = values >= thresholds[pred]
reject_idx1 = values < thresholds1[pred] # idx of successfully rejected samples.
reject_idx2 = values < thresholds2[pred] # idx of successfully rejected samples.
# adv_correct += pred[confidence_idx].eq(y[confidence_idx]).sum().item()
n_successful_adv += successful_idx.float().sum().item()
n_rejected_adv1 += reject_idx1.float().sum().item()
n_rejected_adv2 += reject_idx2.float().sum().item()
l2_distortion_list.append(l2_distortion)
if batch_id % 10 == 0:
print('Evaluating on {}-th batch ...'.format(batch_id + 1))
n = len(test_loader.dataset)
reject_rate1 = n_rejected_adv1 / n_successful_adv
reject_rate2 = n_rejected_adv2 / n_successful_adv
success_adv_rate = n_successful_adv / n_correct
print('Test set, clean classification accuracy: {}/{}={:.4f}'.format(n_correct, n, n_correct / n))
print('success rate of adv examples generation: {}/{}={:.4f}'.format(n_successful_adv, n_correct, success_adv_rate))
print('Mean L2 distortion of Adv Examples: {:.4f}'.format(np.mean(l2_distortion_list)))
print('1st percentile, reject success rate: {}/{}={:.4f}'.format(n_rejected_adv1, n_successful_adv, reject_rate1))
print('2nd percentile, reject success rate: {}/{}={:.4f}'.format(n_rejected_adv2, n_successful_adv, reject_rate2))
def fgsm_attack(model, hps):
eps_list = [0., 0.1, 0.2, 0.3, 0.4, 0.5]
print('============== FGSM Summary ===============')
for eps in eps_list:
adversary = GradientSignAttack(
model,
loss_fn=nn.CrossEntropyLoss(reduction="sum"),
eps=eps,
clip_min=-1.,
clip_max=1.,
targeted=hps.targeted
)
print('epsilon = {:.4f}'.format(adversary.eps))
#attack_run(model, adversary, hps)
attack_run_rejection_policy(model, adversary, hps)
print('============== FGSM Summary ===============')
def linfPGD_attack(model, hps):
eps_list = [0., 0.1, 0.2, 0.3, 0.4, 0.5]
#hps.n_batch_test = 5
print('============== LinfPGD Summary ===============')
for eps in eps_list:
adversary = LinfPGDAttack(
model, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps=eps,
nb_iter=40, eps_iter=0.01, rand_init=True, clip_min=-1.0,
clip_max=1.0, targeted=hps.targeted)
print('epsilon = {:.4f}'.format(adversary.eps))
attack_run(model, adversary, hps)
#attack_run_rejection_policy(model, adversary, hps)
print('============== LinfPGD Summary ===============')
def cw_l2_attack(model, hps):
print('============== CW_l2 Summary ===============')
confidence = hps.cw_confidence
adversary = CarliniWagnerL2Attack(model,
num_classes=10,
confidence=confidence,
clip_min=0.,
clip_max=1.,
max_iterations=1000
)
print('confidence = {}'.format(confidence))
attack_run_rejection_policy(model, adversary, hps)
print('============== CW_l2 Summary ===============')
def jsma_attack(model, hps):
print('============== JSMA Summary ===============')
adversary = JacobianSaliencyMapAttack(model,
num_classes=10,
clip_min=0.,
clip_max=1.
)
attack_run_rejection_policy(model, adversary, hps)
print('============== JSMA Summary ===============')
if __name__ == "__main__":
# This enables a ctr-C without triggering errors
import signal
signal.signal(signal.SIGINT, lambda x, y: sys.exit(0))
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", action='store_true', help="Verbose mode")
parser.add_argument("--inference", action="store_true",
help="Used in inference mode")
parser.add_argument("--log_dir", type=str,
default='./logs', help="Location to save logs")
parser.add_argument("--attack_dir", type=str,
default='./attack_logs', help="Location to save logs")
# Dataset hyperparams:
parser.add_argument("--problem", type=str, default='cifar10',
help="Problem (mnist/fashion/cifar10")
parser.add_argument("--n_classes", type=int,
default=10, help="number of classes of dataset.")
parser.add_argument("--data_dir", type=str, default='data',
help="Location of data")
# Optimization hyperparams:
parser.add_argument("--n_batch_train", type=int,
default=128, help="Minibatch size")
parser.add_argument("--n_batch_test", type=int,
default=100, help="Minibatch size")
parser.add_argument("--optimizer", type=str,
default="adam", help="adam or adamax")
parser.add_argument("--lr", type=float, default=0.001,
help="Base learning rate")
parser.add_argument("--beta1", type=float, default=.9, help="Adam beta1")
parser.add_argument("--polyak_epochs", type=float, default=1,
help="Nr of averaging epochs for Polyak and beta2")
parser.add_argument("--weight_decay", type=float, default=1.,
help="Weight decay. Switched off by default.")
parser.add_argument("--epochs", type=int, default=500,
help="Total number of training epochs")
# Model hyperparams:
parser.add_argument("--image_size", type=int,
default=32, help="Image size")
parser.add_argument("--mi_units", type=int,
default=256, help="output size of 1x1 conv network for mutual information estimation")
parser.add_argument("--rep_size", type=int,
default=64, help="size of the global representation from encoder")
parser.add_argument("--encoder_name", type=str, default='resnet25',
help="encoder name: resnet#")
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
# Inference hyperparams:
parser.add_argument("--percentile", type=float, default=0.01,
help="percentile value for inference with rejection.")
parser.add_argument("--cw_confidence", type=float, default=0,
help="confidence for CW attack.")
# Attack parameters
parser.add_argument("--targeted", action="store_true",
help="whether perform targeted attack")
parser.add_argument("--attack", type=str, default='pgdinf',
help="Location of data")
# Ablation
parser.add_argument("--seed", type=int, default=123, help="Random seed")
hps = parser.parse_args() # So error if typo
use_cuda = not hps.no_cuda and torch.cuda.is_available()
torch.manual_seed(hps.seed)
hps.device = torch.device("cuda" if use_cuda else "cpu")
if hps.problem == 'cifar10':
hps.image_channel = 3
elif hps.problem == 'svhn':
hps.image_channel = 3
elif hps.problem == 'mnist':
hps.image_channel = 1
prefix = ''
if hps.encoder_name.startswith('sdim_'):
prefix = 'sdim_'
hps.encoder_name = hps.encoder_name.strip('sdim_')
model = SDIM(rep_size=hps.rep_size,
mi_units=hps.mi_units,
encoder_name=hps.encoder_name,
image_channel=hps.image_channel
).to(hps.device)
checkpoint_path = os.path.join(hps.log_dir, 'sdim_{}_{}_d{}.pth'.format(hps.encoder_name, hps.problem, hps.rep_size))
model.load_state_dict(torch.load(checkpoint_path, map_location=lambda storage, loc: storage))
else:
n_encoder_layers = int(hps.encoder_name.strip('resnet'))
model = build_resnet_32x32(n=n_encoder_layers,
fc_size=hps.n_classes,
image_channel=hps.image_channel
).to(hps.device)
checkpoint_path = os.path.join(hps.log_dir, '{}_{}.pth'.format(hps.encoder_name, hps.problem))
model.load_state_dict(torch.load(checkpoint_path, map_location=lambda storage, loc: storage))
print('Model name: {}'.format(hps.encoder_name))
print('==> # Model parameters: {}.'.format(cal_parameters(model)))
if not os.path.exists(hps.log_dir):
os.mkdir(hps.log_dir)
if not os.path.exists(hps.attack_dir):
os.mkdir(hps.attack_dir)
if hps.attack == 'pgdinf':
linfPGD_attack(model, hps)
elif hps.attack == 'jsma':
jsma_attack(model, hps)
elif hps.attack == 'cw':
cw_l2_attack(model, hps)
elif hps.attack == 'fgsm':
fgsm_attack(model, hps)