Pytorch | 利用NI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击
Pytorch | 利用NI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击
- CIFAR数据集
- NI-FGSM介绍
- 背景
- 算法原理
- NI-FGSM代码实现
- NI-FGSM算法实现
- 攻击效果
- 代码汇总
- nifgsm.py
- train.py
- advtest.py
之前已经针对CIFAR10训练了多种分类器:
Pytorch | 从零构建AlexNet对CIFAR10进行分类
Pytorch | 从零构建Vgg对CIFAR10进行分类
Pytorch | 从零构建GoogleNet对CIFAR10进行分类
Pytorch | 从零构建ResNet对CIFAR10进行分类
Pytorch | 从零构建MobileNet对CIFAR10进行分类
Pytorch | 从零构建EfficientNet对CIFAR10进行分类
Pytorch | 从零构建ParNet对CIFAR10进行分类
本篇文章我们使用Pytorch实现NI-FGSM对CIFAR10上的ResNet分类器进行攻击.
CIFAR数据集
CIFAR-10数据集是由加拿大高级研究所(CIFAR)收集整理的用于图像识别研究的常用数据集,基本信息如下:
- 数据规模:该数据集包含60,000张彩色图像,分为10个不同的类别,每个类别有6,000张图像。通常将其中50,000张作为训练集,用于模型的训练;10,000张作为测试集,用于评估模型的性能。
- 图像尺寸:所有图像的尺寸均为32×32像素,这相对较小的尺寸使得模型在处理该数据集时能够相对快速地进行训练和推理,但也增加了图像分类的难度。
- 类别内容:涵盖了飞机(plane)、汽车(car)、鸟(bird)、猫(cat)、鹿(deer)、狗(dog)、青蛙(frog)、马(horse)、船(ship)、卡车(truck)这10个不同的类别,这些类别都是现实世界中常见的物体,具有一定的代表性。
下面是一些示例样本:
NI-FGSM介绍
NI-FGSM(Nesterov Iterative Fast Gradient Sign Method)即涅斯捷罗夫迭代快速梯度符号法,是一种在对抗攻击领域中对FGSM进行改进的迭代攻击算法,以下是其详细介绍:
背景
- 传统的FGSM及其一些迭代改进版本如I-FGSM等,在生成对抗样本时存在一些局限性,例如可能会在迭代过程中陷入局部最优,导致攻击效果不够理想或生成的对抗样本转移性较差。NI-FGSM借鉴了优化算法中的Nesterov加速梯度法的思想,旨在更有效地利用梯度信息,提高攻击的效率和效果。
算法原理
- 初始化:与其他对抗攻击方法类似,需要一个待攻击的目标模型 f f f、损失函数 J J J、原始图像 x x x及其对应的真实标签 y y y,同时还需要设定攻击步长 ϵ \epsilon ϵ、迭代次数 T T T等参数。
- 迭代更新:在每次迭代 t t t中,首先计算一个“前瞻”点 x t l o o k a h e a d x_{t}^{lookahead} xtlookahead,它是基于当前迭代点 x t x_{t} xt和上一次迭代的梯度信息进行的一个预估更新点,公式为 x t l o o k a h e a d = x t + α ⋅ sign ( ∇ x J ( x t , y ) ) x_{t}^{lookahead}=x_{t}+\alpha \cdot \text{sign}\left(\nabla_{x} J\left(x_{t}, y\right)\right) xtlookahead=xt+α⋅sign(∇xJ(xt,y)),其中 α \alpha α是一个类似于步长的参数。然后,计算在这个“前瞻”点处的损失梯度 ∇ x J ( x t l o o k a h e a d , y ) \nabla_{x} J\left(x_{t}^{lookahead}, y\right) ∇xJ(xtlookahead,y),并根据该梯度来更新当前迭代点 x t x_{t} xt,更新公式为 x t + 1 = x t + ϵ ⋅ sign ( ∇ x J ( x t l o o k a h e a d , y ) ) x_{t + 1}=x_{t}+\epsilon \cdot \text{sign}\left(\nabla_{x} J\left(x_{t}^{lookahead}, y\right)\right) xt+1=xt+ϵ⋅sign(∇xJ(xtlookahead,y))。
- 投影操作:与其他对抗攻击方法一样,为了确保生成的对抗样本在合理的范围内,如像素值在 [ 0 , 1 ] [0, 1] [0,1]或 [ − 1 , 1 ] [-1, 1] [−1,1]之间,需要对每次迭代更新后的样本进行投影操作。
NI-FGSM代码实现
NI-FGSM算法实现
import torch
import torch.nn as nndef NI_FGSM(model, criterion, original_images, labels, epsilon, alpha=0.001, num_iterations=10):"""NI-FGSM (Nesterov Iterative Fast Gradient Sign Method) 参数:- model: 要攻击的模型,需为继承自nn.Module的类实例,实现了前向传播逻辑- criterion: 损失函数,如nn.CrossEntropyLoss等,用于计算模型输出与真实标签间的损失- original_images: 原始输入图像数据,形状为 (batch_size, channels, height, width) 的torch.Tensor,值范围通常在[0, 1]- labels: 对应的真实标签,形状为 (batch_size,) 的torch.Tensor- epsilon: 最大扰动幅度,控制生成对抗样本的扰动程度- alpha: 每次迭代的步长,用于更新对抗样本- num_iterations: 迭代次数,即执行多少次梯度更新来生成对抗样本"""# 复制原始图像作为初始的对抗样本,并设置其需要计算梯度perturbed_image = original_images.clone().detach().requires_grad_(True)for _ in range(num_iterations):# 计算 "前瞻" 点(基于当前对抗样本和当前梯度方向预估的下一步位置)lookahead_image = perturbed_image + alpha * torch.sign(perturbed_image.grad.data) if perturbed_image.grad is not None else perturbed_image# 前向传播得到模型输出outputs = model(lookahead_image)# 计算损失loss = criterion(outputs, labels)# 清空模型之前的梯度信息model.zero_grad()# 反向传播计算梯度loss.backward()# 获取当前梯度数据data_grad = lookahead_image.grad.data if lookahead_image.grad is not None else torch.zeros_like(original_images)# 计算符号梯度sign_data_grad = torch.sign(data_grad)# 更新对抗样本perturbed_image = perturbed_image + epsilon * sign_data_grad# 投影操作,确保扰动后的图像仍在合理范围内(这里假设图像范围是[0, 1])perturbed_image = torch.where(perturbed_image > original_images + epsilon,original_images + epsilon, perturbed_image)perturbed_image = torch.where(perturbed_image < original_images - epsilon,original_images - epsilon, perturbed_image)perturbed_image = torch.clamp(perturbed_image, 0, 1).detach().requires_grad_(True)return perturbed_image
攻击效果
代码汇总
nifgsm.py
import torch
import torch.nn as nn
import torch.nn.functional as Fdef NI_FGSM(model, criterion, original_images, labels, epsilon, alpha=0.001, num_iterations=10, mu=1):"""NI-FGSM (Natural Adversarial Examples Generation with Fast Gradient Sign Method) 参数:- model: 要攻击的模型- criterion: 损失函数- original_images: 原始图像- labels: 原始图像的标签- epsilon: 最大扰动幅度- alpha: 每次迭代的步长- num_iterations: 迭代次数- mu: 用于计算自然噪声的参数,控制噪声的大小和影响"""# 复制原始图像作为初始的对抗样本perturbed_image = original_images.clone().detach().requires_grad_(True)for _ in range(num_iterations):# 生成自然噪声noise = torch.randn_like(original_images).to(original_images.device)noise = mu * noise / torch.std(noise, dim=(1, 2, 3), keepdim=True)noise_image = original_images + noiseoutputs = model(noise_image)loss = criterion(outputs, labels)model.zero_grad()loss.backward()data_grad = noise_image.grad.datasign_data_grad = data_grad.sign()# 更新对抗样本perturbed_image = perturbed_image + alpha * sign_data_grad# 投影操作,确保扰动后的图像仍在合理范围内(这里假设图像范围是[0, 1])perturbed_image = torch.where(perturbed_image > original_images + epsilon,original_images + epsilon, perturbed_image)perturbed_image = torch.where(perturbed_image < original_images - epsilon,original_images - epsilon, perturbed_image)perturbed_image = torch.clamp(perturbed_image, 0, 1).detach().requires_grad_(True)return perturbed_image
train.py
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from models import ResNet18# 数据预处理
transform_train = transforms.Compose([transforms.RandomCrop(32, padding=4),transforms.RandomHorizontalFlip(),transforms.ToTensor(),transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])transform_test = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])# 加载Cifar10训练集和测试集
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=False, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2)testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=False, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)# 定义设备(GPU或CPU)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")# 初始化模型
model = ResNet18(num_classes=10)
model.to(device)# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)if __name__ == "__main__":# 训练模型for epoch in range(10): # 可以根据实际情况调整训练轮数running_loss = 0.0for i, data in enumerate(trainloader, 0):inputs, labels = data[0].to(device), data[1].to(device)optimizer.zero_grad()outputs = model(inputs)loss = criterion(outputs, labels)loss.backward()optimizer.step()running_loss += loss.item()if i % 100 == 99:print(f'Epoch {epoch + 1}, Batch {i + 1}: Loss = {running_loss / 100}')running_loss = 0.0torch.save(model.state_dict(), f'weights/epoch_{epoch + 1}.pth')print('Finished Training')
advtest.py
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from models import *
from attacks import *
import ssl
import os
from PIL import Image
import matplotlib.pyplot as pltssl._create_default_https_context = ssl._create_unverified_context# 定义数据预处理操作
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.491, 0.482, 0.446), (0.247, 0.243, 0.261))])# 加载CIFAR10测试集
testset = torchvision.datasets.CIFAR10(root='./data', train=False,download=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=128,shuffle=False, num_workers=2)# 定义设备(GPU优先,若可用)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model = ResNet18(num_classes=10).to(device)criterion = nn.CrossEntropyLoss()# 加载模型权重
weights_path = "weights/epoch_10.pth"
model.load_state_dict(torch.load(weights_path, map_location=device))if __name__ == "__main__":# 在测试集上进行FGSM攻击并评估准确率model.eval() # 设置为评估模式correct = 0total = 0epsilon = 0.01 # 可以调整扰动强度for data in testloader:original_images, labels = data[0].to(device), data[1].to(device)original_images.requires_grad = Trueattack_name = 'MI-FGSM'if attack_name == 'FGSM':perturbed_images = FGSM(model, criterion, original_images, labels, epsilon)elif attack_name == 'BIM':perturbed_images = BIM(model, criterion, original_images, labels, epsilon)elif attack_name == 'MI-FGSM':perturbed_images = MI_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name == 'NI-FGSM':perturbed_images = NI_FGSM(model, criterion, original_images, labels, epsilon)perturbed_outputs = model(perturbed_images)_, predicted = torch.max(perturbed_outputs.data, 1)total += labels.size(0)correct += (predicted == labels).sum().item()accuracy = 100 * correct / total# Attack Success RateASR = 100 - accuracyprint(f'Load ResNet Model Weight from {weights_path}')print(f'epsilon: {epsilon}')print(f'ASR of {attack_name} : {ASR}%')