当前位置:网站首页>Convolution kernel and characteristic graph visualization
Convolution kernel and characteristic graph visualization
2022-06-24 14:08:00 【Burn, man】
Convolution kernel , And feature layer visualization
Recently read AlexNet This article CNN It's the beginning of the mountain , There is the visualization of convolution kernel of convolution layer , So record that , Other networks can follow suit , Hope to get a valuable praise .
One 、 Convolution kernel Visualization
1、 Prepare a trained model
It is suggested to use a trained model for visualization , The visualized results can help to observe some potential characteristics ( What I'm using here is AlexNet Pre training model ).
2、 Convolution kernel Visualization
Here is a simple comb :
First layer convolution kernel :torch.Size([64, 3, 11, 11]),
Number of output channels :64, The number of corresponding convolution kernels
Enter the number of channels :3, Number of channels corresponding to convolution kernel
Convolution kernel width :11,
Convolution kernel is high :11
Single channel convolution kernel visualization see the following figure for multi-channel convolution kernel visualization :
The code is as follows :
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
# Take the first two convolution kernels
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) # Number of output channels , Enter the number of channels , Convolution kernel width , Convolution kernel is high
print(kernels.shape)
for o_idx in range(c_out):
kernel_idx = kernels[o_idx, :, :, :].unsqueeze(1) # get (3, h, w), however make_grid need BCHW, Expand here C Dimension becomes (3, 1, h, w)
kernel_grid = vutils.make_grid(kernel_idx, normalize=True, scale_each=True, nrow=8) # The convolution kernel is visualized in the grid
# nrow: Number of images displayed per line
writer.add_image('{}_Convlayer_split_in_channel'.format(kernel_num), kernel_grid, global_step=o_idx)
# name , picture , Picture number
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)))
| Supplementary operation |
|---|
After running the code, a results file , Next ![]() |
Execute the following code :tensorboard --logdir= Stored results File path )![]() |
Click the following link to jump to the visual interface ![]() |
The results are shown in the following figure 

Two 、 Visual feature map
Only the first floor is shown here , Input the visualization of the feature layer after the first layer convolution of a picture
The code is as follows :
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")
# data
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] # The first convolution
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()
The following are the original and effect drawings :


If there are any deficiencies, please point out , Finally, ask for a little praise again .
边栏推荐
猜你喜欢
随机推荐
龙蜥开发者说:首次触电,原来你是这样的龙蜥社区? | 第 8 期
项目经理的晋级之路
redis 数据类型详解
谷歌WayMo提出R4D: 采用参考目标做远程距离估计
Promotion of Project Manager
从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)
杰理之在所有模式下打开喊话增加 mic 自动 mute【篇】
AntD checkbox,限制选中数量
Kotlin asynchronous flow
如何在物联网低代码平台中进行任务管理?
杰理之可能出现有些芯片音乐播放速度快【篇】
万用表测量电阻图解及使用注意事项
Jerry's seamless looping [chapter]
The real project managers are all closed-loop masters!
Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
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
Docker installation redis
2022煤矿瓦斯抽采操作证考试题及模拟考试
取消冒泡
[5g NR] 5g NR system architecture











