-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathattackpipeline.py
277 lines (174 loc) · 8.5 KB
/
attackpipeline.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
import dataset
import utils
from classifiers.classifier import *
from train import *
from torch.utils.data import DataLoader , TensorDataset
from train import *
from dataset import *
import numpy as np
import torch.nn as nn
from art.estimators.classification import PyTorchClassifier
from torch import optim
# ------ CLEVER HANS ------
from cleverhans.torch.attacks.fast_gradient_method import fast_gradient_method
from cleverhans.torch.attacks.projected_gradient_descent import (projected_gradient_descent,)
# ------ TORCH ATTACKS ------
from torchattacks import AutoAttack, EOTPGD
# ------ ADVERSARIAL ROBUSTNESS TOOLBOX ------
from art.attacks.evasion import FastGradientMethod, DeepFool, ProjectedGradientDescent, SquareAttack, ElasticNet, SignOPTAttack
from art.estimators.classification import PyTorchClassifier
"""
attack pipeline.py use the function of attack.py in the adversarial attack folder
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def getConfigs(datasetName, classifierName, attackName=None):
config = utils.loadconfigYaml("/equilibrium/seide/generative_models/silva/Project-Diffusion-Defense/config.yaml")
configAttack = config['attacks'][attackName]
configAttackGeneral = config['attacks']['general']
configData = config['dataset'][datasetName]
configModel = config['classifier'][classifierName]
return configAttack, configAttackGeneral, configData, configModel
def createClassifier(submodel, configsmodel):
r""" Create a pytorch classifier to use ART attacks
"""
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(submodel.parameters(), lr=configsmodel['learning_rate'], momentum=configsmodel['momentum'])
classifier = PyTorchClassifier(model=submodel,clip_values=(0, 1),loss=criterion,optimizer=optimizer,input_shape=(1, 28,28),nb_classes=10,)
return classifier
def createAdversarialData(attack, testset, batch_size):
for i, (images, label) in enumerate(testset):
if i == 0:
x_test_adv = torch.tensor(attack.generate(x = images.cpu().detach().numpy()))
l = label
else:
x_test_adv = torch.cat ([x_test_adv,torch.tensor(attack.generate(x = images.cpu().detach().numpy()))])
l = torch.cat ([l,label ])
return x_test_adv , l
def FGSM_Attack_CH(submodel, datasetname, classifiername, testset, batchSize, type=None):
"""
FGSM ATTACK
"""
print("Attack using FGSM CH....")
for i, (images, label) in enumerate(testset):
images, label = images.to(device), label.to(device)
if i == 0:
advtestset = fast_gradient_method(model_fn=submodel, x=images, norm=np.inf,eps=0.3).detach().clone()
l = label
else:
advtestset = torch.cat ([advtestset,fast_gradient_method(model_fn=submodel, x=images, norm=np.inf,eps=0.3).detach().clone()])
l = torch.cat ([l,label ])
print("Attack conclude....")
return advtestset , l
def PGD_Attack_CH(submodel, datasetname, classifiername, testset, batchSize, type):
print("Attack using PGD CH....")
#get configs
for i, (images, label) in enumerate(testset):
images, label = images.to(device), label.to(device)
if i == 0:
advtestset = projected_gradient_descent(model_fn=submodel,x=images,eps=0.3,norm=np.inf, eps_iter=0.05, nb_iter = 5).detach().clone()
l = label
else:
advtestset = torch.cat ([advtestset,projected_gradient_descent(model_fn=submodel,x=images,eps=0.3,norm=np.inf, eps_iter=0.05, nb_iter = 5).detach().clone()])
l = torch.cat ([l,label])
print("Attack conclude....")
return advtestset , l
def FGSM_Attack(submodel, datasetname, classifiername, testset, batchSize, type):
print("Attack using FGSM....")
#get configs
configAttack, _, _, configModel = getConfigs(datasetname, type, "fgsm")
# Defining the model to attack
classifier = createClassifier(submodel, configModel)
#Defining the attack
attack = FastGradientMethod(estimator=classifier, eps=configAttack['eps'], norm=configAttack['norm'])
# Attaching testset and create a dataloader with adv_images
advtestloader, l = createAdversarialData(attack, testset, batchSize)
print("Attack conclude....")
return advtestloader, l
def SA_Attack(submodel, datasetname, classifiername, testset, batchSize,type):
print("Attack using SA....")
#get configs
_, _, _, configModel = getConfigs(datasetname, type, "fgsm")
#attack
classifier = createClassifier(submodel, configModel)
attack = SquareAttack(estimator=classifier)
advtestloader, l = createAdversarialData(attack, testset, batchSize)
print("Attack conclude....")
return advtestloader, l
def SOPT_Attack(submodel, datasetname, classifiername, testset, batchSize, type):
print("Attack using SOPT....")
#get configs
_, _, _, configModel = getConfigs(datasetname, type, "fgsm")
#attack
classifier = createClassifier(submodel, configModel)
attack = SignOPTAttack(estimator=classifier, targeted=False, max_iter=100, batch_size=100)
advtestloader, l = createAdversarialData(attack, testset, batchSize)
print("Attack conclude....")
return advtestloader, l
def EOTPGD_Attack(submodel, datasetname, classifiername, testset, batchSize, type):
print("Attack using EOTPGD....")
attack = EOTPGD(model=submodel, eps = 0.3, steps=20, eot_iter=10)
submodel = submodel.to(device)
for i, (images, label) in enumerate(testset):
images, label = images.to(device), label.to(device)
output = submodel(images)
_, predicated = torch.max(output.data, 1)
if i == 0:
x_test_adv = attack(images, predicated)
l = label
else:
x_test_adv = torch.cat ([x_test_adv,attack(images, predicated)])
l = torch.cat ([l,label ])
print("Attack conclude....")
return x_test_adv , l
def AA_Attack(submodel, datasetname, classifiername, testset, batchSize, type):
print("Attack using AutoAttack....")
attack = AutoAttack(model=submodel, eps = 0.3)
submodel = submodel.to(device)
for i, (images, label) in enumerate(testset):
images, label = images.to(device), label.to(device)
output = submodel(images)
_, predicated = torch.max(output.data, 1)
if i == 0:
x_test_adv = attack(images, predicated)
l = label
else:
x_test_adv = torch.cat ([x_test_adv,attack(images, predicated)])
l = torch.cat ([l,label ])
print("Attack conclude....")
return x_test_adv.clone().detach() , l
def DF_Attack(submodel, datasetname, classifiername,testset, batchSize, type):
print("Attack using DF....")
#get configs
_, _, _, configModel = getConfigs(datasetname, type, 'cw')
# Defining the model to attack
classifier = createClassifier(submodel, configModel)
#Defining the attack
attack = DeepFool(classifier=classifier, batch_size=32)
# Attaching testset and create a dataloader with adv_images
advtestloader, l = createAdversarialData(attack, testset, batchSize)
print("Attack conclude....")
return advtestloader, l
def PGD_Attack(submodel, datasetname, classifiername,testset, batchSize, type):
print("Attack using PGD....")
#get configs
_, _, _, configModel = getConfigs(datasetname, type, 'cw')
# Defining the model to attack
classifier = createClassifier(submodel, configModel)
#Defining the attack
attack = ProjectedGradientDescent(classifier,norm=np.inf,eps=0.3,eps_step=0.01,max_iter=20,targeted=False,num_random_init=5)
# Attaching testset and create a dataloader with adv_images
advtestloader,l = createAdversarialData(attack, testset, batchSize)
print("Attack conclude....")
return advtestloader, l
def EN_Attack(submodel, datasetname, classifiername,testset, batchSize, type):
print("Attack using Elastic NET....")
#get configs
_, _, _, configModel = getConfigs(datasetname, type, 'cw')
# Defining the model to attack
classifier = createClassifier(submodel, configModel)
#Defining the attack
attack = ElasticNet(classifier,batch_size=batchSize)
# Attaching testset and create a dataloader with adv_images
advtestloader,l = createAdversarialData(attack, testset, batchSize)
print("Attack conclude....")
return advtestloader, l