Visualization in pytorch: network model visualization and feature map visualization

1. Use netron tool to visualize pytorch model, tensorboard is too ugly and not intuitive.

Project address: https://github.com/lutzroeder/Netro

Reference: https://blog.csdn.net/jieleiping/article/details/102975939

1. Install netron

pip install netron

2. Case demo

The support for pytorch model format (.pt/.pth) is not friendly, so it needs to be saved as onnx. Fortunately pytorch supports!

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.onnx

import netron


class ForwardNet(nn.Module):
    def __init__(self):
        super(ForwardNet, self).__init__()
        self.block1 = nn.Sequential(
            nn.Conv2d(64, 64, 3, padding=1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 32, 1, bias=False),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace=True),
            nn.Conv2d(32, 64, 3, padding=1, bias=False),
            nn.BatchNorm2d(64)
        )

        self.conv1 = nn.Conv2d(3, 64, 3, padding=1, bias=False)
        self.output = nn.Sequential(
            nn.Conv2d(64, 1, 3, padding=1, bias=True),
            nn.Sigmoid()
        )

    def forward(self, x):
        x = self.conv1(x)
        identity = x
        x = F.relu(self.block1(x) + identity)
        x = self.output(x)
        return x


input = torch.rand(1, 3, 416, 416)
model = ForwardNet()
output = model(input)

onnx_path = "netForwatch.onnx"
torch.onnx.export(model, input, onnx_path)

netron.start(onnx_path)

 3. Results

After executing the above code, the local browser will be called to open, the form is similar to tensorboard.

Two, feature map visualization

Reference: https://zhuanlan.zhihu.com/p/60753993

 

 

Guess you like

Origin blog.csdn.net/MasterCayman/article/details/112140868