-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattack_dio.py
316 lines (264 loc) · 12.8 KB
/
attack_dio.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
"""
Created on Mon Mar 09 2020
@author: fanghenshao
"""
from __future__ import print_function
import torch
import torch.nn as nn
import os
import sys
import ast
import copy
import argparse
import numpy as np
from utils import setup_seed
from utils import get_datasets, get_model
from utils import AverageMeter, accuracy
from utils import Logger
from advertorch.attacks import GradientSignAttack
from advertorch.attacks import LinfPGDAttack
from advertorch.attacks import CarliniWagnerL2Attack
from autoattack import AutoAttack
# ======== fix data type ========
torch.set_default_tensor_type(torch.FloatTensor)
# ======== options ==============
parser = argparse.ArgumentParser(description='Attack DIO')
# -------- file param. --------------
parser.add_argument('--data_dir',type=str,default='/media/Disk1/KunFang/data/CIFAR10/',help='file path for data')
parser.add_argument('--output_dir',type=str,default='./output/',help='folder to store output')
parser.add_argument('--dataset',type=str,default='CIFAR10',help='data set name')
parser.add_argument('--arch',type=str,default='vgg16',help='model architecture')
parser.add_argument('--model_path',type=str,default='./save/CIFAR10-VGG.pth',help='saved model path')
# -------- training param. ----------
parser.add_argument('--batch_size',type=int,default=256,help='batch size for test (default: 256)')
# -------- save adv. images --------
parser.add_argument('--save_adv_img',type=ast.literal_eval,dest='save_adv_img',help='save adversarial examples')
# -------- hyper parameters -------
parser.add_argument('--num_heads',type=int,default=10,help='number of orthogonal paths')
# -------- attack param. ----------
parser.add_argument('--attack_type',type=str,default='fgsm',help='attack method')
parser.add_argument('--test_eps', default=8., type=float, help='epsilon of attack during testing')
parser.add_argument('--test_step', default=20, type=int, help='itertion number of attack during testing')
parser.add_argument('--test_gamma', default=2., type=float, help='step size of attack during testing')
args = parser.parse_args()
# -------- initialize output store dir.
save_name = os.path.split(args.model_path)[-1].replace(".pth", "-"+args.attack_type.upper()+".log")
save_param = os.path.split(os.path.split(args.model_path)[-2])[-1]
if 'adv' in args.model_path:
if not os.path.exists(os.path.join(args.output_dir,args.dataset,args.arch+'-adv',save_param)):
os.makedirs(os.path.join(args.output_dir,args.dataset,args.arch+'-adv',save_param))
args.output_path = os.path.join(args.output_dir,args.dataset,args.arch+'-adv',save_param,save_name)
else:
if not os.path.exists(os.path.join(args.output_dir,args.dataset,args.arch,save_param)):
os.makedirs(os.path.join(args.output_dir,args.dataset,args.arch,save_param))
args.output_path = os.path.join(args.output_dir,args.dataset,args.arch,save_param,save_name)
sys.stdout = Logger(filename=args.output_path,stream=sys.stdout)
# -------- main function
def main():
# ======== fix seed =============
setup_seed(666)
# ======== get data set =============
trainloader, testloader = get_datasets(args)
print('-------- DATA INFOMATION --------')
print('---- dataset: '+args.dataset)
# ======== load network ========
checkpoint = torch.load(args.model_path, map_location=torch.device("cpu"))
backbone, head = get_model(args)
backbone, head = backbone.cuda(), head.cuda()
backbone.load_state_dict(checkpoint['state_dict_backbone'])
head.load_state_dict(checkpoint['state_dict_head'])
backbone.eval()
head.eval()
print('-------- MODEL INFORMATION --------')
print('---- architecture: '+args.arch)
print('---- saved path: '+args.model_path)
if 'best' in args.model_path:
print('---- best robust acc. achieved at epoch-%d.'%checkpoint['best-epoch'])
args.test_eps /= 255.
args.test_gamma /= 255.
if args.attack_type == 'None':
print('-------- START TESTING --------')
print('Evaluating...')
acc_tr, acc_te = val(backbone, head, trainloader), val(backbone, head, testloader)
acc_tr_str, acc_te_str = '', ''
acc_tr_val, acc_te_val = np.zeros(args.num_heads), np.zeros(args.num_heads)
for idx in range(args.num_heads):
acc_tr_str += '%.3f'%acc_tr[idx].avg+'\t'
acc_te_str += '%.2f'%acc_te[idx].avg+'\t'
acc_tr_val[idx] = acc_tr[idx].avg
acc_te_val[idx] = acc_te[idx].avg
print('training acc. on each path: \n'+acc_tr_str)
print('test acc. on each path: \n'+acc_te_str)
print("mean/std. acc. on clean training set:\t"+"%.2f"%np.mean(acc_tr_val)+"\t"+"%.2f"%np.std(acc_tr_val))
print("mean/std. acc. on clean test set:\t"+"%.2f"%np.mean(acc_te_val)+"\t"+"%.2f"%np.std(acc_te_val))
elif args.attack_type == 'fgsm':
print('-------- START ATTACKING --------')
print('-------- ADVERSARY INFORMATION --------')
print('---- FGSM attack with bound %d/255.'%(args.test_eps*255))
# --------
print('-------- START FGSM ATTACK...')
print('-------- Randomly-forward...')
acc_fgsm = attack(backbone, head, testloader)
acc_fgsm_str = ''
for head_idx in range(args.num_heads):
acc = acc_fgsm[head_idx]
acc_fgsm_str += '%.2f'%acc+'\t'
print('--------')
print('Attacked acc. on each path: \n'+acc_fgsm_str)
print("Attacked mean/std. acc.:\t"+"%.2f"%np.mean(acc_fgsm)+"\t"+"%.2f"%np.std(acc_fgsm))
elif args.attack_type == 'pgd':
print('-------- START ATTACKING --------')
print('-------- ADVERSARY INFORMATION --------')
print('---- PGD attack with %d/255 step size, %d iterations and bound %d/255.'%(args.test_gamma*255, args.test_step, args.test_eps*255))
# --------
print('-------- START PGD ATTACK...')
print('-------- Randomly-forward...')
acc_pgd = attack(backbone, head, testloader)
acc_pgd_str = ''
for head_idx in range(args.num_heads):
acc = acc_pgd[head_idx]
acc_pgd_str += '%.2f'%acc+'\t'
print('--------')
print('Attacked acc. on each path: \n'+acc_pgd_str)
print("Attacked mean/std. acc.:\t"+"%.2f"%np.mean(acc_pgd)+"\t"+"%.2f"%np.std(acc_pgd))
elif args.attack_type == 'pgd100':
args.test_step = 100
print('-------- START ATTACKING --------')
print('-------- ADVERSARY INFORMATION --------')
print('---- PGD attack with %d/255 step size, %d iterations and bound %d/255.'%(args.test_gamma*255, args.test_step, args.test_eps*255))
# --------
print('-------- START PGD ATTACK...')
print('-------- Randomly-forward...')
acc_pgd = attack(backbone, head, testloader)
acc_pgd_str = ''
for head_idx in range(args.num_heads):
acc = acc_pgd[head_idx]
acc_pgd_str += '%.2f'%acc+'\t'
print('--------')
print('Attacked acc. on each path: \n'+acc_pgd_str)
print("Attacked mean/std. acc.:\t"+"%.2f"%np.mean(acc_pgd)+"\t"+"%.2f"%np.std(acc_pgd))
elif args.attack_type == 'cw':
print('-------- START ATTACKING --------')
print('-------- ADVERSARY INFORMATION --------')
print('---- C&W attack with default settings in AdverTorch (L2 attack with max-iterations=3).')
# --------
print('-------- START C&W ATTACK...')
print('-------- Randomly-forward...')
acc_cw = attack(backbone, head, testloader)
acc_cw_str = ''
for head_idx in range(args.num_heads):
acc = acc_cw[head_idx]
acc_cw_str += '%.2f'%acc+'\t'
print('--------')
print('Attacked acc. on each path: \n'+acc_cw_str)
print("Attacked mean/std. acc.:\t"+"%.2f"%np.mean(acc_cw)+"\t"+"%.2f"%np.std(acc_cw))
elif args.attack_type == 'square':
print('-------- START ATTACKING --------')
print('-------- ADVERSARY INFORMATION --------')
print('---- SQUARE attack with default settings in AutoAttack (bound=L2-0.5)')
# --------
print('-------- START SQUARE ATTACK...')
print('-------- Randomly-forward...')
acc_square = attack(backbone, head, testloader)
acc_square_str = ''
for head_idx in range(args.num_heads):
acc = acc_square[head_idx]
acc_square_str += '%.2f'%acc+'\t'
print('--------')
print('Attacked acc. on each path: \n'+acc_square_str)
print("Attacked mean/std. acc.:\t"+"%.2f"%np.mean(acc_square)+"\t"+"%.2f"%np.std(acc_square))
elif args.attack_type == 'aa':
print("-------- START AUTO-ATTACK...")
print('-------- ADVERSARY INFORMATION --------')
print('---- AutoAttack with default settings (bound=Linf-%d/255)'%(args.test_eps*255))
# --------
print('-------- START AUTO-ATTACK...')
print('-------- Randomly-forward...')
acc_aa = attack(backbone, head, testloader)
acc_aa_str = ''
for head_idx in range(args.num_heads):
acc = acc_aa[head_idx]
acc_aa_str += '%.2f'%acc+'\t'
print('--------')
print('Attacked acc. on each path: \n'+acc_aa_str)
print("Attacked mean/std. acc.:\t"+"%.2f"%np.mean(acc_aa)+"\t"+"%.2f"%np.std(acc_aa))
else:
assert False, "Unknown attack : {}".format(args.attack_type)
print('-------- Results saved path: ', args.output_path)
print('-------- FINISHED.')
return
# ======== evaluate model ========
def val(backbone, head, dataloader):
backbone.eval()
head.eval()
acc = []
for idx in range(args.num_heads):
measure = AverageMeter()
acc.append(measure)
with torch.no_grad():
# -------- compute the accs. of train, test set
for test in dataloader:
images, labels = test
images, labels = images.cuda(), labels.cuda()
# ------- forward
all_logits = head(backbone(images), 'all')
for idx in range(args.num_heads):
logits = all_logits[idx]
logits = logits.detach().float()
prec1 = accuracy(logits.data, labels)[0]
acc[idx].update(prec1.item(), images.size(0))
return acc
# -------- attack model --------
# -------- RANDOMLY FORWARD PATH
def attack(backbone, head, testloader):
backbone.eval()
head.eval()
top1 = []
for _ in range(args.num_heads):
top1.append(AverageMeter())
if args.attack_type == 'fgsm':
def forward(input):
return head(backbone(input), 'random')
adversary = GradientSignAttack(forward, loss_fn=nn.CrossEntropyLoss(), eps=args.test_eps, clip_min=0.0, clip_max=1.0, targeted=False)
elif args.attack_type == 'pgd' or args.attack_type == 'pgd100':
def forward(input):
return head(backbone(input), 'random')
adversary = LinfPGDAttack(forward, loss_fn=nn.CrossEntropyLoss(), eps=args.test_eps, nb_iter=args.test_step, eps_iter=args.test_gamma, rand_init=True, clip_min=0.0, clip_max=1.0, targeted=False)
elif args.attack_type == 'cw':
def forward(input):
return head(backbone(input), 'random')
adversary = CarliniWagnerL2Attack(forward, num_classes=args.num_classes, max_iterations=3)
elif args.attack_type == 'square':
def forward(input):
return head(backbone(input), 'random')
adversary = AutoAttack(forward, norm='L2', eps=0.5, version='standard', verbose=False)
adversary.attacks_to_run = ['square']
elif args.attack_type == 'aa':
def forward(input):
return head(backbone(input), 'random')
adversary = AutoAttack(forward, norm='Linf', eps=args.test_eps, version='standard', verbose=False)
else:
assert False, "Unknown attack : {}".format(args.attack_type)
for test in testloader:
image, label = test
image, label = image.cuda(), label.cuda()
# generate adversarial examples
if args.attack_type == "fgsm" or args.attack_type == 'pgd' or args.attack_type == 'cw' or args.attack_type == 'pgd100':
perturbed_image = adversary.perturb(image, label)
elif args.attack_type == 'square' or args.attack_type == 'aa':
perturbed_image = adversary.run_standard_evaluation(image, label, bs=image.size(0))
else:
assert False, "Unknown attack : {}".format(args.attack_type)
# re-classify
all_logits = head(backbone(perturbed_image), 'all')
for index in range(args.num_heads):
logits = all_logits[index]
logits = logits.detach()
prec1 = accuracy(logits.data, label)[0]
top1[index].update(prec1.item(), image.size(0))
for index in range(args.num_heads):
top1[index] = top1[index].avg
return top1
# -------- start point
if __name__ == '__main__':
main()