Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python's visdom tool

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use python's visdom tool". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use python's visdom tool.

Overview

Visdom: a flexible visualization tool that can be used to create, organize and share real-time, rich data. Support Torch and Numpy as well as pytorch.

Visdom

It can realize the visualization of remote data, which is of great help to scientific experiments. We can send pictures and data remotely, display them in the ui interface, check the experimental results, or debug.

To use this, you need to install it first. For the python module, the installation is quite simple:

Pip install visdom

Open it with a direct input code each time after installation:

Python-m visdom.server

Then enter the corresponding address in the browser as prompted. The default address is: http://localhost:8097/

Use example 1. Vis.text (), vis.image () import visdom # add visdom library import numpy as np # add numpy library vis = visdom.Visdom (env='test') # set the name of the environment window, if you don't set the name, default to mainvis.text ('test', win='main') # use text output vis.image (np.ones (3,100,100) # to draw a picture with a size of 3 * 100 * 100 The pixel values of the picture are all 1.

Where:

Visdom.Visdom (env=' named New Environment')

Vis.text ('text', win=' environment name')

Vis.image ('picture', win=' environment name')

two。 Draw a straight line .line () An import visdomimport numpy as npvis = visdom.Visdom (env='my_windows') # set the name of the environment window. If you do not set the name, it defaults to mainx = list (range (10)) y = list (range (10)) # draw a straight line using the line function and choose to display the axis vis.line (X=np.array (x), Y=np.array (y), opts=dict (showlegend=True))

Vis.line ([x], [y], opts=dict (showlegend=True) [presentation instructions])

Two

Import visdomimport numpy as npvis = visdom.Visdom (env='my_windows') x = list (range (10)) y = list (range (10)) z = list (range (1m 11)) vis.line (X=np.array (x), Y=np.column_stack ((np.array (y), np.array (z)), opts=dict (showlegend=True))

Vis.line ([x], [y=np.column_stack ((np.array (y), np.array (z), np.array (can also be added)

Np.column_stack, which means that the two matrices are merged by column

Sin (x) curve import visdomimport torchvis = visdom.Visdom (env='sin') x = torch.arange (0,100,0.1) y = torch.sin (x) vis.line (XonomxMagic Yanzhizhong windowproof sin (x)', opts=dict (showlegend=True)) continuously update the chart import visdomimport numpy as npvis = visdom.Visdom (env='my_windows') # update the image x = 0y = 0my_win = vis.line (X=np.array ([x]), Y=np.array ([y]) with update Opts=dict (title='Update')) for i in range (10): X + = 1 y + = I vis.line (X=np.array ([x]), Y=np.array ([y]), win=my_win, update='append')

Use "append" to append data, "replace" to use new data, and "remove" to delete the trace specified in "name".

Vis.images () import visdomimport torch# create a new connection client # specify env = 'test1' The default is' main', pay attention to switching the environment in the browser interface vis = visdom.Visdom (env='test1') # draw the sine function x = torch.arange (1,100,0.01) y = torch.sin (x) vis.line (win='sinx',opts= {'title':'y=sin (x)'}) # draw 36 random color pictures vis.images (torch.randn), nrow=6, win='imgs' Opts= {'title':'imgs'}) draw the changing trend of loss function # draw the changing trend of loss Parameter 1 is the value of the Y axis, parameter 2 is the value of the X axis, parameter 3 is the form name, parameter 4 is the table name, and parameter 5 is the update option. Vis.line (Y=np.array ([totalloss.item ()]), X=np.array ([traintime]), win= ('train_loss'), opts=dict (title='train_loss'), update=None if traintime = = 0 else' append') actual code can be updated from the second point.

This code comes from an implementation in utils.py of CycleGAN

# record the training log and display the generated graph Class class Logger () for drawing loss curves: def _ _ init__ (self, n_epochs) Batches_epoch):'': param n_epochs: how many epochs: param batches_epoch: an epoch has several batches''self.viz = Visdom () # the default env is the main function self.n_epochs = n_epochs self.batches_epoch = batches_epoch self.epoch = 1 # current epoch number self.batch = 1 # current number of batch self.prev_time = time.time () self.mean_period = 0 self.losses = {} self.loss_windows = {} # Save dictionary collection of loss graphs self.image_windows = {} # Save dictionary collection def log (self) of generated graphs Losses=None, images=None): self.mean_period + = (time.time ()-self.prev_time) self.prev_time = time.time () sys.stdout.write ('Epoch ddeband d [d bank d] -'% (self.epoch, self.n_epochs, self.batch, self.batches_epoch)) for I Loss_name in enumerate (losses.keys ()): if loss_name not in self.losses: self.losses [loss _ name] = losses [loss _ name] .data.item () # where losses [loss _ name] .data is a tensor (data structure wrapped around the value) To use the item method to take the value else: self.losses [loss _ name] = losses [loss _ name] .data.item () if (I + 1) = = len (losses.keys ()): sys.stdout.write ('% s:% .4f -'% (loss_name) Self.losses [loss _ name] / self.batch) else: sys.stdout.write ('% s:% .4f |'% (loss_name) Self.losses [loss _ name] / self.batch) batches_done = self.batches_epoch * (self.epoch-1) + self.batch batches_left = self.batches_epoch * (self.n_epochs-self.epoch) + self.batches_epoch-self.batch sys.stdout.write ('ETA:% s'% (datetime.timedelta (seconds=batches_left*self.mean_period/batches_done) # Show generation diagram for image_name Tensor in images.items (): # dictionary .items () returns the key value pair if image_name not in self.image_windows: self.image_ windows [image _ name] = self.viz.image (tensor2image (tensor.data), opts= {'title':image_name}) else: self.viz.image (tensor2image (tensor.data), win=self.image_ windows [image _ name]) Opts= {'title':image_name}) # End of each epoch if (self.batch% self.batches_epoch) = 0: # at the end of an epoch # draw loss curve for loss_name Loss in self.losses.items (): if loss_name not in self.loss_windows: self.loss_ windows [loss _ name] = self.viz.line (X=np.array ([self.epoch]), Y=np.array ([loss/self.batch]), opts= {'xlabel':'epochs',' ylabel':loss_name 'title':loss_name}) else: self.viz.line (X=np.array ([self.epoch]), Y=np.array ([loss/self.batch]), win=self.loss_ windows [loss _ name] Update='append') # update='append' can constantly update the loss diagram # reset each epoch once loss self.losses [loss _ name] = 0.0 # run an epoch Update the following parameter self.epoch + = 1 self.batch = 1 sys.stdout.write ('') else: self.batch + = 1

The calling code in train.py is

# drawing Loss logger = Logger (opt.n_epochs, len (dataloader)) for epoch in range (opt.epoch, opt.n_epochs): for I, batch in enumerate (dataloader):. # record training log # Progress report (http://localhost:8097) displays the URL of visdom drawing logger.log ({'loss_G': loss_G,' loss_G_identity': (loss_identity_A + loss_identity_B), 'loss_G_GAN': (loss_GAN_A2B + loss_GAN_B2A)) 'loss_G_cycle': (loss_cycle_ABA + loss_cycle_BAB), 'loss_D': (loss_D_A + loss_D_B)}, images= {' real_A': real_A, 'real_B': real_B,' fake_A': fake_A, 'fake_B': fake_B}) Thank you for reading The above is the content of "how to use python's visdom tools". After the study of this article, I believe you have a deeper understanding of how to use python's visdom tools, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report