Pytorch (3): visualization tools (Tensorboard, Visdom)

Foreword: When training a neural network, we hope to be able to know some key information in the network training process in time, including the loss curve, the accuracy rate on the verification set, and the extraction of features at each stage of the input image, etc. This information can help us control the network. the whole training process. Therefore, in order to more intuitively grasp the various indicator information of the network training process, it is a very good choice to visualize this information. The following article will specifically explain the two visualization tools provided by Pytorch, Tensorboard and Visdom.

Table of contents

1、Tensorboard

1.1 Use of Tensorboard

1.2 Methods in Tensorboard

2、Wisdom


1、Tensorboard

        Tensorboard was originally a visualization tool specially equipped for the deep learning framework Tensorflow, which was deeply loved by academic researchers. Later, Pytorch also provided an interface that can support Tensorboard visualization. Before learning how to use Tensorboard, let's first understand the operating mechanism of Tensorboard: ①Record the data to be visualized in the python script; ②Save these data to the hard disk in the form of event file; ③The terminal reads the event file from the hard disk in Tensorboard is visualized and displayed on the web. Tensorboard's web interface is shown below:

1.1 Use of Tensorboard

        Step 1: Import the SummaryWriter class from the torch.utils.tensorboard module. The code implementation is as follows:

from torch.utils.tensorboard import SummaryWriter

The SummaryWriter class must be used to record data in the Python script and save it to the hard disk as an event file. SummaryWriter is a high-level interface that provides an event file.

        The second step is to create an object of the SummaryWriter class, the code is as follows:

writer=SummaryWriter("./logs")

The parameters in the brackets are the path to be created when the visualization data is saved. After the code is run, a folder as shown in the figure below will appear in the directory.

        The third step is to add the content to be visualized to the writer (events file) through the method in Tensorboard. For example, to visualize the loss curve, use the add_scalar method to add the loss value scalar to the writer. The code is as follows:

writer.add_scalar("train_loss",loss.item(),total_train_step)

The first parameter is the name of the loss graph, the second parameter is the ordinate of the graph, and the third parameter is the abscissa of the graph.

        The fourth step is to enter the following command in the Terminal terminal:

tensorboard --logdir "file path"

The terminal will return the Tensorboard visualization web URL, as shown in the figure below, click the URL to enter the Tensorboard visualization interface.

1.2 Methods in Tensorboard

①add_scalar: This method is usually used to visualize various scalar indicators in the network training process, such as loss, accuracy rate, learning rate, etc.

add_scalar(tag, scalar_value, global_step=None, walltime=None, new_style=False, double_precision=False)

This function commonly uses the first three parameters, the first parameter is the image title, the second parameter is the value of the ordinate, and the third parameter is the value of the abscissa.

Example: visualize y=2xthe function image, the code implementation is as follows:

from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("scalar_logs")
x = range(100)
for i in x:
    writer.add_scalar('y=2x', i * 2, i)
writer.close()

②add_scalars: This method is similar to add_scalar, the only difference is that this method can draw multiple curves in one graph.

add_scalars(main_tag, tag_scalar_dict, global_step=None, walltime=None)

When the number of Y-axis data is greater than 1, use writer.add_scalars() instead of the add_scalar function. The first parameter is the image title, the second parameter is a dictionary of ordinate values, and the third parameter is the abscissa value.

Example:

from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter("scalars_logs")
r = 5
for i in range(100):
    writer.add_scalars('run_14h', {'xsinx':i*np.sin(i/r),
                                    'xcosx':i*np.cos(i/r),
                                    'tanx': np.tan(i/r)}, i)
writer.close()

③add_histogram: This method is used to visualize histograms and multi-quantile line charts.

add_histogram(tag, values, global_step=None, bins='tensorflow', walltime=None, max_bins=None)

The first parameter is the title of the graph, the second parameter is the abscissa (that is, the value to build the histogram), and the third parameter is the ordinate (that is, the global step value to be recorded)

Example:

from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter("histogram_logs")
for i in range(10):
    x = np.random.random(1000)
    writer.add_histogram('distribution centers', x + i, i)
writer.close()

So how to understand this picture, look at the first data block, which represents the call of the following code,

writer.add_histogram('distribution centers', x , 0)

