神经网络已经在很多场景下表现出了很好的识别能力,但是缺乏解释性一直所为人诟病。《Grad-CAM:Visual Explanations from Deep Networks via Gradient-based Localization》这篇论文基于梯度为其可解释性做了一些工作,它可以显著描述哪块图片区域对识别起了至关重要的作用,以热度图的方式可视化神经网络的注意力。本博客主要是基于pytorch的简单工程复现。原文见这里,本代码基于这里

  1 import torch
2 import torchvision
3 from torchvision import models
4 from torchvision import transforms
5 from PIL import Image
6 import pylab as plt
7 import numpy as np
8 import cv2
9
10
11 class Extractor():
12 """
13 pytorch在设计时,中间层的梯度完成回传后就释放了
14 这里用hook工具在保存中间参数的梯度
15 """
16 def __init__(self, model, target_layer):
17 self.model = model
18 self.target_layer = target_layer
19 self.gradient = None
20
21 def save_gradient(self, grad):
22 self.gradient=grad
23
24 def __call__(self, x):
25 outputs = []
26 self.gradients = []
27 for name,module in self.model.features._modules.items():
28 x = module(x)
29 if name == self.target_layer:
30 x.register_hook(self.save_gradient)
31 target_activation=x
32 x=x.view(1,-1)
33 for name,module in self.model.classifier._modules.items():
34 x = module(x)
35 # 维度为(1,c, h, w) , (1,class_num)
36 return target_activation, x
37
38
39 def preprocess_image(path):
40 means=[0.485, 0.456, 0.406]
41 stds=[0.229, 0.224, 0.225]
42 m_transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize(means,stds)])
43 img=Image.open(path)
44 return m_transform(img).reshape(1,3,224,224)
45
46
47 class GradCam():
48 def __init__(self, model, target_layer_name, use_cuda):
49 self.model = model
50 self.model.eval()
51 self.cuda = use_cuda
52 if self.cuda:
53 self.model = model.cuda()
54
55 self.extractor = Extractor(self.model, target_layer_name)
56
57
58 def __call__(self, input, index = None):
59 if self.cuda:
60 target_activation, output = self.extractor(input.cuda())
61 else:
62 target_activation, output = self.extractor(input)
63
64 # index是想要查看的类别,未指定时选择网络做出的预测类
65 if index == None:
66 index = np.argmax(output.cpu().data.numpy())
67
68 # batch维为1(我们默认输入的是单张图)
69 one_hot = np.zeros((1, output.size()[-1]), dtype = np.float32)
70 one_hot[0][index] = 1.0
71 one_hot = torch.tensor(one_hot)
72 if self.cuda:
73 one_hot = torch.sum(one_hot.cuda() * output)
74 else:
75 one_hot = torch.sum(one_hot * output)
76
77 self.model.zero_grad()
78 one_hot.backward(retain_graph=True)
79
80 grads_val = self.extractor.gradient.cpu().data.numpy()
81 # 维度为(c, h, w)
82 target = target_activation.cpu().data.numpy()[0]
83 # 维度为(c,)
84 weights = np.mean(grads_val, axis = (2, 3))[0, :]
85 # cam要与target一样大
86 cam = np.zeros(target.shape[1 : ], dtype = np.float32)
87 for i, w in enumerate(weights):
88 cam += w * target[i, :, :]
89
90 # 每个位置选择c个通道上最大的最为输出
91 cam = np.maximum(cam, 0)
92 cam = cv2.resize(cam, (224, 224))
93 cam = cam - np.min(cam)
94 cam = cam / np.max(cam)
95 return cam
96
97
98 def show_cam_on_image(img, mask):
99 heatmap = cv2.applyColorMap(np.uint8(255*mask), cv2.COLORMAP_JET)
100 heatmap = np.float32(heatmap) / 255
101 cam = heatmap + np.float32(img)
102 cam = cam / np.max(cam)
103 cv2.imwrite("cam2.jpg", np.uint8(255 * cam))
104
105
106 #target_layer 越靠近分类层效果越好
107 grad_cam = GradCam(model = models.vgg19(pretrained=True), target_layer_name = "35", use_cuda=True)
108 input = preprocess_image("both.png")
109 mask = grad_cam(input, None)
110 img = cv2.imread("both.png", 1)
111 #热度图是直接resize加到输入图上的
112 img = np.float32(cv2.resize(img, (224, 224))) / 255
113 show_cam_on_image(img, mask)

原图:

可视化图:

