前言

  • 本博客主要介绍三种深度学习的可视化方法。
  • 为什么深度学习需要可视化?
    • 原因是,如果我们能够可视化深度学习过程,那么就能对结果是如何产生的有一个直观的感受。这样,我们可以利用可视化来将模型向预想的效果改进。比如,当模型产生的结果不正确时,可以查明原因并进行改正,很显然只通过对参数的观察是很难实现这个过程的。

Saliency Maps

Saliency maps 是一个很快的方法来说明图片中的哪些部分影响了模型对于最后那个正确分类label的判断

Saliency Maps告诉我们图像中的每个像素对该图像分类评分的影响程度。为了计算它,我们计算对应于正确类的非归一化分数(标量)对于图像中每个像素的梯度。如果图像形状为(3,H,W)(3,H,W)(3,H,W),那么这个梯度的尺寸也是(3,H,W)(3,H,W)(3,H,W)。这个梯度告诉我们:图像中的一个像素的微小变化将使分类评分发生多大的变化。为了计算显著性图,我们取梯度的绝对值,然后取3个输入通道上的最大值;最终的Saliency Maps因此具有形状(H,W)(H,W)(H,W),并且是非负的。

  • 代码补全

    def compute_saliency_maps(X, y, model):"""Compute a class saliency map using the model for images X and labels y.Input:- X: Input images; Tensor of shape (N, 3, H, W)- y: Labels for X; LongTensor of shape (N,)- model: A pretrained CNN that will be used to compute the saliency map.Returns:- saliency: A Tensor of shape (N, H, W) giving the saliency maps for the inputimages."""# Make sure the model is in "test" modemodel.eval()# Make input tensor require gradientX.requires_grad_()saliency = None############################################################################### TODO: Implement this function. Perform a forward and backward pass through ## the model to compute the gradient of the correct class score with respect  ## to each input image. You first want to compute the loss over the correct   ## scores (we'll combine losses across a batch by summing), and then compute  ## the gradients with a backward pass.                                        ################################################################################ *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****scores=model(X)scores=scores.gather(1,y.view(-1,1)).squeeze() #取出每个正确分类的得分scores.backward(torch.FloatTensor([1.0,1.0,1.0,1.0,1.0])) #正确分类得分对于图像中像素的梯度saliency=X.grad.data #取出梯度saliency=saliency.abs() #绝对值saliency,i=torch.max(saliency,dim=1) #三通道最大值saliency=saliency.squeeze()# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****###############################################################################                             END OF YOUR CODE                               ###############################################################################return saliency
    
  • 绘图后结果

可以看到,红色部分都是有梯度的,也就是说这些点会对正确分类产生影响

Fooling Images

我们可以扰乱一个输入的图片,使它对于人类来说看起来是一样的,但是会被我们的与训练的模型分错类。生成的图像也被称为“fooling image”

给定一幅图像和一个目标类(和给定的图像不一个类),我们可以对图像进行梯度上升以最大化目标类,当网络将该图像分类为目标类时停止。

  • 代码补全

    def make_fooling_image(X, target_y, model):"""Generate a fooling image that is close to X, but that the model classifiesas target_y.Inputs:- X: Input image; Tensor of shape (1, 3, 224, 224)- target_y: An integer in the range [0, 1000)- model: A pretrained CNNReturns:- X_fooling: An image that is close to X, but that is classifed as target_yby the model."""# Initialize our fooling image to the input image, and make it require gradientX_fooling = X.clone()X_fooling = X_fooling.requires_grad_()learning_rate = 1############################################################################### TODO: Generate a fooling image X_fooling that the model will classify as   ## the class target_y. You should perform gradient ascent on the score of the ## target class, stopping when the model is fooled.                           ## When computing an update step, first normalize the gradient:               ##   dX = learning_rate * g / ||g||_2                                         ##                                                                            ## You should write a training loop.                                          ##                                                                            ## HINT: For most examples, you should be able to generate a fooling image    ## in fewer than 100 iterations of gradient ascent.                           ## You can print your progress over iterations to check your algorithm.       ################################################################################ *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****for i in range(100):scores = model(X_fooling)_, index = scores.max(dim=1)if index == target_y:breaktarget_score = scores[0, target_y]target_score.backward()im_grad = X_fooling.gradX_fooling.data += learning_rate * (im_grad / im_grad.norm())X_fooling.grad.zero_()# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****###############################################################################                             END OF YOUR CODE                               ###############################################################################return X_fooling
    

