class Dashboard: def __init__(self, server='http://35.237.68.41', port=8097, env='default'): self.vis = Visdom(port=port, server=server, env=env) self.index = {} self.log_text = '' def loss(self, losses, title): x = np.arange(1, len(losses) + 1, 1) self.vis.line(losses, x, env='loss', opts=dict(title=title)) def image(self, image, title): if image.is_cuda: image = image.cpu() if isinstance(image, Variable): image = image.data image = image.numpy() self.vis.image(image.astype(np.float), env=self.vis.env, opts=dict(title=title)) def plot(self, name, y): """ self.plot('loss',1.00) """ x = self.index.get(name, 0) self.vis.line(Y=np.array([y]), X=np.array([x]), win=name, opts=dict(title=name), update=None if x == 0 else 'append') self.index[name] = x + 1
class Logger(): def __init__(self, n_epochs, batches_epoch, env_name): self.viz = Visdom(env = env_name) self.n_epochs = n_epochs self.batches_epoch = batches_epoch self.epoch = 1 self.batch = 1 self.prev_time = time.time() self.mean_period = 0 self.losses = {} self.loss_windows = {} self.image_windows = {} def log(self, losses=None, images=None): self.mean_period += (time.time() - self.prev_time) self.prev_time = time.time() sys.stdout.write('\rEpoch %03d/%03d [%04d/%04d] -- ' % (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].item() else: self.losses[loss_name] += losses[loss_name].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))) # Draw images if images: for image_name, tensor in images.items(): 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 epoch if (self.batch % self.batches_epoch) == 0: # Plot losses 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') # Reset losses for next epoch self.losses[loss_name] = 0.0 self.epoch += 1 self.batch = 1 sys.stdout.write('\n') else: self.batch += 1
class ImageVisdom(object): """Images to Visdom""" def __init__(self, env_name='main', imsize=None): self.vis = Visdom(use_incoming_socket=False) self.env = env_name self.images = {} self.imsize = imsize def show(self, title, image): if self.imsize: image = cv2.resize(image, self.imsize, interpolation=cv2.INTER_LINEAR) image = image.astype(float) image = np.transpose(image, (2, 0, 1)) if title not in self.images: self.images[title] = self.vis.image(image, env=self.env, opts=dict(title=title)) else: self.vis.image(image, env=self.env, win=self.images[title], opts=dict(title=title))
def visualize_nerf_outputs(nerf_out: dict, output_cache: List, viz: Visdom, visdom_env: str): """ Visualizes the outputs of the `RadianceFieldRenderer`. Args: nerf_out: An output of the validation rendering pass. output_cache: A list with outputs of several training render passes. viz: A visdom connection object. visdom_env: The name of visdom environment for visualization. """ # Show the training images. ims = torch.stack([o["image"] for o in output_cache]) ims = torch.cat(list(ims), dim=1) viz.image( ims.permute(2, 0, 1), env=visdom_env, win="images", opts={"title": "train_images"}, ) # Show the coarse and fine renders together with the ground truth images. ims_full = torch.cat( [ nerf_out[imvar][0].permute(2, 0, 1).detach().cpu().clamp(0.0, 1.0) for imvar in ("rgb_coarse", "rgb_fine", "rgb_gt") ], dim=2, ) viz.image( ims_full, env=visdom_env, win="images_full", opts={"title": "coarse | fine | target"}, ) # Make a 3D plot of training cameras and their emitted rays. camera_trace = { f"camera_{ci:03d}": o["camera"].cpu() for ci, o in enumerate(output_cache) } ray_pts_trace = { f"ray_pts_{ci:03d}": Pointclouds( ray_bundle_to_ray_points( o["coarse_ray_bundle"]).detach().cpu().view(1, -1, 3)) for ci, o in enumerate(output_cache) } plotly_plot = plot_scene( { "training_scene": { **camera_trace, **ray_pts_trace, }, }, pointcloud_max_points=5000, pointcloud_marker_size=1, camera_scale=0.3, ) viz.plotlyplot(plotly_plot, env=visdom_env, win="scenes")
class Watcher: def __init__(self, var_dict): self.var_dict = var_dict self.viz = Visdom() self.scalars = [] self.images = [] def add_scalar(self, name, display_name=None): self.viz.line([0], [0], name, opts=dict(title=name)) self.scalars.append(name) def add_image(self, name, display_name=None): self.viz.image(self.var_dict[name], win=name) self.images.append(name) def update(self, step, var_dict=None): if var_dict is not None: self.var_dict = var_dict for key in self.images: if key in self.var_dict: self.viz.image(self.var_dict[key], win=key) for key in self.scalars: if key in self.var_dict: self.viz.line([self.var_dict[key].item()], [step], win=key, update='append')
class Visualizer: def __init__(self): self.vis = Visdom(port=config.visdom_port) self.plot_data = {'x': [], 'y': [], 'legend': []} def show_images(self, images): for label, image in images.items(): self.vis.image(np.array(image).transpose([2, 0, 1]), opts={'title': label}, win=label) def plot_errors(self, epoch, errors): if len(self.plot_data['legend']) == 0: self.plot_data['legend'] = sorted(list(errors.keys())) self.plot_data['x'].append([epoch for _ in self.plot_data['legend']]) self.plot_data['y'].append( [errors[key] for key in self.plot_data['legend']]) self.vis.line(X=np.array(self.plot_data['x']), Y=np.array(self.plot_data['y']), opts={ 'title': '{} loss'.format(config.experiment_name), 'legend': self.plot_data['legend'], 'xlabel': 'epoch', 'ylabel': 'loss' }, win='loss') def print_errors(self, errors): for key, val in errors.items(): print('{:20}: {:.3e}'.format(key, val))
class VisdomLinePlotter(object): """Plots to Visdom""" def __init__(self, env_name='main'): self.viz = Visdom() self.env = env_name self.plots = {} 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, name=split_name, opts=dict(legend=[split_name], title=var_name, xlabel='Epochs', ylabel=var_name)) else: self.viz.line(X=np.array([x, x]), Y=np.array([y, y]), win=self.plots[var_name], env=self.env, name=split_name, update='append') def image(self, map_feature1, map_feature2, map_feature3, map_feature4): self.viz.image(map_feature1, env='map1', opts=dict(title='map1')) self.viz.image(map_feature2, env='map2', opts=dict(title='map2')) self.viz.image(map_feature3, env='map3', opts=dict(title='map3')) self.viz.image(map_feature4, env='map4', opts=dict(title='map4'))
class Logger(): def __init__(self, n_iters, n_epochs): self.viz = Visdom() self.n_iters = n_iters self.n_epochs = n_epochs self.init_time = time.time() def log(self, nepoch, niter, losses=None, images=None): period = time.time() - self.init_time sys.stdout.write('\n Epoch => %03d/%03d [%04d/%04d] >> | ' % (nepoch, self.n_epochs, niter, self.n_iters)) for k, v in losses.items(): sys.stdout.write('%s: %.4f | ' % (k, v)) iters_done = self.n_iters * nepoch + niter + 1 iters_left = self.n_iters * self.n_epochs - iters_done sys.stdout.write('ETA: %s' % (datetime.timedelta(seconds=iters_left/iters_done*period))) # Draw images for k, v in images.items(): self.viz.image(tensor2image(v.data), win = k, opts = {'title' : k} )
class Dashboard: def __init__(self, port, env_name="GrayChannel"): self.vis = Visdom(port=port) self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y): if var_name not in self.plots: self.plots[var_name] = self.vis.line(X=np.array([x, x]), Y=np.array([y, y]), env=self.env, opts=dict(legend=[split_name], title=var_name, xlabel="Iters", ylabel=var_name)) else: self.vis.updateTrace(X=np.array([x, x]), Y=np.array([y, y]), env=self.env, win=self.plots[var_name], name=split_name) def image(self, image, title): if image.is_cuda: image = image.cpu() if isinstance(image, Variable): imgae = image.data image = image.numpy() img_env = self.env + '_images' self.vis.image(image, env=img_env, opts=dict(title=title))
class Logger(): def __init__(self, n_iters, n_epochs): self.viz = Visdom() self.n_iters = n_iters self.n_epochs = n_epochs self.init_time = time.time() def log(self, nepoch, niter, losses=None, images=None, ver='G2RGB'): period = time.time() - self.init_time sys.stdout.write('\n Epoch %02d [%04d/%04d] >> ' % (nepoch, niter, self.n_iters)) for k, v in losses.items(): sys.stdout.write('%s: %.3f | ' % (k, v)) iters_done = self.n_iters * (nepoch - 1) + niter iters_left = self.n_iters * self.n_epochs - iters_done eta = iters_left / iters_done * period sys.stdout.write('ETA: %s' % (datetime.timedelta(seconds=eta))) # Draw imgs for k, v in images.items(): mode = "RGB" if k in ['fake_AB', 'real_B', 'fake_BB'] and ver == "G2LAB": mode = 'LAB' img = tensor2img(v.data, mode) self.viz.image(img, win=k, opts={'title': k})
class VisdomPlotter(object): """ Visualizer """ def __init__(self, port='13579', env='main'): self.cur_win = {} self.env = env self.visdom = Visdom(port=port, env=env) def add_scalar(self, win, x, y, opts=None, trace_name=None): """ Draw line """ if not isinstance(x, list): x = [x] if not isinstance(y, list): y = [y] default_opts = {'title': win} if opts is not None: default_opts.update(opts) update = 'append' if win is not None else None self.visdom.line(X=x, Y=y, opts=default_opts, win=win, env=self.env, update=update, name=trace_name) def add_image(self, win, img, opts=None): """ vis image in visdom """ default_opts = dict(title=win) if opts is not None: default_opts.update(opts) self.visdom.image(img=img, win=win, opts=default_opts, env=self.env) def add_table(self, win, tbl, opts=None): tbl_str = "<table width=\"100%\"> " tbl_str += "<tr> \ <th>[Key]</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': win} if opts is not None: default_opts.update(opts) self.visdom.text(tbl_str, win=win, env=self.env, opts=default_opts) def add_heatmap(self, win, X, opts=None): default_opts = {'title': win, 'xmin': 0, 'xmax': 1} if opts is not None: default_opts.update(opts) self.visdom.heatmap(X=X, win=win, opts=default_opts, env=self.env)
class Trainer: def __init__(self, args, model): self.name = args.name self.model = model self.l1win = None self.l2win = None self.l1meter = AverageValueMeter() self.l2meter = AverageValueMeter() self.visdom = Visdom( port=args.vis_port) if args.vis_steps > 0 else None @property def mode(self): return 'training' if self.model.training else 'testing' @property def losses(self): return self.l1meter.value()[0], self.l2meter.value()[1] def reset(self): self.l1meter.reset() self.l2meter.reset() def log_losses(self, epoch, step): l1, l2 = self.losses message = f'{self.name} is {self.mode} (epoch: {epoch}, step: {step}) ' message += f'l1 average: {l1}, l2 average: {l2}' print(message) def vis_losses(self, epoch): l1, l2 = self.losses x, y1, y2 = np.array([epoch]), np.array([l1]), np.array([l2]) if self.l1win is None or self.l2win is None: opt = dict(xlabel='epochs', xtickstep=1, ylabel='mean loss', width=900) self.l1win = self.visdom.line(X=x, Y=y1, opts=dict( title=f'l1 loss ({self.name})', **opt)) self.l2win = self.visdom.line(X=x, Y=y2, opts=dict( title=f'l2 loss ({self.name})', **opt)) else: n = '1' if self.model.training else '2' self.visdom.updateTrace(X=x, Y=y1, win=self.l1win, name=n) self.visdom.updateTrace(X=x, Y=y2, win=self.l2win, name=n) def vis_images(self, epoch, step, images): title = f'({self.name}, epoch: {epoch}, step: {step})' for key, image in images.items(): self.visdom.image(image.cpu().data, env=self.mode, opts=dict(title=f'{key} {title}'))
class Logger(): """Logger for training.""" def __init__(self, enable_visdom=False, curve_names=None): self.curve_names = curve_names if enable_visdom: self.vis = Visdom() assert self.vis.check_connection() self.curve_x = np.array([0]) else: self.curve_names = None def log(self, xval=None, win_name='loss', **kwargs): """Log and print the information.""" print("##############################################################") for key, value in kwargs.items(): print(key, value, sep='\t') if self.curve_names: if not xval: xval = self.curve_x for i in range(len(self.curve_names)): name = self.curve_names[i] if name not in kwargs: continue yval = np.array([kwargs[name]]) self.vis.line(Y=yval, X=xval, win=win_name, update='append', name=name, opts=dict(showlegend=True)) self.curve_x += 1 def plot_curve(self, yvals, xvals, win_name='pr_curves'): """Plot curve.""" self.vis.line(Y=np.array(yvals), X=np.array(xvals), win=win_name) def plot_marking_points(self, image, marking_points, win_name='mk_points'): """Plot marking points on visdom.""" width, height = image.size draw = ImageDraw.Draw(image) for point in marking_points: p0_x = width * point.x p0_y = height * point.y p1_x = p0_x + 50 * math.cos(point.direction) p1_y = p0_y + 50 * math.sin(point.direction) draw.line((p0_x, p0_y, p1_x, p1_y), fill=(255, 0, 0)) p2_x = p0_x - 50 * math.sin(point.direction) p2_y = p0_y + 50 * math.cos(point.direction) if point.shape > 0.5: draw.line((p2_x, p2_y, p0_x, p0_y), fill=(255, 0, 0)) else: p3_x = p0_x + 50 * math.sin(point.direction) p3_y = p0_y - 50 * math.cos(point.direction) draw.line((p2_x, p2_y, p3_x, p3_y), fill=(255, 0, 0)) image = np.asarray(image, dtype="uint8") image = np.transpose(image, (2, 0, 1)) self.vis.image(image, win=win_name)
class VisdomLogger: def __init__(self, visdom_env='main', log_every=10, prefix=''): self.vis = None self.log_every = log_every self.prefix = prefix if visdom_env is not None: self.vis = Visdom(env=visdom_env) self.vis.close() def on_batch_end(self, state): iters = state['iters'] if self.log_every != -1 and iters % self.log_every == 0: self.log(iters, state['metrics']) def on_epoch_end(self, state): self.log(state['iters'], state['metrics']) def log(self, iters, xs, store_history=[]): if self.vis is None: return for name, x in xs.items(): name = self.prefix + name if isinstance(x, (float, int)): self.vis.line(X=[iters], Y=[x], update='append', win=name, opts=dict(title=name), name=name) elif isinstance(x, str): self.vis.text(x, win=name, opts=dict(title=name)) elif isinstance(x, torch.Tensor): if x.numel() == 1: self.vis.line(X=[iters], Y=[x.item()], update='append', win=name, opts=dict(title=name), name=name) elif x.dim() == 2: self.vis.heatmap(x, win=name, opts=dict(title=name)) elif x.dim() == 3: self.vis.image(x, win=name, opts=dict(title=name, store_history=name in store_history)) elif x.dim() == 4: self.vis.images(x, win=name, opts=dict(title=name, store_history=name in store_history)) else: assert False, "incorrect tensor dim" else: assert False, "incorrect type " + x.__class__.__name__
def visualize_image_outputs(validation_images: List, viz: Visdom, visdom_env: str): ims = torch.cat(validation_images, 2) viz.image( ims, env=visdom_env, win="images", opts={"title": "validation_images"}, )
class VisdomLinePlotter(object): # Plots to Visdom def __init__(self, env_name='main'): self.viz = Visdom(port=args.visdom_port) self.env = env_name self.plots = {} # plot curve graph 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=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') # plot attention map def plot_attention(self, imgs, heatmaps, tasks, alpha=0.5): global meta for i in range(len(tasks)): heatmap = heatmaps[i] heatmap = cv2.resize(heatmap, (224, 224), interpolation=cv2.INTER_CUBIC) heatmap = np.maximum(heatmap, 0) heatmap /= np.max(heatmap) heatmap_marked = np.uint8(cm.gist_rainbow(heatmap)[..., :3] * 255) heatmap_marked = cv2.cvtColor(heatmap_marked, cv2.COLOR_BGR2RGB) heatmap_marked = np.uint8(imgs[i] * alpha + heatmap_marked * (1. - alpha)) heatmap_marked = heatmap_marked.transpose([2, 0, 1]) win_name = 'img %d - %s' % (i, meta.data['ATTRIBUTES'][tasks[i]]) if win_name not in self.plots: self.plots[win_name] = self.viz.image( heatmap_marked, env=self.env, opts=dict(title=win_name)) self.plots[win_name + 'heatmap'] = self.viz.heatmap( heatmap, env=self.env, opts=dict(title=win_name)) else: self.viz.image(heatmap_marked, env=self.env, win=self.plots[win_name], opts=dict(title=win_name)) self.viz.heatmap(heatmap, env=self.env, win=self.plots[win_name + 'heatmap'], opts=dict(title=win_name))
def plot_img(X=None, win=None, env=None, plot=None, port=_port): if plot is None: plot = Visdom(port=port) if X.ndim == 2: plot.heatmap(X=np.flipud(X), win=win, opts=dict(title=win), env=env) elif X.ndim == 3: # X is BWC norm_img = normalize_img(X) plot.image(norm_img.transpose(2, 0, 1), win=win, opts=dict(title=win), env=env)
def visualize_image_list_vertically(images: List, viz: Visdom, visdom_env: str, n_rows: int = 1): images = torch.cat(images, 1) viz.image( images, env=visdom_env, win="images", opts={"title": "validation_images"}, )
def test_03(self): from visdom import Visdom viz = Visdom() # 单张 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'))
class Logger(): def __init__(self, env): self.viz = Visdom(env=env) self.losses = {} self.loss_windows = {} self.image_windows = {} def log(self, epoch=None, losses=None, images=None, image_grid=None, env=None): # Draw images if images: for image_name, tensor in images.items(): # pdb.set_trace() if image_name not in self.image_windows: self.image_windows[image_name] = self.viz.image( masktensor2image(tensor.data), opts={'title': image_name}) else: self.viz.image(masktensor2image(tensor.data), win=self.image_windows[image_name], opts={'title': image_name}) if image_grid: for image_name, tensor in image_grid.items(): if image_name not in self.image_windows: self.image_windows[image_name] = self.viz.images( tensor, env=env, opts={'title': image_name}) else: self.viz.images(tensor, win=self.image_windows[image_name], env=env, opts={'title': image_name}) # Plot losses if losses: for loss_name, loss in losses.items(): if loss_name not in self.loss_windows: self.loss_windows[loss_name] = self.viz.line( X=np.array([epoch]), Y=np.array([loss]), opts={ 'xlabel': 'epochs', 'ylabel': loss_name, 'title': loss_name }) else: self.viz.line(X=np.array([epoch]), Y=np.array([loss]), win=self.loss_windows[loss_name], update='append')
class Visualizer: def __init__(self): self.vis = Visdom() self.real_image = None self.real_heatmap = None self.real_concat = None self.real_batch = None self.fake_image = None self.fake_concat = None self.fake_heatmap = None self.fake_batch = None self.loss = None self.point = 0 def initiate_windows(self): random_image = torch.rand(1, 256, 256) random_batch = torch.rand(8, 1, 256, 256) self.real_image = self.vis.image(random_image, win="real_img", opts={"caption": "Real image"}) self.real_heatmap = self.vis.image(random_image, win="real_map") self.real_concat = self.vis.image(random_image, win="real_cat") self.real_batch = self.vis.images(random_batch, win="real_batch") self.fake_image = self.vis.image(random_image, win="fake_img") self.fake_concat = self.vis.image(random_image, win="fake_cat") self.fake_heatmap = self.vis.image(random_image, win="fake_map") self.fake_batch = self.vis.images(random_batch, win="fake_batch") self.loss = self.vis.line(X=torch.zeros(1), Y=torch.zeros(1, 2), win="loss", opts={ "xlabel": "Iteration", "ylabel": "Loss", "title": "Training progression", "legend": ["pred real", "pred fake"], }) self.point = 0 def update_image(self, img, name): self.vis.image(img, win=name, opts={"caption": name}) def update_batch(self, batch, name): self.vis.images(batch, win=name, opts={"caption": name}, nrow=8) def update_loss(self, pred_real, pred_fake): self.vis.line(X=torch.ones(1, 2) * self.point, Y=torch.stack([pred_real, pred_fake], dim=1), win="loss", update="append", opts={ "xlabel": "Iteration", "ylabel": "Loss", "title": "Here is a title", "legend": ["pred real", "pred fake"], }) self.point += 1
class Dashboard: def __init__(self, port): self.vis = Visdom(port=port) def loss(self, losses, title): x = np.arange(1, len(losses)+1, 1) self.vis.line(losses, x, env='loss', opts=dict(title=title)) def image(self, image, title): if image.is_cuda: image = image.cpu() image = image.numpy() self.vis.image(image, env='images', opts=dict(title=title))
class VisdomLinePlotter(object): """Plots to Visdom""" def __init__(self, env_name='main'): self.viz = Visdom() self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y, exp_name='test', env=None): if env is not None: print_env = env else: print_env = self.env 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=print_env, opts=dict( legend=[split_name], title=var_name, xlabel='Epochs', ylabel=var_name )) else: self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), env=print_env, win=self.plots[var_name], name=split_name) if not os.path.exists('runs/%s/data/'%(exp_name)): os.makedirs('runs/%s/data/'%(exp_name)) file = open('runs/%s/data/%s_%s_data.csv'%(exp_name, split_name, var_name), 'a') file.write('%d, %f\n'%(x, y)) file.close() def plot_mask(self, masks, epoch): self.viz.bar( X=masks, env=self.env, opts=dict( stacked=True, title=epoch, ) ) def plot_image(self, image, epoch, exp_name='test'): self.viz.image(image, env=exp_name+'_img', opts=dict( caption=epoch, )) def plot_images(self, images, run_split, epoch, nrow, padding=2, exp_name='test'): self.viz.images(images, env=exp_name+'_img', nrow=nrow, padding=padding, opts=dict( caption='%s_%d'%(run_split, epoch), # title='Random images', jpgquality=100, ))
def visdom_log(viz: visdom.Visdom, windows: dict, state: dict, action: dict, reward: float, step: int) -> None: ''' Deprecated :param viz: :param windows: :param sensors: :param state: :param action: :param reward: :param step: :return: ''' viz.line(X=[state['location'][0]], Y=[state['location'][1]], win=windows['trace'], update='append') viz.line(X=[step], Y=[reward], win=windows['reward'], update='append') viz.line(X=[step], Y=[action['gas_brake']], win=windows['gas_brake'], update='append') viz.line(X=[step], Y=[action['steer']], win=windows['steer'], update='append') viz.line(X=[step], Y=[state['velocity']], win=windows['velocity'], update='append') viz.line(X=[step], Y=[state['distance_2finish']], win=windows['distance_2finish'], update='append') if 'depth' in state.keys(): img = state['depth'][-1] img = np.moveaxis(img, 2, 0).copy().astype(np.uint8) viz.image(img=img, win=windows['img'], opts=dict(width=800, height=600)) if 'rgb' in state.keys(): img = state['rgb'][-1] img = np.moveaxis(img, 2, 0).copy().astype(np.uint8) viz.image(img=img, win=windows['rgb'], opts=dict(width=800, height=600))
def render(self, viz: visdom.Visdom, render_format='svg'): if not self.graphviz_installed or len(self.param_nodes) == 0: return self.graph.clear(keep_attrs=True) self.draw_model() if render_format == 'svg': svg = self.graph.pipe(format='svg').decode('utf-8') viz.svg(svgstr=svg, win=self.graph.name) elif render_format == 'png': filename = viz.env self.graph.render(filename=filename, cleanup=True) impath = os.path.join(self.graph.directory, filename + '.png') image_rendered = np.transpose(imageio.imread(impath), axes=(2, 0, 1)) viz.image(image_rendered, win=self.graph.name) else: raise NotImplementedError()
class VisdomLogger(): """ Logger that uses visdom to create learning curves Parameters ---------- - env: str, name of the visdom environment - log_checkpoints: bool, whether to use checkpoints or epoch averages for training loss - legend: tuple, names of the different losses that will be plotted. """ def __init__(self, server='http://localhost', port=8097): if Visdom is None: warnings.warn("Couldn't import visdom: `pip install visdom`") else: self.viz = Visdom(server=server, port=port) # self.viz.delete_env() def deleteWindow(self, win): self.viz.close(win=win) def appendLine(self, name, win, X, Y, xlabel='empty', ylabel='empty'): if xlabel == 'empty' or ylabel == 'empty': self.viz.line(X=X, Y=Y, win=win, name=name, update='append', opts=dict(title="Loss")) else: self.viz.line(X=X, Y=Y, win=win, name=name, update='append', opts=dict(title="Loss", xlabel=xlabel, ylabel=ylabel, showlegend=True)) def plotLine(self, name, win, X, Y): self.viz.line(X=X, Y=Y, win=win, name=name) def plotImage(self, image, win, title="Image", caption="Just a Image"): self.viz.image(image, win=win, opts=dict(title=title, caption=caption)) def plotImages(self, images, win, nrow, caption="Validation Output"): self.viz.images(images, win=win, nrow=nrow, opts=dict(caption=caption)) def plot3dScatter(self, point, win): print("Point is", point) self.viz.scatter(X = point, win=win, opts=dict(update='update'))
class Dashboard: def __init__(self, port): self.vis = Visdom(port=port) def loss(self, losses, title): x = np.arange(1, len(losses) + 1, 1) self.vis.line(losses, x, env='loss', opts=dict(title=title)) def image(self, image, title): if image.is_cuda: image = image.cpu() if isinstance(image, Variable): image = image.data image = image.numpy() self.vis.image(image, env='erfnet_fast_enc', opts=dict(title=title))
class VisImageForAE(AbstractCallback): def __init__(self, title, server='https://localhost', port=8080, vis_step=1, scale=10): self.viz = Visdom(server=server, port=port) self.title = title + ' original|predicted' self.windows = {1: None} self.n = 0 self.step = vis_step self.scale = scale self.x_name = 'rgb_x' if 'rgb' in title else 'edge_x' self.y_name = 'rgb_y_pred' if 'rgb' in title else 'edge_y_pred' if 'rgb_edge' in title: self.x_name = 'edge_after_rgb' self.y_name = 'rgb_after_edge' random.seed() def per_batch(self, args, label=1): if self.n % self.step == 0: i = random.randint(0, args[self.x_name].size(0) - 1) for win in self.windows.keys(): if win == label: if self.x_name == 'edge_after_rgb': _x = torch.cat((args[self.x_name][i], ) * 3, dim=0) x = torch.cat((_x, args[self.y_name][i]), dim=2) else: x = torch.cat( (args[self.x_name][i], args[self.y_name][i]), dim=2) self.windows[win] = self.viz.image( torch.clamp( F.interpolate(x.unsqueeze(0), scale_factor=(self.scale, self.scale)).squeeze(), -0.5, 0.5) + 0.5, win=self.windows[win], opts=dict(title=self.title)) self.n += 1 if self.n >= 1000000000: self.n = 0 def per_epoch(self, args): pass def early_stopping(self, args): pass def add_window(self, label): self.windows[label] = None
class VisdomLinePlotter(object): """Plots to Visdom""" def __init__(self, env_name='main'): self.viz = Visdom() self.env = env_name self.plots = {} 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, name=split_name, opts=dict(legend=[split_name], title=var_name, xlabel='Epochs', ylabel=var_name)) else: self.viz.line(X=np.array([x, x]), Y=np.array([y, y]), win=self.plots[var_name], env=self.env, name=split_name, update='append') def image(self, map_feature1, map_feature2, map_feature3, map_feature4): self.viz.image(map_feature1, env='map1', opts=dict(title='map1')) self.viz.image(map_feature2, env='map2', opts=dict(title='map2')) self.viz.image(map_feature3, env='map3', opts=dict(title='map3')) self.viz.image(map_feature4, env='map4', opts=dict(title='map4')) def weight(self, x, state): # the following code can get the name of each layer # for k, v in params.items(): # print(k) # maybe we need to change the layer's name y = state['fc.bias'].view(-1, 1) s = np.array([x, x]) for i in range(0, y.shape[0] - 1): s = np.column_stack((s, np.array([x, x]))) w = np.array([y, y]) if x == 1: self.viz.line(X=s, Y=w, win='weights') self.viz.line(X=s, Y=w, win='weights', update='append')
class VisdomImagePlotter(object): """ Plots Image to Visdom""" def __init__(self,env_name='main'): self.viz = Visdom() self.env = env_name self.image = None def plot(self,image,name): self.image = self.viz.image(image, env = self.env,win=name)
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') # image demo viz.image( np.random.rand(3, 512, 256), opts=dict(title='Random!', caption='How random.'), ) # scatter plots Y = np.random.rand(100) viz.scatter( X=np.random.rand(100, 2), Y=(Y[Y > 0] + 1.5).astype(int), opts=dict( legend=['Apples', 'Pears'], xtickmin=-5, xtickmax=5, xtickstep=0.5, ytickmin=-5, ytickmax=5, ytickstep=0.5,