当前位置:   article > 正文

C&W攻击代码pytorch实现_c&w攻击实例

c&w攻击实例

C&W攻击是数字空间中的对抗攻击算法,下面使用到的一些函数和代码

  1. import torch
  2. import torchvision
  3. from torch.autograd import Variable
  4. from torch.autograd.gradcheck import *
  5. from torchvision import datasets,transforms
  6. import torch.utils.data.dataloader as Data
  7. import torch.nn as nn
  8. from torchvision import models
  9. import numpy as np
  10. import cv2
  11. #获取计算设备 默认是CPU
  12. device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  13. #图像加载以及预处理
  14. image_path="../picture/cow.jpeg"
  15. orig=cv2.imread(image_path_[...,::-1]
  16. orig=cv2.resize(orig,(224,224))
  17. img=orig.copy().astype(np.float32)
  18. mean = [0.485, 0.456, 0.406]
  19. std = [0.229, 0.224, 0.225]
  20. img /= 255.0
  21. img = (img - mean) / std
  22. img = img.transpose(2, 0, 1)
  23. img=np.expand_dims(img,axis=0)# expand_dims扩展维度
  24. print(img.shape)# should be (1,3,224,224)
  25. model=models.alaxnet(pretrained=True).to(device).eval()
  1. max_iterations=1000# 最大迭代次数
  2. learning_rate=0.01
  3. # 二分查找最大次数
  4. binary_search_steps=10
  5. #c的初始值
  6. initial_const=100
  7. confidence=initial_const
  8. # K值
  9. k=40
  10. boxmin=-3.0
  11. boxmax=3.0
  12. num_labels=1000# ImageNet是1000类
  13. # 攻击目标标签,必须使用one hot编码
  14. tatget_label=988
  15. tlab=Variable(torch.from_numpy(np.eye(num_labels)[target_label]).to(device).float())# 将目标标签转变成torch变量
  16. print(tlab)# should be torch.Size([1000])
  17. shape=(1,3,224,224)
  18. # C的初始化边界
  19. lower_bound=0
  20. c=initial_const
  21. upper_bound=1e10
  22. # the best l2,score, and image attack
  23. o_bestl2=1e10
  24. o_bestscore=-1
  25. o_bestattack=[np.zeros(shape)]
  1. # the resulting image, tanh'd to keep bounded from boxmin to boxmax
  2. boxmul=(boxmax-boxmin)/2
  3. boxplus=(boxmin+boxmax)/2
  4. for outer_step in range(binary_search_steps):
  5. print("best_l2={},confidence={}".format(o_bestl2,confidence))
  6. #把原始图像转换成图像数据和扰动的形态
  7. timg=Variable(torch.from_numpy(np.arctanh(img-boxplus)/boxmul*0.999999)).to(device).float())
  8. modifier=Variable(torch.zeros_like(timg).to(device).float())
  9. modifier.requires_grad=True
  10. #定义优化器,仅优化modifer
  11. optimizer =torch.optim.Adam([modifier],lr=learning_rate)
  12. for iteration in range(1,max_iteration+1):
  13. optimizer.zero_grad()# 梯度清零
  14. newimg=torch.tanh(modifier+timg)*boxmul+boxplus
  15. output=model(newimg)
  16. # 定义cw中的损失函数
  17. # l2范数,用torch.dist()计算欧几里得距离,p=2为欧几里得距离,p=1为曼哈顿距离,即l1loss
  18. loss2=torch.dist(newimg,(torch.tanh(timg)*boxmul+boxplus),p=2)
  19. real=torch.max(output*tlab)
  20. other=torch.max((1-tlab)*output)
  21. loss1=other-real+k
  22. loss1=torch.clamp(loss1,min=0)# 用clamp限制loss1最小为0
  23. loss=confidence*loss1
  24. loss=loss1+loss2
  25. # 反向传播+梯度更新 使用retain_graph=True来保留计算图,以便下一次调用backward()方法。如果不设置retain_graph=True,则会在第一次反向传播后自动释放计算图。
  26. loss.backward(retain_graph=True)
  27. optimizer.step()
  28. l2=loss2
  29. sc=output.data.cpu().numpy()
  30. # print out loss every 10%
  31. if iteration%(max_iterations//10) == 0:
  32. print("iteration={} loss={} loss1={} loss2={}".format(iteration,loss,loss1,loss2))
  33. if (l2 < o_bestl2) and (np.argmax(sc) == target_label ):
  34. print("attack success l2={} target_label={}".format(l2,target_label))
  35. o_bestl2 = l2
  36. o_bestscore = np.argmax(sc)
  37. o_bestattack = newimg.data.cpu().numpy()
  38. confidence_old=1
  39. if (o_bestscore==target_label) and o_bestscore!=-1:
  40. # 攻击成功,减小c
  41. upper_bound=min(upper_bound,confidence)
  42. if upper_bound<1e9:
  43. confidence_old=confidence
  44. confidence=(lower_bound+upper_bound)/2
  45. else:
  46. lower_bound=max(lower_bound,confidence)
  47. confidence_old=confidence
  48. if upper_bound < 1e9:
  49. confidence = (lower_bound + upper_bound)/2
  50. else:
  51. confidence *= 10
  52. # torch.sign
  53. print("outer_step={} confidence {}->{}".format(outer_step,confidence_old,confidence))
  1. # 输出图像的shape
  2. print(o_bestattack.shape)
  3. print(img.shape)
  1. #对比展现原始图片和对抗样本图片,之前的函数拿过来直接用
  2. def show_images_diff(original_img,original_label,adversarial_img,adversarial_label):
  3. import matplotlib.pyplot as plt
  4. plt.figure()
  5. #归一化
  6. if original_img.any() > 1.0:
  7. original_img=original_img/255.0
  8. if adversarial_img.any() > 1.0:
  9. adversarial_img=adversarial_img/255.0
  10. plt.subplot(131)
  11. plt.title('Original')
  12. plt.imshow(original_img)
  13. plt.axis('off')
  14. plt.subplot(132)
  15. plt.title('Adversarial')
  16. plt.imshow(adversarial_img)
  17. plt.axis('off')
  18. plt.subplot(133)
  19. plt.title('Adversarial-Original')
  20. difference = adversarial_img - original_img
  21. #(-1,1) -> (0,1)
  22. difference=difference / abs(difference).max()/2.0+0.5
  23. plt.imshow(difference,cmap=plt.cm.gray)
  24. plt.axis('off')
  25. plt.tight_layout()
  26. plt.show()
  1. adv=o_bestattack[0]# 取出图片向量
  2. print(adv.shape)
  3. adv = adv.transpose(1, 2, 0)
  4. adv = (adv * std) + mean
  5. adv = adv * 255.0
  6. adv = np.clip(adv, 0, 255).astype(np.uint8)
  7. show_images_diff(orig,0,adv,0)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/508506
推荐阅读
相关标签
  

闽ICP备14008679号