-
Notifications
You must be signed in to change notification settings - Fork 0
/
attacks.py
191 lines (146 loc) · 5.95 KB
/
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
import numpy as np
from keras.backend import get_session
import tensorflow as tf
from cleverhans.utils_keras import KerasModelWrapper
from cleverhans.attacks import FastGradientMethod, BasicIterativeMethod, CarliniWagnerL2, \
MadryEtAl, SaliencyMapMethod, FastFeatureAdversaries
# # DEBUG
# def debug(s):
# if __debug__:
# print(s)
# GLOBAL VARIABLES
BS = 128
class ABC_Attack:
def __init__(self, attack, attack_params):
self._attack = attack
self._attack_params = attack_params
def _generate(self, model, X, y=None, bs=BS):
targeted_attack = y is not None
# debug("============= ATTACK GRAPH BUILDING ===============")
# Retrieve needs
_in_shape, _n_classes = model.input_shape[1:], model.output_shape[1]
# Convert keras model in tf format
wrap = KerasModelWrapper(model)
cleverhans_attack = eval(self._attack)(wrap, sess=get_session())
# Build graph
x = tf.placeholder(tf.float32, shape=[None, *_in_shape])
if targeted_attack:
y_tgt = tf.placeholder(tf.float32, shape=[None, _n_classes])
self._attack_params['y_target'] = y_tgt
# Attack tensor
x_adv = cleverhans_attack.generate(x, **self._attack_params)
x_adv = tf.stop_gradient(x_adv)
# debug("============= ADVEX GENERATION ===============")
# Generate attack in batches
X_adv = np.zeros_like(X)
n_samples = X.shape[0]
nb_batches = n_samples // bs
sess = get_session()
for i in range(nb_batches + 1):
_start, _end = i * bs, min(n_samples, (i + 1) * bs)
if _start < _end: # TODO: HACK to handle the case in which n_samples is multiple of bs
_X = X[_start:_end]
feeds = {
x: _X
}
# Targeted attack?
if targeted_attack:
_y = y[_start:_end]
feeds[y_tgt] = _y
X_adv[_start:_end] = sess.run(x_adv, feeds)
return X_adv
class FGSM(ABC_Attack):
def __init__(self, eps=0.3, ord=np.inf, clip_min=0.0, clip_max=1.0):
params = {
'eps': eps,
'ord': ord,
'clip_min': clip_min,
'clip_max': clip_max,
}
super(FGSM, self).__init__('FastGradientMethod', params)
def generate(self, model, X, y=None):
return super(FGSM, self)._generate(model, X, y)
class BIM(ABC_Attack):
def __init__(self, eps=0.3, eps_iter=0.05, ord=np.inf, clip_min=0.0, clip_max=1.0, nb_iter=10):
params = {
'eps': eps,
'eps_iter': eps_iter,
'ord': ord,
'clip_min': clip_min,
'clip_max': clip_max,
'nb_iter': nb_iter
}
super(BIM, self).__init__('BasicIterativeMethod', params)
def generate(self, model, X, y=None):
return super(BIM, self)._generate(model, X, y)
class CW_L2(ABC_Attack):
def __init__(self, confidence=0, batch_size=64, clip_min=0.0, clip_max=1.0):
params = {'confidence': confidence,
'batch_size': batch_size,
'clip_min': clip_min,
'clip_max': clip_max
}
super(CW_L2, self).__init__('CarliniWagnerL2', params)
def generate(self, model, X, y=None):
return super(CW_L2, self)._generate(model, X, y, bs=self._attack_params['batch_size'])
class PGD(ABC_Attack):
def __init__(self, eps=0.3, eps_iter=0.01, ord=np.inf, clip_min=0.0, clip_max=1.0, nb_iter=40):
params = {
'eps': eps,
'eps_iter': eps_iter,
'ord': ord,
'clip_min': clip_min,
'clip_max': clip_max,
'nb_iter': nb_iter
}
super(PGD, self).__init__('MadryEtAl', params)
def generate(self, model, X, y=None):
return super(PGD, self)._generate(model, X, y)
class JSMA(ABC_Attack):
def __init__(self, theta=1.0, gamma=1.0, clip_min=0.0, clip_max=1.0):
'''
Jacobian Saliency Map Attack
:param theta: Perturbation induced to modified components (can be positive or negative)
:param gamma: Maximum percentage of perturbed features
:param clip_min: Minimum component value for clipping
:param clip_max: Maximum component value for clipping
'''
params = {
'theta': theta,
'gamma': gamma,
'clip_min': clip_min,
'clip_max': clip_max
}
super(JSMA, self).__init__('SaliencyMapMethod', params)
def generate(self, model, X, y=None):
return super(JSMA, self)._generate(model, X, y, bs=64)
class Features_Attack(ABC_Attack):
def __init__(self, layer, eps=0.3, eps_iter=0.01, ord=np.inf, clip_min=0.0, clip_max=1.0, nb_iter=100):
params = {
'layer': layer,
'eps': eps,
'eps_iter': eps_iter,
'ord': ord,
'clip_min': clip_min,
'clip_max': clip_max,
'nb_iter': nb_iter
}
super(Features_Attack, self).__init__('FastFeatureAdversaries', params)
def generate(self, model, X_src, X_guide):
wrap = KerasModelWrapper(model)
cleverhans_attack = FastFeatureAdversaries(wrap, sess=get_session())
# ============= ATTACK GRAPH BUILDING ===============
assert X_src.shape == X_guide.shape, "Inconsistency between X_src and X_guide shapes!"
x_src = tf.placeholder(tf.float32, shape=[None, *(X_src.shape[1:])])
x_guide = tf.placeholder(tf.float32, shape=[None, *(X_src.shape[1:])])
# Attack tensor
x_adv = cleverhans_attack.generate(x_src, x_guide, **self._attack_params)
x_adv = tf.stop_gradient(x_adv)
# ============= ADVEX GENERATION ===============
sess = get_session()
X_adv = sess.run(x_adv, feed_dict={
x_src: X_src,
x_guide: X_guide
}
)
return X_adv