教程原网址:https://pytorch.apachecn.org/#/docs/1.7/21
我们将通过图像分类器上的示例来探讨该主题。 具体而言,我们将使用最流行的一种攻击方法,即快速梯度符号攻击(FGSM)来欺骗 MNIST 分类器。


跑的代码需要用到的数据集和预训练模型自行下载,最好去官网下载。

跑通的代码如下:

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 pltepsilons = [0, .05, .1, .15, .2, .25, .3]
pretrained_model = "../data/lenet_mnist_model.pth"
use_cuda=True# LeNet Model definition
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 attack code
def fgsm_attack(image, epsilon, data_grad):# Collect the element-wise sign of the data gradientsign_data_grad = data_grad.sign()# Create the perturbed image by adjusting each pixel of the input imageperturbed_image = image + epsilon*sign_data_grad# Adding clipping to maintain [0,1] rangeperturbed_image = torch.clamp(perturbed_image, 0, 1)# Return the perturbed imagereturn perturbed_imagedef test( model, device, test_loader, epsilon ):# Accuracy countercorrect = 0adv_examples = []# Loop over all examples in test setfor data, target in test_loader:# Send the data and label to the devicedata, target = data.to(device), target.to(device)# Set requires_grad attribute of tensor. Important for Attackdata.requires_grad = True# Forward pass the data through the modeloutput = 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 onif init_pred.item() != target.item():continue# Calculate the lossloss = F.nll_loss(output, target)# Zero all existing gradientsmodel.zero_grad()# Calculate gradients of model in backward passloss.backward()# Collect datagraddata_grad = data.grad.data# Call FGSM Attackperturbed_data = fgsm_attack(data, epsilon, data_grad)# Re-classify the perturbed imageoutput = model(perturbed_data)# Check for successfinal_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probabilityif final_pred.item() == target.item():correct += 1# Special case for saving 0 epsilon examplesif (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 laterif 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 epsilonfinal_acc = correct/float(len(test_loader))print("Epsilon: {}\tTest Accuracy = {} / {} = {}".format(epsilon, correct, len(test_loader), final_acc))# Return the accuracy and an adversarial examplereturn final_acc, adv_examplesaccuracies = []
examples = []# Run test for each epsilon
for eps in epsilons:acc, ex = test(model, device, test_loader, eps)accuracies.append(acc)examples.append(ex)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()# 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 += 1plt.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()

运行结果:

D:\code\pytorch\fsgm-gan
(pytorch) λ python fsgm-gan.py
CUDA Available:  True
Epsilon: 0      Test Accuracy = 9810 / 10000 = 0.981
Epsilon: 0.05   Test Accuracy = 9426 / 10000 = 0.9426
Epsilon: 0.1    Test Accuracy = 8510 / 10000 = 0.851
Epsilon: 0.15   Test Accuracy = 6826 / 10000 = 0.6826
Epsilon: 0.2    Test Accuracy = 4301 / 10000 = 0.4301
Epsilon: 0.25   Test Accuracy = 2082 / 10000 = 0.2082
Epsilon: 0.3    Test Accuracy = 869 / 10000 = 0.0869

