[pytoch framework] 4.2.1 visualization in PyTorch using Visdom

import torch
import math
import numpy as np
from visdom import Visdom
import time
torch.__version__
'1.0.0'

4.2.1 visualization in PyTorch using Visdom

Visdom is a visualization tool for PyTorch released by Facebook in 2017. Official website , because of its simple function, Visdom is generally defined as server-side matplot, that is, we can directly use python console mode to develop and execute on the server, transfer some visual data to Visdom service, and visualize through Visdom service

install

The installation of Visdom is very simple. You can directly use the command pip install visdom to install it.
After the installation is complete, use the command Python - M visdom Server starts the server locally, and will prompt it's alive! You can navigate to http://localhost:8097 This indicates that the service is available. We open the browser and enter http://localhost:8097 You can see the page.

Port 8097 is the default port. You can specify the port by adding the - port parameter after starting the command. The commonly used parameters are -- hostname, - base_url, etc

pit

When the Visdom service is started, it will automatically download some static files. Here comes the pit. For some indescribable reasons, the download will fail, such as the prompt ERROR:root:Error 404 while downloading https://unpkg.com/layout-bin-packer@1.4.0 means that the static file has not been downloaded completely, so it may not be opened or there is no menu bar in the page, so it needs to be downloaded manually. Here I package a normal static file and copy it directly to Lib \ site packages \ Visdom.

If you don't know where conda's environment directory is, you can use conda env list to view it

Thanks to the partner of CSDN for the missing document, original text here

Basic concepts

Environments

Environments is used to partition the visualization area. Each user will have a default partition called main, as shown in the figure:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-aEux1Azn-1618486885486)(1.PNG)]
When specified by the program, the default chart will be placed here

Panes

Panes are containers for each visual chart, which can be filled with generated charts, pictures and text. We can drag and drop, delete, resize and destroy panes:
[the external chain picture transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-FgjwY01j-1618486885494)(2.PNG)]
Panes and Environments have a one to many relationship, that is, an environment can contain multiple panes

VIEW

After adjusting Panes, you can manage the status through VIEW:
[the external chain picture transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-iMVWT4rT-1618486885496)(3.PNG)]

Visual interface

Visdom is a visualization support provided by Plotly, so the following visualization interfaces are provided:

  • vis. Scatter: 2D or 3D scatter
  • vis.line: line diagram
  • vis.stem: stem leaf diagram
  • vis.heatmap: heat map
  • vis.bar: bar chart
  • vis.histogram: histogram
  • vis.boxplot: box diagram
  • vis.surf: surface drawing
  • vis.contour: Outline
  • vis. Quilver: draw a two-dimensional vector field
  • vis.image: image
  • vis.text: Text
  • vis.mesh: grid diagram
  • vis.save: serialization status

use

Draw simple graphics

Here we use the official DEMO as an example

env = Visdom() 
assert env.check_connection() #Test the link. If the link is wrong, an error will be reported

Here, two curve data of sin and cos are generated

Y = np.linspace(0, 2 * math.pi, 70)
X = np.column_stack((np.sin(Y), np.cos(Y)))

Display with stem and leaf diagram

env.stem(
        X=X,
        Y=Y,
        opts=dict(legend=['Sine', 'Cosine'])
    )
'window_36f18bc34b4992'

You can specify Environments with the env parameter if the name contains underscores_ Then visdom will be divided and automatically grouped according to the underline

envtest = Visdom(env='test_mesh')
assert envtest.check_connection()

Generate a grid diagram

x = [0, 0, 1, 1, 0, 0, 1, 1]
y = [0, 1, 1, 0, 0, 1, 1, 0]
z = [0, 0, 0, 0, 1, 1, 1, 1]
X = np.c_[x, y, z]
i = [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2]
j = [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3]
k = [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6]
Y = np.c_[i, j, k]
envtest.mesh(X=X, Y=Y, opts=dict(opacity=0.5))
'window_36f18bc533e990'

Update loss function

During training, we will print the training loss and test accuracy in each batch. The chart displayed in this way needs to dynamically add data. Let's simulate this situation

x,y=0,0
env2 = Visdom()
pane1= env2.line(
    X=np.array([x]),
    Y=np.array([y]),
    opts=dict(title='dynamic data'))
for i in range(10):
    time.sleep(1) #Print data every second
    x+=i
    y=(y+i)*1.5
    print(x,y)
    env2.line(
        X=np.array([x]),
        Y=np.array([y]),
        win=pane1,#The win parameter confirms which pane to use
        update='append') #The action we do is to add. There are other ways besides adding. We won't introduce it here
0 0.0
1 1.5
3 5.25
6 12.375
10 24.5625
15 44.34375
21 75.515625
28 123.7734375
36 197.66015625
45 309.990234375

After running the above program, switch to visdom to see the effect

After introducing the basic usage of visdom, the next section introduces the more powerful tensorboardx

Keywords: Pytorch

Added by DF7 on Mon, 07 Mar 2022 16:30:43 +0200