TensorBoard
SummaryWriter类
下面是一些初始化的例子:
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/
add_scalar()方法
Add scalar data to summary.
参数
示例:
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
for i in range(100):
writer.add_scalar("y=2x", 2*i, i) #tag,y,x
writer.close()
在控制台执行
tensorboard --logdir=logs --port=6007
点击控制台连接
add_image()
Add image data to summary
参数
首先关于图像类型,我们能直接用Image.open()得到的图像吗?
很明显是不可以的,因为这个图像类型不是上面三种的任何一种,所以要类型转换
用numpy将img变量转化成numpy.array类型
那改完参数看看是不是可以用了呢?
sorry 报错啦!!
再回到源文档可以知道,还有一个shape限制,也就是说图像参数的shape必须默认是CHW
现在这个图像类型参数的shape是HWC:
如果不是怎么办?添加一个参数就行dataformats=‘HWC’
writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC')
最后的代码是:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer = SummaryWriter("logs")
image_path = "data/train/ants_image/0013035.jpg"
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL)
print(img_array.shape)
writer.add_image("test", img_array, 1, dataformats='HWC')
for i in range(100):
writer.add_scalar("y=2x", 2*i, i) #tag,y,x
writer.close()
刷新页面点击上方导航栏的IMAGE就可以看到图像啦
如果改变step,会怎么样呢
writer.add_image("test", img_array, 2, dataformats='HWC')
观察下面的图可以知道,其实就是在这个test的标题下面多了一个step2的图片,左滑动就是step1的图片
如果换个title呢?顺便也换了个图片
image_path = "data/train/ants_image/5650366_e22b7e1065.jpg"
writer.add_image("ants", img_array, 2, dataformats='HWC')