目录

  • 1 笔者言
  • 2 coding
    • 2.1 训练模型
    • 2.2 DeepFool对抗样本生成
    • 2.3 测试鲁棒性
    • 2.4 可视化展示
  • 附录

1 笔者言

  • 虽说标题有DeepFool原理,但能力有限,这个我确实讲不清楚。与FGSM,BIM,MIM,CW等生成对抗样本的方法相比,deepfool是最难的了。给你推荐一个我看懂了的文章,但切记,想要真正明白deepfool的原理,就一定要耐下性子认真看,还要多动笔画示意图。言至此,传送门
  • 本文主要讲解代码,完成生成deepfool对抗样本的完整过程。直接复制代码就能跑。相信我,不骗你。
  • 使用pytorch实现deepfool。pytorch不会?跳转
  • CVPR 2016 论文地址

2 coding

  • 实验步骤:
  1. 训练一个简单模型(mnist手写数字分类任务)
  2. 通过该模型生成对抗样本
  3. 测试生成对抗样本的鲁棒性
  4. 可视化展示对抗样本效果

2.1 训练模型

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
from tqdm import tqdm# 加载mnist数据集
test_loader = torch.utils.data.DataLoader(datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([transforms.ToTensor(),])),batch_size=10, shuffle=True)
train_loader = torch.utils.data.DataLoader(datasets.MNIST('../data', train=True, download=True, transform=transforms.Compose([transforms.ToTensor(),])),batch_size=10, shuffle=True)# 超参数设置
batch_size = 10
epoch = 1
learning_rate = 0.001
# 生成对抗样本的个数
adver_nums = 1000# 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)# 选择设备
device = torch.device("cuda" if (torch.cuda.is_available()) else "cpu")# 初始化网络,并定义优化器
simple_model = Net().to(device)
optimizer1 = torch.optim.SGD(simple_model.parameters(),lr = learning_rate,momentum=0.9)
print (simple_model)

Output:

  • 如上代码,下载数据,设置超参数以及构建模型
  • 如下代码,开始训练模型,并进行测试,观察模型准确率
# 训练模型
def train(model,optimizer):for i in range(epoch):for j,(data,target) in tqdm(enumerate(train_loader)):data = data.to(device)target = target.to(device)logit = model(data)loss = F.nll_loss(logit,target)model.zero_grad()# 如下:因为其中的loss是单个tensor就不能用加上一个tensor的维度限制loss.backward()# 如下有两种你形式表达,一种是原生,一种是使用optim优化函数直接更新参数# 为什么原生的训练方式没有效果???代表参数没有更新,就离谱。# 下面的detach与requires_grad_有讲究哦,终于明白了;但是为什么下面代码不能work还是没搞懂# for params in model.parameters():#   params = (params - learning_rate * params.grad).detach().requires_grad_()optimizer.step()if j % 1000 == 0:print ('第{}个数据,loss值等于{}'.format(j,loss))
train(simple_model,optimizer1)# eval eval ,老子被你害惨了
# 训练完模型后,要加上,固定DROPOUT层
simple_model.eval()# 模型测试
def test(model,name):correct_num = torch.tensor(0).to(device)for j,(data,target) in tqdm(enumerate(test_loader)):data = data.to(device)target = target.to(device)logit = model(data)pred = logit.max(1)[1]num = torch.sum(pred==target)correct_num = correct_num + numprint (correct_num)print ('\n{} correct rate is {}'.format(name,correct_num/10000))
test(simple_model,'simple model')

Output:

2.2 DeepFool对抗样本生成

