使用pytorch训练大型数据集是常常需要通过loss的下降曲线或者acc准确率的上升情况直观上判断模型的设计是否合理,使用tensorboardX将迭代的loss和acc加入scale中,方便查看中间过程,及时调整模型。
安装:
pip install tensorflow
pip install tensorboardX
定义:
import numpy as np
from tensorboardX import SummaryWriter
writer = SummaryWriter()
添加scale:
for n_iter in range(100):
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)
writer.close()
添加graph:
resnet18 = models.resnet18(False)
dumpty_input = torch.randn(1, 3, 224, 224)
writer.add_graph(resnet18, (dummy_input,))
demo代码:
# 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()
训练后生成的目录文件如下:
linux端:
tensorboard --logdir=./runs
windows端:
ssh -L local_port:127.0.0.1:tensorboard_port username@server_port
浏览器复制地址:
http://localhost:6012/
出现如下界面:
参考代码:tensorboardX_github
来源:CSDN
作者:qq_37172182
链接:https://blog.csdn.net/qq_37172182/article/details/104371497