注意
点击此处下载完整的示例代码
对抗样本生成¶
创建于:2018 年 8 月 14 日 | 最后更新:2025 年 1 月 27 日 | 最后验证:未验证
作者: Nathan Inkawhich
如果您正在阅读本文,希望您能体会到某些机器学习模型是多么有效。研究不断推动 ML 模型变得更快、更准确和更高效。然而,设计和训练模型时经常被忽视的一个方面是安全性和稳健性,尤其是在面对希望欺骗模型的攻击者时。
本教程将提高您对 ML 模型安全漏洞的认识,并深入了解对抗性机器学习的热门话题。您可能会惊讶地发现,向图像添加不可察觉的扰动可以导致模型性能发生巨大变化。鉴于这是一个教程,我们将通过图像分类器上的示例来探索该主题。具体而言,我们将使用最早也是最流行的攻击方法之一,即快速梯度符号攻击 (FGSM),来欺骗 MNIST 分类器。
威胁模型¶
对于上下文,对抗性攻击有很多类别,每种类别都有不同的目标和对攻击者知识的假设。但是,总的来说,首要目标是以最小的扰动量添加到输入数据中,以导致所需的错误分类。关于攻击者知识的假设有几种,其中两种是:白盒和黑盒。白盒攻击假设攻击者完全了解并可以访问模型,包括架构、输入、输出和权重。黑盒攻击假设攻击者只能访问模型的输入和输出,并且对底层架构或权重一无所知。目标也有几种类型,包括错误分类和源/目标错误分类。错误分类的目标意味着攻击者只想输出分类错误,但并不关心新的分类是什么。源/目标错误分类意味着攻击者想要更改原始为特定源类别的图像,使其被分类为特定目标类别。
在本例中,FGSM 攻击是一种白盒攻击,目标是错误分类。有了这些背景信息,我们现在可以详细讨论该攻击。
快速梯度符号攻击¶
迄今为止,最早也是最流行的对抗性攻击之一被称为快速梯度符号攻击 (FGSM),由 Goodfellow 等人在解释和利用对抗性示例中描述。该攻击非常强大,而且直观。它旨在通过利用神经网络的学习方式(梯度)来攻击神经网络。这个想法很简单,攻击不是通过基于反向传播的梯度调整权重来最小化损失,而是基于相同的反向传播梯度调整输入数据以最大化损失。换句话说,攻击使用损失相对于输入数据的梯度,然后调整输入数据以最大化损失。
在我们开始编写代码之前,让我们看一下著名的 FGSM 熊猫示例并提取一些符号。