pytorch1.7教程实验——对抗示例生成FGSM相关推荐

  1. pytorch1.7教程实验——DCGAN生成对抗网络

    教程原网址:https://pytorch.apachecn.org/#/docs/1.7/22 DCGAN 是上述 GAN 的直接扩展,不同之处在于,DCGAN 分别在判别器和生成器中分别使用卷积和 ...

  2. PyTorch 1.0 中文官方教程:对抗性示例生成

    译者:cangyunye 作者: Nathan Inkawhich 如果你正在阅读这篇文章,希望你能理解一些机器学习模型是多么有效.现在的研究正在不断推动ML模型变得更快.更准确和更高效.然而,在设计 ...

  3. pytorch1.7教程实验——分类器训练

    近来想大致总结一下自己知识学习的脉络,发现自己除了大量的工程经验外,对模型算法的研究还是不够深入,而且大多都是关于目标检测方向,锚框或非锚框以及transformer,其他的涉猎不足,认识不够清晰.而 ...

  4. pytorch1.7教程实验——使用 TensorBoard 可视化模型,数据和训练

    可视化还是很重要的,作为官方教程的开头部分,还是有必要好好看看,毕竟使用服务器没有桌面,不用直接使用画图函数看图像,使用tensorboard可以作为一种可视化方法,而且训练过程的损失曲线等也可以通过 ...

  5. pytorch1.7教程实验——迁移学习训练卷积神经网络进行图像分类

    只是贴上跑通的代码以供参考学习 参考网址:迁移学习训练卷积神经网络进行图像分类 需要用到的数据集下载网址: https://download.pytorch.org/tutorial/hymenopt ...

  6. pytorch生成对抗示例

    pytorch生成对抗示例 本文对ML(机器学习)模型的安全漏洞的认识,并将深入了解对抗性机器学习的热门话题.图像添加难以察觉的扰动会导致模型性能大不相同.通过图像分类器上的示例探讨该主题.使用第一种 ...

  7. 动量迭代式对抗噪声生成方法 | VALSE2018年度杰出学生论文奖

    编者按:深度模型的精度和速度长期以来成为了评价模型性能的核心标准,但即使性能优越的深度神经网络也很容易被对抗样本攻击.因此,寻找到合适的对抗攻击策略可有效提升模型本身的鲁棒性.本文作者提出了基于动量的 ...

  8. GAN 系列的探索与pytorch实现 (数字对抗样本生成)

    GAN 系列的探索与pytorch实现 (数字对抗样本生成) 文章目录 GAN 系列的探索与pytorch实现 (数字对抗样本生成) GAN的简单介绍 生成对抗网络 GAN 的基本原理 大白话版本 非 ...

  9. FGPM:文本对抗样本生成新方法

    ©PaperWeekly 原创 · 作者|孙裕道 学校|北京邮电大学博士生 研究方向|GAN图像生成.情绪对抗样本生成 论文标题: Fast Gradient Projection Method fo ...

最新文章

  1. error LNK2019: 无法解析的外部符号,该符号在函数 _main 中被引用的解决方法
  2. Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论
  3. java培训:Java的十大算法
  4. SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
  5. iptables实现访问A的请求重定向到B
  6. 幻读(phantom read)
  7. MariaDB 脚本
  8. angular面试题及答案_关于最流行的Angular问题的StackOverflow上的48个答案
  9. 系统权限安全责任书_权限越大,责任越大
  10. 德国人认真起来,自己都怕!
  11. java web ssh jar_java web 汽车美容管理系统 ssh 毕设作品
  12. 更新Sogou代理服务器程序,支持HTTPS
  13. IAR软件调试延时时间
  14. 各代iphone尺寸_iPhone12系列尺寸对比-历代iPhone机型尺寸比较
  15. myBattery电池应用正式登陆WP8
  16. 编写Shell脚本实现用户管理
  17. 在JS中的数字存储问题
  18. html颜色代码大全word,DIV+CSS网页设计之网页配色方案
  19. Esp8266 进阶之路20 【高级篇】深入学习esp8266的esp now模式组网,仿机智云做一个小网关,实现无需网络下轻松彼此连接通讯交互数据。(附带Demo)
  20. 2 数据分析之Numpy模块(1)

热门文章

  1. 微信开发SDK,Jeewx-Api 1.3.1 版本发布
  2. good-Windows下搭建PHP开发环境(Apache+PHP+MySQL)
  3. Excel中PMT计算月供函数的java实现
  4. 前端开发工具包-WijmoJS,部署授权详解
  5. Android学习笔记(二)基础知识(1)
  6. 并发数据结构- 1.1.1 性能
  7. zip-gzip-bzip2_压缩文件
  8. list接口中的常用方法例子
  9. vba编程把纯文本转换成html,如何将一列文本与html标签转换为Excel中的vba格式文本...
  10. linux查看帮助信息,命令帮助信息的获取