You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
2.0 KiB

  1. import random
  2. import torch
  3. from torch.utils.tensorboard import SummaryWriter
  4. from plotting_utils import plot_alignment_to_numpy, plot_spectrogram_to_numpy
  5. from plotting_utils import plot_gate_outputs_to_numpy
  6. class Tacotron2Logger(SummaryWriter):
  7. def __init__(self, logdir):
  8. super(Tacotron2Logger, self).__init__(logdir)
  9. def log_training(self, reduced_loss, grad_norm, learning_rate, duration,
  10. iteration):
  11. self.add_scalar("training.loss", reduced_loss, iteration)
  12. self.add_scalar("grad.norm", grad_norm, iteration)
  13. self.add_scalar("learning.rate", learning_rate, iteration)
  14. self.add_scalar("duration", duration, iteration)
  15. def log_validation(self, reduced_loss, model, y, y_pred, iteration):
  16. self.add_scalar("validation.loss", reduced_loss, iteration)
  17. _, mel_outputs, gate_outputs, alignments = y_pred
  18. mel_targets, gate_targets = y
  19. # plot distribution of parameters
  20. for tag, value in model.named_parameters():
  21. tag = tag.replace('.', '/')
  22. self.add_histogram(tag, value.data.cpu().numpy(), iteration)
  23. # plot alignment, mel target and predicted, gate target and predicted
  24. idx = random.randint(0, alignments.size(0) - 1)
  25. self.add_image(
  26. "alignment",
  27. plot_alignment_to_numpy(alignments[idx].data.cpu().numpy().T),
  28. iteration, dataformats='HWC')
  29. self.add_image(
  30. "mel_target",
  31. plot_spectrogram_to_numpy(mel_targets[idx].data.cpu().numpy()),
  32. iteration, dataformats='HWC')
  33. self.add_image(
  34. "mel_predicted",
  35. plot_spectrogram_to_numpy(mel_outputs[idx].data.cpu().numpy()),
  36. iteration, dataformats='HWC')
  37. self.add_image(
  38. "gate",
  39. plot_gate_outputs_to_numpy(
  40. gate_targets[idx].data.cpu().numpy(),
  41. torch.sigmoid(gate_outputs[idx]).data.cpu().numpy()),
  42. iteration, dataformats='HWC')