-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattack.py
More file actions
197 lines (160 loc) · 6.84 KB
/
attack.py
File metadata and controls
197 lines (160 loc) · 6.84 KB
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
import foolbox as fb
import torch
import torch.nn as nn
from autoattack import AutoAttack
class Attack():
"""
This class used to generate adversarial images.
when create object specify epsilon: float, attack_type: 'FGSM, CW, BIM, L2PGD, PGD, LinfBIM'.
generate method return images and success tensors.
test_model method, give the accuracy of the model after passing the adversarial examples.
succecces tensor shows whether the example succed to fool the model or not
"""
def __init__(self, epsilon, attack_type, model) :
self.epsilon= epsilon
self.attack_type = attack_type
self.model_fool = fb.models.PyTorchModel(model ,bounds=(0,1))
self.adversary = AutoAttack(model, norm='Linf', eps=self.epsilon, version='standard')
def FGSM(self, samples, labels):
"""
Generate FGSM attacks.
Args:
samples -> clean images
labels -> labels of clean images
return:
adversarial images generated from the clean images
success tensor shows whether the attack succeded in fooling the model or not
"""
attack_func = fb.attacks.FGSM()
_, adv_images, success = attack_func(self.model_fool,
samples,
labels,
epsilons = self.epsilon)
return adv_images, success
def L2PGD(self, samples, labels):
"""
Generate L2 PGD attacks.
Args:
samples -> clean images
labels -> labels of clean images
return:
adversarial images generated from the clean images
success tensor shows whether the attack succeded in fooling the model or not
"""
attack_func = fb.attacks.L2PGD()
_, adv_images, success = attack_func(self.model_fool,
samples,
labels,
epsilons = self.epsilon)
return adv_images, success
def CW(self, samples, labels):
"""
Generate Carlini & Wagner attacks.
Args:
samples -> clean images
labels -> labels of clean images
return:
adversarial images generated from the clean images
success tensor shows whether the attack succeded in fooling the model or not
"""
attack_func = fb.attacks.L2CarliniWagnerAttack(6,1000,0.01,0)
_, adv_images, success = attack_func(self.model_fool,
samples,
labels,
epsilons= self.epsilon)
print(f'Sum = {sum(success)}')
return adv_images, success
def BIM(self, samples, labels):
"""
Generate BIM attacks.
Args:
samples -> clean images
labels -> labels of clean images
return:
adversarial images generated from the clean images
success tensor shows whether the attack succeded in fooling the model or not
"""
attack_func = fb.attacks.L2BasicIterativeAttack()
_, adv_images, success = attack_func(self.model_fool,
samples,
labels,
epsilons = self.epsilon)
return adv_images, success
def PGD(self, samples, labels):
"""
Generate Linf PGD attacks.
Args:
samples -> clean images
labels -> labels of clean images
return:
adversarial images generated from the clean images
success tensor shows whether the attack succeded in fooling the model or not
"""
attack_func = fb.attacks.PGD()
_, adv_images, success = attack_func(self.model_fool,
samples,
labels,
epsilons = self.epsilon)
return adv_images, success
def LinfBIM(self, samples, labels):
"""
Generate Linf BIM attacks.
Args:
samples -> clean images
labels -> labels of clean images
return:
adversarial images generated from the clean images
success tensor shows whether the attack succeded in fooling the model or not
"""
attack_func = fb.attacks.LinfBasicIterativeAttack()
_, adv_images, success = attack_func(self.model_fool,
samples,
labels,
epsilons = self.epsilon)
return adv_images, success
def AutoAttack(self, samples, labels):
"""
Generate Auto attacks.
Args:
samples -> clean images
labels -> labels of clean images
return:
adversarial images generated from the clean images
success tensor shows whether the attack succeded in fooling the model or not
"""
x_adv = self.adversary.run_standard_evaluation(samples, labels, bs=15)
success = None
return x_adv, success
def generate_attack(self, samples, labels):
"""
Generate attacks.
Args:
samples -> clean images
labels -> labels of clean images
return:
adversarial images -> generated from the clean images
success tensor -> shows whether the attack succeded in fooling the model or not
"""
if self.attack_type == 'FGSM':
adv_img, success = self.FGSM(samples, labels)
return adv_img, success
elif self.attack_type == 'CW':
adv_img, success = self.CW(samples, labels)
return adv_img, success
elif self.attack_type == 'L2PGD':
adv_img, success = self.L2PGD(samples, labels)
return adv_img, success
elif self.attack_type == 'BIM':
adv_img, success = self.BIM(samples, labels)
return adv_img, success
elif self.attack_type == 'PGD':
adv_img, success = self.PGD(samples, labels)
return adv_img, success
elif self.attack_type =='LinfBIM':
adv_img, success = self.LinfBIM(samples, labels)
return adv_img, success
elif self.attack_type =='AutoAttack':
adv_img, success = self.AutoAttack(samples, labels)
return adv_img, success
else:
print(f'Attacks of type {self.attack_type} is not supported')