可以看到,我们朝着目标target class修改后,人眼看上去没什么区别,但将差别放大十倍,可以看到,还是有区别的。这在一定程度上反映了模型将图片判定为 stingray 这个类时关注的一些点。

Class visualization

我们可以合成一张图片来最大化一个特定类的打分;这可以给我们一些直观感受,来看看模型在判断图片是当前这个类的时候它在关注的是图片的哪些部分。

从一张随机噪声图开始,通过不断向target class应用梯度上升迭代参数,我们可以得到一张让网络判断为target class的图像。

​ I∗=arg⁡max⁡I(sy(I)−R(I))I^* = \arg\max_I (s_y(I) - R(I))I∗=argmaxI​(sy​(I)−R(I))

  • III是图像

  • yyy是 target class

  • Sy(I)S_y(I)Sy​(I)是神经网络判断图像 III 在分类 yyy 上的得分

  • R(I)R(I)R(I) 是正则项。本次实验中使用 L2L_2L2​ norm : R(I)=λ∥I∥22R(I) = \lambda \|I\|_2^2R(I)=λ∥I∥22​

  • 我们的目的是:让产生的图像在神经网络中正确分类的得分最大。这个目标可以通过梯度上升来实现(计算的是正确分类的得分对生成图像的梯度)。

  • 将通过显示L2L_2L2​ norm 和 隐式正则化来定期模糊生成图像

  • 代码补全

    def create_class_visualization(target_y, model, dtype, **kwargs):"""Generate an image to maximize the score of target_y under a pretrained model.Inputs:- target_y: Integer in the range [0, 1000) giving the index of the class- model: A pretrained CNN that will be used to generate the image- dtype: Torch datatype to use for computationsKeyword arguments:- l2_reg: Strength of L2 regularization on the image- learning_rate: How big of a step to take- num_iterations: How many iterations to use- blur_every: How often to blur the image as an implicit regularizer- max_jitter: How much to gjitter the image as an implicit regularizer- show_every: How often to show the intermediate result"""model.type(dtype)l2_reg = kwargs.pop('l2_reg', 1e-3)learning_rate = kwargs.pop('learning_rate', 25)num_iterations = kwargs.pop('num_iterations', 100)blur_every = kwargs.pop('blur_every', 10)max_jitter = kwargs.pop('max_jitter', 16)show_every = kwargs.pop('show_every', 25)# Randomly initialize the image as a PyTorch Tensor, and make it requires gradient.img = torch.randn(1, 3, 224, 224).mul_(1.0).type(dtype).requires_grad_()for t in range(num_iterations):# Randomly jitter the image a bit; this gives slightly nicer resultsox, oy = random.randint(0, max_jitter), random.randint(0, max_jitter)img.data.copy_(jitter(img.data, ox, oy))######################################################################### TODO: Use the model to compute the gradient of the score for the     ## class target_y with respect to the pixels of the image, and make a   ## gradient step on the image using the learning rate. Don't forget the ## L2 regularization term!                                              ## Be very careful about the signs of elements in your code.            ########################################################################## *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****score = model(img)loss = score[0, target_y] - l2_reg * img.norm()**2loss.backward()img.data += learning_rate * img.gradimg.grad.zero_()model.zero_grad()# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****#########################################################################                             END OF YOUR CODE                         ########################################################################## Undo the random jitterimg.data.copy_(jitter(img.data, -ox, -oy))# As regularizer, clamp and periodically blur the imagefor c in range(3):lo = float(-SQUEEZENET_MEAN[c] / SQUEEZENET_STD[c])hi = float((1.0 - SQUEEZENET_MEAN[c]) / SQUEEZENET_STD[c])img.data[:, c].clamp_(min=lo, max=hi)if t % blur_every == 0:blur_image(img.data, sigma=0.5)# Periodically show the imageif t == 0 or (t + 1) % show_every == 0 or t == num_iterations - 1:plt.imshow(deprocess(img.data.clone().cpu()))class_name = class_names[target_y]plt.title('%s\nIteration %d / %d' % (class_name, t + 1, num_iterations))plt.gcf().set_size_inches(4, 4)plt.axis('off')plt.show()return deprocess(img.data.cpu())
    

生成结果(狼蛛图片,谨慎观看)