从图中可以看出,\(\mathbf{x}\) 是原始输入图像,正确分类为“熊猫”,\(y\) 是 \(\mathbf{x}\) 的真实标签,\(\mathbf{\theta}\) 表示模型参数,\(J(\mathbf{\theta}, \mathbf{x}, y)\) 是用于训练网络的损失。攻击将梯度反向传播回输入数据以计算 \(\nabla_{x} J(\mathbf{\theta}, \mathbf{x}, y)\)。然后,它在将最大化损失的方向(即 \(sign(\nabla_{x} J(\mathbf{\theta}, \mathbf{x}, y))\))上,按小步长(图片中为 \(\epsilon\) 或 \(0.007\))调整输入数据。然后,目标网络将生成的扰动图像 \(x'\) 错误分类为“长臂猿”,而它仍然明显是“熊猫”。
希望现在本教程的动机很明确了,让我们开始实现。
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
实现¶
在本节中,我们将讨论本教程的输入参数,定义受攻击的模型,然后编写攻击代码并运行一些测试。
输入¶
本教程只有三个输入,定义如下
epsilons
- 运行中要使用的 epsilon 值列表。将 0 保留在列表中很重要,因为它代表了原始测试集上的模型性能。此外,直观上我们期望 epsilon 值越大,扰动越明显,但在降低模型准确性方面,攻击也越有效。由于此处的数据范围为 \([0,1]\),因此任何 epsilon 值都不应超过 1。pretrained_model
- 预训练 MNIST 模型的路径,该模型使用 pytorch/examples/mnist 进行训练。为简单起见,请在此处下载预训练模型。
epsilons = [0, .05, .1, .15, .2, .25, .3]
pretrained_model = "data/lenet_mnist_model.pth"
# Set random seed for reproducibility
torch.manual_seed(42)
<torch._C.Generator object at 0x7fd64938a3d0>
受攻击的模型¶
如前所述,受攻击的模型与 pytorch/examples/mnist 中的 MNIST 模型相同。您可以训练并保存自己的 MNIST 模型,也可以下载并使用提供的模型。此处的 Net 定义和测试数据加载器已从 MNIST 示例中复制。本节的目的是定义模型和数据加载器,然后初始化模型并加载预训练权重。
# LeNet Model definition
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout(0.25)
self.dropout2 = nn.Dropout(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
output = F.log_softmax(x, dim=1)
return output
# MNIST Test dataset and dataloader declaration
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)),
])),
batch_size=1, shuffle=True)
# We want to be able to train our model on an `accelerator <https://pytorch.ac.cn/docs/stable/torch.html#accelerators>`__
# such as CUDA, MPS, MTIA, or XPU. If the current accelerator is available, we will use it. Otherwise, we use the CPU.
device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu"
print(f"Using {device} device")
# Initialize the network
model = Net().to(device)
# Load the pretrained model
model.load_state_dict(torch.load(pretrained_model, map_location=device, weights_only=True))
# Set the model in evaluation mode. In this case this is for the Dropout layers
model.eval()
0%| | 0.00/9.91M [00:00<?, ?B/s]
100%|##########| 9.91M/9.91M [00:00<00:00, 128MB/s]
0%| | 0.00/28.9k [00:00<?, ?B/s]
100%|##########| 28.9k/28.9k [00:00<00:00, 44.8MB/s]
0%| | 0.00/1.65M [00:00<?, ?B/s]
100%|##########| 1.65M/1.65M [00:00<00:00, 201MB/s]
0%| | 0.00/4.54k [00:00<?, ?B/s]
100%|##########| 4.54k/4.54k [00:00<00:00, 18.9MB/s]
Using cuda device
Net(
(conv1): Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1))
(conv2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))
(dropout1): Dropout(p=0.25, inplace=False)
(dropout2): Dropout(p=0.5, inplace=False)
(fc1): Linear(in_features=9216, out_features=128, bias=True)
(fc2): Linear(in_features=128, out_features=10, bias=True)
)
FGSM 攻击¶
现在,我们可以定义函数,通过扰动原始输入来创建对抗样本。fgsm_attack
函数接受三个输入,image 是原始干净图像 (\(x\)),epsilon 是像素级扰动量 (\(\epsilon\)),data_grad 是损失相对于输入图像的梯度 (\(\nabla_{x} J(\mathbf{\theta}, \mathbf{x}, y)\))。然后,该函数创建扰动图像,如下所示
最后,为了保持数据的原始范围,扰动图像被裁剪到范围 \([0,1]\)。
# FGSM attack code
def fgsm_attack(image, epsilon, data_grad):
# Collect the element-wise sign of the data gradient
sign_data_grad = data_grad.sign()
# Create the perturbed image by adjusting each pixel of the input image
perturbed_image = image + epsilon*sign_data_grad
# Adding clipping to maintain [0,1] range
perturbed_image = torch.clamp(perturbed_image, 0, 1)
# Return the perturbed image
return perturbed_image
# restores the tensors to their original scale
def denorm(batch, mean=[0.1307], std=[0.3081]):
"""
Convert a batch of tensors to their original scale.
Args:
batch (torch.Tensor): Batch of normalized tensors.
mean (torch.Tensor or list): Mean used for normalization.
std (torch.Tensor or list): Standard deviation used for normalization.
Returns:
torch.Tensor: batch of tensors without normalization applied to them.
"""
if isinstance(mean, list):
mean = torch.tensor(mean).to(device)
if isinstance(std, list):
std = torch.tensor(std).to(device)
return batch * std.view(1, -1, 1, 1) + mean.view(1, -1, 1, 1)
测试函数¶
最后,本教程的核心结果来自 test
函数。每次调用此测试函数都会对 MNIST 测试集执行完整的测试步骤,并报告最终的准确率。但是,请注意此函数还接受 epsilon 输入。这是因为 test
函数报告了模型在强度为 \(\epsilon\) 的攻击者的攻击下的准确率。更具体地说,对于测试集中的每个样本,该函数计算损失相对于输入数据的梯度 (\(data\_grad\)),使用 fgsm_attack
创建扰动图像 (\(perturbed\_data\)),然后检查扰动示例是否是对抗性的。除了测试模型的准确率之外,该函数还保存并返回一些成功的对抗样本,以便稍后可视化。
def test( model, device, test_loader, epsilon ):
# Accuracy counter
correct = 0
adv_examples = []
# Loop over all examples in test set
for data, target in test_loader:
# Send the data and label to the device
data, target = data.to(device), target.to(device)
# Set requires_grad attribute of tensor. Important for Attack
data.requires_grad = True
# Forward pass the data through the model
output = model(data)
init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
# If the initial prediction is wrong, don't bother attacking, just move on
if init_pred.item() != target.item():
continue
# Calculate the loss
loss = F.nll_loss(output, target)
# Zero all existing gradients
model.zero_grad()
# Calculate gradients of model in backward pass
loss.backward()
# Collect ``datagrad``
data_grad = data.grad.data
# Restore the data to its original scale
data_denorm = denorm(data)
# Call FGSM Attack
perturbed_data = fgsm_attack(data_denorm, epsilon, data_grad)
# Reapply normalization
perturbed_data_normalized = transforms.Normalize((0.1307,), (0.3081,))(perturbed_data)
# Re-classify the perturbed image
output = model(perturbed_data_normalized)
# Check for success
final_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
if final_pred.item() == target.item():
correct += 1
# Special case for saving 0 epsilon examples
if epsilon == 0 and len(adv_examples) < 5:
adv_ex = perturbed_data.squeeze().detach().cpu().numpy()
adv_examples.append( (init_pred.item(), final_pred.item(), adv_ex) )
else:
# Save some adv examples for visualization later
if len(adv_examples) < 5:
adv_ex = perturbed_data.squeeze().detach().cpu().numpy()
adv_examples.append( (init_pred.item(), final_pred.item(), adv_ex) )
# Calculate final accuracy for this epsilon
final_acc = correct/float(len(test_loader))
print(f"Epsilon: {epsilon}\tTest Accuracy = {correct} / {len(test_loader)} = {final_acc}")
# Return the accuracy and an adversarial example
return final_acc, adv_examples
运行攻击¶
实现的最后一部分是实际运行攻击。在这里,我们为 epsilons 输入中的每个 epsilon 值运行完整的测试步骤。对于每个 epsilon,我们还保存最终的准确率和一些成功的对抗样本,以便在接下来的部分中绘制。请注意,打印的准确率如何随着 epsilon 值的增加而降低。另请注意,\(\epsilon=0\) 情况表示原始测试准确率,没有攻击。
accuracies = []
examples = []
# Run test for each epsilon
for eps in epsilons:
acc, ex = test(model, device, test_loader, eps)
accuracies.append(acc)
examples.append(ex)
Epsilon: 0 Test Accuracy = 9912 / 10000 = 0.9912
Epsilon: 0.05 Test Accuracy = 9605 / 10000 = 0.9605
Epsilon: 0.1 Test Accuracy = 8743 / 10000 = 0.8743
Epsilon: 0.15 Test Accuracy = 7111 / 10000 = 0.7111
Epsilon: 0.2 Test Accuracy = 4877 / 10000 = 0.4877
Epsilon: 0.25 Test Accuracy = 2717 / 10000 = 0.2717
Epsilon: 0.3 Test Accuracy = 1418 / 10000 = 0.1418
结果¶
准确率与 Epsilon 的关系¶
第一个结果是准确率与 epsilon 关系图。如前所述,随着 epsilon 的增加,我们预计测试准确率会降低。这是因为较大的 epsilon 意味着我们在将最大化损失的方向上迈出更大的步长。请注意,即使 epsilon 值呈线性间隔,曲线的趋势也不是线性的。例如,\(\epsilon=0.05\) 时的准确率仅比 \(\epsilon=0\) 时低约 4%,但 \(\epsilon=0.2\) 时的准确率比 \(\epsilon=0.15\) 时低 25%。另请注意,对于 10 类分类器,模型的准确率在 \(\epsilon=0.25\) 和 \(\epsilon=0.3\) 之间达到随机准确率。
plt.figure(figsize=(5,5))
plt.plot(epsilons, accuracies, "*-")
plt.yticks(np.arange(0, 1.1, step=0.1))
plt.xticks(np.arange(0, .35, step=0.05))
plt.title("Accuracy vs Epsilon")
plt.xlabel("Epsilon")
plt.ylabel("Accuracy")
plt.show()

