-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo_attack.py
707 lines (539 loc) · 24.1 KB
/
demo_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
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
import time
from parsers import BaseParser
import os
from datasets import BaseDataset
import torchvision.transforms as transforms
import torch
from torch.utils.data import DataLoader
from torch.autograd import Variable
import numpy as np
from torchvision.transforms import ToPILImage
import torch.backends.cudnn as cudnn
import inception_v3
cudnn.benchmark = True
def after_attack(x, net, original_img, original_class, target_class, post, loss_type, tar, iters, val_w1, val_w2, max_epsilon):
if post == 1:
s1 = 1e-3
s2 = 1e-4
max_iter = 40000
else:
s1 = val_w2
s2 = val_w1
max_iter = iters
mask = torch.where(torch.abs(x.data) > 0, torch.ones(1).cuda(), torch.zeros(1).cuda())
logist = net(x.data+original_img.data)
_,target=torch.max(logist,1)
pre_x = x.data
for i in range(max_iter):
temp = Variable(x.data, requires_grad=True)
logist = net(temp + original_img.data)
if tar == 1:
if loss_type == 'ce':
ce = torch.nn.CrossEntropyLoss()
Loss = ce(logist,torch.ones(1).long().cuda()*target_class)
elif loss_type == 'cw':
Loss = CWLoss(logist, torch.ones(1).long().cuda()*target_class, kappa=0, tar = True)
else:
Loss = CWLoss(logist, torch.ones(1).long().cuda()*target_class, kappa=0, tar = False)
net.zero_grad()
if temp.grad is not None:
temp.grad.data.fill_(0)
Loss.backward()
grad = temp.grad
temp2 = Variable(x.data, requires_grad=True)
Loss2 = torch.norm(temp2, p=float("inf"))
net.zero_grad()
if temp2.grad is not None:
temp2.grad.data.fill_(0)
Loss2.backward()
grad2 = temp2.grad
pre_x = x.data
pre_noise = temp2.data
if post == 0:
temp2 = temp2.data - s1*grad2.data*mask - s2*grad.data*mask
else:
temp2 = temp2.data - s1*grad2.data*mask
thres = max_epsilon
temp2 = torch.clamp(temp2.data, -thres, thres)
temp2 = torch.clamp(original_img.data+temp2.data, 0, 1)
x = temp2.data - original_img.data
logist = net(x.data + original_img.data)
_,pre=torch.max(logist,1)
if(post == 1):
if tar == 1:
if(pre.item() != target_class):
success = 1
return pre_x
break
else:
if(pre.item() == target_class):
success = 1
return pre_x
break
return x
def F(x, loss_type, net, lambda1, original_img, target_class, tar):
temp = Variable(x.data, requires_grad=False)
logist = net(temp+original_img.data)
if tar == 1:
if loss_type == 'ce':
ce = torch.nn.CrossEntropyLoss()
Loss = ce(logist,torch.ones(1).long().cuda()*target_class)
elif loss_type == 'cw':
Loss = CWLoss(logist, torch.ones(1).long().cuda()*target_class, kappa=0, tar = True)
else:
Loss = CWLoss(logist, torch.ones(1).long().cuda()*target_class, kappa=0, tar = False)
res = Loss.item() + lambda1*torch.norm(x.data,0).item()
net.zero_grad()
return res
def prox_pixel(x, alpha, lambda1, original_img, max_epsilon):
temp_x = x.data * torch.ones(x.shape).cuda()
thres = max_epsilon
clamp_x = torch.clamp(temp_x.data, -thres, thres)
temp_img = original_img.data + clamp_x.data
temp_img = torch.clamp(temp_img.data, 0, 1)
clamp_x = temp_img.data - original_img.data
val = 1 / (2*alpha*lambda1)
cond = 1 + val * (clamp_x-temp_x)*(clamp_x-temp_x) > val * temp_x*temp_x
cond = cond.cuda()
res = torch.zeros(x.shape).cuda()
res = torch.where(cond, res, clamp_x.data)
return res
def nmAPG(x0, loss_type, net, eta, delta, rho, original_img, lambda1, search_lambda_inc, search_lambda_dec, target_class, original_class, tar, max_update, maxiter, max_epsilon):
x0_norm0 = torch.norm(torch.ones(x0.shape).cuda()*x0.data,0).item()
max_update = max_update
temp = Variable(x0.data, requires_grad=False)
logist = net(temp+original_img.data)
if tar == 1:
if loss_type == 'ce':
ce = torch.nn.CrossEntropyLoss()
Loss = ce(logist, torch.ones(1).long().cuda()*target_class)
elif loss_type == 'cw':
Loss = CWLoss(logist, torch.ones(1).long().cuda()*target_class, kappa=0, tar=True)
else:
Loss = CWLoss(logist, torch.ones(1).long().cuda()*target_class, kappa=0, tar=False)
net.zero_grad()
z = x0
y_pre = torch.zeros(original_img.shape).cuda()
pre_loss = 0
cur_loss = 0
counter = 0
success = 0
alpha_y = 1e-3
alpha_x = 1e-3
alpha_min = 1e-20
alpha_max = 1e20
x_pre = x0
x = x0
t = 1
t_pre = 0
c = Loss + lambda1*torch.norm(x.data,0)
q = 1
k = 0
while True:
y = x + t_pre/t*(z-x) + (t_pre-1)/t*(x-x_pre)
if k > 0:
s = y - y_pre.data
#gradient of yk
temp_y = Variable(y.data, requires_grad=True)
logist_y = net(temp_y+original_img.data)
if tar == 1:
if loss_type == 'ce':
ce = torch.nn.CrossEntropyLoss()
Loss_y = ce(logist_y, torch.ones(1).long().cuda()*target_class)
elif loss_type == 'cw':
Loss_y = CWLoss(logist_y, torch.ones(1).long().cuda()*target_class, kappa=0, tar = True)
else:
Loss_y = CWLoss(logist_y, torch.ones(1).long().cuda()*target_class, kappa=0, tar = False)
net.zero_grad()
if temp_y.grad is not None:
temp_y.grad.data.fill_(0)
Loss_y.backward()
grad_y = temp_y.grad
#gradient of yk-1
temp_y_pre = Variable(y_pre.data, requires_grad=True)
logist_y_pre = net(temp_y_pre+original_img.data)
if tar == 1:
if loss_type == 'ce':
ce = torch.nn.CrossEntropyLoss()
Loss_y_pre = ce(logist_y_pre, torch.ones(1).long().cuda()*target_class)
elif loss_type == 'cw':
Loss_y_pre = CWLoss(logist_y_pre, torch.ones(1).long().cuda()*target_class, kappa=0, tar = True)
else:
Loss_y_pre = CWLoss(logist_y_pre, torch.ones(1).long().cuda()*target_class, kappa=0, tar = False)
net.zero_grad()
if temp_y_pre.grad is not None:
temp_y_pre.grad.data.fill_(0)
Loss_y_pre.backward()
grad_y_pre = temp_y_pre.grad
r = grad_y - grad_y_pre
#prevent error caused by numerical inaccuracy
if torch.norm(s,1) < 1e-5:
s = torch.ones(1).cuda()*1e-5
if torch.norm(r,1) < 1e-10:
r = torch.ones(1).cuda()*1e-10
alpha_y = torch.sum(s*r)/torch.sum(r*r)
alpha_y = alpha_y.item()
temp_alpha = alpha_y
if temp_alpha < alpha_min:
temp_alpha = alpha_min
if temp_alpha > alpha_max:
temp_alpha = alpha_max
if np.isnan(temp_alpha):
temp_alpha = alpha_min
alpha_y = temp_alpha
count1 = 0
while True:
count1 = count1 + 1
if count1 > 1000:
break
temp_y = Variable(y.data, requires_grad=True)
logist_y = net(temp_y+original_img.data)
if tar == 1:
if loss_type == 'ce':
ce = torch.nn.CrossEntropyLoss()
Loss_y = ce(logist_y,torch.ones(1).long().cuda()*target_class)
elif loss_type == 'cw':
Loss_y = CWLoss(logist_y, torch.ones(1).long().cuda()*target_class, kappa=0, tar = True)
else:
Loss_y = CWLoss(logist_y, torch.ones(1).long().cuda()*target_class, kappa=0, tar = False)
net.zero_grad()
if temp_y.grad is not None:
temp_y.grad.data.fill_(0)
Loss_y.backward()
grad_y = temp_y.grad
z = prox_pixel(x=y-alpha_y*grad_y,alpha=alpha_y,lambda1=lambda1,original_img=original_img, max_epsilon=max_epsilon)
#increase lambda
if(search_lambda_inc == 1):
if(torch.norm(z,1) != 0):
return 0
else:
return 1
#decrease lambda
if(search_lambda_dec == 1):
if(torch.norm(z,1) == 0):
return 0
else:
return lambda1
alpha_y = alpha_y * rho
cond1 = F(z, loss_type, net, lambda1, original_img,target_class,tar) <= F(y, loss_type, net, lambda1, original_img,target_class,tar) - delta*(torch.norm(z-y,2)*torch.norm(z-y,2))
cond2 = F(z, loss_type, net, lambda1, original_img,target_class,tar) <= c - delta*(torch.norm(z-y,2)*torch.norm(z-y,2))
if(cond1 | cond2):
break
if F(z, loss_type, net, lambda1, original_img,target_class,tar) <= c - delta*(torch.norm(z-y,2)*torch.norm(z-y,2)):
x_pre = x
temp_norm0 = torch.norm(torch.ones(z.shape).cuda()*z.data,0).item()
if np.abs(temp_norm0 - x0_norm0) > max_update:
temp_z = torch.abs((torch.ones(z.shape).cuda()*z.data).reshape(1,-1))
val, idx = temp_z.topk(k=int(x0_norm0+max_update))
thres = val[0,int(x0_norm0+max_update-1)]
z = torch.where(torch.abs(z.data) < thres, torch.zeros(1).cuda(), z.data)
x = z
else:
x = z
else:
if k > 0:
s = x - y_pre.data
temp_x = Variable(x.data, requires_grad=True)
logist_x = net(temp_x+original_img.data)
if tar == 1:
if loss_type == 'ce':
ce = torch.nn.CrossEntropyLoss()
Loss_x = ce(logist_x,torch.ones(1).long().cuda()*target_class)
elif loss_type == 'cw':
Loss_x = CWLoss(logist_x, torch.ones(1).long().cuda()*target_class, kappa=0, tar = True)
else:
Loss_x = CWLoss(logist_x, torch.ones(1).long().cuda()*target_class, kappa=0, tar = False)
net.zero_grad()
if temp_x.grad is not None:
temp_x.grad.data.fill_(0)
Loss_x.backward()
grad_x = temp_x.grad
temp_y_pre = Variable(y_pre.data, requires_grad=True)
logist_y_pre = net(temp_y_pre+original_img.data)
if tar == 1:
if loss_type == 'ce':
ce = torch.nn.CrossEntropyLoss()
Loss_y_pre = ce(logist_y_pre,torch.ones(1).long().cuda()*target_class)
elif loss_type == 'cw':
Loss_y_pre = CWLoss(logist_y_pre, torch.ones(1).long().cuda()*target_class, kappa=0, tar = True)
else:
Loss_y_pre = CWLoss(logist_y_pre, torch.ones(1).long().cuda()*target_class, kappa=0, tar = False)
net.zero_grad()
if temp_y_pre.grad is not None:
temp_y_pre.grad.data.fill_(0)
Loss_y_pre.backward()
grad_y_pre = temp_y_pre.grad
r = grad_x - grad_y_pre
if torch.norm(s, 1) < 1e-5:
s = torch.ones(1).cuda() * 1e-5
if torch.norm(r,1) < 1e-10:
r = torch.ones(1).cuda()*1e-10
alpha_x = torch.sum(s*r)/torch.sum(r*r)
alpha_x = alpha_x.item()
temp_alpha = alpha_x
if temp_alpha < alpha_min:
temp_alpha = alpha_min
if temp_alpha > alpha_max:
temp_alpha = alpha_max
if np.isnan(temp_alpha):
temp_alpha = alpha_min
alpha_x = temp_alpha
count2 = 0
while True:
count2 = count2 + 1
if count2 > 10:
break
temp_x = Variable(x.data, requires_grad=True)
logist_x = net(temp_x + original_img.data)
if tar == 1:
if loss_type == 'ce':
ce = torch.nn.CrossEntropyLoss()
Loss_x = ce(logist_x, torch.ones(1).long().cuda() * target_class)
elif loss_type == 'cw':
Loss_x = CWLoss(logist_x, torch.ones(1).long().cuda() * target_class, kappa=0, tar=True)
else:
Loss_x = CWLoss(logist_x, torch.ones(1).long().cuda() * target_class, kappa=0, tar=False)
net.zero_grad()
if temp_x.grad is not None:
temp_x.grad.data.fill_(0)
Loss_x.backward()
grad_x = temp_x.grad
v = prox_pixel(x=x-alpha_x*grad_x,alpha=alpha_x,lambda1=lambda1,original_img=original_img, max_epsilon=max_epsilon)
alpha_x = rho * alpha_x
cond3 = F(v, loss_type, net, lambda1, original_img,target_class,tar) <= c - delta*(torch.norm(v-x,2)*torch.norm(v-x,2))
if cond3:
break
if torch.abs(F(v, loss_type, net, lambda1, original_img,target_class,tar) - (c - delta*(torch.norm(v-x,2)*torch.norm(v-x,2)) )) < 1e-3:
break
if F(z, loss_type, net, lambda1, original_img,target_class,tar) <= F(v, loss_type, net, lambda1, original_img,target_class,tar):
x_pre = x
temp_norm0 = torch.norm(torch.ones(z.shape).cuda() * z.data, 0).item()
if np.abs(temp_norm0 - x0_norm0) > max_update:
temp_z = torch.abs((torch.ones(z.shape).cuda() * z.data).reshape(1, -1))
val, idx = temp_z.topk(k=int(x0_norm0 + max_update))
thres = val[0, int(x0_norm0 + max_update - 1)]
z = torch.where(torch.abs(z.data) < thres, torch.zeros(1).cuda(), z.data)
x = z
else:
x = z
else:
x_pre = x
temp_norm0 = torch.norm(torch.ones(v.shape).cuda() * v.data, 0).item()
if np.abs(temp_norm0 - x0_norm0) > max_update:
temp_v = torch.abs((torch.ones(v.shape).cuda() * v.data).reshape(1, -1))
val, idx = temp_v.topk(k=int(x0_norm0 + max_update))
thres = val[0, int(x0_norm0 + max_update - 1)]
v = torch.where(torch.abs(v.data) < thres, torch.zeros(1).cuda(), v.data)
x = v
else:
x = v
thres = max_epsilon
x = torch.clamp(x.data,-thres,thres)
temp_img = original_img.data + x.data
temp_img = torch.clamp(temp_img.data, 0, 1)
x = temp_img.data - original_img.data
y_pre = y.data
t = (np.sqrt(4*t*t+1)+1)/2
q = eta*q + 1
c = (eta*q*c + F(x, loss_type, net, lambda1, original_img,target_class,tar))/q
logist = net(x.data+original_img.data)
_,target=torch.max(logist,1)
k = k + 1
pre_loss = cur_loss
if tar == 0:
cur_loss = CWLoss(logist.data, torch.ones(1).long().cuda()*target_class, kappa=0, tar = False).item()
else:
if loss_type == 'cw':
cur_loss = CWLoss(logist.data, torch.ones(1).long().cuda()*target_class, kappa=0, tar = True).item()
else:
ce = torch.nn.CrossEntropyLoss()
cur_loss = ce(logist.data, torch.ones(1).long().cuda() * target_class).item()
net.zero_grad()
#success
if tar == 1:
if(target == target_class):
success = 1
break
else:
if((target != target_class)):
success = 1
break
if ((success == 0) and (k >= maxiter) and (np.abs(pre_loss-cur_loss) < 1e-3) and (counter==1)):
break
if((k >= maxiter) and (np.abs(pre_loss-cur_loss) < 1e-3)):
counter = 1
return x, success
def search_lambda(loss_type, net, original_img, target_class, original_class, tar, val_c, max_update, maxiter, max_epsilon):
lambda1 = 1e-6
x0 = torch.zeros(original_img.shape).cuda()
k1 = 0
while True:
k1 = k1 + 1
temp = nmAPG(x0=x0, loss_type=loss_type, net=net, eta=0.9, delta=0.3, rho=0.8, original_img=original_img, lambda1=lambda1,
search_lambda_inc=1, search_lambda_dec=0, target_class=target_class, original_class=original_class, tar=tar, max_update=max_update, maxiter=maxiter, max_epsilon=max_epsilon)
if temp == 0:
lambda1 = lambda1 + 1e-6
if temp == 1:
break
k2 = 0
while True:
k2 = k2 + 1
temp = nmAPG(x0=x0, loss_type=loss_type, net=net, eta=0.9, delta=0.3, rho=0.8, original_img=original_img, lambda1=lambda1,
search_lambda_inc=0, search_lambda_dec=1, target_class=target_class, original_class=original_class, tar=tar,max_update=max_update, maxiter=maxiter, max_epsilon=max_epsilon)
if temp == 0:
lambda1 = lambda1 * 0.99
else:
break
lambda1 = lambda1 * val_c
print('attack lambda = ', lambda1)
return lambda1
def homotopy(loss_type, net, original_img, target_class, original_class, tar, max_epsilon, dec_factor, val_c, val_w1, val_w2, max_update, maxiter, val_gamma):
lambda1 = search_lambda(loss_type, net, original_img, target_class, original_class,tar, val_c, max_update, maxiter, max_epsilon=max_epsilon)
x = torch.zeros(original_img.shape).cuda()
pre_norm0 = 0
cur_norm0 = 0
max_norm0 = torch.norm(torch.ones(x.shape).cuda(),0).item()
outer_iter = 0
val_max_update = max_update
while True:
outer_iter = outer_iter + 1
x, success = nmAPG(x0=x, loss_type=loss_type, net=net, eta=0.9, delta=0.3, rho=0.8, original_img=original_img, lambda1=lambda1,
search_lambda_inc=0, search_lambda_dec=0, target_class=target_class, original_class=original_class, tar=tar, max_update=max_update, maxiter=maxiter, max_epsilon=max_epsilon)
max_update = val_max_update
pre_norm0 = cur_norm0
cur_norm0 = torch.norm(torch.ones(x.shape).cuda()*x.data,0).item()
cur_norm1 = torch.norm(torch.ones(x.shape).cuda() * x.data, 1).item()
#attack fail
if(cur_norm0 > max_norm0*0.95 and outer_iter*max_update > max_norm0*0.95):
break
iters = 0
if (cur_norm1 <= cur_norm0 * max_epsilon * val_gamma):
max_update = 10
iters = 200
if cur_norm0 >= 500:
iters = 400
if cur_norm0 >= 1000:
iters = 600
if cur_norm0 >= 1500:
iters = 800
if cur_norm0 >= 2000:
iters = 1000
if cur_norm0 >= 2500:
iters = 1200
if success == 0:
x = after_attack(x, net, original_img, original_class, target_class, post=0, loss_type=loss_type, tar=tar, iters=iters, val_w1=val_w1, val_w2=val_w2, max_epsilon=max_epsilon)
lambda1 = dec_factor * lambda1
else:
break
logi = net(x.data+original_img.data)
_,cur_class=torch.max(logi,1)
if tar == 1:
if((cur_class == target_class)):
break
else:
if((cur_class != target_class)):
break
x = after_attack(x, net, original_img, original_class, target_class, post=1, loss_type=loss_type,tar=tar, iters=iters, val_w1=val_w1, val_w2=val_w2, max_epsilon=max_epsilon)
return x
def main():
args = BaseParser().parse()
#model loading
net = inception_v3.inception_v3(pretrained=False)
net.load_state_dict(torch.load('./pretrain/inception_v3_google-1a9a5a14.pth'))
net.eval()
net.cuda()
test_dataset = BaseDataset(
args.imgdir,
transform=transforms.Compose([
transforms.Resize((299, 299)),
transforms.ToTensor()
]),
)
test_loader = DataLoader(test_dataset, batch_size=args.batchSize, shuffle=False)
root = os.path.join('./test/', args.name)
counter = 0
sum_l1 = 0
sum_l2 = 0
sum_l_inf = 0
sum_l0 = 0
total_start_time = time.time()
num_success = 0
for idx, data in enumerate(test_loader):
input = data['img']
input = input.cuda()
ori_image = Variable(input,requires_grad = False)
image_names = data['name']
print(idx)
print(image_names)
if ori_image.shape[0] != 1 or ori_image.shape[1] != 3 or ori_image.shape[2] != 299 or ori_image.shape[3] != 299:
continue
logist = net(ori_image)
_,target=torch.max(logist,1)
print('original class = ', target)
original_class = target.item()
'''
if(original_class != labels[idx]):
continue
'''
print('target = ', args.target_class)
if original_class == args.target_class:
continue
counter = counter + 1
iter_start_time = time.time()
adv = homotopy(loss_type='ce', net=net, original_img=ori_image, target_class=args.target_class, original_class=original_class, tar=1, max_epsilon=args.max_epsilon,
dec_factor=args.dec_factor, val_c=args.val_c, val_w1=args.val_w1, val_w2=args.val_w2, max_update=args.max_update, maxiter=args.maxiter, val_gamma=args.val_gamma)
end_time = time.time()
print('total time = ', end_time-iter_start_time)
adv = adv.data+ori_image.data
logist = net(adv.data)
_,target=torch.max(logist,1)
print('after attack class = ',target)
noise = adv.data - ori_image.data
cur_l0 = int(torch.norm(noise.data, 0).item())
cur_l1 = float(torch.norm(noise.data, 1).item())
cur_l2 = float(torch.norm(noise.data, 2).item())
cur_l_inf = float(torch.norm(noise.data, p=float("inf")).item())
if cur_l0 > 0 and args.target_class == target.item():
num_success = num_success + 1
sum_l1 = sum_l1 + cur_l1
sum_l2 = sum_l2 + cur_l2
sum_l_inf = sum_l_inf + cur_l_inf
sum_l0 = sum_l0 + cur_l0
print('L0 = ', cur_l0)
print('L1 = ', cur_l1)
print('L2 = ', cur_l2)
print('L-inf = ', cur_l_inf)
print()
if not os.path.exists(root):
os.makedirs(root)
if not os.path.exists(os.path.join(root,'benign')):
os.makedirs(os.path.join(root,'benign'))
if not os.path.exists(os.path.join(root,'adv')):
os.makedirs(os.path.join(root,'adv'))
for i in range(input.size(0)):
real_img = ToPILImage()(ori_image[i])
adv_path = os.path.join(root, 'adv', image_names[i] + '_' + 'adv' + '.png')
adv_img = transforms.ToPILImage()(adv.cpu()[0]).convert('RGB')
adv_img.save(adv_path)
real_path = os.path.join(root,'benign', image_names[i] +'_' + 'ori' + '.png')
real_img.save(real_path)
print()
print('counter = ', counter)
print('num_success = ', num_success)
print('sum_l0 = ', sum_l0)
print('sum_l1 = ', sum_l1)
print('sum_l2 = ', sum_l2)
print('sum_l_inf = ', sum_l_inf)
print('total time = ', time.time()-total_start_time)
def CWLoss(logits, target, kappa=0, tar = True):
target = torch.ones(logits.size(0)).type(torch.cuda.FloatTensor).mul(target.float())
target_one_hot = Variable(torch.eye(1000).type(torch.cuda.FloatTensor)[target.long()].cuda())
real = torch.sum(target_one_hot*logits, 1)
other = torch.max((1-target_one_hot)*logits - (target_one_hot*10000), 1)[0]
kappa = torch.zeros_like(other).fill_(kappa)
if tar:
return torch.sum(torch.max(other-real, kappa))
else :
return torch.sum(torch.max(real-other, kappa))
if __name__ == '__main__':
main()