import numpy as np
from torch.autograd import Variable
import torch as torch
import copy
# 下面导入的类是很早版本的,现在版本已经没有了
# from torch.autograd.gradcheck import zero_gradientsdef deepfool(image, net, num_classes=10, overshoot=0.02, max_iter=100):is_cuda = torch.cuda.is_available()if is_cuda:# print("Using GPU")image = image.cuda()net = net.cuda()f_image = net.forward(Variable(image[None, :, :, :], requires_grad=True)).data.cpu().numpy().flatten()I = (np.array(f_image)).flatten().argsort()[::-1]I = I[0:num_classes]label = I[0]input_shape = image.cpu().numpy().shapepert_image = copy.deepcopy(image)w = np.zeros(input_shape)r_tot = np.zeros(input_shape)loop_i = 0x = Variable(pert_image[None, :], requires_grad=True)fs = net.forward(x)fs_list = [fs[0,I[k]] for k in range(num_classes)]k_i = labelwhile k_i == label and loop_i < max_iter:pert = np.inffs[0, I[0]].backward(retain_graph=True)grad_orig = x.grad.data.cpu().numpy().copy()for k in range(1, num_classes):if x.grad is not None:x.grad.zero_()fs[0, I[k]].backward(retain_graph=True)cur_grad = x.grad.data.cpu().numpy().copy()# set new w_k and new f_kw_k = cur_grad - grad_origf_k = (fs[0, I[k]] - fs[0, I[0]]).data.cpu().numpy()pert_k = abs(f_k)/np.linalg.norm(w_k.flatten())# determine which w_k to useif pert_k < pert:pert = pert_kw = w_k# compute r_i and r_tot# Added 1e-4 for numerical stabilityr_i =  (pert+1e-4) * w / np.linalg.norm(w)r_tot = np.float32(r_tot + r_i)if is_cuda:pert_image = image + (1+overshoot)*torch.from_numpy(r_tot).cuda()else:pert_image = image + (1+overshoot)*torch.from_numpy(r_tot)x = Variable(pert_image, requires_grad=True)fs = net.forward(x)k_i = np.argmax(fs.data.cpu().numpy().flatten())loop_i += 1r_tot = (1+overshoot)*r_totreturn r_tot, loop_i, label, k_i, pert_image# 这几个变量主要用于之后的测试以及可视化
adver_example_by_FOOL = torch.zeros((batch_size,1,28,28)).to(device)
adver_target = torch.zeros(batch_size).to(device)
clean_example = torch.zeros((batch_size,1,28,28)).to(device)
clean_target = torch.zeros(batch_size).to(device)
# 从test_loader中选取1000个干净样本,使用deepfool来生成对抗样本
for i,(data,target) in enumerate(test_loader):if i >= adver_nums/batch_size :breakif i == 0:clean_example = dataelse:clean_example = torch.cat((clean_example,data),dim = 0)cur_adver_example_by_FOOL = torch.zeros_like(data).to(device)for j in range(batch_size):r_rot,loop_i,label,k_i,pert_image = deepfool(data[j],simple_model)cur_adver_example_by_FOOL[j] = pert_image# 使用对抗样本攻击VGG模型pred = simple_model(cur_adver_example_by_FOOL).max(1)[1]# print (simple_model(cur_adver_example_by_FOOL).max(1)[1])if i == 0:adver_example_by_FOOL = cur_adver_example_by_FOOLclean_target = targetadver_target = predelse:adver_example_by_FOOL = torch.cat((adver_example_by_FOOL , cur_adver_example_by_FOOL), dim = 0)clean_target = torch.cat((clean_target,target),dim = 0)adver_target = torch.cat((adver_target,pred),dim = 0)print (adver_example_by_FOOL.shape)
print (adver_target.shape)
print (clean_example.shape)
print (clean_target.shape)
  • 如上,最核心的代码就是deepfool函数。deepfool的函数,论文作者是发表在了github上的,飞过去。

  • deepfool函数实现也不难,只要你理解了deepfool的原理,只要按照如下图的算法实现即可。关键还是理解难啊!!!

  • 如上,对抗样本已经生成,存储在变量 adver_example_by_FOOL 中。

  • 其中 adver_target,clean_example,clean_target,都是为了后面的可视化作准备。

2.3 测试鲁棒性

  • 我们希望生成的对抗样本能使得靶模型有很低的准确率,即攻击效果好
