当前位置:网站首页>卷积核、特征图可视化
卷积核、特征图可视化
2022-06-24 13:00:00 【燃烧吧哥们】
卷积核,及特征层可视化
最近在读AlexNet这篇CNN的开山之作,里面有卷积层卷积核可视化这一部分,故记录一下,其他网络也可照猫画虎,希望得到一个宝贵的赞。
一、卷积核可视化
1、准备一个训练好的模型
这里建议使用一个训练好的模型进行可视化,这样可视化出来的结果可以帮助观察出一些潜在的特性的(我这里采用的是AlexNet预训练模型)。
2、卷积核可视化
这里简单进行一个梳理:
第一层卷积核:torch.Size([64, 3, 11, 11]),
输出通道数:64, 对应卷积核的数量
输入通道数:3, 对应卷积核的通道数
卷积核宽:11,
卷积核高:11
单通道卷积核可视化多通道卷积核可视化见下图:
代码如下:
import os
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
import torchvision.utils as vutils
import torchvision.models as models
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
log_dir = os.path.join(BASE_DIR, "results")
writer = SummaryWriter(log_dir=log_dir, filename_suffix="_kernel")
path_state_dict = os.path.join("data", "alexnet-owt-4df8aa71.pth")
alexnet = models.alexnet()
alexnet.load_state_dict(torch.load(path_state_dict))
kernel_num = -1
vis_max = 1
# 取前两层卷积核
for sub_module in alexnet.modules():
if not isinstance(sub_module, nn.Conv2d):
continue
if kernel_num >= vis_max:
break
kernel_num += 1
kernels = sub_module.weight
c_out, c_int, k_h, k_w = tuple(kernels.shape) # 输出通道数,输入通道数,卷积核宽,卷积核高
print(kernels.shape)
for o_idx in range(c_out):
kernel_idx = kernels[o_idx, :, :, :].unsqueeze(1) # 获得(3, h, w), 但是make_grid需要 BCHW,这里拓展C维度变为(3, 1, h, w)
kernel_grid = vutils.make_grid(kernel_idx, normalize=True, scale_each=True, nrow=8) # 将卷积核于网格中可视化
# nrow:每一行显示的图像数
writer.add_image('{}_Convlayer_split_in_channel'.format(kernel_num), kernel_grid, global_step=o_idx)
# 名称,图片,第几张图片
kernel_all = kernels.view(-1, 3, k_h, k_w) # 3, h, w
kernel_grid = vutils.make_grid(kernel_all, normalize=True, scale_each=True, nrow=8) # c, h, w
writer.add_image('{}_all'.format(kernel_num), kernel_grid, global_step=620)
print("{}_convlayer shape:{}".format(kernel_num, tuple(kernels.shape)))
| 补充操作 |
|---|
运行代码后会生成一个results文件,接下来 ![]() |
执行如下代码:tensorboard --logdir= 所存储的results文件路径)![]() |
点击如下链接即可跳转到可视化界面![]() |
结果如下图所示

二、特征图可视化
这里只展示第一层,输入一张图片经过第一层卷积后所得特征层的可视化
代码如下:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
log_dir = os.path.join(BASE_DIR, "results")
path_state_dict = os.path.join("data", "alexnet-owt-4df8aa71.pth")
alexnet = models.alexnet()
alexnet.load_state_dict(torch.load(path_state_dict))
writer = SummaryWriter(log_dir=log_dir, filename_suffix="_feature map")
# 数据
path_img = os.path.join(BASE_DIR,"data", "tiger cat.jpg") # your path to image
img_transforms = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.49139968, 0.48215827, 0.44653124], [0.24703233, 0.24348505, 0.26158768])
])
img_pil = Image.open(path_img).convert('RGB')
img_tensor = img_transforms(img_pil)
img_tensor.unsqueeze_(0) # chw -> bchw
convlayer1 = alexnet.features[0] # 第一层卷积层
fmap_1 = convlayer1(img_tensor)
fmap_1.transpose_(0, 1) # bchw=(1, 64, 55, 55) --> (64, 1, 55, 55)
fmap_1_grid = vutils.make_grid(fmap_1, normalize=True, scale_each=True, nrow=8)
writer.add_image('feature map in conv1', fmap_1_grid, global_step=620)
writer.close()
下面是原图和效果图:


如有不足还望指出,最后再次请求一个小小的赞。
边栏推荐
- HarmonyOS.2
- 《中国数据库安全能力市场洞察,2022》报告研究正式启动
- Google waymo proposed r4d: remote distance estimation using reference target
- 4个不可不知的“安全左移”的理由
- [5g NR] 5g NR system architecture
- Goldfish rhca memoirs: do447 managing projects and conducting operations -- creating a project for ansible scripts
- 杰理之红外滤波【篇】
- Seven challenges faced by data scientists and Solutions
- Harmony os. (2)
- 一键生成大学、专业甚至录取概率,AI填报志愿卡这么神奇?
猜你喜欢

markdown/LaTeX中在字母下方输入圆点的方法

**Unity中莫名其妙得小问题-灯光和天空盒

融云通信“三板斧”,“砍”到了银行的心坎上

Dragon lizard developer said: first time you got an electric shock, so you are such a dragon lizard community| Issue 8

These default routes and static routes can not be configured and deployed. What kind of network workers are they!

万用表测量电阻图解及使用注意事项

杰理之红外滤波【篇】

npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)

图扑软件数字孪生海上风电 | 向海图强,奋楫争先

位于相同的分布式端口组但不同主机上的虚拟机无法互相通信
随机推荐
AntD checkbox,限制选中数量
智源社区周刊#86:Gary Marcus谈大模型研究可借鉴的三个语言学因素;谷歌提出媲美Imgen的文生图模型Parti;OpenAI提出视频预训练模型VPT,可玩MC游戏
【5G NR】5G NR系统架构
详解kubernetes备份恢复利器 Velero | 深入了解Carina系列第三期
2022年施工升降机司机(建筑特殊工种)考试试题及在线模拟考试
位于相同的分布式端口组但不同主机上的虚拟机无法互相通信
Rongyun communication has "hacked" into the heart of the bank
Jerrys timer0 uses the default pa13 to detect the pulse width [chapter]
Activity lifecycle
吉时利静电计宽测量范围
Kotlin inheritance, class, overload
How to avoid serious network security accidents?
JS remove string spaces
Jerry's serial port receiving IO needs to set the digital function [chapter]
HarmonyOS-3
90%的项目经理都跳过的坑,你现在还在坑里吗?
The hidden corner of codefarming: five things that developers hate most
Seven challenges faced by data scientists and Solutions
[AI player cultivation record] use AI to identify what kind of wealth is next door
redis 数据类型详解


