Visdom 可视化工具 教程 (pytorch)

#### 3.31 进行一点点微小的更新###########

  • 增加了数据图的类型
  • 调整下界面
  • 最近在写pytorch的教程,可以期待下, O_O
  • 有关visdom动态更新, 希望有兴趣交流的同学私信我

######################################


目录

  • 1 几个基本概念
  • 2 安装和启动
  • 3 接口
  • 4 示例
    • 4.1 vis.image( 图片/单张和多张)
    • 4.2 vis.scatter(散点图,2D/3D)
    • 4.3 viz.line( 曲线图)
    • 4.4 viz.bar(柱方图)
    • 4.5 vis.heat/contour/surface (热程图,地理图,表面图)
    • 4.6 viz.boxplot(箱形图)/ stem(茎干图)/ quiver (箭状图)
    • 4.7 viz.text( 文字) / pie(饼图) / mesh (网丝图)

最近几天在看pytorch, 找到了可视化的工具visdom,但目前网上的教程较少,决定自己写一个,方便记录


Visdom:一个灵活的可视化工具,可用来对于 实时,富数据的 创建,组织和共享。支持Torch和Numpy还有pytorch

visdom 可以实现远程数据的可视化,对科学实验有很大帮助。我们可以远程的发送图片和数据,并进行在ui界面显示出来,检查实验结果,或者debug


几个基本概念:

Panes(窗格): interface刚打开是个白板,可以用将数据和图片发送到backend,生成多个对应的窗格,panes可以进行拖放,删除,panes保存在envs中,envs的状态存与绘画之间

Environments(环境):env对可视化空间进行分区,每个用户默认有个main的env, envs的状态是长期保持的

State(状态):当你创建一些可视化后,服务器自动缓存这些可视化,重新加载页面,重新打开服务器都会再现这些可视化。save:可以序列化env的状态,并以json文件保持在电脑里,fork:当输入一个新的名字,会复制当前的状态到这个new env下。


安装和启动:

安装: pip install visdom
启动:python -m visdom.server
远程托管:待


接口:

大多数接口可以输入一个tensor(保存数据),和一个可选的tensor Y(标签或者时间戳), 另外都可以指定窗口win,和汇出图添加到那个env上,另外options输入可以修改默认的绘图属性,输入参数基于表中键的匹配,有一些通用的options可以选择,下面列出了通用的可视化options(除了plot.image和plot.text外)

  • opts.title : figure title
  • opts.width : figure width
  • opts.height : figure height
  • opts.showlegend : show legend (true or false)
  • opts.xtype : type of x-axis ('linear' or 'log')
  • opts.xlabel : label of x-axis
  • opts.xtick : show ticks on x-axis (boolean)
  • opts.xtickmin : first tick on x-axis (number)
  • opts.xtickmax : last tick on x-axis (number)
  • opts.xtickvals : locations of ticks on x-axis (table of numbers)
  • opts.xticklabels : ticks labels on x-axis (table of strings)
  • opts.xtickstep : distances between ticks on x-axis (number)
  • opts.ytype : type of y-axis ('linear' or 'log')
  • opts.ylabel : label of y-axis
  • opts.ytick : show ticks on y-axis (boolean)
  • opts.ytickmin : first tick on y-axis (number)
  • opts.ytickmax : last tick on y-axis (number)
  • opts.ytickvals : locations of ticks on y-axis (table of numbers)
  • opts.yticklabels : ticks labels on y-axis (table of strings)
  • opts.ytickstep : distances between ticks on y-axis (number)
  • opts.marginleft : left margin (in pixels)
  • opts.marginright : right margin (in pixels)
  • opts.margintop : top margin (in pixels)
  • opts.marginbottom: bottom margin (in pixels)


代码实例:


viz.maplotlib( matplotlib显示)

(新增) 在最新的github版本中,显示matplotlib实例, 可以通过visdom显示matplotlib的图片, 这样的话我们还可以同时可以在jupyter notebook中进行交互. pip 似乎还没有更新,想尝试的请参照github

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from visdom import Visdom
import numpy as np
import math
import os.path
import getpass
from sys import platform as _platform
from six.moves import urllib

viz = Visdom()
assert viz.check_connection()

try:
    import matplotlib.pyplot as plt
    plt.plot([1, 23, 2, 4])
    plt.ylabel('some numbers')
    viz.matplot(plt)
except BaseException as err:
    print('Skipped matplotlib example')
    print('Error message: ', err)


Vis.video(视频)

try:
    # video demo: download video from http://media.w3.org/2010/05/sintel/trailer.ogv
    video_url = 'http://media.w3.org/2010/05/sintel/trailer.ogv'
    # linux
    if _platform == "linux" or _platform == "linux2":
        videofile = '/home/%s/trailer.ogv' % getpass.getuser()
    # MAC OS X
    elif _platform == "darwin":
        videofile = '/Users/%s/trailer.ogv' % getpass.getuser()
    # download video
    urllib.request.urlretrieve(video_url, videofile)

    if os.path.isfile(videofile):
        viz.video(videofile=videofile)
except ImportError:
    print('Skipped video example')


vis.image( 图片/单张和多张)

