Saliency Maps

这部分想探究一下 CNN 内部的原理,参考论文 Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps.

一般我们反向传播 CNN 的时候,是可以得到图片的梯度(Image
Gradient)的,但是因为网络要学习的参数是权重 W,因此都不会用到这个梯度。这篇论文可视化了一下图片的梯度,称作是 saliency map,发现其实是网络对不同处像素值关注的权重。得到的结果甚至可以辅助做 segmentation 问题。

通俗来说就是,给定一张图片X,我们想要知道到底是图片中的哪些部分决定了该图片的最终分类结果,我们可以通过反向传播求出X关于loss function的偏导矩阵,这个偏导矩阵就是该图片的图像梯度,然后计算出类显著度图(class saliency map, csm)。Karen Simonyan论文的3.1节给出了计算方法:如果图片是灰度图,那么csm就取图像梯度的绝对值;如果是RGB图,csm就取图像梯度3个通道中绝对值最大的那个通道。csm中元素值的大小表示对应位置的图片像素对最终分类结果的影响程度。
 
from cs231n.layers import softmax_loss

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, of shape (N, 3, H, W)
- y: Labels for X, of shape (N,)
- model: A PretrainedCNN that will be used to compute the saliency map. Returns:
- saliency: An array of shape (N, H, W) giving the saliency maps for the input
images.
"""
saliency = None
##############################################################################
# TODO: Implement this function. You should use the forward and backward #
# methods of the PretrainedCNN class, and compute gradients with respect to #
# the unnormalized class score of the ground-truth classes in y. #
##############################################################################
scores, cache = model.forward(X)
loss, dscores = softmax_loss(scores, y)
dX, grads = model.backward(dscores, cache)
saliency = dX.max(axis=1) return saliency

Fooling Images

给定一个类别标签,CNN 希望对应能输入什么样的图片呢?可以考虑把图片当做变量,固定模型中的权重,来优化下面的目标函数,

其中是给定类标签 y 时模型的评分。

def make_fooling_image(X, target_y, model):
"""
Generate a fooling image that is close to X, but that the model classifies
as target_y. Inputs:
- X: Input image, of shape (1, 3, 64, 64)
- target_y: An integer in the range [0, 100)
- model: A PretrainedCNN Returns:
- X_fooling: An image that is close to X, but that is classifed as target_y
by the model.
"""
X_fooling = X.copy()
##############################################################################
# TODO: Generate a fooling image X_fooling that the model will classify as #
# the class target_y. Use gradient ascent on the target class score, using #
# the model.forward method to compute scores and the model.backward method #
# to compute image gradients. #
# #
# HINT: For most examples, you should be able to generate a fooling image #
# in fewer than 100 iterations of gradient ascent. #
##############################################################################
while True:
print i
scores, cache = model.forward(X_fooling, mode='test')
if scores[0].argmax() == target_y:
break
loss, dscores = softmax_loss(scores, target_y) # 使用目标分类计算分类层梯度
dX, grads = model.backward(dscores, cache) # 逆向传播推导图片梯度
X_fooling -= dX * 1000 # 修改图片,为了fooling的目的学习率设定的超大 return X_fooling

最新文章

  1. python 接口开发(一)
  2. 腾讯QQ表情为什么如此成功呢
  3. [Ionic] Ionic Quickstart for Windows
  4. NOI十连测 第六测 T1
  5. hdu1003 最大连续子序和
  6. Java泛型和集合之泛型VS模板
  7. APP金融安全白皮书十大安全注意事项及各项注意安全
  8. CentOS6软raid配置与管理
  9. 快速学习Bash
  10. 关于responseBody注解中文乱码的问题解决
  11. ABP框架系列之三十七:(Navigation-导航)
  12. Python-生产者消费模型 线程
  13. 3D打印材料PLA,ABS对比
  14. LeetCode(16):最接近的三数之和
  15. Octave及其工具包的安装
  16. Oracle EBS 请求
  17. selenium+java多层级frame切换的问题
  18. Spark源码分析 -- TaskScheduler
  19. Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密
  20. 洛谷—— P2562 [AHOI2002]Kitty猫基因编码

热门文章

  1. 查看firefox浏览器 驱动geckodriver.exe文件的版本号的方法,以及下载链接
  2. android 图片大小适配,如何在不同屏幕上适配图片,如何设置可以自适应不同分辨率?
  3. Qt 学习之路 2(55):数据库操作
  4. schema与catalog的理解
  5. https的设置
  6. 20145335郝昊《网络攻防》Exp4 Msf基础
  7. 如何解析读取excel数据
  8. dp练习 2016.2.24
  9. Python3基础 file open 打开txt文件并打印出全文
  10. luogu P2114 [NOI2014]起床困难综合症 位运算 二进制