淘先锋技术网

首页 1 2 3 4 5 6 7

1 前言

  • 对抗样本库,即进行对抗样本攻击或防御的工具。cleverhans,foolbox,advertorch这三个对抗样本库是比较常用的。github搜索关键字即可找到。
  • cleverhans在github有5k个star,foolbox 2k个star,advertorch 1k个star。通过该信息自然大家都会选择使用cleverfans。
  • cleverhans在之前的版本中只支持tensorflow。如果习惯使用tensorflow,完全可以pip install cleverhans == v3.1.0,下载之前的版本,其中包括很全面的对抗样本攻击。
  • 相较而言,我更喜欢使用torch,这就需要下载cleverhans的最新版本,直接pip install cleverhans即可。这个版本兼容torch,tensorflow以及jax,但是这个版本的库仍然在github维护中,其中只包括部分对抗样本攻击实现。
  • 因此本文会介绍在cleverhans最新版本下,如何使用torch来实现对抗样本的攻击。

2 cleverhans使用

实验步骤:

  1. 构建并训练网络模型
  2. 使用cleverhans实现PGD,DeepFool以及CW攻击
  3. 可视化

2.1 构建并训练模型

from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm

# 加载mnist数据集
test_loader = torch.utils.data.DataLoader(
    datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([
            transforms.ToTensor(),
            ])),
        batch_size=10, shuffle=True)
train_loader = torch.utils.data.DataLoader(
    datasets.MNIST('../data', train=True, download=True, transform=transforms.Compose([
            transforms.ToTensor(),
            ])),
        batch_size=10, shuffle=True)

# 超参数设置
batch_size = 10
epoch = 1
learning_rate = 0.001
# 生成对抗样本的个数
adver_nums = 1000


# LeNet Model definition
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

# 选择设备
device = torch.device("cuda" if (torch.cuda.is_available()) else "cpu")

# 初始化网络,并定义优化器
simple_model = Net().to(device)
optimizer1 = torch.optim.SGD(simple_model.parameters(),lr = learning_rate,momentum=0.9)
print (simple_model)


# 训练模型
def train(model,optimizer):
  for i in range(epoch):
    for j,(data,target) in tqdm(enumerate(train_loader)):
      data = data.to(device)
      target = target.to(device)
      logit = model(data)
      loss = F.nll_loss(logit,target)
      model.zero_grad()
      # 如下:因为其中的loss是单个tensor就不能用加上一个tensor的维度限制
      loss.backward()
      # 如下有两种你形式表达,一种是原生,一种是使用optim优化函数直接更新参数
      # 为什么原生的训练方式没有效果???代表参数没有更新,就离谱。
      # 下面的detach与requires_grad_有讲究哦,终于明白了;但是为什么下面代码不能work还是没搞懂
      # for params in model.parameters():
      #   params = (params - learning_rate * params.grad).detach().requires_grad_()
      optimizer.step()
      if j % 1000 == 0:
        print ('第{}个数据,loss值等于{}'.format(j,loss))
train(simple_model,optimizer1)

# eval eval ,老子被你害惨了
# 训练完模型后,要加上,固定DROPOUT层
simple_model.eval()

# 模型测试
def test(model,name):
  correct_num = torch.tensor(0).to(device)
  for j,(data,target) in tqdm(enumerate(test_loader)):
    data = data.to(device)
    target = target.to(device)
    logit = model(data)
    pred = logit.max(1)[1]
    num = torch.sum(pred==target)
    correct_num = correct_num + num
  print (correct_num)
  print ('\n{} correct rate is {}'.format(name,correct_num/10000))
test(simple_model,'simple model')

Output:
在这里插入图片描述

2.2 cleverhans攻击及可视化

  • 以下代码完成CW,PGD以及DeepFool 攻击
from cleverhans.torch.attacks.fast_gradient_method import fast_gradient_method  
from cleverhans.torch.attacks.carlini_wagner_l2 import carlini_wagner_l2
from cleverhans.torch.attacks.projected_gradient_descent import projected_gradient_descent

