flashtorch:卷积神经网络的可视化

GitHub:FlashTorch

saliency maps论文地址: Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps

FlashTorch简介

A Python visualization toolkit, built with PyTorch, for neural networks in PyTorch.

Neural networks are often described as “black box”. The lack of understanding on how neural networks make predictions enables unpredictable/biased models, causing real harm to society and a loss of trust in AI-assisted systems.

Feature visualization is an area of research, which aims to understand how neural networks perceive images. However, implementing such techniques is often complicated.

FlashTorch was created to solve this problem!

You can apply feature visualization techniques (such as saliency maps and activation maximization) on your model, with as little as a few lines of code.

It is compatible with pre-trained models that come with torchvision, and seamlessly integrates with other custom models built in PyTorch.

FlashTorch安装

使用命令行,通过pip安装。安装代码如下:

pip install flashtorch

安装结果如下:

如果要安装到Anaconda的Python下,参见博客:python+pip——使用pip将函数库安装到Python环境或Anaconda环境。

flashtorch中demo的实现

saliency maps(显著图)

import matplotlib.pyplot as plt
import torchvision.models as  modelsfrom flashtorch.utils import apply_transforms, load_image
from flashtorch.saliency import Backprop
image = load_image('C:/Users/73416/PycharmProjects/flashtorch-master/examples/images/peacock.jpg')# ------------------------显示图像------------------------
plt.imshow(image)
plt.title('Original image')
plt.axis('off')
plt.show()          # 自加# ------------------------可视化------------------------
peacock = apply_transforms(image)model = models.alexnet(pretrained=True)backprop = Backprop(model)backprop.visualize(peacock, 84, guided=True, use_gpu=True)
plt.show()# 标号不对时,产生的warning:
# C:\Users\73416\PycharmProjects\flashtorch-master\flashtorch\saliency\backprop.py:111: UserWarning: The predicted class index 251 does notequal the target class index 96. Calculatingthe gradient w.r.t. the predicted class.
#   'the gradient w.r.t. the predicted class.'

解析:

  • 网络模型采用ImageNet的预训练模型,此处采用AlexNet。model = models.alexnet(pretrained=True)

  • 调用.visualize()plt.imshow()显示之后,需要使用plt.show()来显示图像。

  • 记得更改路径。

  • 标号不对时,产生的warning:

    C:\Users\73416\PycharmProjects\flashtorch-master\flashtorch\saliency\backprop.py:111: UserWarning: The predicted class index 251 does not equal the target class index 96. Calculatingthe gradient w.r.t. the predicted class.
    

结果显示:


activation maximization(激活最大化)

初始化
# %matplotlib inline
# %config InlineBackend.figure_format = 'retina'import torchvision.models as models
from flashtorch.activmax import GradientAscent
import matplotlib.pyplot as plt# ------------------------------自加显示网络结构的函数------------------------------
def show_architecture(architecture):
# input: list of network architecturefor i in range(len(architecture)):print(architecture[i])# ------------------------------Load a pre-trained Model------------------------------
model = models.vgg16(pretrained=True)
architecture=list(model.features.named_children())       # Print layers and corresponding indicies
show_architecture(architecture)

解析:

首先选择网络模型,为VGG16。model = models.vgg16(pretrained=True)。之后以list类型,显示网络结构。

注意网络是经过预训练的,也就是说VGG16是在ImageNet上训练好的。

结果如下所示:

