default=DEFAULT_PORT, help='port the visdom server is running on.') parser.add_argument('-server', metavar='server', type=str, default=DEFAULT_HOSTNAME, help='Server address of the target to run the demo on.') FLAGS = parser.parse_args() try: viz = Visdom(port=FLAGS.port, server=FLAGS.server) assert viz.check_connection(timeout_seconds=3), \ 'No connection could be formed quickly' textwindow = viz.text('Hello World!') updatetextwindow = viz.text('Hello World! More text should be here') assert updatetextwindow is not None, 'Window was none' viz.text('And here it is', win=updatetextwindow, append=True) # text window with Callbacks txt = 'This is a write demo notepad. Type below. Delete clears text:<br>' callback_text_window = viz.text(txt) def type_callback(event): if event['event_type'] == 'KeyPress': curr_txt = event['pane_data']['content'] if event['key'] == 'Enter': curr_txt += '<br>' elif event['key'] == 'Backspace':
# LICENSE file in the root directory of this source tree. 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 viz = Visdom() textwindow = viz.text('Hello World!') # video demo: try: video = np.empty([256, 250, 250, 3], dtype=np.uint8) for n in range(256): video[n, :, :, :].fill(n) viz.video(tensor=video) # video demo: download video from http://media.w3.org/2010/05/sintel/trailer.ogv videofile = '/home/%s/trailer.ogv' % getpass.getuser() if os.path.isfile(videofile): viz.video(videofile=videofile) except ImportError: print('Skipped video example')
class Experiment(object): """ Experiment class """ def __init__(self, name, hparams, desc=None): """ Args: name (string): the name of the experiment hparams (object): the hypermarameters used for this experiment """ self.name = name self.desc = desc self.hparams = hparams self.metrics = defaultdict(Metric) self.timestamp_start = datetime.now() self.timestamp_update = datetime.now() self.last_update = time.time() # dir_path = os.path.dirname(os.path.realpath(__file__)) # self.db = TinyDB(os.path.join(dir_path, "db.json")) # self.db_record = self.db.insert({}) self.viz = Visdom() self.viz.close() self.vis_params() if desc is not None: self.vis_desc() def update_plots(self): for exp_name, metric in self.metrics.items(): metric.update_plot() # self.save_experiment() def vis_params(self): lines = [] for param, value in self.hparams.items(): lines.append("{}: {}".format(param, value)) self.viz.text("<pre>{}</pre>".format("\n".join(lines)), opts=dict( width=max([len(x) for x in lines]) * 10, height=len(lines) * 20, )) def vis_desc(self): self.viz.text("<pre>{}</pre>".format(self.desc), opts=dict( width=max([len(x) for x in self.desc.split("\n")]) * 8.5, height=len(self.desc.split("\n")) * 20, )) def add_metric(self, metric): """ Add a metric to the experiment Args: metric (Metric): a metric object Returns: """ metric.vic_context = self.viz self.metrics[metric.name] = metric def get_score(self, metric, tag): return self.metrics[metric]._values[tag][-1] def save_experiment(self): """ Implement a saving mechanism (in text, csv or a database) Returns: """ self.timestamp_update = datetime.now() self.db.update( { 'name': self.name, 'desc': self.desc, 'hparams': self.hparams, 'metrics': self.metrics, 'timestamp_start': self.timestamp_start, 'timestamp_update': self.timestamp_update, 'last_update': self.last_update }, doc_ids=[self.db_record]) def visualize_experiment(self): raise NotImplementedError
ylabel='Loss', title='Training loss (per epoch)', ), ) win_global_acc_train = viz.line( Y=np.array([1]), X=np.array([1]), opts=dict( xlabel='Epoch', ylabel='Accuracy', title='Training accuracy (per epoch)', ), ) win_train = viz.text("Training:\n") win_global_loss_val = viz.line( Y=np.array([1]), X=np.array([1]), opts=dict( xlabel='Epoch', ylabel='Loss', title='Validation loss (per epoch)', ), ) win_global_acc_val = viz.line( Y=np.array([1]), X=np.array([1]), opts=dict(
from visdom import Visdom vis = Visdom() vis.text('Hello, world !')
#!/usr/bin/env python3 # coding: utf-8 import torch from visdom import Visdom vis = Visdom(env='first') vis.text('first visdom', win='text1') vis.text('hello PyTorch', win='text1', append=True) for i in range(20): vis.line(X=torch.FloatTensor([i]), Y=torch.FloatTensor([-i**2+20*i+1]), opts={'title': 'y=-x^2+20x+1'}, win='loss', update='append') vis.image(torch.randn(3, 256, 256), win='random_image')
class VisdomWriter: def __init__(self, env, server, port=8097, username=None, password=None, use_incoming_socket=True): try: from visdom import Visdom except ImportError: raise ImportError( "Visdom visualization requires installation of Visdom") self.env = env self.scalar_dict = {} self.server_connected = False self.vis = Visdom(server=server, port=port, env=env, use_incoming_socket=use_incoming_socket, username=username, password=password) self.windows = {} def add_scalar(self, tag, scalar_value, global_step=None): """Add scalar data to Visdom. Plots the values in a plot titled {main_tag}-{tag}. Args: tag (string): Data identifier scalar_value (float or string/blobname): Value to save global_step (int): Global step value to record """ assert '_' in tag, "tag needs to _, i.e. prefix_name" main_tag = tag.split('_')[0] if self.scalar_dict.get(main_tag) is None: self.scalar_dict[main_tag] = {} exists = self.scalar_dict[main_tag].get(tag) is not None self.scalar_dict[main_tag][tag] = self.scalar_dict[main_tag][tag] \ + [scalar_value] if exists else [scalar_value] # plot_name = '{}_{}'.format(main_tag, tag) plot_name = tag # If there is no global_step provided, follow sequential order x_val = len(self.scalar_dict[main_tag] [tag]) if not global_step else global_step if exists: # Update our existing Visdom window self.vis.line( X=make_np(x_val), Y=make_np(scalar_value), name=plot_name, update='append', win=self.windows[plot_name], ) else: # Save the window if we are creating this graph for the first time self.windows[plot_name] = self.vis.line( X=make_np(x_val), Y=make_np(scalar_value), name=plot_name, opts={ 'title': plot_name, 'xlabel': 'epoch', 'ylabel': tag.split('_')[-1], }, ) self.save() def add_scalars(self, main_tag, tag_scalar_dict, global_step=None): """Adds many scalar data to summary. Note that this function also keeps logged scalars in memory. In extreme case it explodes your RAM. Args: tag (string): Data identifier main_tag (string): Data group identifier tag_scalar_dict (dict): Key-value pair storing the tag and corresponding values global_step (int): Global step value to record Examples:: writer.add_scalars('run_14h',{'xsinx':i*np.sin(i/r), 'xcosx':i*np.cos(i/r), 'arctanx': numsteps*np.arctan(i/r)}, i) This function adds three plots: 'run_14h-xsinx', 'run_14h-xcosx', 'run_14h-arctanx' with the corresponding values. """ for key in tag_scalar_dict.keys(): self.add_scalar(key, tag_scalar_dict[key], global_step, main_tag) self.save() def export_scalars_to_json(self, path): """Exports to the given 'path' an ASCII file containing all the scalars written so far by this instance, with the following format: {writer_id : [[timestamp, step, value], ...], ...} The scalars saved by ``add_scalars()`` will be flushed after export. """ with open(path, "w") as f: json.dump(self.scalar_dict, f) self.scalar_dict = {} self.save() def add_histogram(self, tag, values, global_step=None, bins='tensorflow'): """Add histogram to summary. Args: tag (string): Data identifier values (torch.Tensor, numpy.array, or string/blobname): Values to build histogram global_step (int): Global step value to record bins (string): one of {'tensorflow', 'auto', 'fd', ...}, this determines how the bins are made. You can find other options in: https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html """ values = make_np(values) self.vis.histogram(make_np(values), opts={'title': tag}) self.save() def add_image(self, tag, img_tensor, global_step=None, caption=None): """Add image data to summary. Note that this requires the ``pillow`` package. Args: tag (string): Data identifier img_tensor (torch.Tensor, numpy.array, or string/blobname): Image data global_step (int): Global step value to record Shape: img_tensor: :math:`(C, H, W)`. Use ``torchvision.utils.make_grid()`` to prepare it is a good idea. C = colors (can be 1 - grayscale, 3 - RGB, 4 - RGBA) """ fn = self.vis.images if len(img_tensor.shape) > 3 else self.vis.image img_tensor = make_np(img_tensor) fn(img_tensor, win=tag, opts={'title': tag, 'caption': caption}) self.save() def add_figure(self, tag, figure, global_step=None, close=True): """Render matplotlib figure into an image and add it to summary. Note that this requires the ``matplotlib`` package. Args: tag (string): Data identifier figure (matplotlib.pyplot.figure) or list of figures: figure or a list of figures global_step (int): Global step value to record close (bool): Flag to automatically close the figure """ self.add_image(tag, figure_to_image(figure, close), global_step) self.save() def add_video(self, tag, vid_tensor, global_step=None, fps=4): """Add video data to summary. Note that this requires the ``moviepy`` package. Args: tag (string): Data identifier vid_tensor (torch.Tensor): Video data global_step (int): Global step value to record fps (float or int): Frames per second Shape: vid_tensor: :math:`(B, C, T, H, W)`. (if following tensorboardX format) vid_tensor: :math:`(T, H, W, C)`. (if following visdom format) B = batches, C = colors (1, 3, or 4), T = time frames, H = height, W = width """ shape = vid_tensor.shape # A batch of videos (tensorboardX format) is a 5D tensor if len(shape) > 4: for i in range(shape[0]): # Reshape each video to Visdom's (T x H x W x C) and write each video # TODO: reverse the logic here, shoudl do the permutation in numpy if isinstance(vid_tensor, np.ndarray): import torch ind_vid = torch.from_numpy( vid_tensor[i, :, :, :, :]).permute(1, 2, 3, 0) else: ind_vid = vid_tensor[i, :, :, :, :].permute(1, 2, 3, 0) scale_factor = 255 if np.any((ind_vid > 0) & (ind_vid < 1)) else 1 # Visdom looks for .ndim attr, this is something raw Tensors don't have # Cast to Numpy array to get .ndim attr ind_vid = ind_vid.numpy() ind_vid = (ind_vid * scale_factor).astype(np.uint8) assert ind_vid.shape[3] in [1, 3, 4], \ 'Visdom requires the last dimension to be color, which can be 1 (grayscale), 3 (RGB) or 4 (RGBA)' self.vis.video(tensor=ind_vid, opts={'fps': fps}) else: self.vis.video(tensor=vid_tensor, opts={'fps': fps}) self.save() def add_audio(self, tag, snd_tensor, global_step=None, sample_rate=44100): """Add audio data to summary. Args: tag (string): Data identifier snd_tensor (torch.Tensor, numpy.array, or string/blobname): Sound data global_step (int): Global step value to record sample_rate (int): sample rate in Hz Shape: snd_tensor: :math:`(1, L)`. The values should lie between [-1, 1]. """ snd_tensor = make_np(snd_tensor) self.vis.audio(tensor=snd_tensor, opts={'sample_frequency': sample_rate}) self.save() def add_text(self, tag, text_string, global_step=None): """Add text data to summary. Args: tag (string): Data identifier text_string (string): String to save global_step (int): Global step value to record Examples:: writer.add_text('lstm', 'This is an lstm', 0) writer.add_text('rnn', 'This is an rnn', 10) """ if text_string is None: # Visdom doesn't support tags, write the tag as the text_string text_string = tag self.vis.text(text_string) # self.save(), MEH: don't save env just because of text def add_graph_onnx(self, prototxt): # TODO: Visdom doesn't support graph visualization yet, so this is a no-op return def add_graph(self, model, input_to_model=None, verbose=False, **kwargs): # TODO: Visdom doesn't support graph visualization yet, so this is a no-op return def add_embedding(self, mat, metadata=None, label_img=None, global_step=None, tag='default', metadata_header=None): # TODO: Visdom doesn't support embeddings yet, so this is a no-op return def add_pr_curve(self, tag, labels, predictions, global_step=None, num_thresholds=127, weights=None): """Adds precision recall curve. Args: tag (string): Data identifier labels (torch.Tensor, numpy.array, or string/blobname): Ground truth data. Binary label for each element. predictions (torch.Tensor, numpy.array, or string/blobname): The probability that an element be classified as true. Value should in [0, 1] global_step (int): Global step value to record num_thresholds (int): Number of thresholds used to draw the curve. """ labels, predictions = make_np(labels), make_np(predictions) raw_data = compute_curve(labels, predictions, num_thresholds, weights) # compute_curve returns np.stack((tp, fp, tn, fn, precision, recall)) # We want to access 'precision' and 'recall' precision, recall = raw_data[4, :], raw_data[5, :] self.vis.line( X=recall, Y=precision, name=tag, opts={ 'title': 'PR Curve for {}'.format(tag), 'xlabel': 'recall', 'ylabel': 'precision', }, ) self.save() def add_pr_curve_raw(self, tag, true_positive_counts, false_positive_counts, true_negative_counts, false_negative_counts, precision, recall, global_step=None, num_thresholds=127, weights=None): """Adds precision recall curve with raw data. Args: tag (string): Data identifier true_positive_counts (torch.Tensor, numpy.array, or string/blobname): true positive counts false_positive_counts (torch.Tensor, numpy.array, or string/blobname): false positive counts true_negative_counts (torch.Tensor, numpy.array, or string/blobname): true negative counts false_negative_counts (torch.Tensor, numpy.array, or string/blobname): false negative counts precision (torch.Tensor, numpy.array, or string/blobname): precision recall (torch.Tensor, numpy.array, or string/blobname): recall global_step (int): Global step value to record num_thresholds (int): Number of thresholds used to draw the curve. see: https://github.com/tensorflow/tensorboard/blob/master/tensorboard/plugins/pr_curve/README.md """ precision, recall = make_np(precision), make_np(recall) self.vis.line( X=recall, Y=precision, name=tag, opts={ 'title': 'PR Curve for {}'.format(tag), 'xlabel': 'recall', 'ylabel': 'precision', }, ) self.save() def save(self): self.vis.save([self.env]) def close(self): if hasattr(self, 'vis'): del self.vis if hasattr(self, 'scalar_dict'): del self.scalar_dict gc.collect()
class VisdomLogger(Logger): """Logs attack results to Visdom.""" def __init__(self, env="main", port=8097, hostname="localhost"): if not port_is_open(port, hostname=hostname): raise socket.error(f"Visdom not running on {hostname}:{port}") self.vis = Visdom(port=port, server=hostname, env=env) self.env = env self.port = port self.hostname = hostname self.windows = {} self.sample_rows = [] def __getstate__(self): state = {i: self.__dict__[i] for i in self.__dict__ if i != "vis"} return state def __setstate__(self, state): self.__dict__ = state self.vis = Visdom(port=self.port, server=self.hostname, env=self.env) def log_attack_result(self, result): text_a, text_b = result.diff_color(color_method="html") result_str = result.goal_function_result_str(color_method="html") self.sample_rows.append([result_str, text_a, text_b]) def log_summary_rows(self, rows, title, window_id): self.table(rows, title=title, window_id=window_id) def flush(self): self.table( self.sample_rows, title="Sample-Level Results", window_id="sample_level_results", ) def log_hist(self, arr, numbins, title, window_id): self.bar(arr, numbins=numbins, title=title, window_id=window_id) def text(self, text_data, title=None, window_id="default"): if window_id and window_id in self.windows: window = self.windows[window_id] self.vis.text(text_data, win=window) else: new_window = self.vis.text(text_data, opts=dict(title=title)) self.windows[window_id] = new_window def table(self, rows, window_id=None, title=None, header=None, style=None): """Generates an HTML table.""" if not window_id: window_id = title # Can provide either of these, if not title: title = window_id # or both. table = html_table_from_rows(rows, title=title, header=header, style_dict=style) self.text(table, title=title, window_id=window_id) def bar(self, X_data, numbins=10, title=None, window_id=None): window = None if window_id and window_id in self.windows: window = self.windows[window_id] self.vis.bar(X=X_data, win=window, opts=dict(title=title, numbins=numbins)) else: new_window = self.vis.bar(X=X_data, opts=dict(title=title, numbins=numbins)) if window_id: self.windows[window_id] = new_window def hist(self, X_data, numbins=10, title=None, window_id=None): window = None if window_id and window_id in self.windows: window = self.windows[window_id] self.vis.histogram(X=X_data, win=window, opts=dict(title=title, numbins=numbins)) else: new_window = self.vis.histogram(X=X_data, opts=dict(title=title, numbins=numbins)) if window_id: self.windows[window_id] = new_window
parser.add_argument('--output_dir', type=str, default=os.path.expanduser('~/code/neural-tangents/output')) parser.add_argument('--exp_name', type=str, default='exp010') parser.add_argument('--run_name', type=str, default=datetime.datetime.now().strftime('%d-%m-%Y_%H:%M:%S:%f')) parser.add_argument('--debug', action='store_true') # derive additional args and serialize args = parser.parse_args() if args.debug: args.run_name = 'debug' args.log_dir = os.path.join(args.output_dir, args.exp_name, args.run_name) os.makedirs(args.log_dir, exist_ok=True) json.dump(obj=vars(args), fp=open(os.path.join(args.log_dir, 'config.json'), 'w'), sort_keys=True, indent=4) # initialize visdom viz = Visdom(port=8000, env=f'{args.exp_name}_{args.run_name}') viz.text(json.dumps(obj=vars(args), sort_keys=True, indent=4)) # build network if args.dataset == 'sinusoid': net_init, f = mlp(n_output=1, n_hidden_layer=args.n_hidden_layer, n_hidden_unit=args.n_hidden_unit, bias_coef=args.bias_coef, activation=args.activation, norm=args.norm) _, params = net_init(rng=random.PRNGKey(42), input_shape=(-1, 1)) elif args.dataset == 'omniglot': net_init, f = conv_net(n_output=args.n_way, n_conv_layer=args.n_hidden_layer, n_filter=args.n_hidden_unit,
callback_image_window = show_color_image_window(image_color) def image_callback(event): global image_color if event['event_type'] == 'KeyPress': if event['key'] == 'ArrowRight': image_color = min(image_color + 0.2, 1) if event['key'] == 'ArrowLeft': image_color = max(image_color - 0.2, 0) show_color_image_window(image_color, callback_image_window) viz.register_event_handler(image_callback, callback_image_window) # text window with Callbacks txt = 'This is a write demo notepad. Type below. Delete clears text:<br>' callback_text_window = viz.text(txt) def type_callback(event): if event['event_type'] == 'KeyPress': curr_txt = event['pane_data']['content'] if event['key'] == 'Enter': curr_txt += '<br>' elif event['key'] == 'Backspace': curr_txt = curr_txt[:-1] elif event['key'] == 'Delete': curr_txt = txt elif len(event['key']) == 1: curr_txt += event['key'] viz.text(curr_txt, win=callback_text_window) viz.register_event_handler(type_callback, callback_text_window)
class Visualizer(object): """ 封装visdom的基本操作 """ def __init__(self, env, **kwargs): self.vis = Visdom(env=env, use_incoming_socket=False, **kwargs) # 以文字和图的形式展示 # {'loss1':23,'loss2':24} self.plot_index = {} self.log_index = {} def reinit(self, env='default', **kwargs): self.vis = Visdom(env=env, **kwargs) def img(self, name, img_, **kwargs): # img_(tensor or numpy): batchsize*channels*H*W或者C*H*W 0-1 # 这里代码已经发生了变化, # caption 是在图片的左下角,神奇 opts = dict(title=name).update(kwargs) self.vis.images( img_.cpu(), win=name, opts=opts, ) def log(self, win='log_text', info='', update=True): """ self.log({'loss':1,'lr':0.0001}) @param info: @param win: @return: """ log_text = self.log_index.get(win, '') opts = dict(title=win) if update: log_text += ('[{time}]{info}<br>'.format( time=time.strftime('%m%d_%H:%M:%S'), info=info)) else: log_text = ('[{time}]{info}<br>'.format( time=time.strftime('%m%d_%H:%M:%S'), info=info)) self.vis.text(log_text, win=win, opts=opts) self.log_index[win] = log_text def plot(self, win, y, **kwargs): """ plot('loss',2) :param win: :param y: :param kwargs: :return: """ x = self.plot_index.get(win, 0) self.vis.line(X=np.array([x]), Y=np.array([y]), win=win, opts=dict(title=win), update=None if x == 0 else 'append', **kwargs) self.plot_index[win] = x + 1 def img_many(self, d): # d: {'1.jpg':b*c*H*W,'2.jpg':b*c*H*W} for k, v in d.items(): self.img(k, v) def plot_many(self, d): # d:{'loss1':2,'loass2':4} for k, v in d.items(): self.plot(k, v) def __getattr__(self, name): # self,function->self.vis.funtion return getattr(self.vis, name)
def main(): parser = Flags() parser.set_arguments() parser.add_argument('--z_dim', type=int, default=100) FG = parser.parse_args() vis = Visdom(port=FG.vis_port, env=FG.model) report = parser.report(end='<br>') vis.text(report, win='report f{}'.format(FG.cur_fold)) transform = transforms.Compose([ transforms.RandomResizedCrop(64, scale=(0.5, 1.0)), transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, )) ]) trainset = datasets.MNIST(root='./mnist', train=True, transform=transform) trainloader = torch.utils.data.DataLoader( trainset, batch_size=FG.batch_size, worker_init_fn=lambda _: torch.initial_seed(), num_workers=5, shuffle=True, pin_memory=True, drop_last=True) # trainloader, _ = get_dataloader(FG.fold, FG.cur_fold, FG.data_root, FG.modality, # labels=FG.labels, batch_size=FG.batch_size, # balancing=FG.data_balancing) torch.cuda.set_device(FG.devices[0]) device = torch.device(FG.devices[0]) netG = nn.DataParallel(Generator(FG.ckpt_dir, FG.z_dim).weight_init(), device_ids=FG.devices) netD = nn.DataParallel(Discriminator(FG.ckpt_dir).weight_init(), device_ids=FG.devices) optimG = Adam(netG.parameters(), lr=FG.lr, amsgrad=True) optimD = Adam(netD.parameters(), lr=FG.lr, amsgrad=True) z_sampler = ZSampler(torch.randn, (FG.batch_size, FG.z_dim, 1, 1), device=device) trainer = create_gan_trainer(netG, netD, optimG, optimD, F.binary_cross_entropy, z_sampler, device=device, non_blocking=True) monitoring_metrics = ['LD', 'LG', 'Dx', 'DGz1', 'DGz2'] RunningAverage(alpha=0.98, output_transform=lambda x: x['LD']).attach(trainer, 'LD') RunningAverage(alpha=0.98, output_transform=lambda x: x['LG']).attach(trainer, 'LG') RunningAverage(alpha=0.98, output_transform=lambda x: x['Dx']).attach(trainer, 'Dx') RunningAverage(alpha=0.98, output_transform=lambda x: x['DGz1']).attach( trainer, 'DGz1') RunningAverage(alpha=0.98, output_transform=lambda x: x['DGz2']).attach( trainer, 'DGz2') real_rate = Accuracy() fake_rate = Accuracy() trackers = dict() for monitoring_metric in monitoring_metrics: trackers[monitoring_metric] = Scalar(vis, monitoring_metric, monitoring_metric, opts=dict( title=monitoring_metric, y_label=monitoring_metric, xlabel='epoch', showlegend=True)) trackers['real_rate'] = Scalar(vis, 'real_rate', 'real_rate', opts=dict(title='real_rate', y_label='real_rate', ytickmin=0, ytickmax=1, xlabel='epoch', showlegend=True)) trackers['fake_rate'] = Scalar(vis, 'fake_rate', 'fake_rate', opts=dict(title='fake_rate', y_label='fake_rate', ytickmin=0, ytickmax=1, xlabel='epoch', showlegend=True)) fakeshow = Image2D(vis, 'fake') realshow = Image2D(vis, 'real') @trainer.on(Events.ITERATION_COMPLETED) def track_logs(engine): i = engine.state.iteration / len(trainloader) metrics = engine.state.metrics for key in metrics.keys(): trackers[key](i, metrics[key]) y_pred_real = (engine.state.output['output_real'] >= 0.5).long() y_pred_fake = (engine.state.output['output_fake'] < 0.5).long() real_rate.update((y_pred_real, z_sampler.real_label.long())) fake_rate.update((y_pred_fake, z_sampler.fake_label.long())) @trainer.on(Events.EPOCH_COMPLETED) def show_fake_example(engine): netG.eval() fake = netG(z_sampler.fixed_noise) fakeshow('fake_images', fake * 0.5 + 0.5) realshow('real_images', engine.state.batch[0] * 0.5 + 0.5) trackers['real_rate'](engine.state.epoch, real_rate.compute()) trackers['fake_rate'](engine.state.epoch, fake_rate.compute()) real_rate.reset() fake_rate.reset() trainer.run(trainloader, FG.num_epoch)
class Visualizer: def __init__(self, env="main", server="http://localhost", port=8097, base_url="/", http_proxy_host=None, http_proxy_port=None, log_to_filename=None): self._viz = Visdom(env=env, server=server, port=port, http_proxy_host=http_proxy_host, http_proxy_port=http_proxy_port, log_to_filename=log_to_filename, use_incoming_socket=False) self._viz.close(env=env) self.plots = {} def plot_line(self, name, tag, title, value, step=None): if name not in self.plots: y = numpy.array([value, value]) if step is not None: x = numpy.array([step, step]) else: x = numpy.array([1, 1]) opts = dict(title=title, xlabel='steps', ylabel=name) if tag is not None: opts["legend"] = [tag] self.plots[name] = self._viz.line(X=x, Y=y, opts=opts) else: y = numpy.array([value]) x = numpy.array([step]) self._viz.line(X=x, Y=y, win=self.plots[name], name=tag, update='append') def plot_text(self, text, title, pre=True): _width = max([len(x) for x in text.split("\n")]) * 10 _heigth = len(text.split("\n")) * 20 _heigth = max(_heigth, 120) if pre: text = "<pre>{}</pre>".format(text) self._viz.text(text, win=title, opts=dict(title=title, width=min(_width, 450), height=min(_heigth, 300))) def plot_bar(self, data, labels, title): self._viz.bar(win=title, X=data, opts=dict(legend=labels, stacked=False, title=title)) def plot_hist(self, data, title, numbins=20): self._viz.histogram(win=title, X=data, opts=dict(numbins=numbins, title=title)) def plot_scatter(self, data, title, targets=None, labels=None): self._viz.scatter( win=title, X=data, Y=targets, opts=dict( # legend=labels, title=title, markersize=5, webgl=True, width=400, height=400, markeropacity=0.5)) def plot_heatmap(self, data, labels, title): height = min(data.shape[0] * 20, 600) width = min(data.shape[1] * 25, 600) self._viz.heatmap( win=title, X=data, opts=dict( # title=title, columnnames=labels[1], rownames=labels[0], width=width, height=height, layoutopts={ 'plotly': { 'showscale': False, 'showticksuffix': False, 'showtickprefix': False, 'xaxis': { 'side': 'top', 'tickangle': -60, # 'autorange': "reversed" }, 'yaxis': { 'autorange': "reversed" }, } }))
from visdom import Visdom import numpy as np import math import os.path viz = Visdom() textwindow = viz.text("Hello Pytorch") image_window = viz.image(np.random.rand(3, 256, 256), opts=dict(title="random", caption="random noise")) images_window = viz.images(np.random.rand(10, 3, 64, 64), opts=dict(title="random", caption="random noise"))
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() textwindow = viz.text('Hello World!') updatetextwindow = viz.text('Hello World! More text should be here') assert updatetextwindow is not None, 'Window was none' viz.text('And here it is', win=updatetextwindow, append=True) # video demo: try: video = np.empty([256, 250, 250, 3], dtype=np.uint8) for n in range(256): video[n, :, :, :].fill(n) viz.video(tensor=video) # 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
class GPU_logger(): def __init__(self, host_names, time_interval, time_range, show_summary=True, show_monitor=True, show_cpu_mem=False, port=8098, env_name='main', verbose=False): self.viz = Visdom(port=port) self.env = env_name self.hosts = host_names self.sleep = time_interval self.range = time_range self.show_summary = show_summary self.show_monitor = show_monitor self.show_cpu_mem = show_cpu_mem self.verbose = verbose self.win = {} self.num_gpus = 4 self.max_length = self.range // self.sleep self.tick_ds = self.max_length // 6 self.time_indices = deque() self.memory_queue = np.zeros([len(host_names), self.num_gpus, 0]) self.usages_queue = np.zeros([len(host_names), self.num_gpus, 0]) self.table_W = 780 self.table_H = 470 self.restore(PWD) def reset(self): self.viz.close(win=None, env=self.env) def save(self, output_dir): if not isdir(output_dir): os.makedirs(output_dir) with open(join(output_dir, 'cached_data'), 'wb') as f: pickle.dump( { 'time': self.time_indices, 'memory': self.memory_queue, 'usages': self.usages_queue }, f, pickle.HIGHEST_PROTOCOL) def restore(self, output_dir): if isfile(join(output_dir, 'cached_data')): with open(join(output_dir, 'cached_data'), 'rb') as f: cache = pickle.load(f) self.time_indices = cache['time'] self.memory_queue = cache['memory'] self.usages_queue = cache['usages'] def record(self): if self.show_summary: gpu_tabel_opts = { 'title': 'README', 'resize': False, 'width': self.table_W, 'height': self.table_H } col_W = self.table_W / len(self.hosts) self.win['summary'] = self.viz.text(README, env=self.env, opts=gpu_tabel_opts) if self.show_cpu_mem: cpu_tabel_opts = { 'title': 'CPU Memory', 'resize': True, 'width': self.table_W, 'height': 150 } col_W_c = self.table_W / len(self.hosts) self.win['cpu_mem'] = self.viz.text('', env=self.env, opts=cpu_tabel_opts) while True: if len(self.time_indices) >= self.max_length: self.time_indices.popleft() self.time_indices.append(time.strftime("%H:%M")) if isfile(join(HOME, '.tcshrc')): # hotfix to handle my zsh os.rename(join(HOME, '.tcshrc'), join(HOME, '.tcshrc.bak')) #gpu_memory = ssh_host(self.hosts, mode='memory') #gpu_usage = ssh_host(self.hosts, mode='gpu') gpu_memory, gpu_usage, cpu_memory = ssh_host(self.hosts, mode='all') if self.verbose: print('gpu memory:', gpu_memory) print('gpu usage:', gpu_usage) print('cpu memory:', cpu_memory) if isfile(join(HOME, '.tcshrc.bak')): os.rename(join(HOME, '.tcshrc.bak'), join(HOME, '.tcshrc')) if self.memory_queue.shape[-1] < self.max_length: self.memory_queue = np.append( self.memory_queue, np.reshape(gpu_memory, [len(self.hosts), self.num_gpus, 1]), -1) self.usages_queue = np.append( self.usages_queue, np.reshape(gpu_usage, [len(self.hosts), self.num_gpus, 1]), -1) else: self.memory_queue = np.append( np.delete(self.memory_queue, 0, -1), np.reshape(gpu_memory, [len(self.hosts), self.num_gpus, 1]), -1) self.usages_queue = np.append( np.delete(self.usages_queue, 0, -1), np.reshape(gpu_usage, [len(self.hosts), self.num_gpus, 1]), -1) gpu_status_table, cpu_status_table = [], [] gpu_status_table.append( get_tablerow('', ['GPU:01', 'GPU:02', 'GPU:03', 'GPU:04'])) cpu_status_table.append( get_tablerow('', ['Total:', 'Used:', 'Available:'])) for k, host in enumerate(self.hosts): fig = plt.figure(figsize=(20, 2.7)) fig.suptitle(host, size=24, fontweight="bold", y=1.) gpu_status_row = [] for idx in range(4): memory_, usage_ = self.memory_queue[ k, idx, :], self.usages_queue[k, idx, :] nonzero_idx = np.where(memory_ > 1)[0] m_memory_ = np.mean(memory_[nonzero_idx[0]:]) if len( nonzero_idx) > 0 else np.mean(memory_) m_usage_ = np.mean(usage_[nonzero_idx]) if len( nonzero_idx) > 0 else np.mean(usage_) if m_memory_ == -1: # I've set empty gpu to -1 gpu_status_row.append(' ') continue if np.any( memory_[-5:] > 1 ): #if last 5 checkpoint is empty -> not used now status = get_gpu_health(m_memory_, m_usage_) else: status = status_kw[0] box_prop = dict(facecolor=color_map[status], edgecolor='none', pad=2, alpha=0.6) gpu_status_row.append(status) plt.subplot(1, 4, idx + 1) plt.xticks( np.arange(0, len(self.time_indices), self.tick_ds)) plt.fill_between(list(self.time_indices), memory_, color='r', label="Memory", alpha=0.4) plt.fill_between(list(self.time_indices), usage_, color='g', label="Usages", alpha=0.4) plt.ylim(-1, 101) plt.title('GPU-{:02d}'.format(idx), size=15, bbox=box_prop) plt.xlabel('Average Memory: {:.1f}% Average Usage: {:.1f}%'\ .format(m_memory_, m_usage_), size=14) plt.tight_layout() plt.legend() gpu_status_table.append( get_tablerow(host.replace('emon', '-'), gpu_status_row)) #shorten hostname cpu_status_table.append( get_tablerow(host.replace('emon', '-'), [ str(cpu_memory[k][0]) + 'G', str(cpu_memory[k][1]) + 'G', str(cpu_memory[k][-1]) + 'G' ])) if self.show_monitor: opts = { 'title': host, 'resizable': True, 'height': 190, 'width': 1400 } if host not in self.win.keys(): self.win[host] = self.viz.matplot(plt, env=self.env, opts=opts) else: self.viz.matplot(plt, win=self.win[host], env=self.env, opts=opts) plt.close() TIMESTAMP = f"Updated at {time.strftime('%Y/%m/%d-%H:%M:%S')}<br>" if 'summary' in self.win.keys(): status_table_g = [list(t) for t in zip(*gpu_status_table)] table_ = HTML.table(status_table_g, border='3', cellpadding=5, col_width=[col_W] * (len(self.hosts) + 1), col_align=['center'] * (len(self.hosts) + 1)) self.viz.text(README + table_ + TIMESTAMP + ACKNOWLEDGE, env=self.env, win=self.win['summary'], opts=gpu_tabel_opts) if 'cpu_mem' in self.win.keys(): status_table_c = [list(t) for t in zip(*cpu_status_table)] table_c = HTML.table( status_table_c, border='2', cellpadding=2, col_width=[col_W_c] * (len(self.hosts) + 1), col_align=['center'] * (len(self.hosts) + 1)) self.viz.text(table_c + TIMESTAMP, env=self.env, win=self.win['cpu_mem'], opts=cpu_tabel_opts) time.sleep(self.sleep)
from tqdm import tqdm import os from visdom import Visdom import time filter_sizes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 19] num_filters = [100, 200, 200, 200, 200, 100, 100, 100, 100, 100, 160, 160] batch_size = 64 epochs_size = 100 # 查看是否可以使用cuda cuda = torch.cuda.is_available() viz = Visdom() line = viz.line(np.arange(2)) text = viz.text("<h1>Text convolution Nueral Network</h1>") def train(corpus_, cnn, criterion, optimizer): best_acc = 0 time_p = [] # 可视化数据 tr_acc = [] ev_acc = [] tr_mean_loss = [] ev_mean_loss = [] start_time = time.time() # 训练数据集 for epoch in range(epochs_size): tr_loss_list = [] tr_acc_list = []
global_step += 1 if batch_idx % 100 == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item())) viz.line([loss.item()], [global_step], win='train_loss', update='append') test_loss = 0 correct = 0 for data, target in test_loader: data = data.view(-1, 28 * 28) logits = net(data) test_loss += criteon(logits, target).item() pred = logits.argmax(dim=1) correct += pred.eq(target).float().sum().item() test_loss /= len(test_loader.dataset) print( '\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( test_loss, correct, len(test_loader.dataset), 100. * correct / len(test_loader.dataset))) viz.line([[test_loss, correct / len(test_loader.dataset)]], [global_step], win='test', update='append') viz.images(data.view(-1, 1, 28, 28), win='x') viz.text(str(pred.numpy()), win='pred', opts=dict(title='pred'))
class Visualizer(object): def __init__(self, config: Config): # logging_level = logging._checkLevel("INFO") # logging.getLogger().setLevel(logging_level) # VisdomServer.start_server(port=VisdomServer.DEFAULT_PORT, env_path=config.vis_env_path) self.reinit(config) def reinit(self, config): self.config = config try: self.visdom = Visdom(env=config.visdom_env) self.connected = self.visdom.check_connection() if not self.connected: print("Visdom server hasn't started, please run command 'python -m visdom.server' in terminal.") # try: # print("Visdom server hasn't started, do you want to start it? ") # if 'y' in input("y/n: ").lower(): # os.popen('python -m visdom.server') # except Exception as e: # warn(e) except ConnectionError as e: warn("Can't open Visdom because " + e.strerror) with open(self.config.log_file, 'a') as f: info = "[{time}]Initialize Visdom\n".format(time=timestr('%m-%d %H:%M:%S')) info += str(self.config) f.write(info + '\n') def save(self, save_path: str = None) -> str: retstr = self.visdom.save([self.config.visdom_env]) # return current environments name in format of json try: ret = json.loads(retstr)[0] if ret == self.config.visdom_env: if isinstance(save_path, str): from shutil import copy copy(self.config.vis_env_path, save_path) print('Visdom Environment has saved into ' + save_path) else: print('Visdom Environment has saved into ' + self.config.vis_env_path) with open(self.config.vis_env_path, 'r') as fp: env_str = json.load(fp) return env_str except: pass return None def clear(self): self.visdom.close() @staticmethod def _to_numpy(value): if isinstance(value, t.Tensor): value = value.cpu().detach().numpy() elif isinstance(value, np.ndarray): pass else: value = np.array(value) if value.ndim == 0: value = value[np.newaxis] return value def plot(self, y, x, line_name, win, legend=None): # type:(float,float,str,str,list(str))->None """Plot a (sequence) of y point(s) (each) with one x value(s), loop this method to draw whole plot""" update = None if not self.visdom.win_exists(win) else 'append' opts = dict(title=win) if legend is not None: opts["legend"] = legend y = Visualizer._to_numpy(y) x = Visualizer._to_numpy(x) return win == self.visdom.line(y, x, win=win, env=self.config.visdom_env, update=update, name=line_name, opts=opts) def bar(self, y, win, rowindices=None): opts = dict(title=win) y = Visualizer._to_numpy(y) if isinstance(rowindices, list) and len(rowindices) == len(y): opts["rownames"] = rowindices return win == self.visdom.bar(y, win=win, env=self.config.visdom_env, opts=opts) def log(self, msg, name, append=True, log_file=None): # type:(str,str,bool,bool,str)->None if log_file is None: log_file = self.config.log_file info = "[{time}]{msg}".format(time=timestr('%m-%d %H:%M:%S'), msg=msg) append = append and self.visdom.win_exists(name) ret = self.visdom.text(info, win=name, env=self.config.visdom_env, opts=dict(title=name), append=append) mode = 'a+' if append else 'w+' with open(log_file, mode) as f: f.write(info + '\n') return ret == name def log_process(self, num, total, msg, name, append=True): # type:(int,int,str,Visdom,str,str,dict,bool)->None info = "[{time}]{msg}".format(time=timestr('%m-%d %H:%M:%S'), msg=msg) append = append and self.visdom.win_exists(name) ret = self.visdom.text(info, win=(name), env=self.config.visdom_env, opts=dict(title=name), append=append) with open(self.config.log_file, 'a') as f: f.write(info + '\n') self.processBar(num, total, msg) return ret == name def processBar(self, num, total, msg='', length=50): rate = num / total rate_num = int(rate * 100) clth = int(rate * length) if len(msg) > 0: msg += ':' # msg = msg.replace('\n', '').replace('\r', '') if rate_num == 100: r = '\r%s[%s%d%%]\n' % (msg, '*' * length, rate_num,) else: r = '\r%s[%s%s%d%%]' % (msg, '*' * clth, '-' * (length - clth), rate_num,) sys.stdout.write(r) sys.stdout.flush return r.replace('\r', ':')
mask2 = mp(mask1) mask3 = mp(mask2) mask4 = mp(mask3) mask5 = mp(mask4) input_data = content.data if args.init_noize: input_data = torch.randn(content.data.size()).type(torch.cuda.FloatTensor) input_param = nn.Parameter(input_data) optimizer = optim.LBFGS([input_param]) Fc = vgg(content) Fs = vgg(style) viz = Visdom() viz.text('style_weight:{:.2e}\ncontent_weight:{:.2e}'.format( args.style_weight, args.content_weight)) # viz.text('content_weight:{}'.format(args.content_weight)) textwindow = viz.text('') imageWindow = viz.images(input_param.data.cpu().numpy()) styleLossWindow = viz.line(Y=np.array([0])) contentLossWindow = viz.line(Y=np.array([0])) loss_style = [] loss_content = [] if args.start_epoch > 0: temp = np.load(args.saved_loss) loss_style = list(temp['style']) loss_content = list(temp['content']) print_flag = [0] # if not os.path.exists(args.out_dir): # os.mkdir(args.out_dir)
net = CNN(1, 10) if gpu_status: net = net.cuda() print("#" * 26, "使用gpu", "#" * 26) else: print("#" * 26, "使用cpu", "#" * 26) # loss、optimizer 函数设置 loss_f = nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), lr=LR) # 起始时间设置 start_time = time.time() # 可视化所需数据点 time_p, tr_acc, ts_acc, loss_p = [], [], [], [] # 创建可视化数据视窗 text = viz.text("<h1>convolution Nueral Network</h1>") for epoch in range(EPOCHS): # 由于分批次学习,输出loss为一批平均,需要累积or平均每个batch的loss,acc sum_loss, sum_acc, sum_step = 0., 0., 0. for i, (tx, ty) in enumerate(train_loader, 1): if gpu_status: tx, ty = tx.cuda(), ty.cuda() tx = Variable(tx) ty = Variable(ty) out = net(tx) loss = loss_f(out, ty) sum_loss += loss.data[0] * len(ty) pred_tr = torch.max(out, 1)[1] sum_acc += sum(pred_tr == ty).data[0] sum_step += ty.size(0) # 学习反馈
class Visualizer(object): """ Visualizer """ def __init__(self, port='13579', env='main', id=None): self.cur_win = {} self.vis = Visdom(port=port, env=env) self.id = id self.env = env # Restore ori_win = self.vis.get_window_data() ori_win = json.loads(ori_win) #print(ori_win) self.cur_win = {v['title']: k for k, v in ori_win.items()} pass def vis_scalar(self, name, x, y, opts=None): if not isinstance(x, list): x = [x] if not isinstance(y, list): y = [y] if self.id is not None: name = "[%s]" % self.id + name default_opts = {'title': name} if opts is not None: default_opts.update(opts) win = self.cur_win.get(name, None) if win is not None: self.vis.line(X=x, Y=y, opts=default_opts, update='append', win=win) else: self.cur_win[name] = self.vis.line(X=x, Y=y, opts=default_opts) pass def vis_image(self, name, img, env=None, opts=None): """ vis image in visdom """ if env is None: env = self.env if self.id is not None: name = "[%s]" % self.id + name win = self.cur_win.get(name, None) default_opts = {'title': name} if opts is not None: default_opts.update(opts) if win is not None: self.vis.image(img=img, win=win, opts=opts, env=env) else: self.cur_win[name] = self.vis.image(img=img, opts=default_opts, env=env) pass def vis_table(self, name, tbl, opts=None): win = self.cur_win.get(name, None) tbl_str = "<table width=\"100%\"> " tbl_str += "<tr> \ <th>Term</th> \ <th>Value</th> \ </tr>" for k, v in tbl.items(): tbl_str += "<tr> \ <td>%s</td> \ <td>%s</td> \ </tr>" % (k, v) tbl_str += "</table>" default_opts = {'title': name} if opts is not None: default_opts.update(opts) if win is not None: self.vis.text(tbl_str, win=win, opts=default_opts) else: self.cur_win[name] = self.vis.text(tbl_str, opts=default_opts) pass
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 import traceback viz = Visdom() viz.text('Hello, Visdom!') # 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)) # image demo viz.image( np.random.rand(3, 512, 256), opts=dict(title='Random!', caption='How random.'), ) # grid of images viz.images( np.random.randn(20, 3, 64, 64), opts=dict(title='Random images', caption='How random.') )
# print(all_out) print("Test__Epoch:%d loss:%f, auc:%f" % (epoch, loss.item(), auc)) return auc if __name__ == '__main__': viz = Visdom() viz.line([0.], [0.], win='train_loss', opts=dict(title='loss')) viz.line([0.], [0.], win='test_loss', opts=dict(title='test_loss')) viz.line([[0.0, 0.0]], [0.], win='auc', opts=dict(title='auc', legend=['train_auc', 'test_auc'])) viz.text( f"学习率:{lr } weight_decay:{weight_decay} model_type: my_resnet18", win='info', opts=dict(title='info')) global_step = 0 metrics = [] save_path = r'E:\pycharm_project\Huaxi_Yinjie_Jiang\Unet_encoder_weights_pre' for epoch in range(epoch): global_step += 1 train_auc = train(epoch) # valid(epoch) test_auc = my_test(epoch) viz.line([[train_auc, test_auc]], [global_step], win='auc', opts=dict(title='auc', legend=['train_auc', 'test_auc']), update='append')
loss = Loss(F.cross_entropy) precision = Precision() sensitivity = Sensitivity() specificity = Specificity() for i in range(FG.fold): parser.args.cur_fold = i output, target = run_fold(parser, vis) output = torch.cat(output) target = torch.cat(target) arg = (output, target) acc.update(arg) loss.update(arg) precision.update(arg) sensitivity.update(arg) specificity.update(arg) end = '<br>' text = 'Over all result<br>' text += 'accuracy: ' + '{:.4f}'.format(acc.compute()) + end text += 'loss: ' + '{:.4f}'.format(loss.compute()) + end text += 'precision: ' + '{}'.format(precision.compute()) + end text += 'sensitivity: ' + '{}'.format(sensitivity.compute()) + end text += 'specificity: ' + '{}'.format(specificity.compute()) + end vis.text(text, 'result_overall') vis.save([vis.env])
if __name__ == '__main__': FG = GAN_parser() if FG.clean_ckpt: shutil.rmtree(FG.checkpoint_root) if not os.path.exists(FG.checkpoint_root): os.makedirs(FG.checkpoint_root, exist_ok=True) logger = logging.Logger(FG.checkpoint_root) FG.seed = 1 torch.manual_seed(FG.seed) torch.cuda.manual_seed(FG.seed) cudnn.benchmark = True EPS = 1e-12 vis = Visdom(port=FG.vis_port, env=str(FG.vis_env)) vis.text(argument_report(FG, end='<br>'), win='config') save_dir = str(FG.vis_env) if not os.path.exists(save_dir): os.makedirs(save_dir) # torch setting device = torch.device('cuda:{}'.format(FG.devices[0])) torch.cuda.set_device(FG.devices[0]) timer = SimpleTimer() printers = dict(lr=Scalar(vis, 'lr', opts=dict(showlegend=True, title='lr', ytickmin=0,
class VisdomPlotter(object): """Plots to Visdom""" def __init__(self, env_name='main', modality=''): self.viz = Visdom() self.env = env_name self.modality = modality self.plots = {} self.paramList = {} def argsTile(self, argsDict): self.paramList = self.viz.text(self.modality + '\n <b>Training Parameters:</b>\n', env=self.env, opts=dict(width=220, height=320)) for key, value in argsDict.items(): self.viz.text(str(key) + ' = ' + str(value) + '\n', env=self.env, win=self.paramList, append=True) def plot(self, var_name, split_name, x, y): if var_name not in self.plots: self.plots[var_name] = self.viz.line( X=np.array([x, x]), Y=np.array([y, y]), env=self.env, opts=dict(legend=[split_name], title=self.modality + var_name, xlabel='Epochs', ylabel=var_name)) else: self.viz.line(X=np.array([x]), Y=np.array([y]), env=self.env, win=self.plots[var_name], name=split_name, update='append') def showImage(self, imageTensor): # self.viz.image(imageTensor, win=self.images, env=self.env, opts=dict(title='Original and Reconstructed', caption='How random.'),) self.viz.images( imageTensor, win=self.images, env=self.env, opts=dict(title='Original and Reconstructed', caption='How random.', nrow=2), ) def plotPerformance(self, var_name, split_name, x, y): if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=x, Y=y, env=self.env, opts=dict(legend=[split_name], title=var_name, xlabel='Epochs', ylabel=var_name)) else: self.viz.line(X=x, Y=y, env=self.env, win=self.plots[var_name], name=split_name, update='append')
win='accuracy', env='mobilenet', opts=dict(title='Accuracy', xlabel='Iteration', ylabel='accuracy', legend=['Accuracy'], markers=True, showlegend=True ), update=None if iteration == 1 else 'append') print(f'Iteration {iteration} finished! Train loss: {loss_train_iteration}\t' f'Val loss: {loss_total_val}\t' f'Accuracy: {accuracy}') env.text(f'Iteration {iteration} finished! Train loss: {loss_train_iteration}\t' f'Val loss: {loss_total_val}\t' f'Accuracy: {accuracy}\n\n', win='log', env='mobilenet', append=False if iteration == 1 else True) print() pass print(f'Epoch {epoch} finished!\n') env.text(f'Epoch {epoch} finished!\n', win='log', env='mobilenet', append=False if iteration == 1 else True) plt.plot(range(1,EPOCHS+1), train_loss_curve, label='train loss') plt.plot(range(1,EPOCHS+1), val_loss_curve, label='val loss') plt.title('Mobilenet')
class VisdomReporter(Reporter): def __init__(self, port=6006, save_dir=None): from visdom import Visdom super(VisdomReporter, self).__init__(save_dir) self._viz = Visdom(port=port, env=self._now) self._lines = defaultdict() assert self._viz.check_connection(), f""" Please launch visdom.server before calling VisdomReporter. $python -m visdom.server -port {port} """ def add_scalar(self, x, name: str, idx: int, **kwargs): self.add_scalars({name: x}, name=name, idx=idx, **kwargs) def add_scalars(self, x: dict, name, idx: int, **kwargs): x = {k: self._to_numpy(v) for k, v in x.items()} num_lines = len(x) is_new = self._lines.get(name) is None self._lines[name] = 1 for k, v in x.items(): self._register_data(v, k, idx) opts = dict(title=name, legend=list(x.keys())) opts.update(**kwargs) X = np.column_stack((self._to_numpy(idx) for _ in range(num_lines))) Y = np.column_stack(x.values()) self._viz.line(X=X, Y=Y, update=None if is_new else "append", win=name, opts=opts) def add_parameters(self, x, name: str, idx: int, **kwargs): # todo raise NotImplementedError def add_text(self, x, name: str, idx: int): self._register_data(x, name, idx) self._viz.text(x) def add_image(self, x, name: str, idx: int): x, dim = self._tensor_type_check(x) assert dim == 3 self._viz.image(self._normalize(x), opts=dict(title="name", caption=str(idx))) def add_images(self, x, name: str, idx: int): x, dim = self._tensor_type_check(x) assert dim == 4 self._viz.images(self._normalize(x), opts=dict(title="name", caption=str(idx))) def _to_numpy(self, x): if isinstance(x, numbers.Number): x = np.array([x]) elif "Tensor" in str(type(x)): x = x.numpy() return def __exit__(self, exc_type, exc_val, exc_tb): super(VisdomReporter, self).__exit__(exc_type, exc_val, exc_tb) self._viz.close() @staticmethod def _normalize(x): # normalize tensor values in (0, 1) _min, _max = x.min(), x.max() return (x - _min) / (_max - _min)
class Visualizer(object): """ 封装visdom的基本操作 """ def __init__(self, env='default', **kwargs): self.vis = Visdom(env=env, **kwargs) # 以文字和图的形式展示 # {'loss1':23,'loss2':24} self.index = {} self.log_text = '' def reinit(self, env='default', **kwargs): self.vis = Visdom(env=env, **kwargs) def img(self, name, img_, **kwargs): # img_: batchsize*channels*H*W self.vis.images(img_.cpu().numpy(), win=name, opts=dict(title=name), **kwargs) def log(self, win='log_text', info=''): """ self.log({'loss':1,'lr':0.0001}) :param info: :param win: :return: """ self.log_text += ('[{time}]{info}<br>'.format( time=time.strftime('%m%d_%H:%M:%S'), info=info)) self.vis.text(self.log_text, win) def plot(self, win, y, **kwargs): """ plot('loss',2) :param win: :param y: :param kwargs: :return: """ x = self.index.get(win, 0) self.vis.line(X=np.array([x]), Y=np.array([y]), win=win, opts=dict(title=win), update=None if x == 0 else 'append', **kwargs) self.index[win] = x + 1 def img_many(self, d): # d: {'1.jpg':b*c*H*W,'2.jpg':b*c*H*W} for k, v in d.items(): self.img(k, v) def plot_many(self, d): for k, v in d.items(): self.plot(k, v) def __getattr__(self, name): # self,function->self.vis.funtion return getattr(self.vis, name)
from visdom import Visdom viz = Visdom() viz.line([0.], [0.], win='train_loss', opts=dict(title='train loss')) viz.line([loss.item()], [global_step], win='train_loss', update='append') #多条曲线 viz.line([[0., 0.]], win='test', opts=dict(title='train loss&acc.', legend=['loss', 'acc.'])) viz.line([[test_loss, correct / len(testloader.dataset)]], [global_step], win='test', update='append') #keshihua viz.images(data.view(-1, 1, 28, 28), win='x') viz.text(str(pred.detach().cpu().numpy()), win='pred', opts=dict(title='pred'))
class VisdomViewer(object): """ The VisdomViewer class implements the live viewer plots via visdom. When extending implement your plot as methos and call it in update. Using this class make it necessary starting a visdom server beforehand $ python -m visdom.server """ def __init__(self, project, port=8097, server="http://localhost"): """ The constructor wants a HyppopyProject and accepts a visdom server port and a server name. :param project: [HyppopyProject] project instance :param port: [int] server port, default=8097 :param server: [str] server name, default=http://localhost """ self._viz = Visdom(port=port, server=server) self._enabled = self._viz.check_connection(timeout_seconds=3) if not self._enabled: warnings.warn("No connection to visdom server established. Visualization cannot be displayed!") self._project = project self._best_win = None self._best_loss = None self._loss_iter_plot = None self._status_report = None self._axis_tags = None self._axis_plots = None def plot_losshistory(self, input_data): """ This function plots the loss history loss over iteration :param input_data: [dict] trail infos """ loss = np.array([input_data["loss"]]) iter = np.array([input_data["iterations"]]) if self._loss_iter_plot is None: self._loss_iter_plot = self._viz.line(loss, X=iter, opts=dict( markers=True, markersize=5, dash=np.array(['dashdot']), title="Loss History", xlabel='iteration', ylabel='loss' )) else: self._viz.line(loss, X=iter, win=self._loss_iter_plot, update='append') def plot_hyperparameter(self, input_data): """ This function plots each hyperparameter axis :param input_data: [dict] trail infos """ if self._axis_plots is None: self._axis_tags = [] self._axis_plots = {} for item in input_data.keys(): if item == "refresh_time" or item == "book_time" or item == "iterations" or item == "status" or item == "loss": continue self._axis_tags.append(item) for axis in self._axis_tags: xlabel = "value" if isinstance(input_data[axis], str): if self._project.hyperparameter[axis]["domain"] == "categorical": xlabel = '-'.join(self._project.hyperparameter[axis]["data"]) input_data[axis] = self._project.hyperparameter[axis]["data"].index(input_data[axis]) axis_loss = np.array([input_data[axis], input_data["loss"]]).reshape(1, -1) self._axis_plots[axis] = self._viz.scatter(axis_loss, opts=dict( markersize=5, title=axis, xlabel=xlabel, ylabel='loss')) else: for axis in self._axis_tags: if isinstance(input_data[axis], str): if self._project.hyperparameter[axis]["domain"] == "categorical": input_data[axis] = self._project.hyperparameter[axis]["data"].index(input_data[axis]) axis_loss = np.array([input_data[axis], input_data["loss"]]).reshape(1, -1) self._viz.scatter(axis_loss, win=self._axis_plots[axis], update='append') def show_statusreport(self, input_data): """ This function prints status report per iteration :param input_data: [dict] trail infos """ duration = input_data['refresh_time'] - input_data['book_time'] duration, time_format = time_formatter(duration.total_seconds()) report = "Iteration {}: {}{} -> {}\n".format(input_data["iterations"], duration, time_format, input_data["status"]) if self._status_report is None: self._status_report = self._viz.text(report) else: self._viz.text(report, win=self._status_report, append=True) def show_best(self, input_data): """ Shows best parameter set :param input_data: [dict] trail infos """ if self._best_win is None: self._best_loss = input_data["loss"] txt = "Best Parameter Set:<hr>Loss: {}<hr><ul>".format(self._best_loss) for axis in self._axis_tags: if self._project.hyperparameter[axis]["domain"] == "categorical": txt += "<li>{} = {}</li>".format(axis, self._project.hyperparameter[axis]["data"][input_data[axis]]) else: txt += "<li>{} = {}</li>".format(axis, input_data[axis]) txt += "</ul>" self._best_win = self._viz.text(txt) else: if input_data["loss"] < self._best_loss: self._best_loss = input_data["loss"] txt = "Best Parameter Set:<hr>Loss: {}<hr><ul>".format(self._best_loss) for axis in self._axis_tags: if self._project.hyperparameter[axis]["domain"] == "categorical": txt += "<li>{} = {}</li>".format(axis, self._project.hyperparameter[axis]["data"][input_data[axis]]) else: txt += "<li>{} = {}</li>".format(axis, input_data[axis]) txt += "</ul>" self._viz.text(txt, win=self._best_win, append=False) def update(self, input_data): """ This function calls all visdom displaying routines :param input_data: [dict] trail infos """ if self._enabled: self.show_statusreport(input_data) self.plot_losshistory(input_data) self.plot_hyperparameter(input_data) self.show_best(input_data)