首页 > 其他分享 >论文解读(PGD)《Towards deep learning models resistant to adversarial attacks》

论文解读(PGD)《Towards deep learning models resistant to adversarial attacks》

时间:2023-04-22 11:12:48浏览次数:53  
标签:loss PGD Towards nn models self planes targets out

 论文信息

论文标题:Towards deep learning models resistant to adversarial attacks
论文作者:Aleksander Madry, Aleksandar Makelov, Ludwig Schmidt, Dimitris Tsipras, Adrian Vladu
论文来源:ICLR 2018
论文地址:download 
论文代码:download
视屏讲解:click

1 介绍

  对抗攻击

2 方法

2.1 问题建模

  在本文中,作者将对抗样本的攻击防御问题总结为以下公式:

    $\min _{\theta} \rho(\theta), \quad \text { where } \quad \rho(\theta)=\mathbb{E}_{(x, y) \sim \mathcal{D}}\left[\max _{\delta \in \mathcal{S}} L(\theta, x+\delta, y)\right]$

  该问题分为两个部分,分别为 内部损失函数最大化 和 外部经验风险最小化:

    • 先看内部问题,式中  $L(\cdot)$  为损失函数,在神经网络优化中通常取交叉熵,$x$  是原始样本, $ \delta$  为扰动信息,  $S$  为扰动信 息的集合,  $y$  为原始样本的标签。内部问题的优化目标级寻找一个扰动,使得添加扰动后的样本不 属于原始标签的风险最大化;
    • 对于外部最小化问题,  $D$  是数据  $(x, y)$  满足的分布,$\theta$  是深度神经网络的参数,外部问题的目标是寻找参数  $\theta$  使得  $E_{(x, y) \sim D}[L(x, y, \theta)]$  风险最小。即内部问题 是寻找一个原始样本的对抗版本来最大化损失函数,即攻击模型;外部问题则是基于这种攻击方法去训练一个更具鲁棒性的神经网络,以防御对抗样本的攻击;

2.2 问题解决

  作者考虑了两种攻击模型,第一个是专栏已多次提及的快速梯度法(FGSM),文中主要以此作为单步攻击算法的代表,公式如下:

    $x+\varepsilon \operatorname{sgn}\left(\nabla_{x} L(\theta, x, y)\right)$

  第二种是迭代的快速梯度法,本质是在损失函数负方向上进行投影梯度下降(Projection Gradient Descent),简称此算法为PGD,其公式如下:

    $x^{t+1}=\Pi_{x+\mathcal{S}}\left(x^{t}+\alpha \operatorname{sgn}\left(\nabla_{x} L(\theta, x, y)\right)\right)$

3 代码

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms

class BasicBlock(nn.Module):
    expansion = 1
    def __init__(self, in_planes, planes, stride=1):
        super(BasicBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion*planes:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*planes)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        out += self.shortcut(x)
        out = F.relu(out)
        return out

class ResNet(nn.Module):
    def __init__(self, block, num_blocks, num_classes=10):
        super(ResNet, self).__init__()
        self.in_planes = 64

        self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
        self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
        self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
        self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
        self.linear = nn.Linear(512*block.expansion, num_classes)

    def _make_layer(self, block, planes, num_blocks, stride):
        strides = [stride] + [1]*(num_blocks-1)
        layers = []
        for stride in strides:
            layers.append(block(self.in_planes, planes, stride))
            self.in_planes = planes * block.expansion
        return nn.Sequential(*layers)

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.layer1(out)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.layer4(out)
        out = F.avg_pool2d(out, 4)
        out = out.view(out.size(0), -1)
        out = self.linear(out)
        return out

def ResNet18():
    return ResNet(BasicBlock, [2,2,2,2])

learning_rate = 0.1
epsilon = 0.0314
k = 7
alpha = 0.00784
file_name = 'pgd_adversarial_training'

device = 'cuda' if torch.cuda.is_available() else 'cpu'
transform_train = transforms.Compose([transforms.RandomCrop(32, padding=4),transforms.RandomHorizontalFlip(),transforms.ToTensor(),])
transform_test = transforms.Compose([transforms.ToTensor(),])
train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
test_dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True, num_workers=4)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=100, shuffle=False, num_workers=4)

class PGDAttack(object):
    def __init__(self, model,loss=nn.CrossEntropyLoss(),alpha=0.00784,epsilon=0.0314):
        self.model = model
        self.loss = loss

        self.alpha = alpha
        self.epsilon = epsilon

    def perturb(self, x_natural, y):
        x = x_natural.detach()
        x = x + torch.zeros_like(x).uniform_(-epsilon, epsilon)
        for i in range(k):
            x.requires_grad_()
            with torch.enable_grad():
                logits = self.model(x)
                loss = self.loss(logits, y)
            grad = torch.autograd.grad(loss, [x])[0]
            x = x.detach() + self.alpha * torch.sign(grad.detach())
            x = torch.min(torch.max(x, x_natural - self.epsilon), x_natural + self.epsilon)
            x = torch.clamp(x, 0, 1)
        return x

net = ResNet18()
net = net.to(device)
cudnn.benchmark = True

adversary = PGDAttack(net)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9, weight_decay=0.0002)

