tensorboardX api手册& 简单教程(看着像官方发布的):
https://tensorboardx.readthedocs.io/en/latest/tutorial.html
tensorboardX github地址:
https://github.com/lanpa/tensorboardX
这个东西非常简单,很多网友的博客对初学者不利,加了自己的东西,致使简单上手的tensorboardx变得困难。
下面搬运github readme 部分(请务必多看几遍,官方说明,没有比它更合适的学习途径)
用简单的函数调用写TensorBoard事件。
支持scalar
,image
,figure
,histogram
,audio
,text
,graph
,onnx_graph
,embedding
,pr_curve
和video
摘要。
要求demo_graph.py
是tensorboardX> = 1.2和pytorch> = 0.4
常问问题
在anaconda2 / anaconda3上测试,PyTorch 1.0.0 / torchvision 0.2.1 / tensorboard 1.12.0
pip install tensorboardX
或者从源代码构建:
git clone https://github.com/lanpa/tensorboardX && cd tensorboardX && python setup.py install
python examples/demo.py
tensorboard --logdir runs
(needs to install TensorFlow)# 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("./data")
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()
To add more ticks for the slider (show more image history), check https://github.com/lanpa/tensorboardX/issues/44 orhttps://github.com/tensorflow/tensorboard/pull/1138