1.下载可视化代码
git clone https://github.com/lanpa/tensorboard-pytorch.git
2.安装PyTorch 1.0 +torchvision 0.2
3.安装Tensorflow和Tensorboard:
pip install tensorflow
pip install tensorboard==1.7.0
4.安装可视化工具:
pip install tensorboardX
5.运行下面的测试代码demo_LeNet.py :
import torch
import torch.nn as nn
from tensorboardX import SummaryWriter
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Sequential( #input_size=(1*28*28)
nn.Conv2d(1, 6, 5, 1, 2),
nn.ReLU(), #(6*28*28)
nn.MaxPool2d(kernel_size=2, stride=2), #output_size=(6*14*14)
)
self.conv2 = nn.Sequential(
nn.Conv2d(6, 16, 5),
nn.ReLU(), #(16*10*10)
nn.MaxPool2d(2, 2) #output_size=(16*5*5)
)
self.fc1 = nn.Sequential(
nn.Linear(16 * 5 * 5, 120),
nn.ReLU()
)
self.fc2 = nn.Sequential(
nn.Linear(120, 84),
nn.ReLU()
)
self.fc3 = nn.Linear(84, 10)
# 定义前向传播过程,输入为x
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
# nn.Linear()的输入输出都是维度为一的值,所以要把多维度的tensor展平成一维
x = x.view(x.size()[0], -1)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
dummy_input = torch.rand(13, 1, 28, 28) #假设输入13张1*28*28的图片
model = LeNet()
with SummaryWriter(comment='LeNet') as w:
w.add_graph(model, (dummy_input, ))
6.上面的代码运行结束后,会在当前目录生成一个叫run的文件夹,里面存储了可视化所需要的日志信息。用cmd进入到runs文件夹所在的目录中(路劲中不能有中文),然后cmd中输入:
cmd中找到runs文件并执行操作tensorboard --logdir runs
最后会在cmd中得到一个网址,将这个网址复制输入谷歌浏览器中(其他浏览器好像打不开),会弹出LeNet网络可视化结果:
lenet-graph
网友评论