pytorch中的可视化:网络模型可视化以及特征图可视化

一、使用netron工具可视化pytorch模型,tensorboard太丑了不直观。

项目地址:https://github.com/lutzroeder/Netro

参考:https://blog.csdn.net/jieleiping/article/details/102975939

1.安装netron

pip install netron

2.案列demo

对pytorch模型格式(.pt/.pth)支持不友好,因此需要存为onnx,庆幸pytorch支持!

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.结果

执行上面代码后,会调用本地浏览器打开,形式和tensorboard差不多。

二、特征图可视化

参考:https://zhuanlan.zhihu.com/p/60753993

猜你喜欢

转载自blog.csdn.net/MasterCayman/article/details/112140868