神经网络可视化《Grad-CAM:Visual Explanations from Deep Networks via Gradient-based Localization》的更多相关文章

  1. Grad-CAM:Visual Explanations from Deep Networks via Gradient-based Localization

    目录 Grad-CAM:Visual Explanations from Deep Networks via Gradient-based Localization 1.Abstract 2.Intr ...

  2. 【论文简读】 Deep web data extraction based on visual

    <Deep web data extraction based on visual information processing>作者 J Liu 上海海事大学 2017 AIHC会议登载 ...

  3. 深度卷积神经网络用于图像缩放Image Scaling using Deep Convolutional Neural Networks

    This past summer I interned at Flipboard in Palo Alto, California. I worked on machine learning base ...

  4. 论文笔记:SiamRPN++: Evolution of Siamese Visual Tracking with Very Deep Networks

    SiamRPN++: Evolution of Siamese Visual Tracking with Very Deep Networks 2019-04-02 12:44:36 Paper:ht ...

  5. 论文笔记之:Action-Decision Networks for Visual Tracking with Deep Reinforcement Learning

    论文笔记之:Action-Decision Networks for Visual Tracking with Deep Reinforcement Learning  2017-06-06  21: ...

  6. Distill详述「可微图像参数化」:神经网络可视化和风格迁移利器!

    近日,期刊平台 Distill 发布了谷歌研究人员的一篇文章,介绍一个适用于神经网络可视化和风格迁移的强大工具:可微图像参数化.这篇文章从多个方面介绍了该工具. 图像分类神经网络拥有卓越的图像生成能力 ...

  7. WPF中的可视化对象(Visual)

    原文:WPF中的可视化对象(Visual) 这是MSDN对Visual的解释:Visual class:Provides rendering support in WPF, which include ...

  8. TensorSpace:超酷炫3D神经网络可视化框架

    TensorSpace:超酷炫3D神经网络可视化框架 TensorSpace - 一款 3D 模型可视化框架,支持多种模型,帮助你可视化层间输出,更直观地展示模型的输入输出,帮助理解模型结构和输出方法 ...

  9. Deep Learning 8_深度学习UFLDL教程:Stacked Autocoders and Implement deep networks for digit classification_Exercise(斯坦福大学深度学习教程)

    前言 1.理论知识:UFLDL教程.Deep learning:十六(deep networks) 2.实验环境:win7, matlab2015b,16G内存,2T硬盘 3.实验内容:Exercis ...

随机推荐

  1. Spring Retry 在SpringBoot 中的应用

    Spring Boot中使用Spring-Retry重试框架 Spring Retry提供了自动重新调用失败的操作的功能.这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用. 从2.2.0版本开始 ...

  2. 3D 沙盒游戏之人物的点击行走移动

    前言 在 3D 游戏中,都会有一个主人公.我们可以通过点击游戏中的其他位置,使游戏主人公向点击处移动. 那当我们想要实现一个"点击地面,人物移动到点击处"的功能,需要什么前置条件, ...

  3. Day 007:PAT训练--1108 Finding Average (20 分)

    话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...

  4. k8s client-go源码分析 informer源码分析(1)-概要分析

    k8s informer概述 我们都知道可以使用k8s的Clientset来获取所有的原生资源对象,那么怎么能持续的获取集群的所有资源对象,或监听集群的资源对象数据的变化呢?这里不需要轮询去不断执行L ...

  5. [AcWIng 799] 最长连续不重复子序列

    点击查看代码 #include<iostream> using namespace std; const int N = 1e5 + 10; int a[N], s[N]; int mai ...

  6. 干货 | Nginx负载均衡原理及配置实例

    一个执着于技术的公众号 Nginx系列导读 给小白的 Nginx 10分钟入门指南 Nginx编译安装及常用命令 完全卸载nginx的详细步骤 Nginx 配置文件详解 理解正向代理与反向代理的区别 ...

  7. 「Python实用秘技08」一行代码解析地址信息

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第8期 ...

  8. .NET混合开发解决方案12 网页JS调用C#方法访问WinForm或WPF窗体

    系列目录     [已更新最新开发文章,点击查看详细] WebView2控件应用详解系列博客 .NET桌面程序集成Web网页开发的十种解决方案 .NET混合开发解决方案1 WebView2简介 .NE ...

  9. 一文带你读懂 Hbase 的架构组成

    hi,大家好,我是大D.今天咱们继续深挖一下 HBase 的架构组成. Hbase 作为 NoSQL 数据库的代表,属于三驾马车之一 BigTable 的对应实现,HBase 的出现很好地弥补了大数据 ...

  10. WTF表单验证

    WTF表单验证可分为3个步骤: ①导入wtf扩展提供的表单验证器.(from wtforms.validators import DataRequired,EqualTo) ②定义表单类 # 定义表单 ...