对抗性示例生成

译者:cangyunye

作者: Nathan Inkawhich

如果你正在阅读这篇文章,希望你能理解一些机器学习模型是多么有效。现在的研究正在不断推动ML模型变得更快、更准确和更高效。然而,在设计和训练模型中经常会忽视的是安全性和健壮性方面,特别是在面对欺骗模型的对手时。

本教程将提高您对ML模型安全漏洞的认识,并将深入探讨对抗性机器学习这一热门话题。您可能会惊讶地发现,在图像中添加细微的干扰会导致模型性能的巨大差异。鉴于这是一个教程,我们将通过一个图像分类器上的示例来探索这个主题。具体来说,我们将使用第一个也是最流行的攻击方法之一,快速梯度符号攻击Fast Gradient Sign Attack(FGSM),以欺骗一个MNIST分类器。

威胁模型

就上下文而言,有许多类型的对抗性攻击,每一类攻击都有不同的目标和对攻击者知识的假设。然而,总的目标是在输入数据中添加最少的扰动,以导致所需的错误分类。攻击者的知识有几种假设,其中两种是:白盒黑盒。白盒攻击假定攻击者具有对模型的全部知识和访问权,包括体系结构、输入、输出和权重。黑盒攻击假设攻击者只访问模型的输入和输出,对底层架构或权重一无所知。目标也有几种类型,包括错误分类源/目标错误分类。错误分类的目标意味着对手只希望输出分类是错误的,而不关心新的分类是什么。源/目标错误分类意味着对手想要更改原来属于特定源类的图像,以便将其分类为特定的目标类。

在这种情况下,FGSM攻击是一种以错误分类为目标的白盒攻击。有了这些背景信息,我们现在可以详细讨论攻击。

快速梯度符号攻击

到目前为止,最早也是最流行的对抗性攻击之一被称为快速梯度符号攻击(FGSM),由Goodfellow等人在解释和利用对抗性示例( Explaining and Harnessing Adversarial Examples)时介绍到。这种攻击非常强大,而且直观。它被设计用来攻击神经网络,利用他们学习的方式,梯度gradients。这个想法很简单,比起根据后向传播梯度来调整权重使损失最小化,这种攻击是根据相同的反向传播梯度调整输入数据来最大化损失。换句话说,攻击使用了输入数据相关的梯度损失方式,通过调整输入数据,使损失最大化。

在我们深入代码之前,让我们看看著名的FGSM panda示例并提取一些符号。

fgsm_panda_image

