train.py 3.9 KB
Newer Older
L
lifuchen 已提交
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 16 17 18 19 20
import os
import random
import subprocess
import time
from pprint import pprint

21
import argparse
22 23 24
import numpy as np
import paddle.fluid.dygraph as dg
from paddle import fluid
走神的阿圆's avatar
走神的阿圆 已提交
25 26
from visualdl import LogWriter

27 28

import utils
L
liuyibing01 已提交
29
from parakeet.utils import io
L
liuyibing01 已提交
30
from waveflow import WaveFlow
31 32 33


def add_options_to_parser(parser):
34 35 36 37
    parser.add_argument(
        '--model',
        type=str,
        default='waveflow',
38
        help="general name of the model")
39 40 41 42 43 44 45 46 47
    parser.add_argument(
        '--name', type=str, help="specific name of the training model")
    parser.add_argument(
        '--root', type=str, help="root path of the LJSpeech dataset")

    parser.add_argument(
        '--use_gpu',
        type=utils.str2bool,
        default=True,
48 49
        help="option to use gpu training")

50 51 52 53
    parser.add_argument(
        '--iteration',
        type=int,
        default=None,
54 55
        help=("which iteration of checkpoint to load, "
              "default to load the latest checkpoint"))
56 57 58 59
    parser.add_argument(
        '--checkpoint',
        type=str,
        default=None,
60 61 62 63 64 65 66
        help="path of the checkpoint to load")


def train(config):
    use_gpu = config.use_gpu

    # Get the rank of the current training process.
L
liuyibing01 已提交
67 68 69
    rank = dg.parallel.Env().local_rank
    nranks = dg.parallel.Env().nranks
    parallel = nranks > 1
70 71 72

    if rank == 0:
        # Print the whole config setting.
73
        pprint(vars(config))
74 75 76 77

    # Make checkpoint directory.
    run_dir = os.path.join("runs", config.model, config.name)
    checkpoint_dir = os.path.join(run_dir, "checkpoint")
78 79
    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)
80 81

    # Create tensorboard logger.
走神的阿圆's avatar
走神的阿圆 已提交
82 83
    vdl = LogWriter(os.path.join(run_dir, "logs")) \
          if rank == 0 else None
84 85 86 87 88 89 90 91 92 93 94 95 96 97

    # Configurate device
    place = fluid.CUDAPlace(rank) if use_gpu else fluid.CPUPlace()

    with dg.guard(place):
        # Fix random seed.
        seed = config.seed
        random.seed(seed)
        np.random.seed(seed)
        fluid.default_startup_program().random_seed = seed
        fluid.default_main_program().random_seed = seed
        print("Random Seed: ", seed)

        # Build model.
走神的阿圆's avatar
走神的阿圆 已提交
98
        model = WaveFlow(config, checkpoint_dir, parallel, rank, nranks, vdl)
L
liuyibing01 已提交
99
        iteration = model.build()
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

        while iteration < config.max_iterations:
            # Run one single training step.
            model.train_step(iteration)

            iteration += 1

            if iteration % config.test_every == 0:
                # Run validation step.
                model.valid_step(iteration)

            if rank == 0 and iteration % config.save_every == 0:
                # Save parameters.
                model.save(iteration)

    # Close TensorBoard.
    if rank == 0:
走神的阿圆's avatar
走神的阿圆 已提交
117
        vdl.close()
118 119 120 121


if __name__ == "__main__":
    # Create parser.
122 123
    parser = argparse.ArgumentParser(description="Train WaveFlow model")
    #formatter_class='default_argparse')
124 125 126 127 128 129 130
    add_options_to_parser(parser)
    utils.add_config_options_to_parser(parser)

    # Parse argument from both command line and yaml config file.
    # For conflicting updates to the same field, 
    # the preceding update will be overwritten by the following one.
    config = parser.parse_args()
L
liuyibing01 已提交
131
    config = io.add_yaml_config_to_args(config)
132 133
    # Force to use fp32 in model training
    vars(config)["use_fp16"] = False
134
    train(config)