Network Visualization (PyTorch)相关推荐

  1. 深度学习论文: Efficient Multi-order Gated Aggregation Network及其PyTorch实现

    深度学习论文: Efficient Multi-order Gated Aggregation Network及其PyTorch实现 Efficient Multi-order Gated Aggre ...

  2. 全球名校课程作业分享系列(10)--斯坦福CS231n之Network visualization

    课程作业原地址:CS231n Assignment 3 作业及整理:@邓姸蕾 && @Molly && @寒小阳 时间:2018年2月. 出处:http://blog. ...

  3. NiN(Network in Network) pytorch实现

    NiN(Network in Network) NiN(Network in Network)是Min Lin等人在2014的论文<Network in Network>中提出的一种结构, ...

  4. 《PHASEN:A Phase and Harmonics-Aware Speech Enhancement Network》Pytorch代码学习Ⅱ

    数据预处理 本文的实验采用的是Voice Bank的数据集,其中训练集大约包含11000条语音.上一篇文章中提到模型的输入是语音数据的短时傅里叶变换(幅值.相位),包含四个维度,分别是[batch, ...

  5. 复现Dense Extreme Inception Network(pytorch)

    github地址:https://github.com/xavysp/DexiNed/tree/master/DexiNed-Pytorch 论文地址:https://arxiv.org/abs/19 ...

  6. 《PHASEN:A Phase and Harmonics-Aware Speech Enhancement Network》Pytorch代码学习

    PHASEN结构 源码地址:https://github.com/huyanxin/phasen PHASEN是一个双流网络,其中幅值流和相位流分别专门用于幅值和相位预测.幅值流主要由卷积操作,频域变 ...

  7. [CS231n Assignment 3 #03] 网络可视化:显著映射、类可视化和欺骗图像

    文章目录 作业介绍 1. Network Visualization (PyTorch) 1.1 Helper Functions 2. Pretrained Model 2.1 Load some ...

  8. Module-based visualization of large-scale graph network data【论文阅读】

    基于模块化的大规模图网络数据可视化(2016) 关键词 网络可视化(Network visualization) 模块分组(Module grouping) 社区检测(Community detect ...

  9. pytorch 模型同一轮两次预测结果不一样_2020年的最新深度学习模型可解释性综述[附带代码]...

    最近low-level vision的炼丹经常出现各种主观评测上的效果问题,无法定位出其对于输入数据的对应关系,出现了问题之后很难进行针对性解决. 这个时候一个很自然的问题就是,都2020年了,深度学 ...

最新文章

  1. JS得到对应字段 的值。遍历
  2. 骑行318、 2016.7.8
  3. 【剑指offer-Java版】20顺时针打印矩阵
  4. Synbak 2.1 发布,系统备份工具
  5. 科大星云诗社动态20210321
  6. Flink EventTime和Watermarks原理结合代码分析(转载+解决+精简记录)
  7. Appnium安装-Mac平台
  8. linux添加php到环境,Linux系统为已编译的PHP环境添加扩展
  9. Docker 的两类存储资源 - 每天5分钟玩转 Docker 容器技术(38)
  10. 当你拥有足够的经验时,自然就会想到的东西---面向对象的设计原则!
  11. Vue_(组件)实例属性
  12. 保姆式手把手教你接入易班开放平台接入个人外部网站
  13. 第一篇,从0开始安装Ubuntu
  14. Android 来电秀总结
  15. switch语句实现周一到周五输出weekday;周六周天weekend C语言
  16. Hive小咖,是时候穿上你的振金战衣! 与职场高阶雷神之锤High-Five了!!!
  17. 网络空间安全要学c语言吗,2019年硕士研究生入学考试网络空间安全学院专业课考研大纲...
  18. [COGS 2583]南极科考旅行
  19. 分布式锁系列--04关于分布式锁的选型分析02-Redlock的实现原理
  20. 选择vray Next for SketchUp创建具启发性的、逼真的渲染的8大理由!

热门文章

  1. 【SEED Labs 2.0】Virtual Private Network (V*N) Lab
  2. 我在知乎上对“如何评价清华特等奖学金得主韩衍隽?”的回答
  3. BCD和十进制互相转换——C实现
  4. 2021年Android工作或更难找,系列教学
  5. 2021年四川高考一诊成绩查询,成都一诊二诊三诊成绩查询入口http://118.122.90.31:7001/bst/...
  6. 农场游戏开发记录十八
  7. 农场游戏开发记录十七(控制台版本完成)
  8. java下载文件excel格式错乱,excel获取文件表格数据格式化-excel表格里的文件突然格式全部乱了,怎么恢复?...
  9. 【Java基础】arraycopy
  10. 人工智能导论 基础搜索算法