visdom 使用教程

visdom安装与启动服务

安装visdom

pip install visdom

打开服务

python -m visdom.server

浏览器中打开对应的网址。
在这里插入图片描述
在这里插入图片描述

visdom常用功能

首先创建一个新的环境,如不创建则默认在main环境下

#_*_coding:utf-8 _*_
import numpy as np
import visdom
import time

viz = visdom.Visdom(env="Test1") # 创建环境名为Test1

image窗口:图像显示与更新窗口显示

下面代码在一个image窗口中不断更新显示图像

#单张图像显示与更新demo
image = viz.image(np.random.rand(3,256,256),opts={'title':'image1','caption':'How random.'})
for i in range(10):
    viz.image(np.random.randn( 3, 256, 256),win = image)
    time.sleep(0.5)

在这里插入图片描述

images窗口:多个图像显示与更新窗口显示

#多图像显示与更新demo
images = viz.images(
        np.random.randn(20, 3, 64, 64),
        opts=dict(title='Random images', caption='How random.',nrow=5)
    )
for i in range(10):
    viz.images(np.random.randn(20, 3, 64, 64),win = images)
    time.sleep(0.5) 

在这里插入图片描述

text窗口:显示文本与更新文本

#text 显示与更新显示demo,将在窗口中连续打印字符
text = viz.text('Hello World !')
strForOut = "This is a string for you to print!"
out = ""
for i in range(len(strForOut)):
    out = out + strForOut[i]
    viz.text(out,win = text)
    time.sleep(0.2)

在这里插入图片描述

line窗口:绘制折线图与更新折线图

#绘画折线图演示
x=0
name=['acc','loss','loss2']
for i in range(50):
    y = np.random.randint(5, size=(1, 3))
    viz.line(Y=y,X=np.ones(y.shape)*x,
                    win='line',
                    opts=dict(legend=name,
                        title='line test',
                        width=800,
                        height=800,
                        xlabel='Time',
                        ylabel='Volume'),
                    update=None if x == 0 else 'append'
                    )
    time.sleep(0.1)
    x+=1

在这里插入图片描述

scatter窗口:绘制散点图与更新散点图

# 绘制散点图演示 二维
colors = np.random.randint(0, 255, (3, 3,))#第一维3表示该数据可以分为三类,以三种颜色的三点来比表示
win = viz.scatter(
    X=np.random.rand(255, 2),#表示要展示的散点数据
    #Y=(np.random.rand(255) + 1.5).astype(int),
    Y=np.random.randint(1,4,(255)),#每一个数据的类别,将以其对应的colors中的颜色来显示
    opts=dict(
        markersize=5,
        markercolor=colors,
        legend=['1', '2','3'],
        markersymbol = 'cross-thin-open'
    ),
)
# 绘制散点图演示 三维
colors = np.random.randint(0, 255, (3, 3,))#第一维3表示该数据可以分为三类,以三种颜色的三点来比表示
win = viz.scatter(
    X=np.random.rand(255, 3),#表示要展示的散点数据
    #Y=(np.random.rand(255) + 1.5).astype(int),
    Y=np.random.randint(1,4,(255)),#每一个数据的类别,将以其对应的colors中的颜色来显示
    opts=dict(
        markersize=5,
        markercolor=colors,
        legend=['1', '2','3'],
        markersymbol = 'cross-thin-open'
    ),
)

在这里插入图片描述

#实时更新绘制散点图
legend=['1', '2','3']
Scatter = viz.scatter(
    X=np.array([[0,0]]),
    Y=np.array([1]),
    opts=dict(
        markersize=5,
        legend=legend,
        # markersymbol = 'cross-thin-open'
        ),
    )


for i in range(20):
    X = np.random.rand(1,2)
    Y = np.random.randint(1,4,1)
    print(Y)

    viz.scatter(
        X=X,
        Y=Y,
        win=Scatter,
        update= 'append',
        name = legend[Y[0]-1],
        opts=dict(
            markersize=5,
            # markersymbol = 'cross-thin-open'
            )
    )
    time.sleep(0.5)

在这里插入图片描述
更多应用请参考官方demo:
https://github.com/facebookresearch/visdom/blob/master/example/demo.py

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章