从图像中看,\mathbf{x} 是一个正确分类为“熊猫”(panda)的原始输入图像, y 是对 \mathbf{x} 的真实表征标签ground truth label, \mathbf{\theta} 表示模型参数, 而 J(\mathbf{\theta}, \mathbf{x}, y) 是用来训练网络的损失函数。 这种攻击将梯度后向传播到输入数据来计算 \nabla{x} J(\mathbf{\theta}, \mathbf{x}, y)。然后将输入数据通过一小步 \epsilon 或 如图中的0.007 ) 在(i.e. sign(\nabla{x} J(\mathbf{\theta}, \mathbf{x}, y)) 方向上调整,使损失最大化。结果将得到受到干扰的图像, x’,尽管图片还是“熊猫”,但它一杯目标网络错误分类为“长臂猿”(gibbon)了

希望看到现在的你,已经明确了解了本教程的动机,那么,让我们开始实现它吧。

  1. from __future__ import print_function
  2. import torch
  3. import torch.nn as nn
  4. import torch.nn.functional as F
  5. import torch.optim as optim
  6. from torchvision import datasets, transforms
  7. import numpy as np
  8. import matplotlib.pyplot as plt

实现

在本节中,我们将讨论本教程的输入参数,定义受攻击的模型,然后编写攻击代码并运行一些测试。

输入

本教程只有三个输入,定义如下:

  • epsilons - 要用于运行的epsilon值的列表。在列表中保留0是很重要的,因为它代表了原始测试集上的模型性能。而且,直觉上我们认为,epsilon越大,扰动越明显,但在降低模型精度方面攻击越有效。因为这里的数据范围是 $$[0,1]$$,所以取值不应该超过1。
  • pretrained_model - 表示使用 pytorch/examples/mnist进行训练的预训练MNIST模型的路径。为了简单起见,在这里 下载预先训练的模型。
  • use_cuda - 如果需要和可用,使用CUDA的布尔标志。注意,带有CUDA的GPU对于本教程来说并不重要,因为CPU不会占用太多时间。
  1. epsilons = [0, .05, .1, .15, .2, .25, .3]
  2. pretrained_model = "data/lenet_mnist_model.pth"
  3. use_cuda=True

受攻模型

如前所述,受攻模型与pytorch/examples/mnist中的MNIST模型相同。您可以训练并保存自己的MNIST模型,也可以下载并使用提供的模型。这里的Net定义和测试dataloader是从MNIST示例中复制的。本节的目的是定义模型和加载数据,然后初始化模型并加载预先训练的权重。

  1. # LeNet Model definition
  2. class Net(nn.Module):
  3. def __init__(self):
  4. super(Net, self).__init__()
  5. self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
  6. self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
  7. self.conv2_drop = nn.Dropout2d()
  8. self.fc1 = nn.Linear(320, 50)
  9. self.fc2 = nn.Linear(50, 10)
  10. def forward(self, x):
  11. x = F.relu(F.max_pool2d(self.conv1(x), 2))
  12. x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
  13. x = x.view(-1, 320)
  14. x = F.relu(self.fc1(x))
  15. x = F.dropout(x, training=self.training)
  16. x = self.fc2(x)
  17. return F.log_softmax(x, dim=1)
  18. # MNIST Test dataset and dataloader declaration
  19. test_loader = torch.utils.data.DataLoader(
  20. datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([
  21. transforms.ToTensor(),
  22. ])),
  23. batch_size=1, shuffle=True)
  24. # Define what device we are using
  25. print("CUDA Available: ",torch.cuda.is_available())
  26. device = torch.device("cuda" if (use_cuda and torch.cuda.is_available()) else "cpu")
  27. # Initialize the network
  28. model = Net().to(device)
  29. # Load the pretrained model
  30. model.load_state_dict(torch.load(pretrained_model, map_location='cpu'))
  31. # Set the model in evaluation mode. In this case this is for the Dropout layers
  32. model.eval()

Out:

  1. Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ../data/MNIST/raw/train-images-idx3-ubyte.gz
  2. Extracting ../data/MNIST/raw/train-images-idx3-ubyte.gz
  3. Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to ../data/MNIST/raw/train-labels-idx1-ubyte.gz
  4. Extracting ../data/MNIST/raw/train-labels-idx1-ubyte.gz
  5. Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to ../data/MNIST/raw/t10k-images-idx3-ubyte.gz
  6. Extracting ../data/MNIST/raw/t10k-images-idx3-ubyte.gz
  7. Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to ../data/MNIST/raw/t10k-labels-idx1-ubyte.gz
  8. Extracting ../data/MNIST/raw/t10k-labels-idx1-ubyte.gz
  9. Processing...
  10. Done!
  11. CUDA Available: True

FGSM 攻击方式

现在,我们可以定义一个通过打乱原始输入来生成对抗性示例的函数。 fgsm_attack 函数有3个输入, image 是原始图像 xepsilon 是像素级干扰量 \epsilondata_grad 是关于输入图像 \nabla_{x} J(\mathbf{\theta}, \mathbf{x}, y) 的损失。然后该函数创建干扰图像如下

perturbed_image = image + epsilonsign(data_grad) = x + \epsilon sign(\nabla_{x} J(\mathbf{\theta}, \mathbf{x}, y))

