Pytorch的网络可视化-Tensorboard-pytorch

https://blog.csdn.net/weixin_40712763/article/details/82292046

安装(点击安装可以查看官方教程)

pip install tensorboardX

下载例程

#或者自己建立py文件
mkdir demo
cd demo
touch demo.py
gedit demo.py
#然后把下列代码复制进去

import torch
import torchvision.utils as vutils
import numpy as np
import torchvision.models as models
from torchvision import datasets
from tensorboardX import SummaryWriter

resnet18 = models.resnet18(False)
writer = SummaryWriter()
sample_rate = 44100
freqs = [262, 294, 330, 349, 392, 440, 440, 440, 440, 440, 440]

for n_iter in range(100):

    dummy_s1 = torch.rand(1)
    dummy_s2 = torch.rand(1)
    # data grouping by `slash`
    writer.add_scalar('data/scalar1', dummy_s1[0], n_iter)
    writer.add_scalar('data/scalar2', dummy_s2[0], n_iter)

    writer.add_scalars('data/scalar_group', {'xsinx': n_iter * np.sin(n_iter),
                                             'xcosx': n_iter * np.cos(n_iter),
                                             'arctanx': np.arctan(n_iter)}, n_iter)

    dummy_img = torch.rand(32, 3, 64, 64)  # output from network
    if n_iter % 10 == 0:
        x = vutils.make_grid(dummy_img, normalize=True, scale_each=True)
        writer.add_image('Image', x, n_iter)

        dummy_audio = torch.zeros(sample_rate * 2)
        for i in range(x.size(0)):
            # amplitude of sound should in [-1, 1]
            dummy_audio[i] = np.cos(freqs[n_iter // 10] * np.pi * float(i) / float(sample_rate))
        writer.add_audio('myAudio', dummy_audio, n_iter, sample_rate=sample_rate)

        writer.add_text('Text', 'text logged at step:' + str(n_iter), n_iter)

        for name, param in resnet18.named_parameters():
            writer.add_histogram(name, param.clone().cpu().data.numpy(), n_iter)

        # needs tensorboard 0.4RC or later
        writer.add_pr_curve('xoxo', np.random.randint(2, size=100), np.random.rand(100), n_iter)

dataset = datasets.MNIST('mnist', train=False, download=True)
images = dataset.test_data[:100].float()
label = dataset.test_labels[:100]

features = images.view(100, 784)
writer.add_embedding(features, metadata=label, label_img=images.unsqueeze(1))

# export scalar data to JSON for external processing
writer.export_scalars_to_json("./all_scalars.json")
writer.close()

#关闭demo.py文件

运行可视化

cd demo
python demo.py
tensorboard --logdir runs
#将会生成runs文件夹
tensorboard --logdir runs
#将会生成一个网址,在浏览器中打开即可

leon@231-XPS-8920:~/Leon/Software/tensorboardX-master$ tensorboard --logdir runs/
TensorBoard 1.11.0a20180901 at http://231-XPS-8920:6006 (Press CTRL+C to quit)

可视化效果呈现

Pytorch的网络可视化-Tensorboard-pytorch_第1张图片


过程中可能出现的一些问题

问题1:

E0902 09:17:37.198537 MainThread program.py:267] TensorBoard attempted to bind to port 6006, but it was already in use

问题原因:端口被其他进程占用

解决方法:

lsof -i:6006

得到:

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
Xvnc4 4969 amax 0u IPv4 655595 0t0 TCP *:x11-6 (LISTEN)

输入命令:

kill -9 4969

继续运行tensorboard,如果还出现该问题,关闭之前打开那个进程的终端,重新打开一次,启动tensorboard。


代码分析(参考了网络上的一些其他代码教程,整理了如下文件)


1.将以下代码复制,建立py文件,然后运行代码,按照上面的教程尝试自己生成过程文件,并且用tensorboard打开(这里是参考了另一个博客)

import torch
from tensorboardX import SummaryWriter
# 设计一个小网络
class Net(torch.nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.dense = torch.nn.Linear(in_features=10,out_features=1)
    def forward(self,x):
        return self.dense(x)
 
# 根据小网络实例化一个模型 net
net = Net()
# 创建文件写控制器,将之后的数值以protocol buffer格式写入到logs文件夹中,空的logs文件夹将被自动创建。
writer = SummaryWriter(log_dir='logs')
# 将网络net的结构写到logs里:
data = torch.rand(2,10)
writer.add_graph(net,input_to_model=(data,))
# 注意:pytorch模型不会记录其输入输出的大小,更不会记录每层输出的尺寸。
#      所以,tensorbaord需要一个假的数据 `data` 来探测网络各层输出大小,并指示输入尺寸。
 
# 写一个新的数值序列到logs内的文件里,比如sin正弦波。
for i in range(100):
    x = torch.tensor(i/10,dtype=torch.float)
    y = torch.sin(x)
    # 写入数据的标注指定为 data/sin, 写入数据是y, 当前已迭代的步数是i。
    writer.add_scalar('data/sin',y,i)
 
writer.close()

运行结果显示:

Pytorch的网络可视化-Tensorboard-pytorch_第2张图片

Pytorch的网络可视化-Tensorboard-pytorch_第3张图片


出现的问题:


1.建立了一个网络模型,在添加网络图的时候

writer.add_graph(model, input, verbose=True)出现问题

如下:

Traceback (most recent call last):
  File "/home/leon/Leon/sparse-to-dense-Leon/main.py", line 352, in 
    main()
  File "/home/leon/Leon/sparse-to-dense-Leon/main.py", line 185, in main
    train(train_loader, model,criterion, optimizer, epoch)  # train for one epoch
  File "/home/leon/Leon/sparse-to-dense-Leon/main.py", line 224, in train
    writer.add_graph(model, input, verbose=True)
  File "/usr/local/lib/python3.5/site-packages/tensorboardX/writer.py", line 520, in add_graph
    self.file_writer.add_graph(graph(model, input_to_model, verbose))
  File "/usr/local/lib/python3.5/site-packages/tensorboardX/pytorch_graph.py", line 98, in graph
    torch.onnx._optimize_trace(trace, False)
  File "/usr/local/lib/python3.5/site-packages/torch/onnx/__init__.py", line 30, in _optimize_trace
    trace.set_graph(utils._optimize_graph(trace.graph(), aten))
  File "/usr/local/lib/python3.5/site-packages/torch/onnx/utils.py", line 95, in _optimize_graph
    graph = torch._C._jit_pass_onnx(graph, aten)
  File "/usr/local/lib/python3.5/site-packages/torch/onnx/__init__.py", line 40, in _run_symbolic_function
    return utils._run_symbolic_function(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/torch/onnx/utils.py", line 368, in _run_symbolic_function
    return fn(g, *inputs, **attrs)
TypeError: upsample_bilinear2d() got an unexpected keyword argument 'align_corners' (occurred when translating upsample_bilinear2d)

调试最终发现问题是onnx模块导致的,这里的解决办法和之前的一个问题采用同样的解决办法:

https://blog.csdn.net/weixin_40712763/article/details/82315056

这个是由于pytorch版本太老导致的,直接

sudo pip install torch --upgrade

就可以解决


最后贴一张自己的网络图作为结束吧

Pytorch-tensorboard进阶

Pytorch的网络可视化-Tensorboard-pytorch_第4张图片

 

你可能感兴趣的:(深度学习,深度学习,Pytorch,Tensorboard)