3.基於梯度的攻擊——PGD


PGD攻擊原論文地址——https://arxiv.org/pdf/1706.06083.pdf

1.PGD攻擊的原理

  PGD(Project Gradient Descent)攻擊是一種迭代攻擊,可以看作是FGSM的翻版——K-FGSM (K表示迭代的次數),大概的思路就是,FGSM是僅僅做一次迭代,走一大步,而PGD是做多次迭代,每次走一小步,每次迭代都會將擾動clip到規定范圍內。

 

 

一般來說,PGD的攻擊效果比FGSM要好,那么原理是什么呢?首先,如果目標模型是一個線性模型,那么用FGSM就可以了,因為此時loss對輸入的導數是固定的,換言之,使得loss下降的方向是明確的,即使你多次迭代,擾動的方向也不會改變。而對於一個非線性模型,僅僅做一次迭代,方向是不一定完全正確的,這也是為什么FGSM的效果一般的原因了。

用畫圖軟件畫了一個很丑的圖,但大致能夠表達我的看法,黑圈是輸入樣本,假設樣本只有兩維,那么樣本可以改變的就有八個方向,坐標系中顯示了loss等高線,以及可以擾動的最大范圍(因為是無窮范數,所以限制范圍是一個方形,負半軸的范圍沒有畫出來),黑圈每一次改變,都是以最優的方向改變,最后一次由於擾動超出了限制,所以直接截斷,如果此時迭代次數沒有用完,那么就在截斷處繼續迭代,直到迭代次數用完。

2.PGD的代碼實現

 

 1 class PGD(nn.Module):
 2     def __init__(self,model):
 3         super().__init__()
 4         self.model=model#必須是pytorch的model
 5         self.device=torch.device("cuda" if (torch.cuda.is_available()) else "cpu")
 6     def generate(self,x,**params):
 7         self.parse_params(**params)
 8         labels=self.y
 9 
10         adv_x=self.attack(x,labels)
11         return adv_x
12     def parse_params(self,eps=0.3,iter_eps=0.01,nb_iter=40,clip_min=0.0,clip_max=1.0,C=0.0,
13                      y=None,ord=np.inf,rand_init=True,flag_target=False):
14         self.eps=eps
15         self.iter_eps=iter_eps
16         self.nb_iter=nb_iter
17         self.clip_min=clip_min
18         self.clip_max=clip_max
19         self.y=y
20         self.ord=ord
21         self.rand_init=rand_init
22         self.model.to(self.device)
23         self.flag_target=flag_target
24         self.C=C
25 
26 
27     def sigle_step_attack(self,x,pertubation,labels):
28         adv_x=x+pertubation
29         # get the gradient of x
30         adv_x=Variable(adv_x)
31         adv_x.requires_grad = True
32         loss_func=nn.CrossEntropyLoss()
33         preds=self.model(adv_x)
34         if self.flag_target:
35             loss =-loss_func(preds,labels)
36         else:
37             loss=loss_func(preds,labels)
38             # label_mask=torch_one_hot(labels)
39             #
40             # correct_logit=torch.mean(torch.sum(label_mask * preds,dim=1))
41             # wrong_logit = torch.mean(torch.max((1 - label_mask) * preds, dim=1)[0])
42             # loss=-F.relu(correct_logit-wrong_logit+self.C)
43 
44         self.model.zero_grad()
45         loss.backward()
46         grad=adv_x.grad.data
47         #get the pertubation of an iter_eps
48         pertubation=self.iter_eps*np.sign(grad)
49         adv_x=adv_x.cpu().detach().numpy()+pertubation.cpu().numpy()
50         x=x.cpu().detach().numpy()
51 
52         pertubation=np.clip(adv_x,self.clip_min,self.clip_max)-x
53         pertubation=clip_pertubation(pertubation,self.ord,self.eps)
54 
55 
56         return pertubation
57     def attack(self,x,labels):
58         labels = labels.to(self.device)
59         print(self.rand_init)
60         if self.rand_init:
61             x_tmp=x+torch.Tensor(np.random.uniform(-self.eps, self.eps, x.shape)).type_as(x).cuda()
62         else:
63             x_tmp=x
64         pertubation=torch.zeros(x.shape).type_as(x).to(self.device)
65         for i in range(self.nb_iter):
66             pertubation=self.sigle_step_attack(x_tmp,pertubation=pertubation,labels=labels)
67             pertubation=torch.Tensor(pertubation).type_as(x).to(self.device)
68         adv_x=x+pertubation
69         adv_x=adv_x.cpu().detach().numpy()
70 
71         adv_x=np.clip(adv_x,self.clip_min,self.clip_max)
72 
73         return adv_x
View Code

 

PGD攻擊的參數並不多,比較重要的就是下面這幾個:

eps: maximum distortion of adversarial example compared to original input

eps_iter: step size for each attack iteration

nb_iter: Number of attack iterations.

我在上面代碼中注釋的這行代碼是CW攻擊的PGD形式,這個在防御論文https://arxiv.org/pdf/1706.06083.pdf中有體現,以后說到CW攻擊再細說。

 

1 # label_mask=torch_one_hot(labels)
2 #
3 # correct_logit=torch.mean(torch.sum(label_mask * preds,dim=1))
4 # wrong_logit = torch.mean(torch.max((1 - label_mask) * preds, dim=1)[0])
5 # loss=-F.relu(correct_logit-wrong_logit+self.C)

 

最后再提一點就是,在上面那篇防御論文中也提到了,PGD攻擊是最強的一階攻擊,如果防御方法對這個攻擊能夠有很好的防御效果,那么其他攻擊也不在話下了。

 

 

 

2019-03-29 20:43:40

 

 

 
 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM