|
|
|
@ -9,169 +9,103 @@ import multiprocessing
|
|
|
|
|
import paddle.v2 as paddle
|
|
|
|
|
from model import DeepSpeech2Model
|
|
|
|
|
from data_utils.data import DataGenerator
|
|
|
|
|
import utils
|
|
|
|
|
|
|
|
|
|
NUM_CPU = multiprocessing.cpu_count() // 2
|
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--batch_size", default=256, type=int, help="Minibatch size.")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--num_passes",
|
|
|
|
|
default=200,
|
|
|
|
|
type=int,
|
|
|
|
|
help="Training pass number. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--num_iterations_print",
|
|
|
|
|
default=100,
|
|
|
|
|
type=int,
|
|
|
|
|
help="Number of iterations for every train cost printing. "
|
|
|
|
|
"(default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--num_conv_layers",
|
|
|
|
|
default=2,
|
|
|
|
|
type=int,
|
|
|
|
|
help="Convolution layer number. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--num_rnn_layers",
|
|
|
|
|
default=3,
|
|
|
|
|
type=int,
|
|
|
|
|
help="RNN layer number. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--rnn_layer_size",
|
|
|
|
|
default=2048,
|
|
|
|
|
type=int,
|
|
|
|
|
help="RNN layer cell number. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--share_rnn_weights",
|
|
|
|
|
default=True,
|
|
|
|
|
type=distutils.util.strtobool,
|
|
|
|
|
help="Whether to share input-hidden weights between forword and backward "
|
|
|
|
|
"directional simple RNNs. Only available when use_gru=False. "
|
|
|
|
|
"(default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--use_gru",
|
|
|
|
|
default=False,
|
|
|
|
|
type=distutils.util.strtobool,
|
|
|
|
|
help="Use GRU or simple RNN. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--adam_learning_rate",
|
|
|
|
|
default=5e-4,
|
|
|
|
|
type=float,
|
|
|
|
|
help="Learning rate for ADAM Optimizer. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--use_gpu",
|
|
|
|
|
default=True,
|
|
|
|
|
type=distutils.util.strtobool,
|
|
|
|
|
help="Use gpu or not. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--use_sortagrad",
|
|
|
|
|
default=True,
|
|
|
|
|
type=distutils.util.strtobool,
|
|
|
|
|
help="Use sortagrad or not. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--specgram_type",
|
|
|
|
|
default='linear',
|
|
|
|
|
type=str,
|
|
|
|
|
help="Feature type of audio data: 'linear' (power spectrum)"
|
|
|
|
|
" or 'mfcc'. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--max_duration",
|
|
|
|
|
default=27.0,
|
|
|
|
|
type=float,
|
|
|
|
|
help="Audios with duration larger than this will be discarded. "
|
|
|
|
|
"(default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--min_duration",
|
|
|
|
|
default=0.0,
|
|
|
|
|
type=float,
|
|
|
|
|
help="Audios with duration smaller than this will be discarded. "
|
|
|
|
|
"(default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--shuffle_method",
|
|
|
|
|
default='batch_shuffle_clipped',
|
|
|
|
|
type=str,
|
|
|
|
|
help="Shuffle method: 'instance_shuffle', 'batch_shuffle', "
|
|
|
|
|
"'batch_shuffle_batch'. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--trainer_count",
|
|
|
|
|
default=8,
|
|
|
|
|
type=int,
|
|
|
|
|
help="Trainer number. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--num_threads_data",
|
|
|
|
|
default=multiprocessing.cpu_count() // 2,
|
|
|
|
|
type=int,
|
|
|
|
|
help="Number of cpu threads for preprocessing data. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--mean_std_filepath",
|
|
|
|
|
default='mean_std.npz',
|
|
|
|
|
type=str,
|
|
|
|
|
help="Manifest path for normalizer. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--train_manifest_path",
|
|
|
|
|
default='datasets/manifest.train',
|
|
|
|
|
type=str,
|
|
|
|
|
help="Manifest path for training. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--dev_manifest_path",
|
|
|
|
|
default='datasets/manifest.dev',
|
|
|
|
|
type=str,
|
|
|
|
|
help="Manifest path for validation. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--vocab_filepath",
|
|
|
|
|
default='datasets/vocab/eng_vocab.txt',
|
|
|
|
|
type=str,
|
|
|
|
|
help="Vocabulary filepath. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--init_model_path",
|
|
|
|
|
default=None,
|
|
|
|
|
type=str,
|
|
|
|
|
help="If set None, the training will start from scratch. "
|
|
|
|
|
"Otherwise, the training will resume from "
|
|
|
|
|
"the existing model of this path. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--output_model_dir",
|
|
|
|
|
default="./checkpoints",
|
|
|
|
|
type=str,
|
|
|
|
|
help="Directory for saving models. (default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--augmentation_config",
|
|
|
|
|
default=open('conf/augmentation.config', 'r').read(),
|
|
|
|
|
type=str,
|
|
|
|
|
help="Augmentation configuration in json-format. "
|
|
|
|
|
"(default: %(default)s)")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--is_local",
|
|
|
|
|
default=True,
|
|
|
|
|
type=distutils.util.strtobool,
|
|
|
|
|
help="Set to false if running with pserver in paddlecloud. "
|
|
|
|
|
"(default: %(default)s)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_arg(argname, type, default, help, **kwargs):
|
|
|
|
|
type = distutils.util.strtobool if type == bool else type
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--" + argname,
|
|
|
|
|
default=default,
|
|
|
|
|
type=type,
|
|
|
|
|
help=help + ' Default: %(default)s.',
|
|
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# yapf: disable
|
|
|
|
|
# configurations of optimization
|
|
|
|
|
add_arg('batch_size', int, 256, "Minibatch size.")
|
|
|
|
|
add_arg('learning_rate', float, 5e-4, "Learning rate.")
|
|
|
|
|
add_arg('use_sortagrad', bool, True, "Use SortaGrad or not.")
|
|
|
|
|
add_arg('trainer_count', int, 8, "# of Trainers (CPUs or GPUs).")
|
|
|
|
|
add_arg('use_gpu', bool, True, "Use GPU or not.")
|
|
|
|
|
add_arg('num_passes', int, 200, "# of training epochs.")
|
|
|
|
|
add_arg('is_local', bool, True, "Use pserver or not.")
|
|
|
|
|
add_arg('num_iter_print', int, 100, "Every # iterations for printing "
|
|
|
|
|
"train cost.")
|
|
|
|
|
# configurations of data preprocess
|
|
|
|
|
add_arg('max_duration', float, 27.0, "Longest audio duration allowed.")
|
|
|
|
|
add_arg('min_duration', float, 0.0, "Shortest audio duration allowed.")
|
|
|
|
|
add_arg('parallels_data', int, NUM_CPU,"# of CPUs for data preprocessing.")
|
|
|
|
|
add_arg('specgram_type', str,
|
|
|
|
|
'linear',
|
|
|
|
|
"Audio feature type. Options: linear, mfcc.",
|
|
|
|
|
choices=['linear', 'mfcc'])
|
|
|
|
|
add_arg('augment_conf_path',str,
|
|
|
|
|
'conf/augmentation.config',
|
|
|
|
|
"Filepath of augmentation configuration file (json-format).")
|
|
|
|
|
add_arg('shuffle_method', str,
|
|
|
|
|
'batch_shuffle_clipped',
|
|
|
|
|
"Shuffle method.",
|
|
|
|
|
choices=['instance_shuffle', 'batch_shuffle', 'batch_shuffle_clipped'])
|
|
|
|
|
# configurations of model structure
|
|
|
|
|
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.")
|
|
|
|
|
add_arg('use_gru', bool, False, "Use GRUs instead of Simple RNNs.")
|
|
|
|
|
add_arg('share_rnn_weights',bool, True, "Share input-hidden weights across "
|
|
|
|
|
"bi-directional RNNs. Not for GRU.")
|
|
|
|
|
# configurations of data io
|
|
|
|
|
add_arg('train_manifest', str,
|
|
|
|
|
'datasets/manifest.train',
|
|
|
|
|
"Filepath of train manifest.")
|
|
|
|
|
add_arg('dev_manifest', str,
|
|
|
|
|
'datasets/manifest.dev',
|
|
|
|
|
"Filepath of validation manifest.")
|
|
|
|
|
add_arg('mean_std_path', str,
|
|
|
|
|
'mean_std.npz',
|
|
|
|
|
"Filepath of normalizer's mean & std.")
|
|
|
|
|
add_arg('vocab_path', str,
|
|
|
|
|
'datasets/vocab/eng_vocab.txt',
|
|
|
|
|
"Filepath of vocabulary.")
|
|
|
|
|
# configurations of model io
|
|
|
|
|
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,
|
|
|
|
|
"./checkpoints",
|
|
|
|
|
"Directory for saving checkpoints.")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
# yapf: disable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def train():
|
|
|
|
|
"""DeepSpeech2 training."""
|
|
|
|
|
train_generator = DataGenerator(
|
|
|
|
|
vocab_filepath=args.vocab_filepath,
|
|
|
|
|
mean_std_filepath=args.mean_std_filepath,
|
|
|
|
|
augmentation_config=args.augmentation_config,
|
|
|
|
|
vocab_filepath=args.vocab_path,
|
|
|
|
|
mean_std_filepath=args.mean_std_path,
|
|
|
|
|
augmentation_config=open(args.augment_conf_path, 'r').read(),
|
|
|
|
|
max_duration=args.max_duration,
|
|
|
|
|
min_duration=args.min_duration,
|
|
|
|
|
specgram_type=args.specgram_type,
|
|
|
|
|
num_threads=args.num_threads_data)
|
|
|
|
|
num_threads=args.parallels_data)
|
|
|
|
|
dev_generator = DataGenerator(
|
|
|
|
|
vocab_filepath=args.vocab_filepath,
|
|
|
|
|
mean_std_filepath=args.mean_std_filepath,
|
|
|
|
|
vocab_filepath=args.vocab_path,
|
|
|
|
|
mean_std_filepath=args.mean_std_path,
|
|
|
|
|
augmentation_config="{}",
|
|
|
|
|
specgram_type=args.specgram_type,
|
|
|
|
|
num_threads=args.num_threads_data)
|
|
|
|
|
num_threads=args.parallels_data)
|
|
|
|
|
train_batch_reader = train_generator.batch_reader_creator(
|
|
|
|
|
manifest_path=args.train_manifest_path,
|
|
|
|
|
manifest_path=args.train_manifest,
|
|
|
|
|
batch_size=args.batch_size,
|
|
|
|
|
min_batch_size=args.trainer_count,
|
|
|
|
|
sortagrad=args.use_sortagrad if args.init_model_path is None else False,
|
|
|
|
|
shuffle_method=args.shuffle_method)
|
|
|
|
|
dev_batch_reader = dev_generator.batch_reader_creator(
|
|
|
|
|
manifest_path=args.dev_manifest_path,
|
|
|
|
|
manifest_path=args.dev_manifest,
|
|
|
|
|
batch_size=args.batch_size,
|
|
|
|
|
min_batch_size=1, # must be 1, but will have errors.
|
|
|
|
|
sortagrad=False,
|
|
|
|
@ -184,21 +118,28 @@ def train():
|
|
|
|
|
rnn_layer_size=args.rnn_layer_size,
|
|
|
|
|
use_gru=args.use_gru,
|
|
|
|
|
pretrained_model_path=args.init_model_path,
|
|
|
|
|
share_rnn_weights=args.share_rnn_weights)
|
|
|
|
|
share_rnn_weights=args.share_weights)
|
|
|
|
|
ds2_model.train(
|
|
|
|
|
train_batch_reader=train_batch_reader,
|
|
|
|
|
dev_batch_reader=dev_batch_reader,
|
|
|
|
|
feeding_dict=train_generator.feeding,
|
|
|
|
|
learning_rate=args.adam_learning_rate,
|
|
|
|
|
learning_rate=args.learning_rate,
|
|
|
|
|
gradient_clipping=400,
|
|
|
|
|
num_passes=args.num_passes,
|
|
|
|
|
num_iterations_print=args.num_iterations_print,
|
|
|
|
|
num_iterations_print=args.num_iter_print,
|
|
|
|
|
output_model_dir=args.output_model_dir,
|
|
|
|
|
is_local=args.is_local)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_arguments(args):
|
|
|
|
|
print("----------- Configuration Arguments -----------")
|
|
|
|
|
for arg, value in sorted(vars(args).iteritems()):
|
|
|
|
|
print("%s: %s" % (arg, value))
|
|
|
|
|
print("------------------------------------------------")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
utils.print_arguments(args)
|
|
|
|
|
print_arguments(args)
|
|
|
|
|
paddle.init(use_gpu=args.use_gpu, trainer_count=args.trainer_count)
|
|
|
|
|
train()
|
|
|
|
|
|
|
|
|
|