forked from anishmadan23/adversarial-attacks-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
targetedBIM.py
74 lines (59 loc) · 2.71 KB
/
targetedBIM.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
# coding: utf-8
# In[1]:
import torch
import torch.nn as nn
import os
import matplotlib.pyplot as plt
import numpy as np
import torch.optim as optim
import torchvision
import torch.nn.functional as F
from Model import get_model
from utils import *
from visualize import visualise
import math
# In[ ]:
class BIM_targeted(object):
def __init__(self,model,criterion,orig_img,orig_label,targetLabel,eps,alpha,num_iters=0,random_state=False):
self.model = model
self.criterion = criterion
self.orig_img = orig_img.clone()
self.eps = eps
self.orig_label = orig_label
self.targetLabel = targetLabel
self.alpha = alpha
self.rand = random_state
self.img_bim = torch.tensor(orig_img.data,requires_grad=True)
if not random_state:
self.num_iters = math.ceil(min(self.eps+4,1.25*self.eps))
else:
self.num_iters=num_iters
# self.num_iters = 3
def attack(self):
if self.rand: # attack changes from BIM to Madry's PGD
delta_init = torch.from_numpy(np.random.uniform(-self.eps,self.eps,self.orig_img.shape)).type(torch.FloatTensor)
self.img_bim = torch.tensor(self.img_bim.data+ delta_init,requires_grad=True)
clipped_delta = torch.clamp(self.img_bim.data-self.orig_img.data, -self.eps,self.eps)
self.img_bim = torch.tensor(self.orig_img.data+clipped_delta,requires_grad=True)
loss_arr = []
output_tr,pred_label,op_probs,pred_prob = getPredictionInfo(self.model,self.orig_img)
# output_tr = self.model(self.orig_img)
# op_probs = F.softmax(output_tr,dim=1)
# pred_prob = ((torch.max(op_probs.data, 1)[0][0]) * 100, 4)
# _,pred_label = torch.max(output_tr.data,1)
# iterative attack
# print('Iters',self.num_iters)
for i in range(self.num_iters):
# print(i)
output = self.model(self.img_bim)
# print(type(output))
# print(type(self.label))
loss = self.criterion(output, self.targetLabel) # compute loss be output and target label
# print(loss)
loss.backward()
delta = self.alpha * torch.sign(self.img_bim.grad.data)
self.img_bim = torch.tensor(self.img_bim.data - delta, requires_grad=True) # adversary without clipping
clipped_delta = torch.clamp(self.img_bim.data-self.orig_img.data, -self.eps,self.eps) #clipping the delta
self.img_bim = torch.tensor(self.orig_img.data-clipped_delta,requires_grad=True) # adding the clipped delta to original image
loss_arr.append(loss)
return self.img_bim, clipped_delta, loss_arr