当前位置:网站首页>Convolution kernel and characteristic graph visualization

Convolution kernel and characteristic graph visualization

2022-06-24 14:08:00 Burn, man


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 .

原网站

版权声明
本文为[Burn, man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241144456011.html