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
12
from error_rate import wer
Y
yangyaming 已提交
13
from error_rate import cer
14
import utils
15

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


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

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    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,
        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)
158

Y
yangyaming 已提交
159 160 161 162 163 164 165
    if args.error_rate_type == 'wer':
        error_rate_func = wer
        error_rate_info = 'wer'
    else:
        error_rate_func = cer
        error_rate_info = 'cer'

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


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


if __name__ == '__main__':
    main()