-
Notifications
You must be signed in to change notification settings - Fork 27
/
Sign_OPT.py
667 lines (575 loc) · 25.5 KB
/
Sign_OPT.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
import time
import numpy as np
from numpy import linalg as LA
import torch
import scipy.spatial
from scipy.linalg import qr
#from qpsolvers import solve_qp
import random
start_learning_rate = 1.0
MAX_ITER = 1000
def quad_solver(Q, b):
"""
Solve min_a 0.5*aQa + b^T a s.t. a>=0
"""
K = Q.shape[0]
alpha = np.zeros((K,))
g = b
Qdiag = np.diag(Q)
for i in range(20000):
delta = np.maximum(alpha - g/Qdiag,0) - alpha
idx = np.argmax(abs(delta))
val = delta[idx]
if abs(val) < 1e-7:
break
g = g + val*Q[:,idx]
alpha[idx] += val
return alpha
def sign(y):
"""
y -- numpy array of shape (m,)
Returns an element-wise indication of the sign of a number.
The sign function returns -1 if y < 0, 1 if x >= 0. nan is returned for nan inputs.
"""
y_sign = np.sign(y)
y_sign[y_sign==0] = 1
return y_sign
class OPT_attack_sign_SGD(object):
def __init__(self, model, k=200, train_dataset=None):
self.model = model
self.k = k
self.train_dataset = train_dataset
self.log = torch.ones(MAX_ITER,2)
def get_log(self):
return self.log
def attack_untargeted(self, x0, y0, alpha = 0.2, beta = 0.001, iterations = 1000, query_limit=20000,
distortion=None, seed=None, svm=False, momentum=0.0, stopping=0.0001):
""" Attack the original image and return adversarial example
model: (pytorch model)
train_dataset: set of training data
(x0, y0): original image
"""
model = self.model
y0 = y0[0]
query_count = 0
ls_total = 0
if (model.predict_label(x0) != y0):
print("Fail to classify the image. No need to attack.")
return x0
if seed is not None:
np.random.seed(seed)
# Calculate a good starting point.
num_directions = 100
best_theta, g_theta = None, float('inf')
print("Searching for the initial direction on %d random directions: " % (num_directions))
timestart = time.time()
for i in range(num_directions):
query_count += 1
theta = np.random.randn(*x0.shape)
if model.predict_label(x0+torch.tensor(theta, dtype=torch.float).cuda())!=y0:
initial_lbd = LA.norm(theta)
theta /= initial_lbd
lbd, count = self.fine_grained_binary_search(model, x0, y0, theta, initial_lbd, g_theta)
query_count += count
if lbd < g_theta:
best_theta, g_theta = theta, lbd
print("--------> Found distortion %.4f" % g_theta)
if g_theta == float('inf'):
num_directions = 100
best_theta, g_theta = None, float('inf')
print("Searching for the initial direction on %d random directions: " % (num_directions))
timestart = time.time()
for i in range(num_directions):
query_count += 1
theta = np.random.randn(*x0.shape)
if model.predict_label(x0+torch.tensor(theta, dtype=torch.float).cuda())!=y0:
initial_lbd = LA.norm(theta)
theta /= initial_lbd
lbd, count = self.fine_grained_binary_search(model, x0, y0, theta, initial_lbd, g_theta)
query_count += count
if lbd < g_theta:
best_theta, g_theta = theta, lbd
print("--------> Found distortion %.4f" % g_theta)
timeend = time.time()
if g_theta == float('inf'):
print("Couldn't find valid initial, failed")
return x0
print("==========> Found best distortion %.4f in %.4f seconds "
"using %d queries" % (g_theta, timeend-timestart, query_count))
self.log[0][0], self.log[0][1] = g_theta, query_count
# Begin Gradient Descent.
timestart = time.time()
xg, gg = best_theta, g_theta
vg = np.zeros_like(xg)
learning_rate = start_learning_rate
prev_obj = 100000
distortions = [gg]
for i in range(iterations):
if svm == True:
sign_gradient, grad_queries = self.sign_grad_svm(x0, y0, xg, initial_lbd=gg, h=beta)
else:
sign_gradient, grad_queries = self.sign_grad_v1(x0, y0, xg, initial_lbd=gg, h=beta)
if False:
# Compare cosine distance with numerical gradient.
gradient, _ = self.eval_grad(model, x0, y0, xg, initial_lbd=gg, tol=beta/500, h=0.01)
print(" Numerical - Sign gradient cosine distance: ",
scipy.spatial.distance.cosine(gradient.flatten(), sign_gradient.flatten()))
# Line search
ls_count = 0
min_theta = xg
min_g2 = gg
min_vg = vg
for _ in range(15):
if momentum > 0:
# # Nesterov
# vg_prev = vg
# new_vg = momentum*vg - alpha*sign_gradient
# new_theta = xg + vg*(1 + momentum) - vg_prev*momentum
new_vg = momentum*vg - alpha*sign_gradient
new_theta = xg + new_vg
else:
new_theta = xg - alpha * sign_gradient
new_theta /= LA.norm(new_theta)
new_g2, count = self.fine_grained_binary_search_local(
model, x0, y0, new_theta, initial_lbd = min_g2, tol=beta/500)
ls_count += count
alpha = alpha * 2
if new_g2 < min_g2:
min_theta = new_theta
min_g2 = new_g2
if momentum > 0:
min_vg = new_vg
else:
break
if min_g2 >= gg:
for _ in range(15):
alpha = alpha * 0.25
if momentum > 0:
# # Nesterov
# vg_prev = vg
# new_vg = momentum*vg - alpha*sign_gradient
# new_theta = xg + vg*(1 + momentum) - vg_prev*momentum
new_vg = momentum*vg - alpha*sign_gradient
new_theta = xg + new_vg
else:
new_theta = xg - alpha * sign_gradient
new_theta /= LA.norm(new_theta)
new_g2, count = self.fine_grained_binary_search_local(
model, x0, y0, new_theta, initial_lbd = min_g2, tol=beta/500)
ls_count += count
if new_g2 < gg:
min_theta = new_theta
min_g2 = new_g2
if momentum > 0:
min_vg = new_vg
break
if alpha < 1e-4:
alpha = 1.0
print("Warning: not moving")
beta = beta*0.1
if (beta < 1e-8):
break
xg, gg = min_theta, min_g2
vg = min_vg
query_count += (grad_queries + ls_count)
ls_total += ls_count
distortions.append(gg)
if query_count > query_limit:
break
if (i+1)%10==0:
print("Iteration %3d distortion %.4f num_queries %d" % (i+1, gg, query_count))
self.log[i+1][0], self.log[i+1][1] = gg, query_count
#if distortion is not None and gg < distortion:
# print("Success: required distortion reached")
# break
# if gg > prev_obj-stopping:
# print("Success: stopping threshold reached")
# break
# prev_obj = gg
target = model.predict_label(x0 + torch.tensor(gg*xg, dtype=torch.float).cuda())
timeend = time.time()
print("\nAdversarial Example Found Successfully: distortion %.4f target"
" %d queries %d \nTime: %.4f seconds" % (gg, target, query_count, timeend-timestart))
self.log[i+1:,0] = gg
self.log[i+1:,1] = query_count
#print(self.log)
#print("Distortions: ", distortions)
return x0 + torch.tensor(gg*xg, dtype=torch.float).cuda()
def sign_grad_v1(self, x0, y0, theta, initial_lbd, h=0.001, D=4, target=None):
"""
Evaluate the sign of gradient by formulat
sign(g) = 1/Q [ \sum_{q=1}^Q sign( g(theta+h*u_i) - g(theta) )u_i$ ]
"""
K = self.k
sign_grad = np.zeros(theta.shape)
queries = 0
### USe orthogonal transform
#dim = np.prod(sign_grad.shape)
#H = np.random.randn(dim, K)
#Q, R = qr(H, mode='economic')
preds = []
for iii in range(K):
# # Code for reduced dimension gradient
# u = np.random.randn(N_d,N_d)
# u = u.repeat(D, axis=0).repeat(D, axis=1)
# u /= LA.norm(u)
# u = u.reshape([1,1,N,N])
u = np.random.randn(*theta.shape)
#u = Q[:,iii].reshape(sign_grad.shape)
u /= LA.norm(u)
sign = 1
new_theta = theta + h*u
new_theta /= LA.norm(new_theta)
# Targeted case.
if (target is not None and
self.model.predict_label(x0+torch.tensor(initial_lbd*new_theta, dtype=torch.float).cuda()) == target):
sign = -1
# Untargeted case
preds.append(self.model.predict_label(x0+torch.tensor(initial_lbd*new_theta, dtype=torch.float).cuda()).item())
if (target is None and
self.model.predict_label(x0+torch.tensor(initial_lbd*new_theta, dtype=torch.float).cuda()) != y0):
sign = -1
queries += 1
sign_grad += u*sign
sign_grad /= K
# sign_grad_u = sign_grad/LA.norm(sign_grad)
# new_theta = theta + h*sign_grad_u
# new_theta /= LA.norm(new_theta)
# fxph, q1 = self.fine_grained_binary_search_local(self.model, x0, y0, new_theta, initial_lbd=initial_lbd, tol=h/500)
# delta = (fxph - initial_lbd)/h
# queries += q1
# sign_grad *= 0.5*delta
return sign_grad, queries
def sign_grad_v2(self, x0, y0, theta, initial_lbd, h=0.001, K=200):
"""
Evaluate the sign of gradient by formulat
sign(g) = 1/Q [ \sum_{q=1}^Q sign( g(theta+h*u_i) - g(theta) )u_i$ ]
"""
sign_grad = np.zeros(theta.shape)
queries = 0
for _ in range(K):
u = np.random.randn(*theta.shape)
u /= LA.norm(u)
ss = -1
new_theta = theta + h*u
new_theta /= LA.norm(new_theta)
if self.model.predict_label(x0+torch.tensor(initial_lbd*new_theta, dtype=torch.float).cuda()) == y0:
ss = 1
queries += 1
sign_grad += sign(u)*ss
sign_grad /= K
return sign_grad, queries
def sign_grad_svm(self, x0, y0, theta, initial_lbd, h=0.001, K=100, lr=5.0, target=None):
"""
Evaluate the sign of gradient by formulat
sign(g) = 1/Q [ \sum_{q=1}^Q sign( g(theta+h*u_i) - g(theta) )u_i$ ]
"""
sign_grad = np.zeros(theta.shape)
queries = 0
dim = np.prod(theta.shape)
X = np.zeros((dim, K))
for iii in range(K):
u = np.random.randn(*theta.shape)
u /= LA.norm(u)
sign = 1
new_theta = theta + h*u
new_theta /= LA.norm(new_theta)
# Targeted case.
if (target is not None and
self.model.predict_label(x0+torch.tensor(initial_lbd*new_theta, dtype=torch.float).cuda()) == target):
sign = -1
# Untargeted case
if (target is None and
self.model.predict_label(x0+torch.tensor(initial_lbd*new_theta, dtype=torch.float).cuda()) != y0):
sign = -1
#if self.model.predict_label(x0+torch.tensor((initial_lbd*1.00001)*new_theta, dtype=torch.float).cuda()) == y0:
# print "Yes"
#else:
# print "No"
queries += 1
X[:,iii] = sign*u.reshape((dim,))
Q = X.transpose().dot(X)
q = -1*np.ones((K,))
G = np.diag(-1*np.ones((K,)))
h = np.zeros((K,))
### Use quad_qp solver
#alpha = solve_qp(Q, q, G, h)
### Use coordinate descent solver written by myself, avoid non-positive definite cases
alpha = quad_solver(Q, q)
sign_grad = (X.dot(alpha)).reshape(theta.shape)
return sign_grad, queries
def fine_grained_binary_search_local(self, model, x0, y0, theta, initial_lbd = 1.0, tol=1e-5):
nquery = 0
lbd = initial_lbd
if model.predict_label(x0+torch.tensor(lbd*theta, dtype=torch.float).cuda()) == y0:
lbd_lo = lbd
lbd_hi = lbd*1.01
nquery += 1
while model.predict_label(x0+torch.tensor(lbd_hi*theta, dtype=torch.float).cuda()) == y0:
lbd_hi = lbd_hi*1.01
nquery += 1
if lbd_hi > 20:
return float('inf'), nquery
else:
lbd_hi = lbd
lbd_lo = lbd*0.99
nquery += 1
while model.predict_label(x0+torch.tensor(lbd_lo*theta, dtype=torch.float).cuda()) != y0 :
lbd_lo = lbd_lo*0.99
nquery += 1
while (lbd_hi - lbd_lo) > tol:
lbd_mid = (lbd_lo + lbd_hi)/2.0
nquery += 1
if model.predict_label(x0 + torch.tensor(lbd_mid*theta, dtype=torch.float).cuda()) != y0:
lbd_hi = lbd_mid
else:
lbd_lo = lbd_mid
return lbd_hi, nquery
def fine_grained_binary_search(self, model, x0, y0, theta, initial_lbd, current_best):
nquery = 0
if initial_lbd > current_best:
if model.predict_label(x0+torch.tensor(current_best*theta, dtype=torch.float).cuda()) == y0:
nquery += 1
return float('inf'), nquery
lbd = current_best
else:
lbd = initial_lbd
lbd_hi = lbd
lbd_lo = 0.0
while (lbd_hi - lbd_lo) > 1e-5:
lbd_mid = (lbd_lo + lbd_hi)/2.0
nquery += 1
if model.predict_label(x0 + torch.tensor(lbd_mid*theta, dtype=torch.float).cuda()) != y0:
lbd_hi = lbd_mid
else:
lbd_lo = lbd_mid
return lbd_hi, nquery
def eval_grad(self, model, x0, y0, theta, initial_lbd, tol=1e-5, h=0.001, sign=False):
# print("Finding gradient")
fx = initial_lbd # evaluate function value at original point
grad = np.zeros_like(theta)
x = theta
# iterate over all indexes in x
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
queries = 0
while not it.finished:
# evaluate function at x+h
ix = it.multi_index
oldval = x[ix]
x[ix] = oldval + h # increment by h
unit_x = x / LA.norm(x)
if sign:
if model.predict_label(x0+torch.tensor(initial_lbd*unit_x, dtype=torch.float).cuda()) == y0:
g = 1
else:
g = -1
q1 = 1
else:
fxph, q1 = self.fine_grained_binary_search_local(model, x0, y0, unit_x, initial_lbd = initial_lbd, tol=h/500)
g = (fxph - fx) / (h)
queries += q1
# x[ix] = oldval - h
# fxmh, q2 = self.fine_grained_binary_search_local(model, x0, y0, x, initial_lbd = initial_lbd, tol=h/500)
x[ix] = oldval # restore
# compute the partial derivative with centered formula
grad[ix] = g
it.iternext() # step to next dimension
# print("Found gradient")
return grad, queries
def attack_targeted(self, x0, y0, target, alpha = 0.2, beta = 0.001, iterations = 5000, query_limit=40000,
distortion=None, seed=None, svm=False, stopping=0.0001):
""" Attack the original image and return adversarial example
model: (pytorch model)
train_dataset: set of training data
(x0, y0): original image
"""
model = self.model
y0 = y0[0]
print("Targeted attack - Source: {0} and Target: {1}".format(y0, target.item()))
if (model.predict_label(x0) == target):
print("Image already target. No need to attack.")
return x0, 0.0
if self.train_dataset is None:
print("Need training dataset for initial theta.")
return x0, 0.0
if seed is not None:
np.random.seed(seed)
num_samples = 100
best_theta, g_theta = None, float('inf')
query_count = 0
ls_total = 0
sample_count = 0
print("Searching for the initial direction on %d samples: " % (num_samples))
timestart = time.time()
# samples = set(random.sample(range(len(self.train_dataset)), num_samples))
# print(samples)
# train_dataset = self.train_dataset[samples]
# Iterate through training dataset. Find best initial point for gradient descent.
for i, (xi, yi) in enumerate(self.train_dataset):
yi_pred = model.predict_label(xi.cuda())
query_count += 1
if yi_pred != target:
continue
theta = xi.cpu().numpy() - x0.cpu().numpy()
initial_lbd = LA.norm(theta)
theta /= initial_lbd
lbd, count = self.fine_grained_binary_search_targeted(model, x0, y0, target, theta, initial_lbd, g_theta)
query_count += count
if lbd < g_theta:
best_theta, g_theta = theta, lbd
print("--------> Found distortion %.4f" % g_theta)
sample_count += 1
if sample_count >= num_samples:
break
if i > 500:
break
# xi = initial_xi
# xi = xi.numpy()
# theta = xi - x0
# initial_lbd = LA.norm(theta.flatten(),np.inf)
# theta /= initial_lbd # might have problem on the defination of direction
# lbd, count, lbd_g2 = self.fine_grained_binary_search_local_targeted(model, x0, y0, target, theta)
# query_count += count
# if lbd < g_theta:
# best_theta, g_theta = theta, lbd
# print("--------> Found distortion %.4f" % g_theta)
timeend = time.time()
if g_theta == np.inf:
return x0, float('inf')
print("==========> Found best distortion %.4f in %.4f seconds using %d queries" %
(g_theta, timeend-timestart, query_count))
# Begin Gradient Descent.
timestart = time.time()
xg, gg = best_theta, g_theta
learning_rate = start_learning_rate
prev_obj = 100000
distortions = [gg]
for i in range(iterations):
if svm == True:
sign_gradient, grad_queries = self.sign_grad_svm(x0, y0, xg, initial_lbd=gg, h=beta, target=target)
else:
sign_gradient, grad_queries = self.sign_grad_v1(x0, y0, xg, initial_lbd=gg, h=beta, target=target)
if False:
# Compare cosine distance with numerical gradient.
gradient, _ = self.eval_grad(model, x0, y0, xg, initial_lbd=gg, tol=beta/500, h=0.01)
print(" Numerical - Sign gradient cosine distance: ",
scipy.spatial.distance.cosine(gradient.flatten(), sign_gradient.flatten()))
# Line search
ls_count = 0
min_theta = xg
min_g2 = gg
for _ in range(15):
new_theta = xg - alpha * sign_gradient
new_theta /= LA.norm(new_theta)
new_g2, count = self.fine_grained_binary_search_local_targeted(
model, x0, y0, target, new_theta, initial_lbd = min_g2, tol=beta/500)
ls_count += count
alpha = alpha * 2
if new_g2 < min_g2:
min_theta = new_theta
min_g2 = new_g2
else:
break
if min_g2 >= gg:
for _ in range(15):
alpha = alpha * 0.25
new_theta = xg - alpha * sign_gradient
new_theta /= LA.norm(new_theta)
new_g2, count = self.fine_grained_binary_search_local_targeted(
model, x0, y0, target, new_theta, initial_lbd = min_g2, tol=beta/500)
ls_count += count
if new_g2 < gg:
min_theta = new_theta
min_g2 = new_g2
break
if alpha < 1e-4:
alpha = 1.0
print("Warning: not moving")
beta = beta*0.1
if (beta < 1e-8):
break
xg, gg = min_theta, min_g2
query_count += (grad_queries + ls_count)
ls_total += ls_count
distortions.append(gg)
if query_count > query_limit:
break
if i%5==0:
print("Iteration %3d distortion %.4f num_queries %d" % (i+1, gg, query_count))
# print("Iteration: ", i, " Distortion: ", gg, " Queries: ", query_count,
# " LR: ", alpha, "grad_queries", grad_queries, "ls_queries", ls_count)
#if distortion is not None and gg < distortion:
# print("Success: required distortion reached")
# break
# if gg > prev_obj-stopping:
# print("Success: stopping threshold reached")
# break
# prev_obj = gg
adv_target = model.predict_label(x0 + torch.tensor(gg*xg, dtype=torch.float).cuda())
if (adv_target == target):
timeend = time.time()
print("\nAdversarial Example Found Successfully: distortion %.4f target"
" %d queries %d LS queries %d \nTime: %.4f seconds" % (gg, target, query_count, ls_total, timeend-timestart))
return x0 + torch.tensor(gg*xg, dtype=torch.float).cuda()
else:
print("Failed to find targeted adversarial example.")
return x0
def fine_grained_binary_search_local_targeted(self, model, x0, y0, t, theta, initial_lbd=1.0, tol=1e-5):
nquery = 0
lbd = initial_lbd
if model.predict_label(x0 + torch.tensor(lbd*theta, dtype=torch.float).cuda()) != t:
lbd_lo = lbd
lbd_hi = lbd*1.01
nquery += 1
while model.predict_label(x0 + torch.tensor(lbd_hi*theta, dtype=torch.float).cuda()) != t:
lbd_hi = lbd_hi*1.01
nquery += 1
if lbd_hi > 100:
return float('inf'), nquery
else:
lbd_hi = lbd
lbd_lo = lbd*0.99
nquery += 1
while model.predict_label(x0 + torch.tensor(lbd_lo*theta, dtype=torch.float).cuda()) == t:
lbd_lo = lbd_lo*0.99
nquery += 1
while (lbd_hi - lbd_lo) > tol:
lbd_mid = (lbd_lo + lbd_hi)/2.0
nquery += 1
if model.predict_label(x0 + torch.tensor(lbd_mid*theta, dtype=torch.float).cuda()) == t:
lbd_hi = lbd_mid
else:
lbd_lo = lbd_mid
# temp_theta = np.abs(lbd_hi*theta)
# temp_theta = np.clip(temp_theta - 0.15, 0.0, None)
# loss = np.sum(np.square(temp_theta))
return lbd_hi, nquery
def fine_grained_binary_search_targeted(self, model, x0, y0, t, theta, initial_lbd, current_best):
nquery = 0
if initial_lbd > current_best:
if model.predict_label(x0 + torch.tensor(current_best*theta, dtype=torch.float).cuda()) != t:
nquery += 1
return float('inf'), nquery
lbd = current_best
else:
lbd = initial_lbd
lbd_hi = lbd
lbd_lo = 0.0
while (lbd_hi - lbd_lo) > 1e-5:
lbd_mid = (lbd_lo + lbd_hi)/2.0
nquery += 1
if model.predict_label(x0 + torch.tensor(lbd_mid*theta, dtype=torch.float).cuda()) != t:
lbd_lo = lbd_mid
else:
lbd_hi = lbd_mid
return lbd_hi, nquery
def __call__(self, input_xi, label_or_target, target=None, distortion=None, seed=None,
svm=False, query_limit=40000, momentum=0.0, stopping=0.0001, TARGETED=False, epsilon=None):
if target is not None:
adv = self.attack_targeted(input_xi, label_or_target, target, distortion=distortion,
seed=seed, svm=svm, query_limit=query_limit, stopping=stopping)
else:
adv = self.attack_untargeted(input_xi, label_or_target, distortion=distortion,
seed=seed, svm=svm, query_limit=query_limit, momentum=momentum,
stopping=stopping)
return adv