pytorch 对抗样本_对抗示例生成

本文介绍了对抗性机器学习的概念,特别是针对图像分类器的快速梯度符号攻击(FGSM)。通过在MNIST分类器上应用FGSM,展示了即使微小的扰动也能导致模型错误分类。随着 epsilon 的增加,模型的测试精度下降,但扰动变得更为明显。
摘要由CSDN通过智能技术生成

0ceb08c89345d38f2162e606ef3be35d.png

本教程将提高您对ML模型的安全漏洞的认识,并深入了解对抗性机器学习的热门话题。您可能会惊讶地发现,对图像添加无法察觉的扰动导致模型性能大不相同。鉴于这是一个教程,我们将通过图像分类器上的示例来探讨该主题。具体来说,我们将使用第一种也是最流行的攻击方法之一,即快速梯度符号攻击(FGSM)来欺骗MNIST分类器。

威胁模型
就上下文而言,有多种类型的对抗性攻击,每种攻击者都有不同的目标和对攻击者知识的假设。但是,总的来说,总体目标是向输入数据添加最少的扰动,以引起所需的错误分类。攻击者的知识有几种假设,其中两种是:white-boxblack-box。一个白盒攻击假设攻击者有充分的知识和访问模型,包括建筑,输入,输出,和权重。一个黑箱攻击假设攻击者只能访问输入和模型的输出,并且一无所知底层架构或权重。目标也有几种类型,包括错误分类源/目标错误分类错误分类的目标意味着对手只希望输出分类错误,而不在乎新分类是什么。甲源/目标误分类装置对手想要改变图像是特定源类的最初使得其被归类为特定的目标类。
在这种情况下,FGSM攻击是白盒攻击,目标是 错误分类。有了这些背景信息,我们现在就可以详细讨论攻击了。

快速梯度符号攻击
迄今为止,最早的也是最流行的对抗性攻击之一被称为“ 快速梯度符号攻击”(FGSM),由Goodfellow等描述。等 在解释和利用对抗例子中的运用。攻击非常强大,而且直观。它旨在利用神经网络的学习方式,梯度来攻击神经网络。这个想法很简单,不是根据反向传播的梯度通过调整权重来使损失最小化,而是根据相同的反向传播的梯度来调整输入数据以使损失最大化。换句话说,攻击使用输入数据的损失梯度,然后调整输入数据以使损失最大化。
在进入代码之前,让我们看一下著名的 FGSM熊猫示例并提取一些表示法。

73412bee715980a894e042eeed77a8a9.png

从图中 xx 是正确分类为“熊猫”的原始输入图像, yy 是地面真相标签 xx, θθ 代表模型参数,并且 J(θ,x,y)J(θ,x,y)是用于训练网络的损耗。攻击会将梯度反向传播回输入数据以进行计算 ∇xJ(θ,x,y)∇xJ(θ,x,y)。然后,通过一小步调整输入数据(ϵϵ 要么 0.0070.007 在图片中)的方向(即 sign(∇xJ(θ,x,y))sign(∇xJ(θ,x,y))),这将使损失最大化。产生的扰动图像x′x′然后 ,在目标网络仍明显是“熊猫”的情况下,它会被目标网络误分类为“长臂猿”。

希望本教程的动机已经明确,所以让我们跳入实施过程。

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

实作

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

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

  • epsilons-用于运行的epsilon值列表。在列表中保留0很重要,因为它表示原始测试集上的模型性能。同样,从直觉上讲,我们期望ε越大,扰动越明显,但是从降低模型准确性的角度来看,攻击越有效。由于这里的数据范围是[0,1][0,1],则epsilon值不得超过1。
  • pretrained_model-使用pytorch / examples / mnist训练的预训练MNIST模型的路径 。为简单起见,请在此处下载预训练的模型。
  • use_cuda-布尔标志,如果需要和可用,则使用CUDA。请注意,带有CUDA的GPU对于本教程而言并不重要,因为CPU不会花费很多时间。
epsilons = [0, .05, .1, .15, .2, .25, .3]
pretrained_model = "data/lenet_mnist_model.pth"
use_cuda=True

受到攻击的模型

