infer.py 5.6 KB
Newer Older
1
"""Inferer for DeepSpeech2 model."""
2 3 4
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
X
Xinghai Sun 已提交
5

6
import argparse
7
import distutils.util
8
import multiprocessing
9 10
import paddle.v2 as paddle
from data_utils.data import DataGenerator
11
from model import DeepSpeech2Model
Y
yangyaming 已提交
12
from error_rate import wer, cer
13
import utils
14

15
parser = argparse.ArgumentParser(description=__doc__)
16
parser.add_argument(
X
Xinghai Sun 已提交
17
    "--num_samples",
Y
Yibing Liu 已提交
18
    default=10,
X
Xinghai Sun 已提交
19
    type=int,
20
    help="Number of samples for inference. (default: %(default)s)")
21
parser.add_argument(
22 23 24 25 26 27 28 29 30 31 32 33 34 35
    "--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=512,
    type=int,
    help="RNN layer cell number. (default: %(default)s)")
X
Xinghai Sun 已提交
36 37
parser.add_argument(
    "--use_gru",
38 39
    default=False,
    type=distutils.util.strtobool,
X
Xinghai Sun 已提交
40
    help="Use GRU or simple RNN. (default: %(default)s)")
41 42 43 44 45
parser.add_argument(
    "--use_gpu",
    default=True,
    type=distutils.util.strtobool,
    help="Use gpu or not. (default: %(default)s)")
46 47
parser.add_argument(
    "--num_threads_data",
48
    default=1,
49 50
    type=int,
    help="Number of cpu threads for preprocessing data. (default: %(default)s)")
Y
Yibing Liu 已提交
51 52
parser.add_argument(
    "--num_processes_beam_search",
53
    default=multiprocessing.cpu_count() // 2,
Y
Yibing Liu 已提交
54 55
    type=int,
    help="Number of cpu processes for beam search. (default: %(default)s)")
56 57 58 59 60 61
parser.add_argument(
    "--specgram_type",
    default='linear',
    type=str,
    help="Feature type of audio data: 'linear' (power spectrum)"
    " or 'mfcc'. (default: %(default)s)")
62 63 64 65 66
parser.add_argument(
    "--trainer_count",
    default=8,
    type=int,
    help="Trainer number. (default: %(default)s)")
67
parser.add_argument(
68 69
    "--mean_std_filepath",
    default='mean_std.npz',
70 71
    type=str,
    help="Manifest path for normalizer. (default: %(default)s)")
72
parser.add_argument(
73
    "--decode_manifest_path",
Y
Yibing Liu 已提交
74
    default='datasets/manifest.test',
75 76
    type=str,
    help="Manifest path for decoding. (default: %(default)s)")
77
parser.add_argument(
78
    "--model_filepath",
Y
Yibing Liu 已提交
79
    default='checkpoints/params.latest.tar.gz',
80 81
    type=str,
    help="Model filepath. (default: %(default)s)")
82 83
parser.add_argument(
    "--vocab_filepath",
84
    default='datasets/vocab/eng_vocab.txt',
85 86
    type=str,
    help="Vocabulary filepath. (default: %(default)s)")
Y
Yibing Liu 已提交
87 88
parser.add_argument(
    "--decode_method",
Y
Yibing Liu 已提交
89
    default='beam_search',
Y
Yibing Liu 已提交
90
    type=str,
91 92
    help="Method for ctc decoding: best_path or beam_search. "
    "(default: %(default)s)")
Y
Yibing Liu 已提交
93 94
parser.add_argument(
    "--beam_size",
95
    default=500,
Y
Yibing Liu 已提交
96 97
    type=int,
    help="Width for beam search decoding. (default: %(default)d)")
Y
Yibing Liu 已提交
98 99
parser.add_argument(
    "--language_model_path",
Y
Yibing Liu 已提交
100
    default="lm/data/common_crawl_00.prune01111.trie.klm",
Y
Yibing Liu 已提交
101
    type=str,
Y
Yibing Liu 已提交
102
    help="Path for language model. (default: %(default)s)")
Y
Yibing Liu 已提交
103 104
parser.add_argument(
    "--alpha",
105
    default=0.36,
Y
Yibing Liu 已提交
106 107 108 109
    type=float,
    help="Parameter associated with language model. (default: %(default)f)")
parser.add_argument(
    "--beta",
110
    default=0.25,
Y
Yibing Liu 已提交
111 112
    type=float,
    help="Parameter associated with word count. (default: %(default)f)")
113 114 115 116 117 118
parser.add_argument(
    "--cutoff_prob",
    default=0.99,
    type=float,
    help="The cutoff probability of pruning"
    "in beam search. (default: %(default)f)")
Y
yangyaming 已提交
119 120 121 122 123
parser.add_argument(
    "--error_rate_type",
    default='wer',
    choices=['wer', 'cer'],
    type=str,
Y
yangyaming 已提交
124 125
    help="Error rate type for evaluation. 'wer' for word error rate and 'cer' "
    "for character error rate. "
Y
yangyaming 已提交
126
    "(default: %(default)s)")
127 128 129
args = parser.parse_args()


130
def infer():
Y
Yibing Liu 已提交
131
    """Inference for DeepSpeech2."""
132
    data_generator = DataGenerator(
133
        vocab_filepath=args.vocab_filepath,
134
        mean_std_filepath=args.mean_std_filepath,
135
        augmentation_config='{}',
136
        specgram_type=args.specgram_type,
137
        num_threads=args.num_threads_data)
138
    batch_reader = data_generator.batch_reader_creator(
139 140
        manifest_path=args.decode_manifest_path,
        batch_size=args.num_samples,
Y
Yibing Liu 已提交
141
        min_batch_size=1,
142
        sortagrad=False,
143
        shuffle_method=None)
144
    infer_data = batch_reader().next()
145

146 147 148 149 150
    ds2_model = DeepSpeech2Model(
        vocab_size=data_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 已提交
151
        use_gru=args.use_gru,
152 153 154 155 156 157 158 159 160 161 162
        pretrained_model_path=args.model_filepath)
    result_transcripts = ds2_model.infer_batch(
        infer_data=infer_data,
        decode_method=args.decode_method,
        beam_alpha=args.alpha,
        beam_beta=args.beta,
        beam_size=args.beam_size,
        cutoff_prob=args.cutoff_prob,
        vocab_list=data_generator.vocab_list,
        language_model_path=args.language_model_path,
        num_processes=args.num_processes_beam_search)
163

Y
yangyaming 已提交
164
    error_rate_func = cer if args.error_rate_type == 'cer' else wer
165 166 167
    target_transcripts = [
        ''.join([data_generator.vocab_list[token] for token in transcript])
        for _, transcript in infer_data
Y
Yibing Liu 已提交
168
    ]
169 170 171
    for target, result in zip(target_transcripts, result_transcripts):
        print("\nTarget Transcription: %s\nOutput Transcription: %s" %
              (target, result))
Y
yangyaming 已提交
172 173
        print("Current error rate [%s] = %f" %
              (args.error_rate_type, error_rate_func(target, result)))
174 175 176


def main():
177
    utils.print_arguments(args)
178
    paddle.init(use_gpu=args.use_gpu, trainer_count=args.trainer_count)
179
    infer()
180 181 182 183


if __name__ == '__main__':
    main()