最后,为了保持数据的原始范围,将扰动后的图像截取范围在 [0,1]

  1. # FGSM attack code
  2. def fgsm_attack(image, epsilon, data_grad):
  3. # Collect the element-wise sign of the data gradient
  4. sign_data_grad = data_grad.sign()
  5. # Create the perturbed image by adjusting each pixel of the input image
  6. perturbed_image = image + epsilon*sign_data_grad
  7. # Adding clipping to maintain [0,1] range
  8. perturbed_image = torch.clamp(perturbed_image, 0, 1)
  9. # Return the perturbed image
  10. return perturbed_image

功能验证

最后,本教程的核心结果来自测试test函数。对这个测试函数的每次调用都在MNIST测试集上执行一个完整的测试步骤,然后给出一个最终准确性报告。但是,注意这个函数也接受一个epsilon输入。这是因为测试test函数报告了一个模型的准确性,该模型正受到强度为\epsilon的对手的攻击。更具体地说,对于测试集中的每个样本,该函数计算和输入数据 data_grad 相关的损失梯度,用fgsm_attack perturbed_data 创建一个干扰图像,然后检查干扰的例子是否是对抗性的。除了检测模型的准确性外,函数还需要保存和返回一些成功性的示例以便日后查看。

  1. def test( model, device, test_loader, epsilon ):
  2. # Accuracy counter
  3. correct = 0
  4. adv_examples = []
  5. # Loop over all examples in test set
  6. for data, target in test_loader:
  7. # Send the data and label to the device
  8. data, target = data.to(device), target.to(device)
  9. # Set requires_grad attribute of tensor. Important for Attack
  10. data.requires_grad = True
  11. # Forward pass the data through the model
  12. output = model(data)
  13. init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
  14. # If the initial prediction is wrong, dont bother attacking, just move on
  15. if init_pred.item() != target.item():
  16. continue
  17. # Calculate the loss
  18. loss = F.nll_loss(output, target)
  19. # Zero all existing gradients
  20. model.zero_grad()
  21. # Calculate gradients of model in backward pass
  22. loss.backward()
  23. # Collect datagrad
  24. data_grad = data.grad.data
  25. # Call FGSM Attack
  26. perturbed_data = fgsm_attack(data, epsilon, data_grad)
  27. # Re-classify the perturbed image
  28. output = model(perturbed_data)
  29. # Check for success
  30. final_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
  31. if final_pred.item() == target.item():
  32. correct += 1
  33. # Special case for saving 0 epsilon examples
  34. if (epsilon == 0) and (len(adv_examples) < 5):
  35. adv_ex = perturbed_data.squeeze().detach().cpu().numpy()
  36. adv_examples.append( (init_pred.item(), final_pred.item(), adv_ex))
  37. else:
  38. # Save some adv examples for visualization later
  39. if len(adv_examples) < 5:
  40. adv_ex = perturbed_data.squeeze().detach().cpu().numpy()
  41. adv_examples.append( (init_pred.item(), final_pred.item(), adv_ex))
  42. # Calculate final accuracy for this epsilon
  43. final_acc = correct/float(len(test_loader))
  44. print("Epsilon: {}\tTest Accuracy = {} / {} = {}".format(epsilon, correct, len(test_loader), final_acc))
  45. # Return the accuracy and an adversarial example
  46. return final_acc, adv_examples

启动攻击

实现的最后一部分是运行攻击操作。在这里,我们对输入中的每个epsilon值运行一个完整的测试步骤。对于每个epsilon,我们也保存最后的精度和一些将在接下来的部分中绘制的成功的对抗性例子。请注意,随着epsilon值的增加,打印出来的精度是如何降低的。另外,注意\epsilon=0用例表示原始未受攻击的测试准确性。

  1. accuracies = []
  2. examples = []
  3. # Run test for each epsilon
  4. for eps in epsilons:
  5. acc, ex = test(model, device, test_loader, eps)
  6. accuracies.append(acc)
  7. examples.append(ex)