对抗样本示例¶
还记得天下没有免费的午餐吗?在这种情况下,随着 epsilon 的增加,测试准确率会降低,但扰动变得更容易被察觉。实际上,攻击者必须权衡准确率下降和可察觉性之间的关系。在这里,我们展示了每个 epsilon 值下一些成功的对抗样本示例。图的每一行显示一个不同的 epsilon 值。第一行是 \(\epsilon=0\) 示例,表示没有扰动的原始“干净”图像。每个图像的标题显示“原始分类 -> 对抗分类”。请注意,扰动在 \(\epsilon=0.15\) 时开始变得明显,在 \(\epsilon=0.3\) 时非常明显。但是,在所有情况下,人类仍然能够识别正确的类别,尽管添加了噪声。
# Plot several examples of adversarial samples at each epsilon
cnt = 0
plt.figure(figsize=(8,10))
for i in range(len(epsilons)):
for j in range(len(examples[i])):
cnt += 1
plt.subplot(len(epsilons),len(examples[0]),cnt)
plt.xticks([], [])
plt.yticks([], [])
if j == 0:
plt.ylabel(f"Eps: {epsilons[i]}", fontsize=14)
orig,adv,ex = examples[i][j]
plt.title(f"{orig} -> {adv}")
plt.imshow(ex, cmap="gray")
plt.tight_layout()
plt.show()

后续步骤?¶
希望本教程能让您深入了解对抗性机器学习的主题。从这里开始,有很多潜在的方向可以发展。这种攻击代表了对抗性攻击研究的开端,此后出现了许多关于如何攻击和防御 ML 模型免受攻击者的想法。事实上,在 NIPS 2017 大会上,举办了一场对抗性攻击和防御竞赛,竞赛中使用的许多方法都在本文中描述:对抗性攻击和防御竞赛。关于防御的工作也引出了使机器学习模型更稳健的想法,使其能够应对自然扰动和对抗性制作的输入。
另一个发展方向是在不同领域进行对抗性攻击和防御。对抗性研究不仅限于图像领域,请查看 此 对语音到文本模型的攻击。但或许了解更多关于对抗性机器学习的最佳方法是亲自动手。尝试实现 NIPS 2017 竞赛中的另一种攻击,看看它与 FGSM 有何不同。然后,尝试防御模型免受您自己的攻击。
根据可用资源,进一步的发展方向是修改代码以支持批量、并行或分布式处理工作,而不是在上述每个 epsilon test()
循环中一次处理一个攻击。
脚本的总运行时间: ( 4 分钟 10.740 秒)