ParakeetEricRoss/examples/fastspeech/synthesis.py

80 lines
2.9 KiB
Python
Raw Normal View History

2020-01-22 15:46:35 +08:00
import os
from tensorboardX import SummaryWriter
from collections import OrderedDict
2020-02-13 14:48:21 +08:00
import argparse
2020-01-22 15:46:35 +08:00
from parse import add_config_options_to_parser
from pprint import pprint
2020-02-13 14:48:21 +08:00
from ruamel import yaml
2020-01-22 15:46:35 +08:00
import numpy as np
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
from parakeet.g2p.en import text_to_sequence
from parakeet import audio
2020-02-13 14:48:21 +08:00
from parakeet.models.fastspeech.fastspeech import FastSpeech
2020-01-22 15:46:35 +08:00
def load_checkpoint(step, model_path):
model_dict, _ = fluid.dygraph.load_dygraph(os.path.join(model_path, step))
new_state_dict = OrderedDict()
for param in model_dict:
if param.startswith('_layers.'):
new_state_dict[param[8:]] = model_dict[param]
else:
new_state_dict[param] = model_dict[param]
return new_state_dict
2020-02-13 14:48:21 +08:00
def synthesis(text_input, args):
place = (fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace())
2020-01-22 15:46:35 +08:00
# tensorboard
2020-02-13 14:48:21 +08:00
if not os.path.exists(args.log_dir):
os.mkdir(args.log_dir)
path = os.path.join(args.log_dir,'synthesis')
with open(args.config_path) as f:
cfg = yaml.load(f, Loader=yaml.Loader)
2020-01-22 15:46:35 +08:00
writer = SummaryWriter(path)
with dg.guard(place):
model = FastSpeech(cfg)
2020-02-13 14:48:21 +08:00
model.set_dict(load_checkpoint(str(args.fastspeech_step), os.path.join(args.checkpoint_path, "fastspeech")))
2020-01-22 15:46:35 +08:00
model.eval()
text = np.asarray(text_to_sequence(text_input))
text = fluid.layers.unsqueeze(dg.to_variable(text),[0])
pos_text = np.arange(1, text.shape[1]+1)
pos_text = fluid.layers.unsqueeze(dg.to_variable(pos_text),[0])
2020-02-13 14:48:21 +08:00
mel_output, mel_output_postnet = model(text, pos_text, alpha=args.alpha)
2020-01-22 15:46:35 +08:00
_ljspeech_processor = audio.AudioProcessor(
2020-02-13 14:48:21 +08:00
sample_rate=cfg['audio']['sr'],
num_mels=cfg['audio']['num_mels'],
min_level_db=cfg['audio']['min_level_db'],
ref_level_db=cfg['audio']['ref_level_db'],
n_fft=cfg['audio']['n_fft'],
win_length= cfg['audio']['win_length'],
hop_length= cfg['audio']['hop_length'],
power=cfg['audio']['power'],
preemphasis=cfg['audio']['preemphasis'],
2020-01-22 15:46:35 +08:00
signal_norm=True,
symmetric_norm=False,
max_norm=1.,
mel_fmin=0,
mel_fmax=None,
clip_norm=True,
griffin_lim_iters=60,
do_trim_silence=False,
sound_norm=False)
mel_output_postnet = fluid.layers.transpose(fluid.layers.squeeze(mel_output_postnet,[0]), [1,0])
wav = _ljspeech_processor.inv_melspectrogram(mel_output_postnet.numpy())
2020-02-13 14:48:21 +08:00
writer.add_audio(text_input, wav, 0, cfg['audio']['sr'])
2020-01-22 15:46:35 +08:00
print("Synthesis completed !!!")
writer.close()
if __name__ == '__main__':
2020-02-13 14:48:21 +08:00
parser = argparse.ArgumentParser(description="Train Fastspeech model")
2020-01-22 15:46:35 +08:00
add_config_options_to_parser(parser)
2020-02-13 14:48:21 +08:00
args = parser.parse_args()
synthesis("Transformer model is so fast!", args)