Out:

  1. Epsilon: 0 Test Accuracy = 9810 / 10000 = 0.981
  2. Epsilon: 0.05 Test Accuracy = 9426 / 10000 = 0.9426
  3. Epsilon: 0.1 Test Accuracy = 8510 / 10000 = 0.851
  4. Epsilon: 0.15 Test Accuracy = 6826 / 10000 = 0.6826
  5. Epsilon: 0.2 Test Accuracy = 4301 / 10000 = 0.4301
  6. Epsilon: 0.25 Test Accuracy = 2082 / 10000 = 0.2082
  7. Epsilon: 0.3 Test Accuracy = 869 / 10000 = 0.0869

结果

准确性 vs Epsilon

第一个结果是相对于epsilon的精确度。正如前面提到的,随着epsilon的增加,我们预期测试的准确性会降低。这是因为更大的epsilon意味着我们在使损失最大化的方向上迈出了更大的一步。注意,即使epsilon值是线性间隔的,曲线的趋势却不是线性的。比如说,精度在\epsilon=0.05 只比\epsilon=0小约4%,但是这个 \epsilon=0.2精度却比 \epsilon=0.15小了25%。 另外,需要注意的是,在 \epsilon=0.25\epsilon=0.3之间做了10次分类的分类器,模型的精度会达到随机精度。

  1. plt.figure(figsize=(5,5))
  2. plt.plot(epsilons, accuracies, "*-")
  3. plt.yticks(np.arange(0, 1.1, step=0.1))
  4. plt.xticks(np.arange(0, .35, step=0.05))
  5. plt.title("Accuracy vs Epsilon")
  6. plt.xlabel("Epsilon")
  7. plt.ylabel("Accuracy")
  8. plt.show()

https://pytorch.org/tutorials/_images/sphx_glr_fgsm_tutorial_001.png

对抗性用例样本

并没有什么尽善尽美之事,在这里,随着epsilon的增加,测试精度降低,但扰动变得更容易察觉。实际上,攻击者必须考虑准确性下降和可感知性之间的权衡。在这里,我们展示了在每个值上成功的对抗性例子。图中的每一行都显示不同的epsilon值。第一行是\epsilon=0的例子,它表示原始的无扰动的纯净图像。每个图像的标题显示“原始分类->干扰分类(adversarial classification)”。请注意,在\epsilon=0.15\epsilon=0.3处开始出现明显的扰动。然而,在所有情况下,尽管添加了躁动因素(干扰),人类仍然能够识别正确的类。

  1. # Plot several examples of adversarial samples at each epsilon
  2. cnt = 0
  3. plt.figure(figsize=(8,10))
  4. for i in range(len(epsilons)):
  5. for j in range(len(examples[i])):
  6. cnt += 1
  7. plt.subplot(len(epsilons),len(examples[0]),cnt)
  8. plt.xticks([], [])
  9. plt.yticks([], [])
  10. if j == 0:
  11. plt.ylabel("Eps: {}".format(epsilons[i]), fontsize=14)
  12. orig,adv,ex = examples[i][j]
  13. plt.title("{} -> {}".format(orig, adv))
  14. plt.imshow(ex, cmap="gray")
  15. plt.tight_layout()
  16. plt.show()

https://pytorch.org/tutorials/_images/sphx_glr_fgsm_tutorial_002.png

接下来的方向

希望本教程对您来说,能够提供一些关于对抗性机器学习主题的见解。从这里开始有很多可能的方向。这种攻击代表了对抗性攻击研究的开始,并且自从有了许多关于如何攻击和保护ML模型不受对手攻击的后续想法以来。事实上,在NIPS 2017年有一场对抗性的攻防竞赛,本文描述了很多比赛中使用的方法:对抗性的攻防及竞赛(Adversarial Attacks and Defences Competition)。在防御方面的工作也引入了使机器学习模型在一般情况下更健壮*robust*的想法,这是一种自然扰动和反向精心设计的输入。

另一个研究方向是不同领域的对抗性攻击和防御。对抗性研究并不局限于图像领域,就比如这种语音到文本模型speech-to-text models的攻击。当然,了解更多关于对抗性机器学习的最好方法是多动手。首先,尝试实现一个不同于NIPS 2017比赛的攻击,看看它与FGSM有什么不同,然后,尝试设计保护模型,使其免于自己的攻击。