-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnn_attack.py
454 lines (385 loc) · 16.1 KB
/
nn_attack.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
"""
Module for implementation of Region Based Attack
"""
import itertools
import os
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from cvxopt import matrix, solvers
import cvxopt.glpk
import numpy as np
from sklearn.neighbors import KDTree
from sklearn.neighbors import KNeighborsClassifier
import joblib
from joblib import Parallel, delayed
from tqdm import tqdm
from .cutils import c_get_half_space, get_all_half_spaces, get_constraints, check_feasibility
from ..utils import solve_lp, solve_qp
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
#solvers.options['solver'] = 'glpk'
#solvers.options['maxiters'] = 30
solvers.options['show_progress'] = False
#solvers.options['feastol'] = 1e-7
#solvers.options['abstol'] = 1e-7
#solvers.options['reltol'] = 1e-7
cvxopt.glpk.options["msg_lev"] = "GLP_MSG_OFF"
CONSTRAINTTOL = 5e-6
def get_half_space(a, b):
w = (b - a)
c = np.dot(w.T, (a + b) / 2)
sign = -np.sign(np.dot(w.T, b) - c)
w = sign * w
c = sign * c
return [w, c]
#@profile
def get_sol(target_x, tuple_x, faropp, kdtree,
glob_trnX, glob_trny, init_x=None, n_jobs=1):
tuple_x = np.asarray(tuple_x)
trnX = np.copy(glob_trnX)
emb_tar = target_x
G, h, _ = get_constraints(trnX, tuple_x, kdtree, faropp, emb_tar)
G, h = matrix(G, tc='d'), matrix(h, tc='d')
#assert (transformer.shape[1] == glob_trnX.shape[1])
n_fets = target_x.shape[0]
Q = 2 * matrix(np.eye(n_fets), tc='d')
#T = matrix(transformer.astype(np.float64), tc='d')
#G = G * T
q = matrix(-2*target_x, tc='d')
temph = h - CONSTRAINTTOL # make sure all constraints are met
status, sol = solve_qp(np.array(Q), np.array(q), np.array(G),
np.array(temph), n_fets)
if status == 'optimal':
ret = sol.reshape(-1)
return True, ret
else:
return False, None
def sol_sat_constraints(G, h) -> bool:
""" Check if the constraint is satisfiable
"""
fet_dim = G.shape[1]
c = matrix(np.zeros(fet_dim), tc='d')
G = matrix(G, tc='d')
temph = matrix(h - CONSTRAINTTOL, tc='d')
sol = solvers.lp(c=c, G=G, h=temph, solver='glpk')
return (sol['status'] == 'optimal')
def get_sol_l1(target_x, tuple_x, faropp, kdtree, glob_trnX,
glob_trny, init_x=None):
tuple_x = np.asarray(tuple_x)
fet_dim = target_x.shape[0]
emb_tar = target_x
trnX = np.copy(glob_trnX)
G, h, dist = get_constraints(trnX, tuple_x, kdtree, faropp, emb_tar)
#G = np.dot(G, transformer)
if init_x is None and not sol_sat_constraints(G, h):
return False, None
c = matrix(np.concatenate((np.zeros(fet_dim), np.ones(fet_dim))), tc='d')
G = np.hstack((G, np.zeros((G.shape[0], fet_dim))))
G = np.vstack((G, np.hstack((np.eye(fet_dim), -np.eye(fet_dim)))))
G = np.vstack((G, np.hstack((-np.eye(fet_dim), -np.eye(fet_dim)))))
h = np.concatenate((h, target_x, -target_x))
G, h = matrix(G, tc='d'), matrix(h, tc='d')
temph = h - CONSTRAINTTOL
if init_x is not None:
sol = solvers.lp(c=c, G=G, h=temph, solver='glpk',
initvals=init_x)
else:
sol = solvers.lp(c=c, G=G, h=temph, solver='glpk')
if sol['status'] == 'optimal':
ret = np.array(sol['x']).reshape(-1)
return True, ret[:len(ret)//2]
else:
#logger.warning("solver error")
return False, None
#@profile
def get_sol_linf(target_x, tuple_x, faropp, kdtree,
glob_trnX, glob_trny, init_x=None, n_jobs=1):
tuple_x = np.asarray(tuple_x)
fet_dim = target_x.shape[0]
emb_tar = target_x
trnX = np.copy(glob_trnX)
G, h, _ = get_constraints(trnX, tuple_x, kdtree, faropp, emb_tar)
#G = np.dot(G, transformer)
if init_x is None and not sol_sat_constraints(G, h):
return False, None
c = np.concatenate((np.zeros(fet_dim), np.ones(1))).reshape((-1, 1))
G2 = np.hstack((np.eye(fet_dim), -np.ones((fet_dim, 1))))
G3 = np.hstack((-np.eye(fet_dim), -np.ones((fet_dim, 1))))
G = np.hstack((G, np.zeros((G.shape[0], 1))))
G = np.vstack((G, G2, G3))
h = np.concatenate((h, target_x, -target_x)).reshape((-1, 1))
temph = h - CONSTRAINTTOL
status, sol = solve_lp(c=c, G=G, h=temph, n=len(c), n_jobs=n_jobs)
if status == 'optimal':
ret = np.array(sol).reshape(-1)
return True, ret[:-1]
else:
return False, None
def get_adv(target_x, target_y, kdtree, n_searches, n_neighbors, faropp,
lp_sols, glob_trnX, glob_trny, ord=2, n_jobs=1):
ind = kdtree.query(target_x.reshape((1, -1)),
k=n_neighbors, return_distance=False)[0]
if target_y != np.argmax(np.bincount(glob_trny[ind])):
# already incorrectly predicted
return np.zeros_like(target_x)
knn = KNeighborsClassifier(n_neighbors=n_neighbors)
knn.fit(glob_trnX, glob_trny)
pred_trny = knn.predict(glob_trnX)
temp = (target_x, np.inf)
if n_searches == -1:
n_searches = glob_trnX.shape[0]
ind = np.arange(glob_trnX.shape[0])
else:
ind = kdtree.query(target_x.reshape((1, -1)),
k=n_searches, return_distance=False)
ind = ind[0]
combs = []
for comb in itertools.combinations(range(n_searches), n_neighbors):
comb = list(comb)
# majority
if target_y != np.argmax(np.bincount(glob_trny[ind[comb]])):
combs.append(comb)
if ord == 1:
get_sol_fn = get_sol_l1
elif ord == 2:
get_sol_fn = get_sol
elif ord == np.inf:
get_sol_fn = get_sol_linf
else:
raise ValueError("Unsupported ord %d" % ord)
def _helper(comb, trnX, trny, init_x):
comb_tup = tuple(ind[comb])
ret, sol = get_sol_fn(target_x, ind[comb], faropp, kdtree,
trnX, trny, init_x=init_x, n_jobs=n_jobs)
return ret, sol
not_vacum = lambda x: tuple(ind[x]) not in lp_sols or lp_sols[tuple(ind[x])]
combs = list(filter(not_vacum, combs))
if n_neighbors == 1:
sols = Parallel(n_jobs=-1, verbose=1)(
delayed(_helper)(comb, glob_trnX, glob_trny,
init_x=glob_trnX[ind[comb[0]]]) for comb in combs)
else:
sols = Parallel(n_jobs=-1, verbose=1)(
delayed(_helper)(comb, glob_trnX, glob_trny, None) for comb in combs)
status, sols = zip(*sols)
sols = np.array(sols)
for i, s in enumerate(status):
if not s:
assert sols[i] is None
if n_neighbors == 1:
# some time region is too small for solver
sols[i] = glob_trnX[ind[combs[i]][0]]
#lp_sols[tuple(ind[combs[i]])] = glob_trnX[ind[combs[i]][0]]
else:
lp_sols[tuple(ind[combs[i]])] = None
#_, sols = list(zip(*list(filter(lambda s: True if s[0] else False, zip(status, sols)))))
sols = np.array(list(filter(lambda x: np.linalg.norm(x) != 0, sols)))
eps = np.linalg.norm(sols - target_x, axis=1, ord=ord)
#temp = (sols[eps.argmin()], eps.min())
return sols[eps.argmin()] - target_x
def attack_with_eps_constraint(perts, ord, eps):
perts = np.asarray(perts)
if isinstance(eps, list):
rret = []
norms = np.linalg.norm(perts, axis=1, ord=ord)
for ep in eps:
t = np.copy(perts)
t[norms > ep, :] = 0
rret.append(t)
return rret
elif eps is not None:
perts[np.linalg.norm(perts, axis=1, ord=ord) > eps, :] = 0
return perts
else:
return perts
def rev_get_adv(target_x, target_y, kdtree, n_searches, n_neighbors, faropp,
lp_sols, glob_trnX, glob_trny, ord=2, method='self',
knn: KNeighborsClassifier = None, n_jobs=1):
if n_searches == -1:
n_searches = glob_trnX.shape[0]
temp = (target_x, np.inf)
# already predicted wrong
if knn.predict(target_x.reshape((1, -1)))[0] != target_y:
return temp[0] - target_x
if ord == 1:
get_sol_fn = get_sol_l1
elif ord == 2:
get_sol_fn = get_sol
elif ord == np.inf:
get_sol_fn = get_sol_linf
else:
raise ValueError("Unsupported ord %d" % ord)
knn = KNeighborsClassifier(n_neighbors=n_neighbors)
knn.fit(glob_trnX, glob_trny)
pred_trny = knn.predict(glob_trnX)
ind = kdtree.query(target_x.reshape((1, -1)),
k=len(glob_trnX), return_distance=False)[0]
ind = list(filter(lambda x: pred_trny[x] != target_y, ind))[:n_searches]
solsss = []
for i in ind:
if method == 'self':
inds = [i]
elif method == 'region':
procedX = glob_trnX[i].reshape((1, -1))
inds = kdtree.query(procedX, k=n_neighbors, return_distance=False)[0]
inds = tuple([_ for _ in inds])
ret, sol = get_sol_fn(target_x, inds, faropp, kdtree,
glob_trnX, glob_trny, init_x=glob_trnX[i], n_jobs=n_jobs)
solsss.append(sol)
if method == 'region':
#assert ret
if not ret:
proc = np.array([glob_trnX[i]])
sol = np.array(glob_trnX[i])
else:
proc = np.array([sol])
assert knn.predict(proc)[0] != target_y
eps = np.linalg.norm(sol - target_x, ord=ord)
if eps < temp[1]:
temp = (sol, eps)
elif ret: # method == 'self'
proc = np.array([sol])
if knn.predict(proc)[0] != target_y:
eps = np.linalg.norm(sol - target_x, ord=ord)
if eps < temp[1]:
temp = (sol, eps)
solsss = np.asarray(solsss)
return temp[0] - target_x
class NNOptAttack():
def __init__(self, trnX, trny, n_neighbors=3, n_searches=-1, faropp=-1,
transformer=None, ord=2, n_jobs=1):
#furthest >= K
self.n_jobs = n_jobs
self.K = n_neighbors
self.trnX = trnX
self.trny = trny
self.n_searches = min(n_searches, len(trnX))
self.faropp = faropp
self.transformer = transformer
self.ord = ord
if transformer is not None:
self.tree = KDTree(self.transformer.transform(self.trnX))
else:
self.tree = KDTree(self.trnX)
self.lp_sols = {}
def perturb(self, X, y, eps=None, logging=False, n_jobs=1):
raise NotImplementedError()
class NNAttack(NNOptAttack):
def __init__(self, trnX, trny, n_neighbors=3, n_searches=-1, faropp=-1,
transformer=None, ord=2, n_jobs=1):
super().__init__(trnX=trnX, trny=trny, n_neighbors=n_neighbors,
n_searches=n_searches, faropp=faropp, transformer=transformer,
ord=ord, n_jobs=n_jobs)
#@profile
def perturb(self, X, y, eps=None, n_jobs=1):
if self.transformer:
transformer = self.transformer.transformer()
else:
transformer = np.eye(self.trnX.shape[1])
#global glob_trnX
#global glob_trny
glob_trnX = self.trnX
glob_trny = self.trny
ret = []
for i, (target_x, target_y) in tqdm(enumerate(zip(X, y)), ascii=True, desc="Perturb"):
ret.append(get_adv(target_x.astype(np.float64), target_y, self.tree,
self.n_searches, self.K, self.faropp,
self.lp_sols,
glob_trnX=glob_trnX,
glob_trny=glob_trny,
ord=self.ord))
self.perts = np.asarray(ret)
return attack_with_eps_constraint(self.perts, self.ord, eps)
class KNNRegionBasedAttackExact(NNAttack):
"""
Exact Region Based Attack (RBA-Exact) for K-NN
Arguments:
trnX {ndarray, shape=(n_samples, n_features)} -- Training data
trny {ndarray, shape=(n_samples)} -- Training label
Keyword Arguments:
n_neighbors {int} -- Number of neighbors for the target k-NN classifier (default: {3})
n_searches {int} -- Number of regions to search, -1 means all regions (default: {-1})
ord {int} -- Order of the norm for perturbation distance, see numpy.linalg.norm for more information (default: {2})
n_jobs {int} -- number of cores to run (default: {1})
"""
def __init__(self, trnX, trny, n_neighbors=3, n_searches=-1, ord=2, n_jobs=1):
super().__init__(trnX=trnX, trny=trny, n_neighbors=n_neighbors,
n_searches=-1, faropp=-1, transformer=None, ord=ord, n_jobs=n_jobs)
class RevNNAttack(NNOptAttack):
"""
Approximated Region Based Attack (RBA-Approx)
Arguments:
trnX {ndarray, shape=(n_samples, n_features)} -- Training data
trny {ndarray, shape=(n_samples)} -- Training label
Keyword Arguments:
n_neighbors {int} -- Number of neighbors for the target k-NN classifier (default: {3})
n_searches {int} -- Number of regions to search (default: {-1})
ord {int} -- Order of the norm for perturbation distance, see numpy.linalg.norm for more information (default: {2})
n_jobs {int} -- number of cores to run (default: {1})
faropp {int} -- Not used (default: {-1})
transformer {[type]} -- Not used (default: {None})
method {str} -- Not used (default: {'region'})
"""
def __init__(self, trnX: np.array, trny: np.array, n_neighbors: int = 3,
n_searches: int = -1, faropp: int = -1, transformer=None, ord=2,
method='region', n_jobs=1):
super().__init__(trnX=trnX, trny=trny, n_neighbors=n_neighbors,
n_searches=n_searches, faropp=faropp, transformer=transformer,
ord=ord)
self.method = method
#@profile
def perturb(self, X, y, eps=None, n_jobs=1):
if self.transformer:
transformer = self.transformer.transformer()
else:
transformer = np.eye(self.trnX.shape[1])
#global glob_trnX
#global glob_trny
glob_trnX = self.trnX
glob_trny = self.trny
knn = KNeighborsClassifier(n_neighbors=self.K)
knn.fit(glob_trnX, glob_trny)
X = X.astype(np.float64)
n_jobs = -1
if n_jobs == 1:
ret = []
for i, (target_x, target_y) in tqdm(enumerate(zip(X, y)), ascii=True, desc="Perturb"):
ret.append(
rev_get_adv(target_x.astype(np.float64), target_y,
self.tree, self.n_searches, self.K, self.faropp,
self.lp_sols, ord=self.ord,
method=self.method, knn=knn, n_jobs=self.n_jobs,
glob_trnX=glob_trnX, glob_trny=glob_trny,
)
)
else:
def _helper(target_x, target_y):
return rev_get_adv(target_x, target_y,
self.tree, self.n_searches, self.K, self.faropp,
dict(), ord=self.ord,
method=self.method, knn=knn, n_jobs=1,
glob_trnX=glob_trnX, glob_trny=glob_trny,
)
ret = Parallel(n_jobs=n_jobs, verbose=1)(
delayed(_helper)(tar_x, tar_y)
for (tar_x, tar_y) in zip(X, y))
self.perts = np.asarray(ret)
return attack_with_eps_constraint(self.perts, self.ord, eps)
class KNNRegionBasedAttackApprox(RevNNAttack):
"""
Approximated Region Based Attack (RBA-Approx) for K-NN
Arguments:
trnX {ndarray, shape=(n_samples, n_features)} -- Training data
trny {ndarray, shape=(n_samples)} -- Training label
Keyword Arguments:
n_neighbors {int} -- Number of neighbors for the target k-NN classifier (default: {3})
n_searches {int} -- Number of regions to search, -1 means all regions (default: {-1})
ord {int} -- Order of the norm for perturbation distance, see numpy.linalg.norm for more information (default: {2})
n_jobs {int} -- number of cores to run (default: {1})
"""
def __init__(self, trnX: np.array, trny: np.array, n_neighbors: int = 3,
n_searches: int = -1, ord=2, n_jobs=1):
super().__init__(trnX=trnX, trny=trny, n_neighbors=n_neighbors,
n_searches=n_searches, faropp=-1, transformer=None, ord=ord,
method='region', n_jobs=n_jobs)