文章目录
- 1.什么是TensorBoard
- 2.安装
- 3.启动
- 4.Pytorch 使用 TensorBoard
- 4.1 写入数据
- 4.2 写入图像
1.什么是TensorBoard
TensorBoard是一个工具,主要用于数据可视化,用大白话的语言来说就是可以记录在机器学习中表格数据、非表格数据(图片、文本、音频等)等变化,从而在模型中更直观的显示。
2.安装
一般安装新版的pytorch会自动安装,如果没安装,则在终端命令行下使用pip install tensorboard语法指令即可安装
3.启动
在终端下使用如下语法:tensorboard --logdir=<directory_name>
其中directory_name为log文件所在的目录,因为Tensorboard面板中展示的数据都来源于log文件,一次完整的运行生成一份log文件。
如下:
此时访问:http://localhost:6006/?darkMode=true
出现如下界面即表示成功
可以自己指定端口:
此时访问就是6007端口了
4.Pytorch 使用 TensorBoard
SummaryWriter类,该类的初始化参数主要有如下几个:
"""
def __init__(
self,
log_dir=None,
comment="",
purge_step=None,
max_queue=10,
flush_secs=120,
filename_suffix="",
):
"""Creates a `SummaryWriter` that will write out events and summaries
to the event file.
Args:
log_dir (str): Save directory location. Default is
runs/**CURRENT_DATETIME_HOSTNAME**, which changes after each run.
Use hierarchical folder structure to compare
between runs easily. e.g. pass in 'runs/exp1', 'runs/exp2', etc.
for each new experiment to compare across them.
comment (str): Comment log_dir suffix appended to the default
``log_dir``. If ``log_dir`` is assigned, this argument has no effect.
purge_step (int):
When logging crashes at step :math:`T+X` and restarts at step :math:`T`,
any events whose global_step larger or equal to :math:`T` will be
purged and hidden from TensorBoard.
Note that crashed and resumed experiments should have the same ``log_dir``.
max_queue (int): Size of the queue for pending events and
summaries before one of the 'add' calls forces a flush to disk.
Default is ten items.
flush_secs (int): How often, in seconds, to flush the
pending events and summaries to disk. Default is every two minutes.
filename_suffix (str): Suffix added to all event filenames in
the log_dir directory. More details on filename construction in
tensorboard.summary.writer.event_file_writer.EventFileWriter.
Examples::
from torch.utils.tensorboard import SummaryWriter
# create a summary writer with automatically generated folder name.
writer = SummaryWriter()
# folder location: runs/May04_22-14-54_s-MacBook-Pro.local/
# create a summary writer using the specified folder name.
writer = SummaryWriter("my_experiment")
# folder location: my_experiment
# create a summary writer with comment appended.
writer = SummaryWriter(comment="LR_0.1_BATCH_16")
# folder location: runs/May04_22-14-54_s-MacBook-Pro.localLR_0.1_BATCH_16/
"""
主要的参数是log_dir,他的设定就是对应log文件所对应的目录文件夹。
主要方法
add_image()添加图片
add_scalar()添加标量数据
4.1 写入数据
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer = SummaryWriter("logs") # 创建SummaryWriter,为log文件起目录
for i in range(100):
writer.add_scalar("y=2x",2*i,i) # 第一个参数相当于标题,第二个参数就相当于纵坐标的值,第三个参数就相当于横坐标的值
writer.close()
命令行打开TensorBoard面板:logs为上面创建的文件夹
访问:http://localhost:6006/?darkMode=true#timeseries 就能看到
4.2 写入图像
add_image()方法
def add_images(
self, tag, img_tensor, global_step=None, walltime=None, dataformats="NCHW"
):
"""Add batched image data to summary.
Note that this requires the ``pillow`` package.
Args:
tag (str): Data identifier
img_tensor (torch.Tensor, numpy.ndarray, 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 (str): Image data format specification of the form
NCHW, NHWC, CHW, HWC, HW, WH, etc.
Shape:
img_tensor: Default is :math:`(N, 3, H, W)`. If ``dataformats`` is specified, other shape will be
accepted. e.g. NCHW or NHWC.
Examples::
from torch.utils.tensorboard import SummaryWriter
import numpy as np
img_batch = np.zeros((16, 3, 100, 100))
for i in range(16):
img_batch[i, 0] = np.arange(0, 10000).reshape(100, 100) / 10000 / 16 * i
img_batch[i, 1] = (1 - np.arange(0, 10000).reshape(100, 100) / 10000) / 16 * i
writer = SummaryWriter()
writer.add_images('my_image_batch', img_batch, 0)
writer.close()
tag这个参数就是定义标题
img_tensor (torch.Tensor, numpy.ndarray, or string/blobname): 可见这个参数规定了读取的图片类型需要为torch.Tensor, numpy.ndarray, or string/blobname这几种类型,所以需要利用OpenCV读取图片,获得numpy类型数据、或者numpy直接转。下面将使用numpy获取。
global_step=None这个参数是指定图片在第几步显示,因为打开TensorBoard面板后会有可移动的step进度条,能供移动显示该图片是位于当前tag下的第几张。
walltime=None这个参数记录发生的时间,默认为 time.time()。
dataformats="NCHW"这个参数是图像数据的格式(图像都有长宽通道三个数值),默认为 ‘CHW’,即 Channel x Height x Width,还可以是 ‘CHW’、‘HWC’ 或 ‘HW’ 等。
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer = SummaryWriter("logs") # 创建SummaryWriter,为log文件起目录
img_path = "hymenoptera_data/train/ants_image/5650366_e22b7e1065.jpg"
img_PIL = Image.open(img_path)
# 因为add_image()这个函数的第二个参数需要的类型为 (torch.Tensor, numpy.ndarray, or string/blobname)这几个才可以,而原本的img_PIL类型不属于那几个
img_array = np.array(img_PIL)
writer.add_image("test",img_array,2,dataformats="HWC")
writer.close()