1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import visdom import numpy as np from time import strftime
class Visualizer(object): def __init__(self, env="default", **kwargs): self.vis = visdom.Visdom(env=env, use_incoming_socket=False, **kwargs) self.index = {} self.log_text = "" def reinit(self, env="default", **kwargs): self.vis = visdom.Visdom(env=env, **kwargs) return self def plot_many(self, d): for i in d.items(): self.plot(*i) def img_many(self, d): for i in d.items(): self.img(*i) def plot(self, name, y, **kwargs): 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", **kwargs ) self.index[name] = x + 1 def img(self, name, img_, **kwargs): self.vis.images(img_.cpu().numpy(), win=name, opts=dict(title=name), **kwargs ) def log(self, info, win="log_text"): now = strftime("%m%d_%H%M%S") self.log_text += f"[{now}]{info}<br/>" self.vis.text(self.log_text, win) def __getattr__(self, item): return getattr(self.vis, item)
|