赞
踩
tensorboard作为Tensorflow中强大的可视化工具,已经被广泛使用
但针对其他框架,例如Pytorch,之前一直没有这么好的可视化工具可用,好在目前Pytorch也可以支持Tensorboard了,那就是通过使用tensorboardX,真是Pytorcher的福利!
Github传送门:Tensorboard , TensorboardX
可以看到 tensorboardX完美支持了tensorboard常用的function
下面介绍tensorboardX安装和基本使用方法:
因为tensorboardX是对tensorboard进行了封装后,开放出来使用,所以必须先安装tensorboard, 再安装tensorboardX,
(而如果不需要,可以不安装tensorflow,只是有些功能会受限)
直接使用pip/conda安装:
安装好后,剩下的和tensorboard使用方法基本一致,
先跑一遍example中的实例,
可以看到example 文件夹有很多实例
运行demo.py:
demo.py运行后,会在该目录生成默认的runs文件夹,里面存储了Demo程序写入的log文件(通过pytorch),这样就可以通过tensorboard对这些数据可视化了:
和往常一样启动tensorboard,web组件会在localhost搭建一个Port默认为6006
这时候打开浏览器(最好用chrome)进入http://localhost:6006/ ,就可以查看数据,还是熟悉的操作:
查看scalars:
images:
projector:
distributions:
Histograms:
pr curves:
其中demo.py如下,可以看到代码上tensorboardX使用方法和tensorboard基本一致。。。
tensorboardX通过SummaryWriter 类操作log data,并且通过add_xxxx记录各类data(如标量,图表、直方图、图片等等),(对应tensorflow1.0之后版本改成了tensorflow.summary.FileWriter, add_xxxx)
具体tensorboard各项功能和使用可以参考官方教程:
https://tensorflow.google.cn/tensorboard/get_started
tensorboardX API介绍详见:
https://tensorboardx.readthedocs.io/en/latest/tensorboard.html
# demo.py
import torch
import torchvision.utils as vutils
import numpy as np
import torchvision.models as models
from torchvision import datasets
from tensorboardX import SummaryWriter
resnet18 = models.resnet18(False)
writer = SummaryWriter()
sample_rate = 44100
freqs = [262, 294, 330, 349, 392, 440, 440, 440, 440, 440, 440]
for n_iter in range(100):
dummy_s1 = torch.rand(1)
dummy_s2 = torch.rand(1)
# data grouping by `slash`
writer.add_scalar('data/scalar1', dummy_s1[0], n_iter)
writer.add_scalar('data/scalar2', dummy_s2[0], n_iter)
writer.add_scalars('data/scalar_group', {'xsinx': n_iter * np.sin(n_iter),
'xcosx': n_iter * np.cos(n_iter),
'arctanx': np.arctan(n_iter)}, n_iter)
dummy_img = torch.rand(32, 3, 64, 64) # output from network
if n_iter % 10 == 0:
x = vutils.make_grid(dummy_img, normalize=True, scale_each=True)
writer.add_image('Image', x, n_iter)
dummy_audio = torch.zeros(sample_rate * 2)
for i in range(x.size(0)):
# amplitude of sound should in [-1, 1]
dummy_audio[i] = np.cos(freqs[n_iter // 10] * np.pi * float(i) / float(sample_rate))
writer.add_audio('myAudio', dummy_audio, n_iter, sample_rate=sample_rate)
writer.add_text('Text', 'text logged at step:' + str(n_iter), n_iter)
for name, param in resnet18.named_parameters():
writer.add_histogram(name, param.clone().cpu().data.numpy(), n_iter)
# needs tensorboard 0.4RC or later
writer.add_pr_curve('xoxo', np.random.randint(2, size=100), np.random.rand(100), n_iter)
dataset = datasets.MNIST('mnist', train=False, download=True)
images = dataset.test_data[:100].float()
label = dataset.test_labels[:100]
features = images.view(100, 784)
writer.add_embedding(features, metadata=label, label_img=images.unsqueeze(1))
# export scalar data to JSON for external processing
writer.export_scalars_to_json("./all_scalars.json")
writer.close()
一篇读懂系列:
LoRa Mesh系列:
网络安全系列:
嵌入式开发系列:
AI / 机器学习系列:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。