当前位置:网站首页>Gpushare.com | 如何使用TensorBoardX可视化工具?
Gpushare.com | 如何使用TensorBoardX可视化工具?
2022-07-24 03:08:00 【恒源云】
TensorBoardX是基于TensorBoard,一款可以用于Pytorch数据可视化的工具,对TensorBoard比较了解的用户,也能够轻松上手TensorBoardX~
我们一起来看看,在咱们平台,如何使用TensorBoardX呢?
安装TensorBoardX
这里演示为Pytorch框架使用 TensorBoardX 可视化,创建一个 Pytorch 框架的实例,然后进行如下操作。
安装tensorboardX
~# pip install tensorboardX
可以选择安装crc32c以加快速度
~# pip install crc32c
从tensorboardX 2.1开始,需要为add_audio()函数安装soundfile
~# pip install soundfile
#安装soundfile所需要的依赖
~# apt-get update -y && apt-get install libsndfile1 -y
上传代码
这里通过tensorboardX的项目提供的代码来运行,大家在训练的过程中需要使用自己的代码并上传到实例中。
~# git clone https://github.com/lanpa/tensorboardX.git
#查看tensorboardX的项目提供代码的示例,主要查看如何调用TensorBoardX进行展示
~# cat tensorboardX/examples/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
import datetime
try:
import soundfile
skip_audio = False
except ImportError:
skip_audio = True
resnet18 = models.resnet18(False)
writer = SummaryWriter()
sample_rate = 44100
freqs = [262, 294, 330, 349, 392, 440, 440, 440, 440, 440, 440]
true_positive_counts = [75, 64, 21, 5, 0]
false_positive_counts = [150, 105, 18, 0, 0]
true_negative_counts = [0, 45, 132, 150, 150]
false_negative_counts = [0, 11, 54, 70, 75]
precision = [0.3333333, 0.3786982, 0.5384616, 1.0, 0.0]
recall = [1.0, 0.8533334, 0.28, 0.0666667, 0.0]
for n_iter in range(100):
s1 = torch.rand(1) # value to keep
s2 = torch.rand(1)
# data grouping by `slash`
writer.add_scalar('data/scalar_systemtime', s1[0], n_iter, summary_description="# markdown is supported!")
# data grouping by `slash`
writer.add_scalar('data/scalar_customtime', s1[0], n_iter, walltime=n_iter, display_name="dudubird")
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)
x = torch.rand(32, 3, 64, 64) # output from network
if n_iter % 10 == 0:
x = vutils.make_grid(x, normalize=True, scale_each=True)
writer.add_image('Image', x, n_iter) # Tensor
writer.add_image_with_boxes('imagebox_label', torch.ones(3, 240, 240) * 0.5,
torch.Tensor([[10, 10, 100, 100], [101, 101, 200, 200]]),
n_iter,
labels=['abcde' + str(n_iter), 'fgh' + str(n_iter)])
if not skip_audio:
x = torch.zeros(sample_rate * 2)
for i in range(x.size(0)):
# sound amplitude should in [-1, 1]
x[i] = np.cos(freqs[n_iter // 10] * np.pi *
float(i) / float(sample_rate))
writer.add_audio('myAudio', x, n_iter)
writer.add_text('Text', 'text logged at step:' + str(n_iter), n_iter)
writer.add_text('markdown Text', '''a|b\n-|-\nc|d''', n_iter)
for name, param in resnet18.named_parameters():
if 'bn' not in name:
writer.add_histogram(name, param, n_iter)
writer.add_pr_curve('xoxo', np.random.randint(2, size=100), np.random.rand(
100), n_iter) # needs tensorboard 0.4RC or later
writer.add_pr_curve_raw('prcurve with raw data', true_positive_counts,
false_positive_counts,
true_negative_counts,
false_negative_counts,
precision,
recall, n_iter)
# export scalar data to JSON for external processing
writer.export_scalars_to_json("./all_scalars.json")
dataset = datasets.MNIST('mnist', train=False, download=True)
images = dataset.data[:100].float()
label = dataset.targets[:100]
features = images.view(100, 784)
writer.add_embedding(features, metadata=label, label_img=images.unsqueeze(1))
writer.add_embedding(features, global_step=1, tag='noMetadata')
images_train = dataset.data[100:200].float()
labels_train = dataset.targets[100:200]
features_train = images_train.view(100, 784)
all_features = torch.cat((features, features_train))
all_labels = torch.cat((label, labels_train))
all_images = torch.cat((images, images_train))
dataset_label = ['test'] * 100 + ['train'] * 100
all_labels = list(zip(all_labels, dataset_label))
writer.add_embedding(all_features, metadata=all_labels, label_img=all_images.unsqueeze(1),
metadata_header=['digit', 'dataset'], global_step=2)
# VIDEO
vid_images = dataset.data[:16 * 48]
vid = vid_images.view(16, 48, 1, 28, 28) # BxTxCxHxW
writer.add_video('video', vid_tensor=vid)
writer.add_video('video_1_fps', vid_tensor=vid, fps=1)
writer.close()
writer.add_scalar('implicit reopen writer', 100, 0)
运行程序
下面示例中通过 tmux 程序来托管程序运行。
#创建一个demo的tmux窗口
~# tmux new -s demo
#运行程序
~# python tensorboardX/examples/demo.py
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to mnist/MNIST/raw/train-images-idx3-ubyte.gz
运行TensorBoardX
上个步骤通过tmux运行了python项目,这里需要重新打开一个ssh终端窗口。
#启动TensorboardX前,需要先关闭官方镜像中安装的tensorboard
~# supervisord ctl stop tensorboard
#启动TensorBoardX也通过tmux程序托管运行
~# tmux new -s tensorboard
~# tensorboard --logdir runs --host 0.0.0.0
访问TensorBoardX
打开 恒源云控制台,然后找到当前运行实例的Tensorboard进行访问即可。

平台文档:
TensorBoardX - 恒源云用户文档
https://gpushare.com/docs/best_practices/tensorboardX/
边栏推荐
- Services et configurations FTP
- Go log package
- Hcip day 10 (initial BGP border gateway protocol)
- Go IO operation - file write
- Daily gossip (I)
- Analyze the space practice field required by maker education activities
- 移动通信的新定义:R&SCMX500 将提高5G设备的IP数据吞吐量
- Example of producer consumer code implemented by the destructor framework without lock
- [C language] file operation
- Generate 13 bit barcode
猜你喜欢

攻防世界WEB練習區(view_source、get_post、robots)

Hcip day 10 (initial BGP border gateway protocol)

Interpretation of steam education with the deepening of educational reform

openEuler 资源利用率提升之道 01:概论

Basic use of Pinia

O3DE 的Lumberyard 游戏引擎

Basic knowledge of trigger (Part 2)

Detailed explanation of wechat official account online customer service access methods and functions

SkyWalking分布式系统应用程序性能监控工具-上

Customize the default width and height of kindeditor rich text
随机推荐
An introductory example of structure and combinatorial ideas in go language
Attack and defense world web practice area (webshell, command_execution, simple_js)
JS Array isaarray () Type of
JS small game running bear and cat source code
软考---程序设计语言基础(上)
Liveqing live broadcast on demand streaming media OBS streaming live broadcast how to obtain interface verification token video verification streamtoken and configure token validity
微信公众号在线客服接入发方法和功能详解
openEuler 资源利用率提升之道 01:概论
Go IO operation - file read
Redux Usage Summary
Lcd1602——斌哥51
Summernote supports custom video upload function
[C language] file operation
summernote支持自定义视频上传功能
Attack and defense world web practice area (backup, cookie, disabled_button)
Dynamic programming-01 knapsack problem
关于Aries框架增删改查-查Demo
Qt自定义类使用自定义含参信号与槽
SolidWorks CAM data cannot be recovered because a processed part has been detected.
TCP data transmission and performance
https://github.com/lanpa/tensorboardX