【解析】TensorBoard 可视化

在深度学习之中,可视化是必不可少的关键一步,虽然很多时候可以将需要的信息在输出窗口打印出来,但是能表达的信息是在太少,比如 loss趋势图、acc趋势图、权重分布图等等,都难以描述出来;Tensorboard 本是为了 Tensorflow而设计的一款可视化工具,后期 Pytorch 也引入支持,极大的小伙伴们的学习难度,虽然也有其他可视化工具支持,但是 Tensorboard可以直接迁移过来使用,上手还是更快。

官方链接:https://pytorch.org/docs/stable/tensorboard.html


毫无疑问,在使用 tensorboard 之前,需要先安装

pip install tensorboard

Tensorboard 主要的类是 SummaryWriter,所以在使用 Tensorboard 的时候,我们需要先创建一个 SummaryWriter 实例

from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter()

PyCharm 的 Terminal 中输入 以下命令即可
注意⚠️:Terminal 的路径必须是在本工程下,不然可能会报错 或 得到意想不到的事!

# 在PyCharm 的 Terminal 中输入
tensorboard --logdir=runs

# 退出 Tensorboard
CTRL + C 键

1、SummaryWriter主要方法

1.1 add_scalar

add_scalar(tag, scalar_value, global_step=None, walltime=None)

Add scalar data to summary.

  • tag (string) – Data identifier
  • scalar_value (float or string/blobname) – Value to save
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) with seconds after epoch of event

1.2 add_scalars

add_scalars(main_tag, tag_scalar_dict, global_step=None, walltime=None)

Adds many scalar data to summary.

  • main_tag (string) – The parent name for the tags
  • tag_scalar_dict (dict) – Key-value pair storing the tag and corresponding values
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

1.3 add_graph

add_graph(model, input_to_model=None, verbose=False)

Add graph data to summary.

Parameters

  • model (torch.nn.Module) – Model to draw.
  • input_to_model (torch.Tensor or list of torch.Tensor) – A variable or a tuple of variables to be fed.
  • verbose (bool) – Whether to print graph structure in console.

1.4 add_histogram

add_histogram(tag, values, global_step=None, bins='tensorflow', walltime=None, max_bins=None)

Add histogram to summary.

  • tag (string) – Data identifier
  • values (torch.Tensor, numpy.array, or string/blobname) – Values to build histogram
  • global_step (int) – Global step value to record
  • bins (string) – One of {‘tensorflow’,’auto’, ‘fd’, …}. This determines how the bins are made.
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

1.5 add_image

add_image(tag, img_tensor, global_step=None, walltime=None, dataformats='CHW')

Add image data to summary.

Note that this requires the pillow package.

  • tag (string) – Data identifier
  • img_tensor (torch.Tensor, numpy.array, or string/blobname) – Image data
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

Shape:
img_tensor: Default is ( 3 , H , W ) (3,H,W) (3,H,W). You can use torchvision.utils.make_grid() to convert a batch of tensor into 3 × H × W 3\times H\times W 3×H×W format or call add_images and let us do the job. Tensor with ( 1 , H , W ) , ( H , W ) , ( H , W , 3 ) (1,H,W) , (H,W) , (H,W,3) (1,H,W),(H,W),(H,W,3) is also suitable as long as corresponding dataformats argument is passed, e.g. CHW, HWC, HW.


1.6 add_images

add_images(tag, img_tensor, global_step=None, walltime=None, dataformats='NCHW')

Add batched image data to summary.

Note that this requires the pillow package.

  • tag (string) – Data identifier
  • img_tensor (torch.Tensor, numpy.array, or string/blobname) – Image data
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event
  • dataformats (string) – Image data format specification of the form NCHW, NHWC, CHW, HWC, HW, WH, etc.

Shape:
img_tensor: Default is (N,3,H,W). If dataformats is specified, other shape will be accepted. e.g. NCHW or NHWC.


2、测试

测试代码

from torch.utils.tensorboard import SummaryWriter
import numpy as np

writer = SummaryWriter()

for n_iter in range(100):
    writer.add_scalar('Loss/train', np.random.random(), n_iter)
    writer.add_scalar('Loss/test', np.random.random(), n_iter)
    writer.add_scalar('Accuracy/train', np.random.random(), n_iter)
    writer.add_scalar('Accuracy/test', np.random.random(), n_iter)

writer.close()
    

当运行完 测试代码/项目工程之后,默认情况下,会在项目下生成一个 rans 的文件夹,文件夹内就是运行的结果,接下来,我们需要在 PyCharm 的 Terminal 中输入 以下命令即可
注意⚠️:Terminal 的路径必须是在本工程下,不然可能会报错 或 得到意想不到的事!

在Terminal 运行完之后,会看到 一个蓝色的 http://localhost:6006/,点击就会自动进入 Tensorboard 的网页界面,见效果图

# 在PyCharm 的 Terminal 中输入
tensorboard --logdir=runs

# 退出 Tensorboard
CTRL + C 键

Win系统

在这里插入图片描述

Mac

在这里插入图片描述

效果

在这里插入图片描述


问题

对于win系统用户而言,可能出现没有相应的趋势图,原因又可能是 浏览器的问题,在此推荐使用火狐浏览器,即可轻松解决

ViatorSun CSDN认证博客专家 深度学习 计算机视觉
研究生在读、Github开源世界贡献者,深度学习 & 计算机视觉分享者;
主要研究【深度学习 & 计算机视觉】相关方向,欢迎感兴趣的小伙伴一起交流、探讨~
相关推荐
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页