mirror of
https://github.com/wassname/TTS.git
synced 2026-09-21 12:40:58 +08:00
29 KiB
29 KiB
In [1]:
TTS_PATH = "/home/erogol/projects/"
WAVERNN_PATH ="/home/erogol/projects/"In [2]:
%load_ext autoreload
%autoreload 2
import os
import sys
import io
import torch
import time
import numpy as np
from collections import OrderedDict
from matplotlib import pylab as plt
%pylab inline
rcParams["figure.figsize"] = (16,5)
# add libraries into environment
sys.path.append(TTS_PATH) # set this if TTS is not installed globally
sys.path.append(WAVERNN_PATH) # set this if TTS is not installed globally
import librosa
import librosa.display
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, setup_model
from TTS.utils.text import text_to_sequence
from TTS.utils.synthesis import synthesis
from TTS.utils.visual import visualize
import IPython
from IPython.display import Audio
import os
os.environ['CUDA_VISIBLE_DEVICES']='1'
os.environ['OMP_NUM_THREADS']='1'
Populating the interactive namespace from numpy and matplotlib
/home/erogol/miniconda3/lib/python3.7/site-packages/IPython/core/magics/pylab.py:160: UserWarning: pylab import has clobbered these variables: ['plt'] `%matplotlib` prevents importing * from pylab and numpy "\n`%matplotlib` prevents importing * from pylab and numpy"
In [3]:
def tts(model, text, CONFIG, use_cuda, ap, use_gl, speaker_id=None, figures=True):
t_1 = time.time()
waveform, alignment, mel_spec, mel_postnet_spec, stop_tokens = synthesis(model, text, CONFIG, use_cuda, ap, truncated=False, speaker_id=speaker_id, enable_eos_bos_chars=CONFIG.enable_eos_bos_chars)
if CONFIG.model == "Tacotron" and not use_gl:
mel_postnet_spec = ap.out_linear_to_mel(mel_postnet_spec.T).T
if not use_gl:
waveform = wavernn.generate(torch.FloatTensor(mel_postnet_spec.T).unsqueeze(0).cuda(), batched=batched_wavernn, target=11000, overlap=550)
print(" > Run-time: {}".format(time.time() - t_1))
if figures:
visualize(alignment, mel_postnet_spec, stop_tokens, text, ap.hop_length, CONFIG, mel_spec)
IPython.display.display(Audio(waveform, rate=CONFIG.audio['sample_rate']))
os.makedirs(OUT_FOLDER, exist_ok=True)
file_name = text.replace(" ", "_").replace(".","") + ".wav"
out_path = os.path.join(OUT_FOLDER, file_name)
ap.save_wav(waveform, out_path)
return alignment, mel_postnet_spec, stop_tokens, waveformIn [9]:
# Set constants
ROOT_PATH = '/media/erogol/data_ssd/Models/libri_tts/5049/'
MODEL_PATH = ROOT_PATH + 'best_model.pth.tar'
CONFIG_PATH = ROOT_PATH + '/config.json'
OUT_FOLDER = "/home/erogol/Dropbox/AudioSamples/benchmark_samples/"
CONFIG = load_config(CONFIG_PATH)
VOCODER_MODEL_PATH = "/media/erogol/data_ssd/Data/models/wavernn/mozilla/mozilla-May24-4763/model_checkpoints/best_model.pth.tar"
VOCODER_CONFIG_PATH = "/media/erogol/data_ssd/Data/models/wavernn/mozilla/mozilla-May24-4763/config.json"
VOCODER_CONFIG = load_config(VOCODER_CONFIG_PATH)
use_cuda = False
# Set some config fields manually for testing
# CONFIG.windowing = False
# CONFIG.prenet_dropout = False
# CONFIG.separate_stopnet = True
# CONFIG.stopnet = True
# Set the vocoder
use_gl = True # use GL if True
batched_wavernn = True # use batched wavernn inference if True[0;31m---------------------------------------------------------------------------[0m [0;31mFileNotFoundError[0m Traceback (most recent call last) [0;32m<ipython-input-9-3306702a6bbc>[0m in [0;36m<module>[0;34m[0m [1;32m 7[0m [0mVOCODER_MODEL_PATH[0m [0;34m=[0m [0;34m"/media/erogol/data_ssd/Data/models/wavernn/mozilla/mozilla-May24-4763/model_checkpoints/best_model.pth.tar"[0m[0;34m[0m[0;34m[0m[0m [1;32m 8[0m [0mVOCODER_CONFIG_PATH[0m [0;34m=[0m [0;34m"/media/erogol/data_ssd/Data/models/wavernn/mozilla/mozilla-May24-4763/config.json"[0m[0;34m[0m[0;34m[0m[0m [0;32m----> 9[0;31m [0mVOCODER_CONFIG[0m [0;34m=[0m [0mload_config[0m[0;34m([0m[0mVOCODER_CONFIG_PATH[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 10[0m [0muse_cuda[0m [0;34m=[0m [0;32mFalse[0m[0;34m[0m[0;34m[0m[0m [1;32m 11[0m [0;34m[0m[0m [0;32m~/projects/TTS/tts_namespace/TTS/utils/generic_utils.py[0m in [0;36mload_config[0;34m(config_path)[0m [1;32m 20[0m [0;32mdef[0m [0mload_config[0m[0;34m([0m[0mconfig_path[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [1;32m 21[0m [0mconfig[0m [0;34m=[0m [0mAttrDict[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0;32m---> 22[0;31m [0;32mwith[0m [0mopen[0m[0;34m([0m[0mconfig_path[0m[0;34m,[0m [0;34m"r"[0m[0;34m)[0m [0;32mas[0m [0mf[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 23[0m [0minput_str[0m [0;34m=[0m [0mf[0m[0;34m.[0m[0mread[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [1;32m 24[0m [0minput_str[0m [0;34m=[0m [0mre[0m[0;34m.[0m[0msub[0m[0;34m([0m[0;34mr'\\\n'[0m[0;34m,[0m [0;34m''[0m[0;34m,[0m [0minput_str[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0;31mFileNotFoundError[0m: [Errno 2] No such file or directory: '/media/erogol/data_ssd/Data/models/wavernn/mozilla/mozilla-May24-4763/config.json'
In [ ]:
# LOAD TTS MODEL
from utils.text.symbols import symbols, phonemes
# load the model
num_chars = len(phonemes) if CONFIG.use_phonemes else len(symbols)
model = setup_model(num_chars, CONFIG)
# load the audio processor
ap = AudioProcessor(**CONFIG.audio)
# load model state
if use_cuda:
cp = torch.load(MODEL_PATH)
else:
cp = torch.load(MODEL_PATH, map_location=lambda storage, loc: storage)
# load the model
model.load_state_dict(cp['model'])
if use_cuda:
model.cuda()
model.eval()
print(cp['step'])In [ ]:
# LOAD WAVERNN
if use_gl == False:
from WaveRNN.models.wavernn import Model
bits = 10
wavernn = Model(
rnn_dims=512,
fc_dims=512,
mode="mold",
pad=2,
upsample_factors=VOCODER_CONFIG.upsample_factors, # set this depending on dataset
feat_dims=VOCODER_CONFIG.audio["num_mels"],
compute_dims=128,
res_out_dims=128,
res_blocks=10,
hop_length=ap.hop_length,
sample_rate=ap.sample_rate,
).cuda()
check = torch.load(VOCODER_MODEL_PATH)
wavernn.load_state_dict(check['model'])
if use_cuda:
wavernn.cuda()
wavernn.eval();
print(check['step'])In [5]:
model.eval()
model.decoder.max_decoder_steps = 2000
speaker_id = 0
sentence = "Bill got in the habit of asking himself “Is that thought true?” And if he wasn’t absolutely certain it was, he just let it go."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) [0;32m<ipython-input-5-e285d5bde9fb>[0m in [0;36m<module>[0;34m[0m [0;32m----> 1[0;31m [0mmodel[0m[0;34m.[0m[0meval[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 2[0m [0mmodel[0m[0;34m.[0m[0mdecoder[0m[0;34m.[0m[0mmax_decoder_steps[0m [0;34m=[0m [0;36m2000[0m[0;34m[0m[0;34m[0m[0m [1;32m 3[0m [0mspeaker_id[0m [0;34m=[0m [0;36m0[0m[0;34m[0m[0;34m[0m[0m [1;32m 4[0m [0msentence[0m [0;34m=[0m [0;34m"Bill got in the habit of asking himself “Is that thought true?” And if he wasn’t absolutely certain it was, he just let it go."[0m[0;34m[0m[0;34m[0m[0m [1;32m 5[0m [0malign[0m[0;34m,[0m [0mspec[0m[0;34m,[0m [0mstop_tokens[0m[0;34m,[0m [0mwav[0m [0;34m=[0m [0mtts[0m[0;34m([0m[0mmodel[0m[0;34m,[0m [0msentence[0m[0;34m,[0m [0mCONFIG[0m[0;34m,[0m [0muse_cuda[0m[0;34m,[0m [0map[0m[0;34m,[0m [0mspeaker_id[0m[0;34m=[0m[0mspeaker_id[0m[0;34m,[0m [0muse_gl[0m[0;34m=[0m[0muse_gl[0m[0;34m,[0m [0mfigures[0m[0;34m=[0m[0;32mTrue[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0;31mNameError[0m: name 'model' is not defined
In [6]:
sentence = "Be a voice, not an echo." # 'echo' is not in training set.
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) [0;32m<ipython-input-6-621056ffa667>[0m in [0;36m<module>[0;34m[0m [1;32m 1[0m [0msentence[0m [0;34m=[0m [0;34m"Be a voice, not an echo."[0m [0;31m# 'echo' is not in training set.[0m[0;34m[0m[0;34m[0m[0m [0;32m----> 2[0;31m [0malign[0m[0;34m,[0m [0mspec[0m[0;34m,[0m [0mstop_tokens[0m[0;34m,[0m [0mwav[0m [0;34m=[0m [0mtts[0m[0;34m([0m[0mmodel[0m[0;34m,[0m [0msentence[0m[0;34m,[0m [0mCONFIG[0m[0;34m,[0m [0muse_cuda[0m[0;34m,[0m [0map[0m[0;34m,[0m [0mspeaker_id[0m[0;34m=[0m[0mspeaker_id[0m[0;34m,[0m [0muse_gl[0m[0;34m=[0m[0muse_gl[0m[0;34m,[0m [0mfigures[0m[0;34m=[0m[0;32mTrue[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m [0;31mNameError[0m: name 'model' is not defined
In [7]:
sentence = "The human voice is the most perfect instrument of all."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) [0;32m<ipython-input-7-26967668a1a1>[0m in [0;36m<module>[0;34m[0m [1;32m 1[0m [0msentence[0m [0;34m=[0m [0;34m"The human voice is the most perfect instrument of all."[0m[0;34m[0m[0;34m[0m[0m [0;32m----> 2[0;31m [0malign[0m[0;34m,[0m [0mspec[0m[0;34m,[0m [0mstop_tokens[0m[0;34m,[0m [0mwav[0m [0;34m=[0m [0mtts[0m[0;34m([0m[0mmodel[0m[0;34m,[0m [0msentence[0m[0;34m,[0m [0mCONFIG[0m[0;34m,[0m [0muse_cuda[0m[0;34m,[0m [0map[0m[0;34m,[0m [0mspeaker_id[0m[0;34m=[0m[0mspeaker_id[0m[0;34m,[0m [0muse_gl[0m[0;34m=[0m[0muse_gl[0m[0;34m,[0m [0mfigures[0m[0;34m=[0m[0;32mTrue[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m [0;31mNameError[0m: name 'model' is not defined
In [8]:
sentence = "I'm sorry Dave. I'm afraid I can't do that."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) [0;32m<ipython-input-8-28cb5023e353>[0m in [0;36m<module>[0;34m[0m [1;32m 1[0m [0msentence[0m [0;34m=[0m [0;34m"I'm sorry Dave. I'm afraid I can't do that."[0m[0;34m[0m[0;34m[0m[0m [0;32m----> 2[0;31m [0malign[0m[0;34m,[0m [0mspec[0m[0;34m,[0m [0mstop_tokens[0m[0;34m,[0m [0mwav[0m [0;34m=[0m [0mtts[0m[0;34m([0m[0mmodel[0m[0;34m,[0m [0msentence[0m[0;34m,[0m [0mCONFIG[0m[0;34m,[0m [0muse_cuda[0m[0;34m,[0m [0map[0m[0;34m,[0m [0mspeaker_id[0m[0;34m=[0m[0mspeaker_id[0m[0;34m,[0m [0muse_gl[0m[0;34m=[0m[0muse_gl[0m[0;34m,[0m [0mfigures[0m[0;34m=[0m[0;32mTrue[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m [0;31mNameError[0m: name 'model' is not defined
In [ ]:
sentence = "This cake is great. It's so delicious and moist."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Generative adversarial network or variational auto-encoder."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Scientists at the CERN laboratory say they have discovered a new particle."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Here’s a way to measure the acute emotional intelligence that has never gone out of style."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "President Trump met with other leaders at the Group of 20 conference."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "The buses aren't the problem, they actually provide a solution."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Generative adversarial network or variational auto-encoder."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Basilar membrane and otolaryngology are not auto-correlations."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = " He has read the whole thing."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "He reads books."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Thisss isrealy awhsome."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "This is your internet browser, Firefox."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "This is your internet browser Firefox."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "The quick brown fox jumps over the lazy dog."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Does the quick brown fox jump over the lazy dog?"
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Eren, how are you?"
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Encouraged, he started with a minute a day."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "His meditation consisted of “body scanning” which involved focusing his mind and energy on each section of the body from head to toe ."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Recent research at Harvard has shown meditating for as little as 8 weeks can actually increase the grey matter in the parts of the brain responsible for emotional regulation and learning . "
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "If he decided to watch TV he really watched it."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
sentence = "Often we try to bring about change through sheer effort and we put all of our energy into a new initiative ."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
# for twb dataset
sentence = "In our preparation for Easter, God in his providence offers us each year the season of Lent as a sacramental sign of our conversion."
align, spec, stop_tokens, wav = tts(model, sentence, CONFIG, use_cuda, ap, speaker_id=speaker_id, use_gl=use_gl, figures=True)In [ ]:
# !zip benchmark_samples/samples.zip benchmark_samples/*