PyTorch深度学习训练可视化工具tensorboardX
点击上方“小白学视觉”,选择加"星标"或“置顶”
重磅干货,第一时间送达
之前笔者提到了PyTorch的专属可视化工具visdom,参看PyTorch深度学习训练可视化工具visdom。但在此之前很多TensorFlow用户更习惯于使用TensorBoard来进行训练的可视化展示。为了能让PyTorch用户也能用上TensorBoard,有开发者提供了PyTorch版本的TensorBoard,也就是tensorboardX。
熟悉TensorBoard的用户可以无缝对接到tensorboardX,安装方式为:
pip install tensorboardX
除了要安装PyTorch之外,还需要安装TensorFlow。跟TensorBoard一样,tensorboardX也支持scalar, image, figure, histogram, audio, text, graph, onnx_graph, embedding, pr_curve,video等不同类型对象的可视化展示方式。
tensorboardX和TensorBoard的启动方式一样,直接在终端下运行:
tensorboard --logdir runs
然后另起一个终端执行Python文件即可:
python demo.py
打开localhost:6006即可看到tensorboardX可视化界面。
tensorboardX本地启动非常容易,但一般情况下我们训练都是在服务器上完成的, 所以要在远程启动tensorboardX需要进行一些简单的设置。以虚拟机工具xshell为例:依此设置文件->属性->ssh->隧道->添加,类型local,源主机填127.0.0.1(本机),端口设置一个,比如12345,目标主机为服务器地址,目标端口一般是6006,如果6006被占了可以改为其他端口。
分别执行tensorboard和python脚本后,本地打开127.0.0.1:12345即可进入远程TensorBoard界面。
以scalar为例来看一下tensorboardX的使用方式:
import numpy as np
from tensorboardX import SummaryWriter
writer = SummaryWriter()
for i in range(100):
writer.add_scalar('data/scalar1', np.random.rand(), i)
writer.add_scalar('data/scalar2', {'xsinx': i*np.sin(i), 'xcosx': i*np.cos(i)}, i)
writer.close()
scalar可视化如下图所示。
一个完整tensorboardX 使用demo如下:
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()
可视化效果如下所示:
参考资料:
https://github.com/lanpa/tensorboardX
https://www.tensorflow.org/tensorboard
交流群
欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉、传感器、自动驾驶、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~