import torch.utils.data as Data
def adver_attack_vgg(model,adver_example,target,name):adver_dataset = Data.TensorDataset(adver_example, target)loader = Data.DataLoader(dataset=adver_dataset,      # 数据,封装进Data.TensorDataset()类的数据batch_size=batch_size      # 每块的大小)correct_num = torch.tensor(0).to(device)for j,(data,target) in tqdm(enumerate(loader)):data = data.to(device)target = target.to(device)pred = model.forward(data).max(1)[1]num = torch.sum(pred==target)correct_num = correct_num + numprint (correct_num)print ('\n{} correct rate is {}'.format(name,correct_num/adver_nums))adver_attack_vgg(simple_model,adver_example_by_FOOL,clean_target,'simple model')

Output:

  • 可以看出,deepfool的攻击效果是非常好的,模型分类准确度只达到了一个百分点。

2.4 可视化展示

def plot_clean_and_adver(adver_example,adver_target,clean_example,clean_target):n_cols = 5n_rows = 5cnt = 1cnt1 = 1plt.figure(figsize=(n_cols*4,n_rows*2))for i in range(n_cols):for j in range(n_rows):plt.subplot(n_cols,n_rows*2,cnt1)plt.xticks([])plt.yticks([])plt.title("{} -> {}".format(clean_target[cnt], adver_target[cnt]))plt.imshow(clean_example[cnt].reshape(28,28).to('cpu').detach().numpy(),cmap='gray')plt.subplot(n_cols,n_rows*2,cnt1+1)plt.xticks([])plt.yticks([])# plt.title("{} -> {}".format(clean_target[cnt], adver_target[cnt]))plt.imshow(adver_example[cnt].reshape(28,28).to('cpu').detach().numpy(),cmap='gray')cnt = cnt + 1cnt1 = cnt1 + 2plt.show()plot_clean_and_adver(adver_example_by_FOOL,adver_target,clean_example,clean_target)

Output:

  • 效果多好,你放大了看。左边是干净样本,右边是对抗样本,放在一起好对比效果。

附录

代码地址:
https://colab.research.google.com/drive/1g9i75kwQbw5vJbAjrZ87NA0P_wb7KXfw?usp=sharing

对抗样本之DeepFool原理coding相关推荐

  1. 对抗样本之MIM原理coding

    目录 1 引言 2 MIM原理 3 coding 3.1 训练模型 3.2 MIM对抗样本生成 3.3 可视化展示 附录 1 引言 MIM,即基于动量的基本迭代法.在BIM上加了动量的操作.不会BIM ...

  2. 对抗样本之CW原理coding

    目录 1 引言 2 算法详解 2.1 常人思路 2.2 CW算法思路 3 攻击直观对比 4 总结 5 附录 1 引言 本文采用手稿模拟的角度,尽量使读者较为直白的面对冷冰冰的公式. 抛去CW算法不谈. ...

  3. 《Comprehensive Privacy Analysis of Deep Learning》补完以及Deepfool对抗样本构造算法

    本文关于推理攻击领域关于深度学习的隐私性的全面分析<Comprehensive Privacy Analysis of Deep Learning>,以及介绍Deepfool对抗样本构造算 ...

  4. AI 以 5:0 比分击败美顶级飞行员;经典对抗攻击 Deepfool 原理详解

    开发者社区技术周刊又和大家见面了,快来看看这周有哪些值得我们开发者关注的重要新闻吧. 2019 年全球公共云服务市场总额达 2334 亿美元 新里程碑!IBM 宣布最高量子体积 64 马斯克将通过实时 ...

  5. 对抗样本生成算法复现代码解析:FGSM和DeepFool

    参考链接: https://www.jianshu.com/p/72c072551e10 论文地址: FGSM:地址 DeepFool:地址 导入包 import numpy as np import ...

  6. 如何看待机器视觉的“对抗样本”问题,其原理是什么?

    这两天正好看过一篇相关的paper,"EXPLAINING AND HARNESSING ADVERSARIAL EXAMPLES" 作者是Ian Goodfellow.Jonat ...

  7. Push Pull:注意力攻击生成可迁移对抗样本

    1 引言  该论文是关于对抗样本可迁移性的文章.在该论文中,作者提出了一种可迁移注意力攻击(TAA\mathrm{TAA}TAA),该方法基于关注图像特定区域和特征对干净图像进行对抗扰动.另外,作者还 ...

  8. 【AI安全之对抗样本】入门知识

    文章目录 00 前言 01 深度学习脆弱性 1.1 偷取模型 1.2 数据投毒 02 对抗样本(adversarial examples) 2.1 对抗样本定义 2.2 对抗样本原理 2.3 针对图像 ...

  9. ACL2020 | 使用强化学习为机器翻译生成对抗样本

    2020-07-12 03:08:49 本文介绍的是 ACL 2020 论文<A Reinforced Generation of Adversarial Examples for Neural ...

  10. [当人工智能遇上安全] 1.人工智能真的安全吗?浙大团队外滩大会分享AI对抗样本技术

    您或许知道,作者后续分享网络安全的文章会越来越少.但如果您想学习人工智能和安全结合的应用,您就有福利了,作者将重新打造一个<当人工智能遇上安全>系列博客,详细介绍人工智能与安全相关的论文. ...

最新文章

  1. JAVA抽象类和接口的区别【附经典分析用例Door】
  2. javascript检测浏览器精简版
  3. XMPP接受发送消息
  4. asp.net cache
  5. 刚体运动中变换矩阵的逆
  6. 删除某个时间段之前的文件
  7. imageset matlab,如何以imageSet或imageDataStore的形式向MATLAB中的BagOfFeatures()函數提供輸入?...
  8. 百度高级Java三面题目!涵盖JVM +Java锁+分布式等
  9. python旋转排序数组_LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现...
  10. dubbo控制台安装
  11. 新建android模拟器无法拨号 真机可以拨号,自己想弄个手机拨号器,可是在模拟器上没有反应,能帮帮忙吗?...
  12. DVWA系列之11 Brute Force中的密码绕过
  13. 过滤掉文本中的javascript标签代码
  14. 基于樽海鞘群算法的线性规划求解matlab程序
  15. 贴片电容器容量怎么换算?
  16. Java基于opencv实现图像数字识别(二)—基本流程
  17. Python之判断闰年
  18. 微信公众号文章中插入的图片如何实现滑动效果
  19. iOS视频播放全屏效果实现
  20. 记录:COMSOL仿真——光子晶体光纤

热门文章

  1. 谈一下我对OOP的了解
  2. 计算机中 加减运算 的 实现原理
  3. python爬虫构建国外代理池_Python爬虫入门(四)教你免费拥有自己的代理IP池
  4. 相比于深度学习,传统的机器学习算法难道就此没落了吗,还有必要去学习吗?
  5. Java、JSP报刊订阅管理系统的设计与实现
  6. _itemmod_extract_enchant
  7. 2022.10.3-10.8 AI行业周刊(第118期):AI训练营
  8. 查询计算机ip地址的方法,计算机的ip地址查询的几种简单方法介绍
  9. 手把手带你调参Yolo v5 (v6.2)(训练)
  10. matlab两个单引号是什么意思,两个单引号什么时候用