def PGD(model):
  adver_example = None
  adver_target = None
  clean_example = None
  clean_target = None
  for i,(data,target) in enumerate(test_loader):
    if i>=1:
      break
    # model_fn = lambda x:F.nll_loss(model(x),target.to(device))
    adver_example = projected_gradient_descent(model, data.to(device),0.1,0.05,40,np.inf)
    adver_target = torch.max(model(adver_example),1)[1]
    clean_example = data
    clean_target = target
  return adver_example,adver_target,clean_example,clean_target,'PGD attack'

def FGSM(model):
  adver_example = None
  adver_target = None
  clean_example = None
  clean_target = None
  for i,(data,target) in enumerate(test_loader):
    if i>=1:
      break
    # model_fn = lambda x:F.nll_loss(model(x),target.to(device))
    adver_example = fast_gradient_method(model, data.to(device), 0.1, np.inf)
    adver_target = torch.max(model(adver_example),1)[1]
    clean_example = data
    clean_target = target
  return adver_example,adver_target,clean_example,clean_target,'FGSM attack'

def CW(model):
  adver_example = None
  adver_target = None
  clean_example = None
  clean_target = None
  for i,(data,target) in enumerate(test_loader):
    if i>=1:
      break
    # model_fn = lambda x:F.nll_loss(model(x),target.to(device))
    adver_example = carlini_wagner_l2(model, data.to(device), 10, y = torch.tensor([3]*batch_size,device = device) ,targeted = True)
    adver_target = torch.max(model(adver_example),1)[1]
    clean_example = data
    clean_target = target
  return adver_example,adver_target,clean_example,clean_target,'CW attack'
# print (adver_target.shape)

def plot_clean_and_adver(adver_example,adver_target,clean_example,clean_target,attack_name):
  n_cols = 2
  n_rows = 5
  cnt = 1
  cnt1 = 1
  plt.figure(figsize=(4*n_rows,2*n_cols))
  for i in range(n_cols):
    for j in range(n_rows):
      plt.subplot(n_cols,n_rows*2,cnt1)
      plt.xticks([])
      plt.yticks([])
      if j == 0:
        plt.ylabel(attack_name,size=15)
      plt.title("{} -> {}".format(clean_target[cnt-1], adver_target[cnt-1]))
      plt.imshow(clean_example[cnt-1].reshape(28,28).to('cpu').detach().numpy(),cmap='gray')
      plt.subplot(n_cols,n_rows*2,cnt1+1)
      plt.xticks([])
      plt.yticks([])
      # plt.title("{} -> {}".format(clean_target[cnt], adver_target[cnt]))
      plt.imshow(adver_example[cnt-1].reshape(28,28).to('cpu').detach().numpy(),cmap='gray')
      cnt = cnt + 1
      cnt1 = cnt1 + 2
  plt.show()
  print ('\n')

adver_example,adver_target,clean_example,clean_target,attack_name= FGSM(simple_model)
plot_clean_and_adver(adver_example,adver_target,clean_example,clean_target,attack_name)

# CW能实现有目标以及无目标攻击
adver_example,adver_target,clean_example,clean_target,attack_name= CW(simple_model)
plot_clean_and_adver(adver_example,adver_target,clean_example,clean_target,attack_name)


adver_example,adver_target,clean_example,clean_target,attack_name= PGD(simple_model)
plot_clean_and_adver(adver_example,adver_target,clean_example,clean_target,attack_name)

Output:
在这里插入图片描述在这里插入图片描述

3 总结

  1. 我对torch的心是不会变的。但是cleverhans(v4.0.0)+torch 仅仅只能实现一点点对抗样本攻击,就这几个对抗样本攻击跳转。当然如果你的偏好是tensorflow,那就使用cleverhans v3.1.0版本的吧,里面的攻击方式很全面。
  2. 说到这里,可以看出cleverhans(v4.0.0)+torch已经不能满足我的欲望。因此 foolbox+torch的使用之旅即将开始!!!

附录

参考资料:

  1. cleverhans 源码库
  2. cleverhans blogs,并没什么用。

源代码: