keithhon commited on
Commit
f438de3
·
1 Parent(s): 39d0bf3

Upload synthesizer/utils/plot.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. synthesizer/utils/plot.py +76 -0
synthesizer/utils/plot.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib
2
+ matplotlib.use("Agg")
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+
6
+
7
+ def split_title_line(title_text, max_words=5):
8
+ """
9
+ A function that splits any string based on specific character
10
+ (returning it with the string), with maximum number of words on it
11
+ """
12
+ seq = title_text.split()
13
+ return "\n".join([" ".join(seq[i:i + max_words]) for i in range(0, len(seq), max_words)])
14
+
15
+ def plot_alignment(alignment, path, title=None, split_title=False, max_len=None):
16
+ if max_len is not None:
17
+ alignment = alignment[:, :max_len]
18
+
19
+ fig = plt.figure(figsize=(8, 6))
20
+ ax = fig.add_subplot(111)
21
+
22
+ im = ax.imshow(
23
+ alignment,
24
+ aspect="auto",
25
+ origin="lower",
26
+ interpolation="none")
27
+ fig.colorbar(im, ax=ax)
28
+ xlabel = "Decoder timestep"
29
+
30
+ if split_title:
31
+ title = split_title_line(title)
32
+
33
+ plt.xlabel(xlabel)
34
+ plt.title(title)
35
+ plt.ylabel("Encoder timestep")
36
+ plt.tight_layout()
37
+ plt.savefig(path, format="png")
38
+ plt.close()
39
+
40
+
41
+ def plot_spectrogram(pred_spectrogram, path, title=None, split_title=False, target_spectrogram=None, max_len=None, auto_aspect=False):
42
+ if max_len is not None:
43
+ target_spectrogram = target_spectrogram[:max_len]
44
+ pred_spectrogram = pred_spectrogram[:max_len]
45
+
46
+ if split_title:
47
+ title = split_title_line(title)
48
+
49
+ fig = plt.figure(figsize=(10, 8))
50
+ # Set common labels
51
+ fig.text(0.5, 0.18, title, horizontalalignment="center", fontsize=16)
52
+
53
+ #target spectrogram subplot
54
+ if target_spectrogram is not None:
55
+ ax1 = fig.add_subplot(311)
56
+ ax2 = fig.add_subplot(312)
57
+
58
+ if auto_aspect:
59
+ im = ax1.imshow(np.rot90(target_spectrogram), aspect="auto", interpolation="none")
60
+ else:
61
+ im = ax1.imshow(np.rot90(target_spectrogram), interpolation="none")
62
+ ax1.set_title("Target Mel-Spectrogram")
63
+ fig.colorbar(mappable=im, shrink=0.65, orientation="horizontal", ax=ax1)
64
+ ax2.set_title("Predicted Mel-Spectrogram")
65
+ else:
66
+ ax2 = fig.add_subplot(211)
67
+
68
+ if auto_aspect:
69
+ im = ax2.imshow(np.rot90(pred_spectrogram), aspect="auto", interpolation="none")
70
+ else:
71
+ im = ax2.imshow(np.rot90(pred_spectrogram), interpolation="none")
72
+ fig.colorbar(mappable=im, shrink=0.65, orientation="horizontal", ax=ax2)
73
+
74
+ plt.tight_layout()
75
+ plt.savefig(path, format="png")
76
+ plt.close()