pytorch实战-01可视化 tensorboardX

功能:展示损失函数变化、原图和变化后图、展示代码逻辑等,
代码如下

from tensorboardX import SummaryWriter
from PIL import Image

# 实例化
writer = SummaryWriter('name')
# 添加图片
img = Image.path(path)
writer.add_image()
# 添加标量
writer.add_scaler()
# 添加流程图
writer.add_graph(model, input)
writer.close()

打开事件文件

在terminal端,
激活pytorch:conda activate pytorch
打开:tensorboard --logdir=logs

ps:
1、若出现tensorboard不是内部命令的错误提示,说明没有安装tensorboardx,需要使用pip安装tensorboardx,注意同时需要下载tensorflow
2、–logdir=后面的目录是logs的绝对目录,需用双引号引起来。

举例一:add_image

from tensorboardX 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(type(img_array))
print(img_array.shape)

writer.add_image("train", img_array, 1, dataformats='HWC')

writer.close()

举例二:add_scalar

from tensorboardX import SummaryWriter

writer = SummaryWriter("../logs")

for i in range(100):
    writer.add_scalar("y=3x", 3*i, i)

writer.close()

你可能感兴趣的:(可视化,pytorch,深度学习,神经网络)