synthesis.py 9.8 KB
Newer Older
L
lifuchen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
L
lifuchen 已提交
14 15 16 17
import os
from scipy.io.wavfile import write
import numpy as np
from tqdm import tqdm
18
from matplotlib import cm
L
lifuchen 已提交
19
from tensorboardX import SummaryWriter
L
lifuchen 已提交
20
from ruamel import yaml
L
lifuchen 已提交
21
from pathlib import Path
L
lifuchen 已提交
22
import argparse
L
lifuchen 已提交
23
from pprint import pprint
24 25 26
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
from parakeet.g2p.en import text_to_sequence
27
from parakeet.models.transformer_tts.utils import *
L
lifuchen 已提交
28
from parakeet import audio
29 30
from parakeet.models.transformer_tts import Vocoder
from parakeet.models.transformer_tts import TransformerTTS
31 32 33 34
from parakeet.modules import weight_norm
from parakeet.models.waveflow import WaveFlowModule
from parakeet.modules.weight_norm import WeightNormWrapper
from parakeet.models.wavenet import UpsampleNet, WaveNet, ConditionalWavenet
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
from parakeet.utils import io


def add_config_options_to_parser(parser):
    parser.add_argument("--config", type=str, help="path of the config file")
    parser.add_argument("--use_gpu", type=int, default=0, help="device to use")
    parser.add_argument(
        "--max_len",
        type=int,
        default=200,
        help="The max length of audio when synthsis.")

    parser.add_argument(
        "--checkpoint_transformer",
        type=str,
        help="transformer_tts checkpoint to synthesis")
51 52 53 54 55 56 57 58
    parser.add_argument(
        "--vocoder",
        type=str,
        default="griffinlim",
        choices=['griffinlim', 'wavenet', 'waveflow'],
        help="vocoder method")
    parser.add_argument(
        "--config_vocoder", type=str, help="path of the vocoder config file")
59 60 61 62 63 64 65 66 67 68
    parser.add_argument(
        "--checkpoint_vocoder",
        type=str,
        help="vocoder checkpoint to synthesis")

    parser.add_argument(
        "--output",
        type=str,
        default="synthesis",
        help="path to save experiment results")
L
lifuchen 已提交
69

L
lifuchen 已提交
70

L
lifuchen 已提交
71
def synthesis(text_input, args):
72 73
    local_rank = dg.parallel.Env().local_rank
    place = (fluid.CUDAPlace(local_rank) if args.use_gpu else fluid.CPUPlace())
L
lifuchen 已提交
74

75
    with open(args.config) as f:
L
lifuchen 已提交
76
        cfg = yaml.load(f, Loader=yaml.Loader)
L
lifuchen 已提交
77 78

    # tensorboard
79 80
    if not os.path.exists(args.output):
        os.mkdir(args.output)
L
lifuchen 已提交
81

82
    writer = SummaryWriter(os.path.join(args.output, 'log'))
L
lifuchen 已提交
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    fluid.enable_dygraph(place)
    with fluid.unique_name.guard():
        network_cfg = cfg['network']
        model = TransformerTTS(
            network_cfg['embedding_size'], network_cfg['hidden_size'],
            network_cfg['encoder_num_head'], network_cfg['encoder_n_layers'],
            cfg['audio']['num_mels'], network_cfg['outputs_per_step'],
            network_cfg['decoder_num_head'], network_cfg['decoder_n_layers'])
        # Load parameters.
        global_step = io.load_parameters(
            model=model, checkpoint_path=args.checkpoint_transformer)
        model.eval()

    # init input
    text = np.asarray(text_to_sequence(text_input))
99
    text = fluid.layers.unsqueeze(dg.to_variable(text).astype(np.int64), [0])
100 101
    mel_input = dg.to_variable(np.zeros([1, 1, 80])).astype(np.float32)
    pos_text = np.arange(1, text.shape[1] + 1)
102 103
    pos_text = fluid.layers.unsqueeze(
        dg.to_variable(pos_text).astype(np.int64), [0])
104 105 106 107

    pbar = tqdm(range(args.max_len))
    for i in pbar:
        pos_mel = np.arange(1, mel_input.shape[1] + 1)
108 109
        pos_mel = fluid.layers.unsqueeze(
            dg.to_variable(pos_mel).astype(np.int64), [0])
110 111 112 113
        mel_pred, postnet_pred, attn_probs, stop_preds, attn_enc, attn_dec = model(
            text, mel_input, pos_text, pos_mel)
        mel_input = fluid.layers.concat(
            [mel_input, postnet_pred[:, -1:, :]], axis=1)
114 115 116 117 118 119 120 121 122
    global_step = 0
    for i, prob in enumerate(attn_probs):
        for j in range(4):
            x = np.uint8(cm.viridis(prob.numpy()[j]) * 255)
            writer.add_image(
                'Attention_%d_0' % global_step,
                x,
                i * 4 + j,
                dataformats="HWC")
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

    _ljspeech_processor = audio.AudioProcessor(
        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'],
        signal_norm=True,
        symmetric_norm=False,
        max_norm=1.,
        mel_fmin=0,
138
        mel_fmax=8000,
139 140 141 142 143
        clip_norm=True,
        griffin_lim_iters=60,
        do_trim_silence=False,
        sound_norm=False)

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    if args.vocoder == 'griffinlim':
        #synthesis use griffin-lim
        wav = synthesis_with_griffinlim(postnet_pred, _ljspeech_processor)
    elif args.vocoder == 'wavenet':
        # synthesis use wavenet
        wav = synthesis_with_wavenet(postnet_pred, args)
    elif args.vocoder == 'waveflow':
        # synthesis use waveflow
        wav = synthesis_with_waveflow(postnet_pred, args,
                                      args.checkpoint_vocoder,
                                      _ljspeech_processor, place)
    else:
        print(
            'vocoder error, we only support griffinlim, cbhg and waveflow, but recevied %s.'
            % args.vocoder)
