Pytorch学习之tensorboard的使用方法
本节内容:
学习tensorboard的使用方法
数据写入文件(主要是标量数据和图片数据)
对图片数据格式认识
对写入的文件进行可视化
# SummaryWriter :的使用方法
from tensorboardX import
SummaryWriter # 从tensorboardX 导入SummaryWriter 类:https://blog.csdn.net/qq_28666313/article/details/106343611
import numpy as np
from PIL import Image
import numpy as np
from PIL import Image
# 目前都是导入tensorboardX,不在采用from torch.untils.tensorboard import SummaryWriter 的方式
## 导入需要的库
writer = SummaryWriter("logs") # 创建实例,我们将内容写入到logs文加夹中,如果填该参数,则写入默认文件夹
image_path = "dataset/hymenoptera_data/train/ants/0013035.jpg"
img_PIL = Image.open(image_path)
image_array = np.array(img_PIL)
writer.add_image('test', image_array, 2,dataformats='HWC')
"""
Shape:
img_tensor: Default is :math:`(3, H, W)`. You can use ``torchvision.utils.make_grid()`` to
convert a batch of tensor into 3xHxW format or use ``add_images()`` and let us do the job.
Tensor with :math:`(1, H, W)`, :math:`(H, W)`, :math:`(H, W, 3)` is also suitible as long as
corresponding ``dataformats`` argument is passed. e.g. CHW, HWC, HW.
# 从官网文件可以看出,该方法接受的数据是,【通道,高度,宽度】===>CHW 这样的数据类型,一般的数据类型都是[高度,宽度,通道]===>HWC
# print(img.shape)
# (512, 768, 3) 图片不符者要求,需要改变其形状
"""
'nShape:n img_tensor: Default is :math:`(3, H, W)`. You can use ``torchvision.utils.make_grid()`` ton convert a batch of tensor into 3xHxW format or use ``add_images()`` and let us do the job.n Tensor with :math:`(1, H, W)`, :math:`(H, W)`, :math:`(H, W, 3)` is also suitible as long asn corresponding ``dataformats`` argument is passed. e.g. CHW, HWC, HW.n# 从官网文件可以看出,该方法接受的数据是,【通道,高度,宽度】===>CHW 这样的数据类型,一般的数据类型都是[高度,宽度,通道]===>HWCn# print(img.shape)n# (512, 768, 3) 图片不符者要求,需要改变其形状n'
'''
在这个类我们一般使用两个方法
writer.add_image() # 添加图片进行可视化
writer.add_scalar() # 添加数字进行可视化scalar为标量其实就是数字
用完之后别忘了关闭
writer.close()
'''
## writer.add_scalar() 的用法
'''
tag: str, # 可以认为是图标的titel
scalar_value: Value to save, if string is passed, it will be treated
as caffe blob name. 这个相当于Y轴数据
global_step: Global step value to record 这个相当于X轴数据
'''
'ntag: str, # 可以认为是图标的titelnscalar_value: Value to save, if string is passed, it will be treatedn as caffe blob name. 这个相当于Y轴数据nglobal_step: Global step value to record 这个相当于X轴数据n'
简单演示一下,
x = range(100)
for i in x:
writer.add_scalar('y=2x', i * 2, i)
writer.add_scalar('y=x**3', i ** 2, i)
writer.add_scalar('y=x**2', i ** 3, i)
writer.close()
## 打开方式
# 在terminal中输入命令:tensorboard--logdir "文件名" # 这里的文件名可以是相对路径也可以是绝对路径