forked from SeanNaren/deepspeech.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
71 lines (59 loc) · 2.55 KB
/
logger.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
63
64
65
66
67
68
69
70
71
import os
import torch
def to_np(x):
return x.cpu().numpy()
class VisdomLogger(object):
def __init__(self, id, num_epochs):
from visdom import Visdom
self.viz = Visdom()
self.opts = dict(title=id, ylabel='', xlabel='Epoch', legend=['Loss', 'WER', 'CER'])
self.viz_window = None
self.epochs = torch.arange(1, num_epochs + 1)
self.visdom_plotter = True
def update(self, epoch, values):
x_axis = self.epochs[0:epoch + 1]
y_axis = torch.stack((values["loss_results"][:epoch + 1],
values["wer_results"][:epoch + 1],
values["cer_results"][:epoch + 1]),
dim=1)
self.viz_window = self.viz.line(
X=x_axis,
Y=y_axis,
opts=self.opts,
win=self.viz_window,
update='replace' if self.viz_window else None
)
def load_previous_values(self, start_epoch, package):
self.update(start_epoch - 1, package) # Add all values except the iteration we're starting from
class TensorBoardLogger(object):
def __init__(self, id, log_dir, log_params):
os.makedirs(log_dir, exist_ok=True)
from tensorboardX import SummaryWriter
self.id = id
self.tensorboard_writer = SummaryWriter(log_dir)
self.log_params = log_params
def update(self, epoch, values, parameters=None):
loss, wer, cer = values["loss_results"][epoch + 1], values["wer_results"][epoch + 1], \
values["cer_results"][epoch + 1]
values = {
'Avg Train Loss': loss,
'Avg WER': wer,
'Avg CER': cer
}
self.tensorboard_writer.add_scalars(self.id, values, epoch + 1)
if self.log_params:
for tag, value in parameters():
tag = tag.replace('.', '/')
self.tensorboard_writer.add_histogram(tag, to_np(value), epoch + 1)
self.tensorboard_writer.add_histogram(tag + '/grad', to_np(value.grad), epoch + 1)
def load_previous_values(self, start_epoch, values):
loss_results = values["loss_results"][:start_epoch]
wer_results = values["wer_results"][:start_epoch]
cer_results = values["cer_results"][:start_epoch]
for i in range(start_epoch):
values = {
'Avg Train Loss': loss_results[i],
'Avg WER': wer_results[i],
'Avg CER': cer_results[i]
}
self.tensorboard_writer.add_scalars(self.id, values, i + 1)