如前所述,受到攻击的模型与pytorch / examples / mnist中的 MNIST模型相同 。您可以训练并保存自己的MNIST模型,也可以下载并使用提供的模型。此处的网络定义和测试数据加载器已从MNIST示例复制而来。本部分的目的是定义模型和数据加载器,然后初始化模型并加载预训练的权重。

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)

# MNIST Test dataset and dataloader declaration
test_loader = torch.utils.data.DataLoader(
    datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([
            transforms.ToTensor(),
            ])),
        batch_size=1, shuffle=True)

# Define what device we are using
print("CUDA Available: ",torch.cuda.is_available())
device = torch.device("cuda" if (use_cuda and torch.cuda.is_available()) else "cpu")

# Initialize the network
model = Net().to(device)

# Load the pretrained model
model.load_state_dict(torch.load(pretrained_model, map_location='cpu'))

# Set the model in evaluation mode. In this case this is for the Dropout layers
model.eval()

FGSM攻击

现在,我们可以通过干扰原始输入来定义创建对抗示例的函数。该fgsm_attack函数需要三个输入,图像是原始的干净图像(xx),epsilon是像素方向的扰动量(ϵϵ),而data_grad 是输入图片(∇xJ(θ,x,y)∇xJ(θ,x,y))。该函数然后创建扰动图像为

perturbed_image=image+epsilon∗sign(data_grad)=x+ϵ∗sign(∇xJ(θ,x,y))perturbed_image=image+epsilon∗sign(data_grad)=x+ϵ∗sign(∇xJ(θ,x,y))

最后,为了保持数据的原始范围,将被扰动的图像裁剪到一定范围 [0,1][0,1]。

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

测试功能

最后,本教程的主要结果来自该test 函数。每次调用此测试功能都会在MNIST测试集上执行完整的测试步骤,并报告最终精度。但是,请注意,此功能还需要输入epsilon。这是因为该 test函数报告了受到对手强大攻击的模型的准确性ϵϵ。更具体地说,对于测试集中的每个样本,函数都会计算输入数据的损耗梯度(data_graddata_grad),使用fgsm_attack(perturbed_dataperturbed_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, dont 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

        # Call FGSM Attack
        perturbed_data = fgsm_attack(data, epsilon, data_grad)

        # Re-classify the perturbed image
        output = model(perturbed_data)

        # 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("Epsilon: {}tTest Accuracy = {} / {} = {}".format(epsilon, correct, len(test_loader), final_acc))

    # Return the accuracy and an adversarial example
    return final_acc, adv_examples

奔跑攻击

实现的最后一部分是实际运行攻击。在这里,我们为epsilons输入中的每个epsilon值运行一个完整的测试步骤。对于每个epsilon,我们还保存最终精度,并在接下来的部分中绘制一些成功的对抗示例。请注意,随着ε值的增加,打印的精度如何降低。另外,请注意ϵ=0ϵ=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
第一个结果是精度与epsilon图的关系。如前所述,随着ε的增加,我们预计测试精度会降低。这是因为更大的ε意味着我们朝着将损失最大化的方向迈出了更大的一步。请注意,即使epsilon值是线性间隔的,曲线中的趋势也不是线性的。例如,在ϵ=0.05ϵ=0.05 仅比约低4% ϵ=0ϵ=0,但精度为 ϵ=0.2ϵ=0.2 比25%低 ϵ=0.15ϵ=0.15。另外,请注意,对于10级分类器 ϵ=0.25ϵ=0.25 和 ϵ=0.3ϵ=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值下成功对抗示例的一些示例。绘图的每一行显示不同的ε值。第一行是ϵ=0ϵ=0代表原始“干净”图像且无干扰的示例。每个图像的标题显示“原始分类->对抗分类”。注意,扰动开始变得明显ϵ=0.15ϵ=0.15 并且在 ϵ=0.3ϵ=0.3。但是,在所有情况下,尽管增加了噪音,人类仍然能够识别正确的类别。

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("Eps: {}".format(epsilons[i]), fontsize=14)
        orig,adv,ex = examples[i][j]
        plt.title("{} -> {}".format(orig, adv))
        plt.imshow(ex, cmap="gray")
plt.tight_layout()
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值