PyTorch deep learning training visualization tool tensorboardX

Click on " Xiaobai Learning Vision " above, and choose to add " star " or " top "

Heavy dry goods, delivered as soon as possible296eb5e0e56ad8c6996c95d324d80623.png

Earlier, the author mentioned PyTorch's exclusive visualization tool visdom, see PyTorch deep learning training visualization tool visdom. But before that, many TensorFlow users were more accustomed to using TensorBoard for visual display of training. In order to allow PyTorch users to use TensorBoard, some developers have provided a PyTorch version of TensorBoard, which is tensorboardX.

1de08a6b1ef1e07dbb77cb5df8b55572.png

Install and start

     Users who are familiar with TensorBoard can seamlessly connect to tensorboardX. The installation method is as follows:

pip install tensorboardX

     In addition to installing PyTorch, you also need to install TensorFlow. Like TensorBoard, tensorboardX also supports the visualization of different types of objects such as scalar, image, figure, histogram, audio, text, graph, onnx_graph, embedding, pr_curve, video, etc.

TensorboardX starts in the same way as TensorBoard, and runs directly under the terminal:

tensorboard --logdir runs

     Then start another terminal to execute the Python file:

python demo.py

     Open localhost:6006 to see the tensorboardX visualization interface.

     It is very easy to start tensorboardX locally, but in general, our training is done on the server, so some simple settings are required to start tensorboardX remotely. Take the virtual machine tool xshell as an example: set file->properties->ssh->tunnel->add, type local, fill in 127.0.0.1 (local machine) for the source host, set a port, such as 12345, and the target host is the server The address, the target port is generally 6006, if 6006 is occupied, it can be changed to other ports.

91e045a320359e1a0aa7f45d4dd5cecd.png

     After executing the tensorboard and python scripts respectively, open 127.0.0.1:12345 locally to enter the remote TensorBoard interface.

Example of use

     Take scalar as an example to see how tensorboardX is used:

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()

     The scalar visualization is shown below.

46f30fd59e73446c6c30abdc92f4c248.png

     A complete tensorboardX demo is as follows:

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()

The visualization looks like this:

e3aa4b6fe2b72728cebfddf220be3d9b.gif

  References:

https://github.com/lanpa/tensorboardX

https://www.tensorflow.org/tensorboard

Download 1: OpenCV-Contrib extension module Chinese version tutorial

Reply in the background of the " Xiaobai Learning Vision " official account: Chinese tutorial on extension module , you can download the first Chinese version of the OpenCV extension module tutorial on the whole network, covering extension module installation, SFM algorithm, stereo vision, target tracking, biological vision, super Resolution processing and more than twenty chapters.

Download 2: Python Visual Combat Project 52 Lectures

Reply in the background of the " Xiaobai Learning Vision " public account: Python visual combat project , you can download including image segmentation, mask detection, lane line detection, vehicle counting, adding eyeliner, license plate recognition, character recognition, emotion detection, text content extraction, Facial recognition and other 31 visual combat projects to help fast school computer vision.

Download 3: OpenCV practical project 20 lectures

Reply in the background of the " Xiaobai Learning Vision " official account: OpenCV practical project 20 lectures , you can download 20 practical projects based on OpenCV to achieve advanced OpenCV learning.

exchange group

Welcome to join the public account reader group to communicate with your peers. Currently, there are WeChat groups such as SLAM, 3D vision, sensors, autonomous driving, computational photography, detection, segmentation, recognition, medical imaging, GAN, and algorithm competitions (will be gradually subdivided in the future), Please scan the WeChat account below to join the group, note: "nickname + school/company + research direction", for example: "Zhang San + Shanghai Jiaotong University + Visual SLAM". Please remark according to the format, otherwise it will not be approved. After the addition is successful, you will be invited to enter the relevant WeChat group according to the research direction. Please do not send advertisements in the group, otherwise you will be invited out of the group, thank you for your understanding~

f3edfedc8998f01c83ea1485d41534f4.png

daf9f50c209cb62094300f9dcdd85281.png

Guess you like

Origin blog.csdn.net/qq_42722197/article/details/123675832