-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.py
81 lines (71 loc) · 3.2 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
72
73
74
75
76
77
78
79
80
81
import random
import torch.nn.functional as F
from tensorboardX import SummaryWriter
from plotting_utils import plot_alignment_to_numpy, plot_spectrogram_to_numpy
from plotting_utils import plot_gate_outputs_to_numpy
import tornado
import logging
import sys
from tornado import options
options.options.mockable()
class Tacotron2Logger(SummaryWriter):
def __init__(self, logdir):
super(Tacotron2Logger, self).__init__(logdir)
def log_training(self, reduced_loss, grad_norm, learning_rate, duration,
iteration):
self.add_scalar("training.loss", reduced_loss, iteration)
self.add_scalar("grad.norm", grad_norm, iteration)
self.add_scalar("learning.rate", learning_rate, iteration)
self.add_scalar("duration", duration, iteration)
def log_validation(self, reduced_loss, model, y, y_pred, iteration):
self.add_scalar("validation.loss", reduced_loss, iteration)
_, mel_outputs, gate_outputs, alignments = y_pred
mel_targets, gate_targets = y
# plot distribution of parameters
for tag, value in model.named_parameters():
tag = tag.replace('.', '/')
self.add_histogram(tag, value.data.cpu().numpy(), iteration)
# plot alignment, mel target and predicted, gate target and predicted
idx = random.randint(0, alignments.size(0) - 1)
self.add_image(
"alignment",
plot_alignment_to_numpy(alignments[idx].data.cpu().numpy().T),
iteration)
self.add_image(
"mel_target",
plot_spectrogram_to_numpy(mel_targets[idx].data.cpu().numpy()),
iteration)
self.add_image(
"mel_predicted",
plot_spectrogram_to_numpy(mel_outputs[idx].data.cpu().numpy()),
iteration)
self.add_image(
"gate",
plot_gate_outputs_to_numpy(
gate_targets[idx].data.cpu().numpy(),
F.sigmoid(gate_outputs[idx]).data.cpu().numpy()),
iteration)
class TornadoLogger():
def __init__(self):
self.logger = logging.getLogger()
self.formatter = logging.Formatter(
'[%(levelname)1.1s %(asctime)s.%(msecs)d '
'%(module)s:%(lineno)d] %(message)s',
"%Y-%m-%d %H:%M:%S")
tornado.options.define("access_to_stdout", default=True, help="Log tornado.access to stdout")
self.bootstrap()
# tornado server logging
def init_logging(self, access_to_stdout=False):
if access_to_stdout:
access_log = logging.getLogger('tornado.access')
access_log.propagate = False
# make sure access log is enabled even if error level is WARNING|ERROR
access_log.setLevel(logging.INFO)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(self.formatter)
access_log.addHandler(stdout_handler)
for handler in self.logger.handlers: # setting format for all handlers
handler.setFormatter(self.formatter)
def bootstrap(self):
tornado.options.parse_command_line(final=True)
self.init_logging(tornado.options.options.access_to_stdout)