最近几天在看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下。
安装和启动
1 2 | 安装: pip install visdom 启动:python -m visdom.server |
接口
大多数接口可以输入一个tensor(保存数据),和一个可选的tensor Y(标签或者时间戳), 另外都可以指定窗口win,和汇出图添加到那个env上,另外options输入可以修改默认的绘图属性,输入参数基于表中键的匹配,有一些通用的options可以选择,下面列出了通用的可视化options(除了plot.image和plot.text外)
opts.title
: figure titleopts.width
: figure widthopts.height
: figure heightopts.showlegend
: show legend (true
orfalse
)opts.xtype
: type of x-axis (\\\\\\'linear\\\\\\'
or\\\\\\'log\\\\\\'
)opts.xlabel
: label of x-axisopts.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
ofnumber
s)opts.xticklabels
: ticks labels on x-axis (table
ofstring
s)opts.xtickstep
: distances between ticks on x-axis (number
)opts.ytype
: type of y-axis (\\\\\\'linear\\\\\\'
or\\\\\\'log\\\\\\'
)opts.ylabel
: label of y-axisopts.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
ofnumber
s)opts.yticklabels
: ticks labels on y-axis (table
ofstring
s)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)
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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: # 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( 图片/单张和多张)
1 2 3 4 5 6 7 8 9 10 | #单张 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #画出随机的散点图 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\\\\', ), ) |
1 2 3 4 5 6 7 8 9 | #3D 散点图 viz.scatter( X=np.random.rand(100, 3), Y=(Y 1.5).astype(int), opts=dict( legend=[\\\'Men\\\', \\\'Women\\\'], markersize=5, ) ) |
vis.bar(柱方图)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 (热程图,地理图,表面图)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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\')) |
文章转载来源:zchky知乎专栏
本站微信群、QQ群(三群号 726282629):
棒~
你好 请问“Y=(Y[Y > 0] 1.5).astype(int)” 是什么意思呢