def train(epoch):
    train_loss = 0
    correct = 0
    total = 0
    net.train()
    for batch_idx, (inputs, targets) in enumerate(train_loader):
        inputs, targets = inputs.to(device), targets.to(device)

        optimizer.zero_grad()

        adv = adversary.perturb(inputs, targets)
        adv_outputs = net(adv)
        loss = criterion(adv_outputs, targets)
        loss.backward()
        optimizer.step()

        train_loss += loss.item()
        _, predicted = adv_outputs.max(1)

        total += targets.size(0)
        correct += predicted.eq(targets).sum().item()

        if batch_idx % 10 == 0:
            print('\nCurrent batch:', str(batch_idx))
            print('Current adversarial train accuracy:', str(predicted.eq(targets).sum().item() / targets.size(0)))
            print('Current adversarial train loss:', loss.item())

    print('\nTotal adversarial train accuarcy:', 100. * correct / total)
    print('Total adversarial train loss:', train_loss)


def test(epoch):
    benign_loss = 0
    adv_loss = 0
    benign_correct = 0
    adv_correct = 0
    total = 0
    net.eval()
    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(test_loader):
            inputs, targets = inputs.to(device), targets.to(device)
            total += targets.size(0)

            outputs = net(inputs)
            loss = criterion(outputs, targets)
            benign_loss += loss.item()
            _, predicted = outputs.max(1)
            benign_correct += predicted.eq(targets).sum().item()

            adv = adversary.perturb(inputs, targets)
            adv_outputs = net(adv)
            loss = criterion(adv_outputs, targets)
            adv_loss += loss.item()

            _, predicted = adv_outputs.max(1)
            adv_correct += predicted.eq(targets).sum().item()

            if batch_idx % 10 == 0:
                print('Current adversarial test accuracy:', str(predicted.eq(targets).sum().item() / targets.size(0)))
                print('Current adversarial test loss:', loss.item())

    print('\nTotal benign test accuarcy:', 100. * benign_correct / total)
    print('Total adversarial test Accuarcy:', 100. * adv_correct / total)
    print('Total benign test loss:', benign_loss)
    print('Total adversarial test loss:', adv_loss)


for epoch in range(0, 200):
    train(epoch)
    test(epoch)

参考

 

 

标签:loss,PGD,Towards,nn,models,self,planes,targets,out
From: https://www.cnblogs.com/BlairGrowing/p/17341931.html

相关文章

  • 报错解决:user.Case: (models.E020) The 'Case.check()' class method is currently ov
    Django在启动时报错,如下:user.Case:(models.E020)The'Case.check()'classmethodiscurrentlyoverriddenby<django.db.models.query_utils.DeferredAttributeobjectat0x0000020331E0AE20>.意思是说:在user.Case.check()在执行的过程中被【django.db.models.query_ut......
  • 关于 ModelScope 的视频 “AI 换脸” 优化方案
    前言    前面一文,初步完成了一下“AI换脸”视频处理程序。完成了视频拆帧,拆帧图片人脸融合,已经音频提取和最后的人脸融合图片的整合(也就是将图片和音频组成视频)。但是在人脸融合部分由于是单线程,处理起来非常耗时,所以这里就对那部分进行优化。 优化前情况  ......
  • 如何用 ModelScope 实现 “AI 换脸” 视频
    前言当下,视频内容火爆,带有争议性或反差大的换脸视频总能吸引人视线。虽然AI换脸在市面上已经流行了许久,相关制作工具或移动应用也是数不胜数。但是多数制作工具多数情况下不是会员就是收费,而且替换模板有限。以下在实战的角度,用阿里ModelScope的图像人脸融合实现一下AI视频......
  • Visualizing MuZero Models
    发表时间:2021文章要点:这篇文章主要想看看muzero里面的model具体学到了什么表征。通过PCA降维的方式,发现最开始编码状态的h函数学到的embedding和动态转移函数g学到的embedding并不统一,存在很大差异。因为muzero里面没有相关的loss来控制他俩一样。然后作者就提出两种loss来约......
  • Django中models模块增删改查
    1django操作数据库1.1创建表和字段fromdjango.dbimportmodelsclassBlog_Model(models.Model):name=models.CharField(db_column='name',max_length=200,blank=True,verbose_name='name')num=models.IntegerField(db_column='num�......
  • models.ForeignKey()的一些相关参数说明
    models.ForeignKey()是DjangoORM中的一个字段类型,用于定义关联关系。在使用models.ForeignKey()时,可以传入一些参数来控制关联行为。以下是一些常用的参数说明:to:指定关联的目标模型类。on_delete:指定当关联对象被删除时的行为。CASCADE或者SET_NULLrelated_name:指定反向......
  • Django之models
    常用字段and非常用字段autofieldint自增列,必须填入参数primary_key=True。当model中如果没有自增列,则自动会创建一个列名为id的列。但是这个基本咋没用过,建表也都是使用的默认idIntegerField一个整数类型,范围在-2147483648to2147483647CharField这个最常用,啥都能用他,......
  • odoo 开发入门教程系列-模型之间的关系(Relations Between Models)
    模型之间的关系(RelationsBetweenModels)上一章介绍了为包含基本字段的模型创建自定义视图。然而,在任何真实的业务场景中,我们都需要不止一个模型。此外,模型之间的链接是必要的。人们可以很容易地想象一个模型包含客户,另一个模型则包含用户列表。你可能需要参考任何现有业务模型......
  • Difformer: Empowering Diffusion Models on the Embedding Space for Text Generatio
    目录概符号说明主要内容GaoZ.,GuoJ.,TanX.,ZhuY.,ZhangF.,BianJ.andXuL.Difformer:Empoweringdiffusionmodelsontheembeddingspacefortextgene......
  • Modelsim SE 下载安装、注册详细教程
    声明:原博客为https://blog.csdn.net/ssj925319/article/details/115353790此文仅为记录安装过程,方便日后查看一、ModelsimSE下载及安装百度网盘下载链接:https://pan......