forked from VCL3D/DeepDepthDenoising
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.py
62 lines (55 loc) · 2.26 KB
/
visualization.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import visdom
import numpy
import torch
class NullVisualizer(object):
def __init__(self):
self.name = __name__
def append_loss(self, epoch, global_iteration, loss, mode='train'):
pass
def show_images(self, images, title):
pass
class VisdomVisualizer(object):
def __init__(self, name, server="http://localhost", count=2):
self.visualizer = visdom.Visdom(server=server, port=8097, env=name,\
use_incoming_socket=False)
self.name = name
self.first_train_value = True
self.first_test_value = True
self.count = count
self.plots = {}
def append_loss(self, epoch, global_iteration, loss, loss_name="total", mode='train'):
plot_name = loss_name + '_train_loss' if mode == 'train' else 'test_loss'
opts = (
{
'title': plot_name,
'xlabel': 'iterations',
'ylabel': loss_name
})
loss_value = float(loss.detach().cpu().numpy())
if loss_name not in self.plots:
self.plots[loss_name] = self.visualizer.line(X=numpy.array([global_iteration]), Y=numpy.array([loss_value]), opts=opts)
else:
self.visualizer.line(X=numpy.array([global_iteration]), Y=numpy.array([loss_value]), win=self.plots[loss_name], name=mode, update = 'append')
def show_images(self, images, title):
b, c, h, w = images.size()
recon_images = images.detach().cpu()[:self.count, [2, 1, 0], :, :]\
if c == 3 else\
images.detach().cpu()[:self.count, :, :, :]
opts = (
{
'title': title, 'width': self.count / 2 * 640,
'height': self.count / 4 * 360
})
self.visualizer.images(recon_images, opts=opts,\
win=self.name + title + "_window")
def show_map(self, maps, title):
b, c, h, w = maps.size()
maps_cpu = maps.detach().cpu()[:self.count, :, :, :]
for i in range(self.count):
opts = (
{
'title': title + str(i), 'colormap': 'Viridis'
})
heatmap = maps_cpu[i, :, :, :].squeeze(0)
self.visualizer.heatmap(heatmap,\
opts=opts, win=self.name + title + "_window_" + str(i))