From 161a26c9dd91a7831e2ed7de6b030fa0a3345eae Mon Sep 17 00:00:00 2001 From: Eren Golge Date: Tue, 13 Nov 2018 12:10:40 +0100 Subject: [PATCH] Plot mel spectrogram if required --- utils/synthesis.py | 3 ++- utils/visual.py | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/utils/synthesis.py b/utils/synthesis.py index 2531473a..1f36cfa3 100644 --- a/utils/synthesis.py +++ b/utils/synthesis.py @@ -17,7 +17,8 @@ def synthesis(m, s, CONFIG, use_cuda, ap): chars_var = chars_var.cuda() mel_spec, linear_spec, alignments, stop_tokens = m.forward(chars_var.long()) linear_spec = linear_spec[0].data.cpu().numpy() + mel_spec = mel_spec[0].data.cpu().numpy() alignment = alignments[0].cpu().data.numpy() wav = ap.inv_spectrogram(linear_spec.T) # wav = wav[:ap.find_endpoint(wav)] - return wav, alignment, linear_spec, stop_tokens \ No newline at end of file + return wav, alignment, linear_spec, mel_spec, stop_tokens \ No newline at end of file diff --git a/utils/visual.py b/utils/visual.py index e61123d8..9114da91 100644 --- a/utils/visual.py +++ b/utils/visual.py @@ -29,11 +29,16 @@ def plot_spectrogram(linear_output, audio): return fig -def visualize(alignment, spectrogram, stop_tokens, text, hop_length, CONFIG): +def visualize(alignment, spectrogram, stop_tokens, text, hop_length, CONFIG, spectrogram2=None): + if spectrogram2 is not None: + num_plot = 4 + else: + num_plot = 3 + label_fontsize = 16 plt.figure(figsize=(16, 32)) - plt.subplot(3, 1, 1) + plt.subplot(num_plot, 1, 1) plt.imshow(alignment.T, aspect="auto", origin="lower", interpolation=None) plt.xlabel("Decoder timestamp", fontsize=label_fontsize) plt.ylabel("Encoder timestamp", fontsize=label_fontsize) @@ -41,14 +46,21 @@ def visualize(alignment, spectrogram, stop_tokens, text, hop_length, CONFIG): plt.colorbar() stop_tokens = stop_tokens.squeeze().detach().to('cpu').numpy() - plt.subplot(3, 1, 2) + plt.subplot(num_plot, 1, 2) plt.plot(range(len(stop_tokens)), list(stop_tokens)) - plt.subplot(3, 1, 3) + plt.subplot(num_plot, 1, 3) librosa.display.specshow(spectrogram.T, sr=CONFIG.audio['sample_rate'], hop_length=hop_length, x_axis="time", y_axis="linear") plt.xlabel("Time", fontsize=label_fontsize) plt.ylabel("Hz", fontsize=label_fontsize) + if spectrogram2 is not None: + plt.subplot(num_plot, 1, 4) + librosa.display.specshow(spectrogram2.T, sr=CONFIG.audio['sample_rate'], + hop_length=hop_length, x_axis="time", y_axis="linear") + plt.xlabel("Time", fontsize=label_fontsize) + plt.ylabel("Hz", fontsize=label_fontsize) + plt.tight_layout() plt.colorbar()