当前位置:网站首页>卷积核、特征图可视化
卷积核、特征图可视化
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()
下面是原图和效果图:


如有不足还望指出,最后再次请求一个小小的赞。
边栏推荐
- kotlin 协程上下文和调度器
- 2022年江西省安全员B证考试题库模拟考试平台操作
- 【R语言数据科学】(十四):随机变量和基本统计量
- Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire
- 一键生成大学、专业甚至录取概率,AI填报志愿卡这么神奇?
- Getting to know cloud native security for the first time: the best guarantee in the cloud Era
- Kotlin inheritance, class, overload
- Zhiyuan community weekly 86: Gary Marcus talks about three linguistic factors that can be used for reference in large model research; Google puts forward the Wensheng graph model parti which is compar
- Jericho may have some chips with fast music playing speed [chapter]
- Activity生命周期
猜你喜欢
随机推荐
项目经理搭建团队,需要看6个特征
Baidu map API drawing points and tips
[R language data science] (XIV): random variables and basic statistics
杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】
npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
Docker installation redis
redis 数据类型详解
Activity lifecycle
融云通信“三板斧”,“砍”到了银行的心坎上
win10系统问题
Vulnerability management mistakes that CIOs still make
Google waymo proposed r4d: remote distance estimation using reference target
Kotlin initialization block
Rasa 3. X learning series - it is a great honor to be a source code contributor of Rasa contributors, and to build and share the rasa community with rasa source code contributors all over the world!
2022年质量员-设备方向-岗位技能(质量员)复训题库及在线模拟考试
Tupu software is the digital twin of offshore wind power, striving to be the first
Usage of multimeter
Mysql题目篇
【sdx62】WCN685X IPA不生效问题分析及解决方案
**Unity中莫名其妙得小问题-灯光和天空盒






![Jerrys timer0 uses the default pa13 to detect the pulse width [chapter]](/img/4e/de0951c8be5ddd765b15b773c04fa7.png)





