coqui-tts/notebooks/TacotronPlayGround.ipynb

5.0 KiB
Raw Blame History

None <html lang="en"> <head> </head>
In [ ]:
%load_ext autoreload
%autoreload 2
import os
import sys
import io
import torch 
import time
import numpy as np
from collections import OrderedDict

%pylab inline
rcParams["figure.figsize"] = (16,5)
sys.path.append('/home/erogol/projects/')

import librosa
import librosa.display

from torchviz import make_dot, make_dot_from_trace

from TTS.models.tacotron import Tacotron 
from TTS.layers import *
from TTS.utils.data import *
from TTS.utils.audio import AudioProcessor
from TTS.utils.generic_utils import load_config
from TTS.utils.text import text_to_sequence

import IPython
from IPython.display import Audio
from utils import *
In [ ]:
def tts(model, text, CONFIG, use_cuda, ap, figures=True):
    t_1 = time.time()
    waveform, alignment, spectrogram = create_speech(model, text, CONFIG, use_cuda, ap) 
    print(" >  Run-time: {}".format(time.time() - t_1))
    if figures:                                                                                                         
        visualize(alignment, spectrogram, CONFIG)                                                                       
    IPython.display.display(Audio(waveform, rate=CONFIG.sample_rate))  
    return alignment, spectrogram
In [ ]:
# Set constants
ROOT_PATH = '/data/shared/erogol_models/March-28-2018_06:24PM/'
MODEL_PATH = ROOT_PATH + '/best_model.pth.tar'
CONFIG_PATH = ROOT_PATH + '/config.json'
OUT_FOLDER = ROOT_PATH + '/test/'
CONFIG = load_config(CONFIG_PATH)
use_cuda = False
In [ ]:
# load the model
model = Tacotron(CONFIG.embedding_size, CONFIG.num_mels, CONFIG.num_freq, CONFIG.r)

# load the audio processor
ap = AudioProcessor(CONFIG.sample_rate, CONFIG.num_mels, CONFIG.min_level_db,
                    CONFIG.frame_shift_ms, CONFIG.frame_length_ms, CONFIG.preemphasis,
                    CONFIG.ref_level_db, CONFIG.num_freq, CONFIG.power, griffin_lim_iters=80)         


# load model state
if use_cuda:
    cp = torch.load(MODEL_PATH)
else:
    cp = torch.load(MODEL_PATH, map_location=lambda storage, loc: storage)

# # small trick to remove DataParallel wrapper
new_state_dict = OrderedDict()
for k, v in cp['model'].items():
    name = k[7:] # remove `module.`
    new_state_dict[name] = v
cp['model'] = new_state_dict

# load the model
model.load_state_dict(cp['model'])
if use_cuda:
    model.cuda()
model.eval()

EXAMPLES FROM TRAINING SET

In [ ]:
import pandas as pd
df = pd.read_csv('/data/shared/KeithIto/LJSpeech-1.0/metadata.csv', delimiter='|')
In [ ]:
sentence = df.iloc[120, 1].lower().replace(',','')
print(sentence)
align = tts(model, sentence, CONFIG, use_cuda, ap)

NEW EXAMPLES

In [ ]:
sentence =  "Will Donald Trump Jr. offer the countrys business leaders a peek into a new U.S.-India relationship in trade? Defense? Terrorism?"
model.decoder.max_decoder_steps = 300
alignment = tts(model, sentence, CONFIG, use_cuda, ap)
</html>