數據可視化:TensorboardX安裝及使用
tensorboard做爲Tensorflow中強大的可視化工具:
https://github.com/tensorflow/tensorboard,已經被普遍使用
html
但針對其餘框架,例如Pytorch,以前一直沒有這麼好的可視化工具可用,好在目前Pytorch也能夠支持Tensorboard了,那就是經過使用tensorboardX,真是Pytorcher的福利!python
Github傳送門:Tensorboard , TensorboardX
能夠看到 tensorboardX完美支持了tensorboard經常使用的function
git
下面介紹tensorboardX安裝和基本使用方法:github
tensorboardX安裝:
由於tensorboardX是對tensorboard進行了封裝後,開放出來使用,因此必須先安裝tensorboard, 再安裝tensorboardX,
(而若是不須要,能夠不安裝tensorflow,只是有些功能會受限)
web
直接使用pip/conda安裝:chrome
- pip install tensorboard
- pip install tensorboardX
tensorboardX使用:
安裝好後,剩下的和tensorboard使用方法基本一致,
先跑一遍example中的實例,
json
- git clone https://github.com/lanpa/tensorboardX.git
能夠看到example 文件夾有不少實例
運行demo.py:
瀏覽器
- python demo.py
demo.py運行後,會在該目錄生成默認的runs文件夾,裏面存儲了Demo程序寫入的log文件(經過pytorch),這樣就能夠經過tensorboard對這些數據可視化了:markdown
- tensorboard --logdir runs
和往常同樣啓動tensorboard,web組件會在localhost搭建一個Port默認爲6006框架
這時候打開瀏覽器(最好用chrome)進入http://localhost:6006/ ,就能夠查看數據,仍是熟悉的操做:
查看scalars:
images:
projector:
distributions:
Histograms:
pr curves:
etc… 具體tensorboard各項功能和使用能夠查看tensorboard官方教程:
https://tensorflow.google.cn/tensorboard/get_started
其中demo.py以下,能夠看到代碼上tensorboardX使用方法和tensorboard基本一致,tensorboardX經過SummaryWriter 類操做log data(也只有這一個類),而且經過add_xxxx記錄各種data(如圖表、直方圖、圖片,標量等等),(對應tensorflow1.0以後版本改爲了tensorflow.summary.FileWriter, add_xxxx)
# 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()