当前位置:网站首页>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 .
边栏推荐
- Ti Xing Shu'an joined the dragon lizard community to jointly create a network security ecosystem
- 杰理之无缝循环播放【篇】
- Operation of simulated examination platform of examination question bank for B certificate of Jiangxi provincial safety officer in 2022
- #21Set经典案例
- HarmonyOS-3
- Gatling 性能测试
- Kotlin composite suspend function
- Redis面试题
- markdown/LaTeX中在字母下方输入圆点的方法
- 文本对比学习综述
猜你喜欢

P2pdb white paper
![Jerry's infrared filtering [chapter]](/img/6b/7c4b52d39a4c90f969674a5c21b2c7.png)
Jerry's infrared filtering [chapter]

吉时利静电计宽测量范围

Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire

数商云:加强供应商管理,助推航空运输企业与供应商高效协同
![二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]](/img/d0/91ab1cc1851d7137a1cab3cf458302.png)
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]

Antd checkbox, limit the selected quantity

HarmonyOS.2

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

Research and development practice of Kwai real-time data warehouse support system
随机推荐
kotlin 协程通道
AQS初探
NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)
如何在物联网低代码平台中进行任务管理?
Return to new list
Halcon 绘制区域 到图片中
How to avoid serious network security accidents?
#21Set经典案例
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!
HarmonyOS-3
[R language data science] (XIV): random variables and basic statistics
Unity 热力图建立方法
Home office should be more efficient - automated office perfectly improves fishing time | community essay solicitation
OpenHarmony 1
Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)
【R语言数据科学】(十四):随机变量和基本统计量
kotlin 协程上下文和调度器
Jupyter notebook操作
群晖向阿里云OSS同步
Redis面试题


