未整理Pytorch使用tensorboardX可视化
pytorch tensorboard_tutorial
一、模型可视化
一个简单的网络可视化工具:torchsummary
安装方法:
pip install torchsummary
源代码地址
当然还有增强版:torchsummaryX
例一:VGG网络可视化
>>> import torch, torchvision
>>> model = torchvision.models.vgg.vgg16()
>>> from torchsummary import summary
>>> summary(model, (3, 224, 224)) # (model, input_size, batch_size=-1, device="cuda")
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 224, 224] 1,792
ReLU-2 [-1, 64, 224, 224] 0
Conv2d-3 [-1, 64, 224, 224] 36,928
ReLU-4 [-1, 64, 224, 224] 0
MaxPool2d-5 [-1, 64, 112, 112] 0
Conv2d-6 [-1, 128, 112, 112] 73,856
ReLU-7 [-1, 128, 112, 112] 0
Conv2d-8 [-1, 128, 112, 112] 147,584
ReLU-9 [-1, 128, 112, 112] 0
MaxPool2d-10 [-1, 128, 56, 56] 0
Conv2d-11 [-1, 256, 56, 56] 295,168
ReLU-12 [-1, 256, 56, 56] 0
Conv2d-13 [-1, 256, 56, 56] 590,080
ReLU-14 [-1, 256, 56, 56] 0
Conv2d-15 [-1, 256, 56, 56] 590,080
ReLU-16 [-1, 256, 56, 56] 0
MaxPool2d-17 [-1, 256, 28, 28] 0
Conv2d-18 [-1, 512, 28, 28] 1,180,160
ReLU-19 [-1, 512, 28, 28] 0
Conv2d-20 [-1, 512, 28, 28] 2,359,808
ReLU-21 [-1, 512, 28, 28] 0
Conv2d-22 [-1, 512, 28, 28] 2,359,808
ReLU-23 [-1, 512, 28, 28] 0
MaxPool2d-24 [-1, 512, 14, 14] 0
Conv2d-25 [-1, 512, 14, 14] 2,359,808
ReLU-26 [-1, 512, 14, 14] 0
Conv2d-27 [-1, 512, 14, 14] 2,359,808
ReLU-28 [-1, 512, 14, 14] 0
Conv2d-29 [-1, 512, 14, 14] 2,359,808
ReLU-30 [-1, 512, 14, 14] 0
MaxPool2d-31 [-1, 512, 7, 7] 0
Linear-32 [-1, 4096] 102,764,544
ReLU-33 [-1, 4096] 0
Dropout-34 [-1, 4096] 0
Linear-35 [-1, 4096] 16,781,312
ReLU-36 [-1, 4096] 0
Dropout-37 [-1, 4096] 0
Linear-38 [-1, 1000] 4,097,000
================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 218.59
Params size (MB): 527.79
Estimated Total Size (MB): 746.96
----------------------------------------------------------------
参考
例二:自定义网络可视化
class Convnet(nn.Module): # 重写module
def __init__(self, x_dim, hid_dim=64, z_dim=64):
super().__init__()
self.encoder = nn.Sequential(
# 4层卷积
conv_block(x_dim, hid_dim),
conv_block(hid_dim, hid_dim),
conv_block(hid_dim, hid_dim),
conv_block(hid_dim, z_dim)
)
def forward(self, x):
x = self.encoder(x)
flatten = x.view(x.size(0), -1)
return flatten
from torchsummary import summary
model = Convnet(x_dim=3)
summary(model=model, input_size=(3, 64, 64), device="cpu") # cpu计算
# 结果
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 64, 64] 1,792
BatchNorm2d-2 [-1, 64, 64, 64] 128
ReLU-3 [-1, 64, 64, 64] 0
MaxPool2d-4 [-1, 64, 32, 32] 0
Conv2d-5 [-1, 64, 32, 32] 36,928
BatchNorm2d-6 [-1, 64, 32, 32] 128
ReLU-7 [-1, 64, 32, 32] 0
MaxPool2d-8 [-1, 64, 16, 16] 0
Conv2d-9 [-1, 64, 16, 16] 36,928
BatchNorm2d-10 [-1, 64, 16, 16] 128
ReLU-11 [-1, 64, 16, 16] 0
MaxPool2d-12 [-1, 64, 8, 8] 0
Conv2d-13 [-1, 64, 8, 8] 36,928
BatchNorm2d-14 [-1, 64, 8, 8] 128
ReLU-15 [-1, 64, 8, 8] 0
MaxPool2d-16 [-1, 64, 4, 4] 0
================================================================
Total params: 113,088
Trainable params: 113,088
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.05
Forward/backward pass size (MB): 8.63
Params size (MB): 0.43
Estimated Total Size (MB): 9.11
----------------------------------------------------------------
二、图像可视化
1 安装Visdom模块
pip install visdom
2 启动服务器
程序运行过程中保持服务器开启:
python -m visdom.server
3 例一(以单通道图像28*28为例)
from visdom import Visdom
vis = Visdom()
# 显示单张图片
vis.image(tensor[1,28,28]) # 只展示格式
# 显示多张图片
vis.images(tensor[5,1,28,28])
4 例二
结果: