/Visualizer

A Simple Tool for Logging and Plotting Values in Visdom

Primary LanguagePython

A Simple Tool for Logging and Plotting Values in Visdom

Wrapped Functions for Easy ploting and logging when training in pytorch

1. Start the visdom server

pip3 install -r requirements.txt
python -m visdom.server

2. import Visualizer and initialize the environment

from Visualizer import Visualizer

#simple init env will set to default value:'main'
vis = Visualizer()

#set up new environment call 'demo'
vis = Visualizer(env='demo')

Logging and Plotting the Ongoing Values in Pytorch (Loss/Accuracy)

Single Value Plot plot: 3 parameters:

  1. name: name for the graph (string)
  2. value: logging value
  3. (optional) step: logging step (default is 1)

put the vis.plot inside the loop

# simple plot with default logging step
for i in range(0,10,1):
    vis.plot('Single Value Plot Step 1',i/10)

# simple plot with logging step 10
for i in range(0,10,1):
    vis.plot('Single Value Plot Step 10',i/10, 10)

Multi-Value Plots into Separate Graphs plot_all: 2 parameters:

  1. dict: dictionary contains each name and value
vis.plot_all({'Acc':0.9,'Loss':0.1})
  1. (optional) step: logging step (default is 1)
# Plot Acc graph and Loss graph at the same time with default logging step
for i in range(0,10,1):
    loss = {'Acc':i/10,
            'Loss':1-i/10}
    vis.plot_all(loss)

Multi-Value Plots in One Single Graph plot_combine: 3 parameters:

  1. name: name for the graph (string)
  2. dict: dictionary contains each name and value
vis.plot_combine('Combine Plot',{'Acc':0.9,'Loss':0.1})
  1. (optional) step: logging step (default is 1)
# Plot acc value and loss value in one graph with default logging step
for i in range(0,10,1):
    loss = {'Acc':i/10,
            'Loss':1-i/10}
    vis.plot_combine('Combine Plot',loss)


Logging String in Pytorch

Text Log log: 3 parameters:

  1. info: logging info
  2. name: name for the visdom window
for i in range(0,10,1):
    loss = {'Acc':i/10,
            'Loss':1-i/10}
    vis.log(loss)


Display Pytorch Tensor as Images

Image Display img: 2 parameters:

  1. name: name for the image (strinf)
  2. img_: pytorch tensors: single image or multi-images
t.Tensor(64,64)
t.Tensor(3,64,64)
t.Tensor(100,1,64,64)
t.Tensor(100,3,64,64)
  1. (optional) nrow: number of image per row (integer)
#Plot Single Images
import torch
from PIL import Image
import numpy as np
img = Image.open('imgs/Lenna.png')
arr = np.array(img).transpose((2, 0, 1))
tensor = torch.from_numpy(arr)
vis.img('SingleImage',tensor)

#Plot Multi Images (4D tensor)
img = Image.open('imgs/Lenna.png')
arr = np.array(img).transpose((2, 0, 1))
tensor = torch.from_numpy(arr)
tensor4D = tensor.unsqueeze(0)
for i in range(5):
    tensor4D=torch.cat((tensor4D,tensor.unsqueeze(0)),0)
vis.img('MultiImage',tensor4D, nrow=3)


Check Visdom Connection: check_connection

vis.check_connection()
True