train.py 5.1 KB
Newer Older
1
"""Trainer for DeepSpeech2 model."""
2 3 4 5
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

6
import argparse
X
Xinghai Sun 已提交
7
import functools
8
import paddle.v2 as paddle
9
from model_utils.model import DeepSpeech2Model
10
from data_utils.data import DataGenerator
11
from utils.utility import add_arguments, print_arguments
X
Xinghai Sun 已提交
12

13
parser = argparse.ArgumentParser(description=__doc__)
X
Xinghai Sun 已提交
14 15
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
16 17 18
add_arg('batch_size',       int,    256,    "Minibatch size.")
add_arg('trainer_count',    int,    8,      "# of Trainers (CPUs or GPUs).")
add_arg('num_passes',       int,    200,    "# of training epochs.")
19
add_arg('num_proc_data',    int,    12,     "# of CPUs for data preprocessing.")
20 21 22
add_arg('num_conv_layers',  int,    2,      "# of convolution layers.")
add_arg('num_rnn_layers',   int,    3,      "# of recurrent layers.")
add_arg('rnn_layer_size',   int,    2048,   "# of recurrent cells per layer.")
23 24 25 26 27
add_arg('num_iter_print',   int,    100,    "Every # iterations for printing "
                                            "train cost.")
add_arg('learning_rate',    float,  5e-4,   "Learning rate.")
add_arg('max_duration',     float,  27.0,   "Longest audio duration allowed.")
add_arg('min_duration',     float,  0.0,    "Shortest audio duration allowed.")
28
add_arg('test_off',         bool,   False,  "Turn off testing.")
29 30
add_arg('use_sortagrad',    bool,   True,   "Use SortaGrad or not.")
add_arg('use_gpu',          bool,   True,   "Use GPU or not.")
31
add_arg('use_gru',          bool,   False,  "Use GRUs instead of simple RNNs.")
32
add_arg('is_local',         bool,   True,   "Use pserver or not.")
33 34 35
add_arg('share_rnn_weights',bool,   True,   "Share input-hidden weights across "
                                            "bi-directional RNNs. Not for GRU.")
add_arg('train_manifest',   str,
36
        'data/librispeech/manifest.train',
37 38
        "Filepath of train manifest.")
add_arg('dev_manifest',     str,
39
        'data/librispeech/manifest.dev-clean',
40 41
        "Filepath of validation manifest.")
add_arg('mean_std_path',    str,
42
        'data/librispeech/mean_std.npz',
43 44
        "Filepath of normalizer's mean & std.")
add_arg('vocab_path',       str,
45
        'data/librispeech/vocab.txt',
46 47 48 49 50 51
        "Filepath of vocabulary.")
add_arg('init_model_path',  str,
        None,
        "If None, the training starts from scratch, "
        "otherwise, it resumes from the pre-trained model.")
add_arg('output_model_dir', str,
52
        "./checkpoints/libri",
53
        "Directory for saving checkpoints.")
54 55 56 57 58 59 60 61 62 63 64
add_arg('augment_conf_path',str,
        'conf/augmentation.config',
        "Filepath of augmentation configuration file (json-format).")
add_arg('specgram_type',    str,
        'linear',
        "Audio feature type. Options: linear, mfcc.",
        choices=['linear', 'mfcc'])
add_arg('shuffle_method',   str,
        'batch_shuffle_clipped',
        "Shuffle method.",
        choices=['instance_shuffle', 'batch_shuffle', 'batch_shuffle_clipped'])
65
# yapf: disable
X
Xinghai Sun 已提交
66
args = parser.parse_args()
67 68 69


def train():
70
    """DeepSpeech2 training."""
71
    train_generator = DataGenerator(
72 73 74
        vocab_filepath=args.vocab_path,
        mean_std_filepath=args.mean_std_path,
        augmentation_config=open(args.augment_conf_path, 'r').read(),
75 76 77
        max_duration=args.max_duration,
        min_duration=args.min_duration,
        specgram_type=args.specgram_type,
78
        num_threads=args.num_proc_data)
79
    dev_generator = DataGenerator(
80 81
        vocab_filepath=args.vocab_path,
        mean_std_filepath=args.mean_std_path,
82 83
        augmentation_config="{}",
        specgram_type=args.specgram_type,
84
        num_threads=args.num_proc_data)
85
    train_batch_reader = train_generator.batch_reader_creator(
86
        manifest_path=args.train_manifest,
87
        batch_size=args.batch_size,
88
        min_batch_size=args.trainer_count,
89
        sortagrad=args.use_sortagrad if args.init_model_path is None else False,
90
        shuffle_method=args.shuffle_method)
91
    dev_batch_reader = dev_generator.batch_reader_creator(
92
        manifest_path=args.dev_manifest,
93
        batch_size=args.batch_size,
94
        min_batch_size=1,  # must be 1, but will have errors.
95
        sortagrad=False,
96
        shuffle_method=None)
97

98 99 100 101 102
    ds2_model = DeepSpeech2Model(
        vocab_size=train_generator.vocab_size,
        num_conv_layers=args.num_conv_layers,
        num_rnn_layers=args.num_rnn_layers,
        rnn_layer_size=args.rnn_layer_size,
X
Xinghai Sun 已提交
103
        use_gru=args.use_gru,
104
        pretrained_model_path=args.init_model_path,
105
        share_rnn_weights=args.share_rnn_weights)
106 107 108 109
    ds2_model.train(
        train_batch_reader=train_batch_reader,
        dev_batch_reader=dev_batch_reader,
        feeding_dict=train_generator.feeding,
110
        learning_rate=args.learning_rate,
111
        gradient_clipping=400,
112
        num_passes=args.num_passes,
113
        num_iterations_print=args.num_iter_print,
114
        output_model_dir=args.output_model_dir,
115 116
        is_local=args.is_local,
        test_off=args.test_off)
117 118 119


def main():
120
    print_arguments(args)
121
    paddle.init(use_gpu=args.use_gpu, trainer_count=args.trainer_count)
122 123 124 125 126
    train()


if __name__ == '__main__':
    main()