159

160 161
    writer.add_audio(text_input + '(' + args.vocoder + ')', wav, 0,
                     cfg['audio']['sr'])
162 163 164
    if not os.path.exists(os.path.join(args.output, 'samples')):
        os.mkdir(os.path.join(args.output, 'samples'))
    write(
165 166
        os.path.join(
            os.path.join(args.output, 'samples'), args.vocoder + '.wav'),
167
        cfg['audio']['sr'], wav)
168 169 170
    print("Synthesis completed !!!")
    writer.close()

171

172
def synthesis_with_griffinlim(mel_output, _ljspeech_processor):
173
    # synthesis with griffin-lim
174 175 176 177 178 179
    mel_output = fluid.layers.transpose(
        fluid.layers.squeeze(mel_output, [0]), [1, 0])
    mel_output = np.exp(mel_output.numpy())
    basis = librosa.filters.mel(22050, 1024, 80, fmin=0, fmax=8000)
    inv_basis = np.linalg.pinv(basis)
    spec = np.maximum(1e-10, np.dot(inv_basis, mel_output))
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    wav = librosa.core.griffinlim(spec**1.2, hop_length=256, win_length=1024)

    return wav


def synthesis_with_wavenet(mel_output, args):
    with open(args.config_vocoder, 'rt') as f:
        config = yaml.safe_load(f)
    n_mels = config["data"]["n_mels"]
    model_config = config["model"]
    filter_size = model_config["filter_size"]
    upsampling_factors = model_config["upsampling_factors"]
    encoder = UpsampleNet(upsampling_factors)

    n_loop = model_config["n_loop"]
    n_layer = model_config["n_layer"]
    residual_channels = model_config["residual_channels"]
    output_dim = model_config["output_dim"]
    loss_type = model_config["loss_type"]
    log_scale_min = model_config["log_scale_min"]
    decoder = WaveNet(n_loop, n_layer, residual_channels, output_dim, n_mels,
                      filter_size, loss_type, log_scale_min)

    model = ConditionalWavenet(encoder, decoder)

    # load model parameters
    iteration = io.load_parameters(
        model, checkpoint_path=args.checkpoint_vocoder)

    for layer in model.sublayers():
        if isinstance(layer, WeightNormWrapper):
            layer.remove_weight_norm()
    mel_output = fluid.layers.transpose(mel_output, [0, 2, 1])
    wav = model.synthesis(mel_output)
    return wav.numpy()[0]


def synthesis_with_cbhg(mel_output, _ljspeech_processor, cfg):
    with fluid.unique_name.guard():
        model_vocoder = Vocoder(
            cfg['train']['batch_size'], cfg['vocoder']['hidden_size'],
            cfg['audio']['num_mels'], cfg['audio']['n_fft'])
        # Load parameters.
        global_step = io.load_parameters(
            model=model_vocoder, checkpoint_path=args.checkpoint_vocoder)
        model_vocoder.eval()
    mag_pred = model_vocoder(mel_output)
    # synthesis with cbhg
    wav = _ljspeech_processor.inv_spectrogram(
        fluid.layers.transpose(fluid.layers.squeeze(mag_pred, [0]), [1, 0])
        .numpy())
    return wav


def synthesis_with_waveflow(mel_output, args, checkpoint, _ljspeech_processor,
                            place):
    mel_output = fluid.layers.transpose(
        fluid.layers.squeeze(mel_output, [0]), [1, 0])
    mel_output = mel_output.numpy()
    #mel_output = (mel_output - mel_output.min())/(mel_output.max() - mel_output.min())
    #mel_output = 5 * mel_output - 4
    #mel_output = np.log(10) * mel_output

    fluid.enable_dygraph(place)
    args.config = args.config_vocoder
    args.use_fp16 = False
    config = io.add_yaml_config_to_args(args)

    mel_spectrogram = dg.to_variable(mel_output)
    mel_spectrogram = fluid.layers.unsqueeze(mel_spectrogram, [0])

    # Build model.
    waveflow = WaveFlowModule(config)
    io.load_parameters(model=waveflow, checkpoint_path=checkpoint)
    for layer in waveflow.sublayers():
        if isinstance(layer, weight_norm.WeightNormWrapper):
            layer.remove_weight_norm()

    # Run model inference.
    wav = waveflow.synthesize(mel_spectrogram, sigma=config.sigma)
    return wav.numpy()[0]
L
lifuchen 已提交
262

L
lifuchen 已提交
263

L
lifuchen 已提交
264
if __name__ == '__main__':
L
lifuchen 已提交
265
    parser = argparse.ArgumentParser(description="Synthesis model")
L
lifuchen 已提交
266
    add_config_options_to_parser(parser)
L
lifuchen 已提交
267
    args = parser.parse_args()
268 269
    # Print the whole config setting.
    pprint(vars(args))
270 271 272
    synthesis(
        "Life was like a box of chocolates, you never know what you're gonna get.",
        args)