TensorBoard:Tensorflow中强大的可视化工具
TensorBoard测试代码
import numpy as np from torch.utils.tensorboard import SummaryWriter writer=SummaryWriter(comment='test_tensorboard') for x in range(100): writer.add_scalar('y=2x',x*2,x) writer.add_scalar('y=pow(2,x)',2**x,x) writer.add_scalars('data/scalar_group',{"sins":x*np.sin(x), "xcox":x*np.cos(x), "arctanx":np.arctan(x)},x) writer.close()在目标文件内命令行内,输入tensorboard --logdir=./runs,其中runs是event_file所在的文件夹
此后在终端内,会出现一个网址一般为:http://localhost:6006/
安装时会提示报错,ModuleNotFoundError:No module named ‘past’
通过pip install future解决
功能:提供创建event file的高级接口 主要属性:
log_fir:event file输出文件夹如果不设置,会在当前文件夹下的runs文件夹comment:不指定log_dir时,文件夹后缀,如果log_dir为空会生效,给runs文件下的文件夹后缀filename_suffix:evebnt file 文件名后缀,给event file文件设置的后缀功能:记录标量
tag:图像的标签名,图的唯一标识scalar_value:要记录的标量global_step:x轴缺陷:只能记录一条曲线
功能:统计直方图与多分位数折线图
tag:图像的标签名,图的唯一标识values:要统计的参数global_step:y轴bins:取直方图bins writer = SummaryWriter(comment='test_comment', filename_suffix="test_suffix") for x in range(2): np.random.seed(x) data_union = np.arange(100) data_normal = np.random.normal(size=1000) writer.add_histogram('distribution union', data_union, x) writer.add_histogram('distribution normal', data_normal, x) plt.subplot(121).hist(data_union, label="union") plt.subplot(122).hist(data_normal, label="normal") plt.legend() plt.show() writer.close()功能:记录图像
tag:图像的标签名,图的唯一标识img_tensor:图像数据,注意尺度,如果所有像素点都是(0,1)之间就默认乘以255,如果有像素点大于1就默认所有像素点都是0~255的尺度global_step:x轴dataformats:数据形式,CHW,HWC,HW writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix") # img 1 random fake_img = torch.randn(3, 512, 512) writer.add_image("fake_img", fake_img, 1) time.sleep(1) # img 2 ones fake_img = torch.ones(3, 512, 512) time.sleep(1) writer.add_image("fake_img", fake_img, 2) # img 3 1.1 fake_img = torch.ones(3, 512, 512) * 1.1 time.sleep(1) writer.add_image("fake_img", fake_img, 3) # img 4 HW fake_img = torch.rand(512, 512) writer.add_image("fake_img", fake_img, 4, dataformats="HW") # img 5 HWC fake_img = torch.rand(512, 512, 3) writer.add_image("fake_img", fake_img, 5, dataformats="HWC") writer.close()功能:制作网格图像 可视化MNIST数据集
transform = Compose([transforms.ToTensor()]) datasets=mnist.MNIST(root='./data',train=True,transform=transform,download=True) datalode=DataLoader(datasets,batch_size=16,num_workers=2) images,labels=next(iter(datalode)) imagesTogather=tutiles.make_grid(images,nrow=4) imagesTogather=imagesTogather.transpose(0,2) import matplotlib.pyplot as plt plt.imshow(imagesTogather) plt.show()Pytorch1.2版本存在bug需要求换到1.3版本
writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix") # 模型 fake_img = torch.randn(1, 3, 32, 32) lenet = LeNet(classes=2) writer.add_graph(lenet, fake_img) writer.close()此工具包属于第三方工具包,需要pip install安装一下 pip install torchsummary
from torchvision.models import alexnet Alexnet=alexnet() from torchsummary import summary print(summary(Alexnet, (3, 256,256), device="cpu"))