#单张
viz.image(
    np.random.rand(3, 512, 256),
    opts=dict(title='Random!', caption='How random.'),
)
#多张
viz.images(
    np.random.randn(20, 3, 64, 64),
    opts=dict(title='Random images', caption='How random.')
)


vis.scatter(散点图,2D/3D)

#画出随机的散点图
import time
Y = np.random.rand(100)
old_scatter = viz.scatter(
    X=np.random.rand(100, 2),
    Y=(Y[Y > 0] + 1.5).astype(int),
    opts=dict(
        legend=['Didnt', 'Update'],
        xtickmin=-50,
        xtickmax=50,
        xtickstep=0.5,
        ytickmin=-50,
        ytickmax=50,
        ytickstep=0.5,
        markersymbol='cross-thin-open',
    ),
)

time.sleep(5)

#对窗口进行更新,包括标注,坐标,样式等
viz.update_window_opts(
    win=old_scatter,
    opts=dict(
        legend=['Apples', 'Pears'],
        xtickmin=0,
        xtickmax=1,
        xtickstep=0.5,
        ytickmin=0,
        ytickmax=1,
        ytickstep=0.5,
        markersymbol='cross-thin-open',
    ),
)
## 通过update='new'添加新散点

import time
win = viz.scatter(
    X=np.random.rand(255, 2),
    opts=dict(
        markersize=10,
        markercolor=np.random.randint(0, 255, (255, 3,)),
    ),
)

# 判断窗口是否存在
assert viz.win_exists(win), 'Created window marked as not existing'
time.sleep(2)

# 向散点图中加入新的描述
viz.scatter(
    X=np.random.rand(255),
    Y=np.random.rand(255),
    win=win,
    name='new_trace',
    update='new'
)


# 2D散点图,分配不同颜色
viz.scatter(
    X=np.random.rand(255, 2),
    #随机指定1或者2
    Y=(np.random.rand(255) + 1.5).astype(int),
    opts=dict(
        markersize=10,
        ## 分配两种颜色
        markercolor=np.random.randint(0, 255, (2, 3,)),
    ),
)
#3D 散点图
viz.scatter(
    X=np.random.rand(100, 3),
    Y=(Y + 1.5).astype(int),
    opts=dict(
        legend=['Men', 'Women'],
        markersize=5,
    )
)


viz.line(曲线图)

viz.line(Y=np.random.rand(10), opts=dict(showlegend=True))

Y = np.linspace(-5, 5, 100)
viz.line(
    Y=np.column_stack((Y * Y, np.sqrt(Y + 5))),
    X=np.column_stack((Y, Y)),
    opts=dict(markers=False),
)


vis.bar(柱方图)

viz.bar(X=np.random.rand(20))
viz.bar(
    X=np.abs(np.random.rand(5, 3)),
    opts=dict(
        stacked=True,
        legend=['Facebook', 'Google', 'Twitter'],
        rownames=['2012', '2013', '2014', '2015', '2016']
    )
)
viz.bar(
    X=np.random.rand(20, 3),
    opts=dict(
        stacked=False,
        legend=['The Netherlands', 'France', 'United States']
    )
)


vis.heat/contour/surface (热程图,地理图,表面图)

viz.heatmap(
    X=np.outer(np.arange(1, 6), np.arange(1, 11)),
    opts=dict(
        columnnames=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
        rownames=['y1', 'y2', 'y3', 'y4', 'y5'],
        colormap='Electric',
    )
)
# contour
x = np.tile(np.arange(1, 101), (100, 1))
y = x.transpose()
X = np.exp((((x - 50) ** 2) + ((y - 50) ** 2)) / -(20.0 ** 2))
viz.contour(X=X, opts=dict(colormap='Viridis'))

# surface
viz.surf(X=X, opts=dict(colormap='Hot'))


viz.boxplot(箱形图)/ stem(茎干图)/ quiver (箭状图)

# boxplot
X = np.random.rand(100, 2)
X[:, 1] += 2
viz.boxplot(
    X=X,
    opts=dict(legend=['Men', 'Women'])
)

# stemplot
Y = np.linspace(0, 2 * math.pi, 70)
X = np.column_stack((np.sin(Y), np.cos(Y)))
viz.stem(
    X=X,
    Y=Y,
    opts=dict(legend=['Sine', 'Cosine'])
)

# quiver plot
X = np.arange(0, 2.1, .2)
Y = np.arange(0, 2.1, .2)
X = np.broadcast_to(np.expand_dims(X, axis=1), (len(X), len(X)))
Y = np.broadcast_to(np.expand_dims(Y, axis=0), (len(Y), len(Y)))
U = np.multiply(np.cos(X), Y)
V = np.multiply(np.sin(X), Y)
viz.quiver(
    X=U,
    Y=V,
    opts=dict(normalize=0.9),
)


viz.text( 文字) / pie(饼图) / mesh (网丝图)

# text window with Callbacks
txt = 'This is a write demo notepad. Type below. Delete clears text:<br>'
callback_text_window = viz.text(txt)

# pie chart
X = np.asarray([19, 26, 55])
viz.pie(
X=X,
opts=dict(legend=['Residential', 'Non-Residential', 'Utility'])
)

# mesh plot
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]
viz.mesh(X=X, Y=Y, opts=dict(opacity=0.5))

编辑于 2018-03-31

文章被以下专栏收录