train.py 7.2 KB
Newer Older
C
chenfeiyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

15
from __future__ import division
C
chenfeiyu 已提交
16 17 18
import os
import ruamel.yaml
import argparse
19
import tqdm
C
chenfeiyu 已提交
20 21
from tensorboardX import SummaryWriter
from paddle import fluid
22
fluid.require_version('1.8.0')
C
chenfeiyu 已提交
23 24
import paddle.fluid.dygraph as dg

25
from parakeet.data import SliceDataset, TransformDataset, CacheDataset, DataCargo, SequentialSampler, RandomSampler
C
chenfeiyu 已提交
26 27
from parakeet.models.wavenet import UpsampleNet, WaveNet, ConditionalWavenet
from parakeet.utils.layer_tools import summary
28
from parakeet.utils import io
C
chenfeiyu 已提交
29 30

from data import LJSpeechMetaData, Transform, DataCollector
31
from utils import make_output_tree, valid_model
C
chenfeiyu 已提交
32 33 34

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
35
        description="Train a WaveNet model with LJSpeech.")
C
chenfeiyu 已提交
36
    parser.add_argument(
C
chenfeiyu 已提交
37 38 39 40 41 42 43 44 45 46 47
        "--data", type=str, help="path of the LJspeech dataset")
    parser.add_argument("--config", type=str, help="path of the config file")
    parser.add_argument("--device", type=int, default=-1, help="device to use")

    g = parser.add_mutually_exclusive_group()
    g.add_argument("--checkpoint", type=str, help="checkpoint to resume from")
    g.add_argument(
        "--iteration",
        type=int,
        help="the iteration of the checkpoint to load from output directory")

C
chenfeiyu 已提交
48
    parser.add_argument(
C
chenfeiyu 已提交
49
        "output", type=str, default="experiment", help="path to save results")
C
chenfeiyu 已提交
50 51 52 53 54

    args = parser.parse_args()
    with open(args.config, 'rt') as f:
        config = ruamel.yaml.safe_load(f)

55 56 57 58 59 60 61
    if args.device == -1:
        place = fluid.CPUPlace()
    else:
        place = fluid.CUDAPlace(args.device)

    dg.enable_dygraph(place)

62 63 64 65
    print("Command Line Args: ")
    for k, v in vars(args).items():
        print("{}: {}".format(k, v))

C
chenfeiyu 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
    ljspeech_meta = LJSpeechMetaData(args.data)

    data_config = config["data"]
    sample_rate = data_config["sample_rate"]
    n_fft = data_config["n_fft"]
    win_length = data_config["win_length"]
    hop_length = data_config["hop_length"]
    n_mels = data_config["n_mels"]
    train_clip_seconds = data_config["train_clip_seconds"]
    transform = Transform(sample_rate, n_fft, win_length, hop_length, n_mels)
    ljspeech = TransformDataset(ljspeech_meta, transform)

    valid_size = data_config["valid_size"]
79 80 81
    ljspeech_valid = CacheDataset(SliceDataset(ljspeech, 0, valid_size))
    ljspeech_train = CacheDataset(
        SliceDataset(ljspeech, valid_size, len(ljspeech)))
C
chenfeiyu 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    model_config = config["model"]
    n_loop = model_config["n_loop"]
    n_layer = model_config["n_layer"]
    filter_size = model_config["filter_size"]
    context_size = 1 + n_layer * sum([filter_size**i for i in range(n_loop)])
    print("context size is {} samples".format(context_size))
    train_batch_fn = DataCollector(context_size, sample_rate, hop_length,
                                   train_clip_seconds)
    valid_batch_fn = DataCollector(
        context_size, sample_rate, hop_length, train_clip_seconds, valid=True)

    batch_size = data_config["batch_size"]
    train_cargo = DataCargo(
        ljspeech_train,
        train_batch_fn,
        batch_size,
        sampler=RandomSampler(ljspeech_train))

    # only batch=1 for validation is enabled
    valid_cargo = DataCargo(
        ljspeech_valid,
        valid_batch_fn,
        batch_size=1,
        sampler=SequentialSampler(ljspeech_valid))

    make_output_tree(args.output)

    if args.device == -1:
        place = fluid.CPUPlace()
    else:
        place = fluid.CUDAPlace(args.device)

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    model_config = config["model"]
    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)
    summary(model)

    train_config = config["train"]
    learning_rate = train_config["learning_rate"]
    anneal_rate = train_config["anneal_rate"]
    anneal_interval = train_config["anneal_interval"]
    lr_scheduler = dg.ExponentialDecay(
        learning_rate, anneal_interval, anneal_rate, staircase=True)
    gradiant_max_norm = train_config["gradient_max_norm"]
    optim = fluid.optimizer.Adam(
        lr_scheduler,
        parameter_list=model.parameters(),
        grad_clip=fluid.clip.ClipByGlobalNorm(gradiant_max_norm))

    train_loader = fluid.io.DataLoader.from_generator(
        capacity=10, return_list=True)
    train_loader.set_batch_generator(train_cargo, place)

    valid_loader = fluid.io.DataLoader.from_generator(
        capacity=10, return_list=True)
    valid_loader.set_batch_generator(valid_cargo, place)

    max_iterations = train_config["max_iterations"]
    checkpoint_interval = train_config["checkpoint_interval"]
    snap_interval = train_config["snap_interval"]
    eval_interval = train_config["eval_interval"]
    checkpoint_dir = os.path.join(args.output, "checkpoints")
    log_dir = os.path.join(args.output, "log")
    writer = SummaryWriter(log_dir)

    # load parameters and optimizer, and update iterations done so far
    if args.checkpoint is not None:
        iteration = io.load_parameters(
            model, optim, checkpoint_path=args.checkpoint)
    else:
        iteration = io.load_parameters(
            model,
            optim,
            checkpoint_dir=checkpoint_dir,
            iteration=args.iteration)

    global_step = iteration + 1
    iterator = iter(tqdm.tqdm(train_loader))
    while global_step <= max_iterations:
        try:
            batch = next(iterator)
        except StopIteration as e:
            iterator = iter(tqdm.tqdm(train_loader))
            batch = next(iterator)

        audio_clips, mel_specs, audio_starts = batch

        model.train()
        y_var = model(audio_clips, mel_specs, audio_starts)
        loss_var = model.loss(y_var, audio_clips)
        loss_var.backward()
        loss_np = loss_var.numpy()

        writer.add_scalar("loss", loss_np[0], global_step)
        writer.add_scalar("learning_rate",
                          optim._learning_rate.step().numpy()[0], global_step)
        optim.minimize(loss_var)
        optim.clear_gradients()
        print("global_step: {}\tloss: {:<8.6f}".format(global_step, loss_np[
            0]))

        if global_step % snap_interval == 0:
            valid_model(model, valid_loader, writer, global_step, sample_rate)

        if global_step % checkpoint_interval == 0:
            io.save_parameters(checkpoint_dir, global_step, model, optim)

        global_step += 1