-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtargetedFGSM.py
47 lines (34 loc) · 3.07 KB
/
targetedFGSM.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
# coding: utf-8
# In[1]:
import torch
import torch.nn.functional as F
from utils import *
# In[ ]:
class FGSM_targeted(object):
def __init__(self,model,criterion,orig_img,orig_label,target_label,eps):
self.model = model
self.criterion = criterion
self.orig_img = orig_img
self.epsilon = eps
self.targetLabel = target_label
self.orig_label = orig_label
def attack(self):
# output = self.model(self.orig_img)
# op_probs = F.softmax(output,dim=1)
# pred_prob = ((torch.max(op_probs.data, 1)[0][0]) * 100, 4)
# _,pred_label = torch.max(output.data,1)
output,pred,op_probs,pred_prob = getPredictionInfo(self.model,self.orig_img)
# print(output.size())
# print(self.orig_label)
loss = self.criterion(output,self.targetLabel)
# print(loss)
loss.backward()
img_grad = torch.sign(self.orig_img.grad.data) # sign of the gradient
adv_img = self.orig_img.data - self.epsilon*img_grad
# output_adv = self.model(torch.tensor(adv_img))
# _,pred_adv = torch.max(output_adv.data,1)
# # print(adv_img.data-img.data)
# op_adv_probs = F.softmax(output_adv, dim=1) #get probability distribution over classes
# adv_pred_prob = ((torch.max(op_adv_probs.data, 1)[0][0]) * 100, 4) #find probability (confidence) of a predicted class
# print(float(adv_pred_prob[0]),float(pred_adv))
return adv_img,img_grad