====== Visdom ======
===== 이미지 축 변환 =====
일반 이미지(height x widht x channel)에서 torch (channel x height x width)로 변환
def hwc2chw(img):
assert img.shape == (84, 84, 3)
return np.transpose(img, (2, 0, 1)) # (3, 84, 84)
def chw2hwc(img):
assert img.shape == (3, 84, 84)
return np.transpose(img, (1, 2, 0)) # (84, 84, 3)
이미지 보이기
viz.image(o, win='name', opts=dict(width=256, height=256))
viz_line 편의함수
viz = visdom.Visdom()
def viz_line_(viz, title, x, y, window_size=25, width=None, height=None, ymin=None, ymax=None):
title_ = title.replace(' ', '_')
if type(y) == dict:
_, update = getattr(viz, '_{}'.format(title_), (None, None))
legend, xs, ys = [], [], []
for k, v in sorted(y.items()):
k_ = k.replace(' ', '_')
dq, _ = getattr(viz, '_{}'.format(k_), (collections.deque(maxlen=window_size), None))
dq.append(v)
legend.append(k)
xs.append([x])
ys.append([np.mean(dq)])
viz.line(X=np.column_stack(xs), Y=np.column_stack(ys), win=title, update=update,
opts=dict(title=title, width=width, height=height, legend=legend))
setattr(viz, '_{}'.format(title_), (None, 'append'))
else:
dq, update = getattr(viz, '_{}'.format(title_), (collections.deque(maxlen=window_size), None))
dq.append(y)
y_mean = np.mean(dq)
viz.line(X=[x], Y=[y_mean], win=title, update=update,
opts=dict(title=f'{title}: {y_mean}', width=width, height=height, ytickmin=ymin, ytickmax=ymax))
setattr(viz, '_{}'.format(title_), (dq, 'append'))
viz_line = functools.partial(viz_line_, viz)