E:\Anaconda\python.exe "C:/Users/73416/PycharmProjects/flashtorch-master/try_Activation Maximization.py"('0', Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('1', ReLU(inplace))
('2', Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('3', ReLU(inplace))
('4', MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False))
('5', Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('6', ReLU(inplace))
('7', Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('8', ReLU(inplace))
('9', MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False))
('10', Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('11', ReLU(inplace))
('12', Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('13', ReLU(inplace))
('14', Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('15', ReLU(inplace))
('16', MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False))
('17', Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('18', ReLU(inplace))
('19', Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('20', ReLU(inplace))
('21', Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('22', ReLU(inplace))
('23', MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False))
('24', Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('25', ReLU(inplace))
('26', Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('27', ReLU(inplace))
('28', Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))
('29', ReLU(inplace))
('30', MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False))
可视化:特定卷积层的特定滤波器
选择要显示的卷积层和滤波器
# ------------------------------Specify layers and filters------------------------------
conv1_2 = model.features[2]
conv1_2_filters = [17, 33, 34, 57]conv2_1 = model.features[5]
conv2_1_filters = [27, 40, 68, 73]conv3_1 = model.features[10]
conv3_1_filters = [31, 61, 147, 182]conv4_1 = model.features[17]
conv4_1_filters = [238, 251, 338, 495]conv5_1 = model.features[24]
conv5_1_filters = [45, 271, 363, 409]

以前两行为例,解说代码含义。

conv1_2 = model.features[2]是指显示的是model的编号为2feature,参见上面的网络结构,可知其为('2', Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))),可知要显示的第一卷积层。

根据该卷积层的信息,可知in_channels=64, out_channels=64。所以该卷积层共有64个filterconv1_2_filters = [17, 33, 34, 57]指明要显示的是fliter的编号是17,33,34,57。

实例化 GradientAscent
# Creating an instance of GradientAscent class
g_ascent = GradientAscent(model.features)
可视化所选定滤波器
# ------------------------------Optimize and visualize filters------------------------------
# optimization and visualization
g_ascent.visualize(conv1_2, conv1_2_filters, title='conv1_2')
plt.show()
g_ascent.visualize(conv2_1, conv2_1_filters, title='conv2_1')
plt.show()
g_ascent.visualize(conv3_1, conv3_1_filters, title='conv3_1')
plt.show()
g_ascent.visualize(conv4_1, conv4_1_filters, title='conv4_1')
plt.show()
g_ascent.visualize(conv5_1, conv5_1_filters, title='conv5_1')
plt.show()

要显示的卷积层由conv5_1来指定,要显示的滤波器的编号由conv5_1_filters给出。





可以看到,越低级的卷积层,可提取到的特征越简单。越高的卷积层,提取到的特征越复杂,比如conv5_1的filter_45会提取到眼睛的信息。

可视化:特定卷积层的随机滤波器
# ----------------------GradientAscent.visualize: randomly select filters----------------------
g_ascent.visualize(conv5_1, title='Randomly selected filters from conv5_1')
plt.show()

要显示的卷积层由conv5_1来指定,通过不传入滤波器编号来表示随机显示。

可视化:特定卷积层的一个滤波器
# -------------------------GradientAscent.visualize: plot one filter-------------------------
g_ascent.visualize(conv5_1, 3, title='conv5_1 filter 3')
plt.show()

可视化并返回 image tensor
# ------------------------GradientAscent.visualize: return image tensor------------------------
output = g_ascent.visualize(conv5_1, 3, title='conv5_1 filter 3', return_output=True)
plt.show()
print('num_iter:', len(output))
print('optimized image:', output[-1].shape)
deepdream
# ------------------------GradientAscent.deepdream: create DeepDream------------------------
g_ascent.deepdream('C:/Users/73416/PycharmProjects/flashtorch-master/examples/images/jay.jpg', conv5_1, 33)
plt.show()

perform optimization only
# -----------GradientAscent.optimize: perform optimization only (no visualization)-----------
output = g_ascent.optimize(conv5_1, 3)
print('num_iter:', len(output))
print('optimized image:', output[-1].shape)

结果如下:

num_iter: 30
optimized image: torch.Size([1, 3, 224, 224])

flashtorch:卷积神经网络的可视化相关推荐

  1. 深度学习笔记:卷积神经网络的可视化--卷积核本征模式

    目录 1. 前言 2. 代码实验 2.1 加载模型 2.2 构造返回中间层激活输出的模型 2.3 目标函数 2.4 通过随机梯度上升最大化损失 2.5 生成滤波器模式可视化图像 2.6 将多维数组变换 ...

  2. DeepDream、反向运行一个卷积神经网络在 DeepDream和卷积神经网络的可视化 中的应用

    日萌社 人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新) 反向运行一个卷积神经网络在 卷积神经网络的可视化 中的应用 D ...

  3. 《Python 深度学习》5.4 卷积神经网络的可视化(代码)

    Visualizing what convnets learn 卷积神经网络的可视化 人们常说,深度学习模型是"黑盒",即模型学到的表示很难用人类可以理解的方式来提取和呈现.虽然对 ...

  4. 可视化卷积神经网络的过滤器_万字长文:深度卷积神经网络特征可视化技术(CAM)最新综述...

    ↑ 点击蓝字 关注极市平台作者丨皮特潘@知乎来源丨https://zhuanlan.zhihu.com/p/269702192编辑丨极市平台 极市导读 本文通过引用七篇论文来论述CAM技术,对CAM的 ...

  5. 窥探黑盒-卷积神经网络的可视化

    这是笔者第N+1次听到专家说,深度学习模型是"黑盒".这个说法不能说他对,也不能说他错.但是这句话从专家那里说出来,感觉就有点不严谨了,想必专家应该长时间不在科研一线了...  对 ...

  6. 综述 | 卷积神经网络表征可视化研究

    点上方计算机视觉联盟获取更多干货 仅作学术分享,不代表本公众号立场,侵权联系删除 转载于:专知,自动化学报 AI博士笔记系列推荐 周志华<机器学习>手推笔记正式开源!可打印版本附pdf下载 ...

  7. 卷积神经网络的可视化(基于keras)

    在通常的认知中,神经网络的模型是一个"黑盒",即模型学到的内容很难用人能够理解的方式来提取和表现,虽然对于某些类型的深度学习模型来说,这种表述部分正确,但对卷积神经网络来说绝对不是 ...

  8. 卷积神经网络的可视化(二)(可视化卷积神经网络的过滤器)

    总结自<Python 深度学习>(François Chollet)第5章. 可视化卷积神经网络的过滤器 想要观察卷积神经网络学到的过滤器,另一种简单的方法是显示每个过滤器所响应的视觉模 ...

  9. 《Python 深度学习》刷书笔记 Chapter 5 Part-4 卷积神经网络的可视化(Fillter)

    文章目录 可视化卷积神经网络 2-25 读入模组 5-26 观察图像 观察卷积层特征提取 5-27 建立多输出模型观察输出 5-28 显示图像 5-29 打印全部的识别图 5-32 为过滤器的可视化定 ...

最新文章

  1. 觉SLAM的主要功能模块分析
  2. DATE_FORMAT() 函数||DATE_SUB() 函数
  3. 12、Java Swing计算器界面的实现
  4. [云炬WEB实战笔记]批量修改WordPress文章时间按天递增
  5. 将R非时间序列的data.frame转变为时序格式
  6. TCP协议可靠性保证(确认应答机制,超时重传机制,流量控制,拥塞窗口)
  7. React Native之通过createStackNavigator实现携带参数的页面与页面之间的跳转
  8. 使用权值衰减算法解决神经网络过拟合问题、python实现
  9. java中的byte
  10. python坐标轴刻度设置为一个函数_Python坐标轴操作及设置代码实例
  11. Poker2的Programmer Dvorak解决方案
  12. 介绍一款喜欢的产品|产品经理面试题第2篇
  13. python操作redis-sentinel集群
  14. Android获取本地IP
  15. ionic 3 http htpps
  16. 高颜值蓝牙耳机有哪些?音质好颜值高的蓝牙耳机推荐
  17. 国画家刘艺青个人介绍
  18. 微信官方支付接口配置教程
  19. 利用Sympy计算sin1°的最小多项式
  20. 通过VISA库实现GPIB通信

热门文章

  1. 你了解语音识别技术吗?
  2. python 3.5 format_python 3.5学习笔记(第四章)
  3. 元素出现在页面时,添加动画,配合animate.css使用
  4. ijkplayer-hook协议实现分析
  5. linux apache 2.2下载,Apache HTTP Server 2.2.26 发布下载
  6. java easyui分页源码_SpringMVC+easyUI中datagrid分页实现_2014.5.1
  7. element的日期选择使用value-format之后表单验证报错
  8. jenkins无法连接仓库:Command “/usr/bin/git ls-remote -h -- https://gitee.com/xxx.git HEAD“ returned
  9. jmx.JmxAdminException
  10. Uncaught TypeError: l.push is not a function