At this time, global_step=0, the data of the same global_step will be placed in the same layer. In the above example, there are 10 global_steps in total, that is, the value range of i is [0,10). The point (0.833, 0, 329) in the above figure represents that there are 329 points near 0.833 in the data layer of global_step=0, and each layer has 1000 points. For example, 315+356+329 in the layer of global_step=0 is just right equals 1000.

④add_graph: This method is used to visualize the network structure of the model.

add_graph(model, input_to_model=None, verbose=False, use_strict_trace=True)
input_to_model:这个参数是输入图片,即训练集

Example:

import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter


class CIFAR_10_Net(nn.Module):

    def __init__(self):
        super().__init__()
        self.modle = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.modle(x)
        return x


cifar_10_net = CIFAR_10_Net()
input = torch.ones((64, 3, 32, 32))
output = cifar_10_net(input)

writer = SummaryWriter("graph_logs")
writer.add_graph(cifar_10_net, input)
writer.close()

⑤add_image: This method is used to visualize the corresponding pixel matrix, such as input image or feature map.

add_image(tag, img_tensor, global_step=None, walltime=None, dataformats='CHW')
参数解释如下:
tag:图片标题
img_tensor:图像数据,该数据可以是torch.Tensor、numpy.array以及string这几种类型的数据
global_step:要记录的全局步长值
dataformats:图像数据格式规范,有CHW,HWC,HW,WH等几种格式,默认是CHW,该参数一定要和img_tensor对应起来

⑥add_images: This method is similar to add_image, the difference is that this method visualizes multiple images at one time. This method is often used to visualize images or feature maps during network training.

add_images(tag, img_tensor, global_step=None, walltime=None, dataformats='NCHW')

Example:

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
#tensorboard是一个用于神经网络训练过程可视化的网络服务器,其可以实现标量值,图像,文本等的可视化

dataset = torchvision.datasets.CIFAR10("../nn/data", train=False,transform=torchvision.transforms.ToTensor(),
                                       download=True)  # pytorch官网提供的一个下载数据集的函数
dataloader = DataLoader(dataset, batch_size=64)  # DataLoader()函数就是数据加载器,其作用就是用来把训练数据分成多个小组,此函数每次抛出一个小组,
# batch_size参数就是限定每组训练数据的数量


class Tudui(nn.Module):#自定义一个Tudui类去继承nn.Moudle这个pytorch官网提供的神经网络模型
    def __init__(self):
        super().__init__()
        self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1)

    def forward(self, x):
        x = self.conv1(x)
        return x


tudui = Tudui()
writer = SummaryWriter("images_logs")#用于自动生成存放可视化内容的文件
step = 0
for data in dataloader:
    imgs,targets = data
    output = tudui(imgs)
    writer.add_images("input", imgs, step)#训练过程中用writer.add_images()函数向tensorboard添加图片
    output = torch.reshape(output, (-1, 3, 30, 30))
    writer.add_images("output", output, step)
    step = step + 1#下载的数据一共有10组,这条语句的作用是每完成一组显示之后就进入下一组显示
# 打开本代码生成的logs文件方法:在Terminal窗口输入语句tensorboard --logdir "D:\PycharmProjects\pythonProject\nn\logs",回车即可

 

 

 ⑦add_figure: This method is used to visualize the objects matplotlibin the package to the web page of Tensoboard to display some more complex pictures.figure

⑧add_pr_curve: This method is used to visualize the Precision-Recall curve during the training process, that is, to observe the balance of precision and recall at different thresholds.

⑨add_embedding: This method is used to visualize high-dimensional vectors in three-dimensional space. By default, the high-dimensional vectors are reduced to the PCA method.

2、Wisdom

        Visdom is an interactive visualization tool dedicated to Pytorch, which can provide rich visualization of real-time data and help us monitor scientific experiments on remote servers in real time. It is similar to Tensorboard and will not be introduced here. These two visualization tools have their own advantages and disadvantages, and one can be used proficiently.

        For the specific content of the Visdom visualization tool, please refer to the article https://blog.csdn.net/weixin_41010198/article/details/117853358?ops_request_misc=%257B%2522request%255Fid%2522%253A%25221658803621167818187439 13%2522%252C%2522scm%2522% 253A%252220140713.130102334..%2522%257D&request_id=165880362116781818743913&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-2 -117853358-null-null.142^v34^control,185^v2^ control&utm_term=Visdom&spm=1018.2226.3001.4187

Guess you like

Origin blog.csdn.net/Mike_honor/article/details/125905703