欢迎光临
我们一直在努力

如何用Foolbox/ART工具箱生成针对图像分类模型的PGD对抗样本?

深入理解与实践:使用Foolbox生成L-inf PGD对抗样本

在AI模型部署到生产环境时,模型的鲁棒性(Robustness)是与准确性同等重要的指标。对抗样本(Adversarial Examples)揭示了深度学习模型的脆弱性。其中,PGD(Projected Gradient Descent,投影梯度下降)攻击是公认最强大的白盒攻击方法之一,常用于基准测试模型的防御能力。

本文将聚焦于如何使用强大的Python库 Foolbox(一个专门用于评估模型鲁棒性的工具箱)来生成L-infinity规范下的PGD对抗样本。

1. 环境准备与依赖安装

我们将使用PyTorch作为后端模型框架,并安装Foolbox及其集成库 foolbox-native


1
2
pip install torch torchvision matplotlib
pip install foolbox foolbox-native

2. 模型加载与Foolbox包装

为了让Foolbox能够理解和操作我们的PyTorch模型,我们需要将模型包装成Foolbox专用的 FModel 接口。这里我们使用一个预训练的ResNet18模型作为目标模型。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms
import numpy as np
from foolbox import PyTorchModel, samples
from foolbox.attacks import LinfProjectedGradientDescent

# 1. 加载预训练模型
model = models.resnet18(pretrained=True).eval()
preprocessing = dict(
    mean=[0.485, 0.456, 0.406],
    std=[0.229, 0.224, 0.225],
    axis=-3
)

# 2. 将PyTorch模型包装成Foolbox FModel
# bounds定义了输入数据的范围,通常是[0, 1]或[0, 255]
fmodel = PyTorchModel(model, bounds=(0, 1), preprocessing=preprocessing)

print("模型已成功包装。")

3. 数据准备

我们将使用一个示例图像(或生成一个随机Tensor)作为PGD攻击的起点。


1
2
3
4
5
6
# 获取一个示例图像(这里使用Foolbox自带的示例,确保数据在[0, 1]范围内)
images, labels = samples.imagenet_example(data_format="channels_first")

# 打印原始预测结果
original_prediction = fmodel.forward(images).argmax(axis=-1)
print(f"原始标签: {labels[0].item()}, 模型预测: {original_prediction[0].item()}")

4. PGD攻击实现

PGD攻击通过迭代地计算梯度,并在每一步将扰动投影回 L-infinity 规范球内(即 $\ell_\infty$ 约束,保证扰动在像素空间中不会超过 $\epsilon$)。

关键参数包括:
1. ****epsilon** ($\\epsilon$):** 扰动的最大幅度(L-inf范数)。
2. ****steps: 迭代次数。
3. ****stepsize: 每步梯度下降的步长。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 实例化 L-inf PGD 攻击
# 设定最大扰动 epsilon = 8/255 (约 0.0313)
eps = 8 / 255
attack = LinfProjectedGradientDescent(steps=40, stepsize=eps/4)

# 运行攻击
raw_advs, clipped_advs, success = attack(
    fmodel,
    images,
    labels,
    epsilons=eps
)

# clipped_advs 是最终生成的对抗样本
adv_images = clipped_advs

# 5. 评估对抗样本的成功率

# 获取对抗样本的预测结果
adv_prediction = fmodel.forward(adv_images).argmax(axis=-1)

print(f"PGD攻击是否成功: {success.item()}")
print(f"对抗样本预测: {adv_prediction[0].item()}")

# 验证:如果成功,则 adv_prediction 应与 original_prediction 或 labels 不同
if success.item() and adv_prediction[0].item() != labels[0].item():
    print("PGD攻击成功导致模型误分类!")
else:
    print("PGD攻击未成功,模型鲁棒性表现良好(在当前参数下)。")

# 可视化扰动(可选)
import matplotlib.pyplot as plt

perturbation = adv_images - images

plt.figure(figsize=(12, 4))

plt.subplot(1, 3, 1)
plt.title("Original Image")
plt.imshow(images[0].permute(1, 2, 0).cpu().numpy())

plt.subplot(1, 3, 2)
plt.title("Perturbation (Scaled)")
# 归一化显示扰动
plt.imshow(perturbation[0].permute(1, 2, 0).cpu().numpy() * 10)

plt.subplot(1, 3, 3)
plt.title("Adversarial Image")
plt.imshow(adv_images[0].permute(1, 2, 0).cpu().numpy())

# plt.show() # 如果在交互式环境中运行,取消注释

总结

通过Foolbox的统一接口,我们能够轻松地对任何标准的PyTorch或TensorFlow模型实施复杂的白盒攻击,如PGD。在AI基础设施建设中,定期使用这种方法测试模型的鲁棒性是确保系统安全和可靠性的重要环节。PGD生成的对抗样本可以进一步用于对抗训练(Adversarial Training),以提高模型抵抗这类攻击的能力。

【本站文章皆为原创,未经允许不得转载】:汤不热吧 » 如何用Foolbox/ART工具箱生成针对图像分类模型的PGD对抗样本?
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址