infer.py 4.0 KB
Newer Older
X
Xinghai Sun 已提交
1 2 3 4
"""
   Inference for a simplifed version of Baidu DeepSpeech2 model.
"""

5
import paddle.v2 as paddle
6
import distutils.util
7 8
import argparse
import gzip
9
from audio_data_utils import DataGenerator
X
Xinghai Sun 已提交
10
from model import deep_speech2
11
from decoder import ctc_decode
12 13

parser = argparse.ArgumentParser(
X
Xinghai Sun 已提交
14
    description='Simplified version of DeepSpeech2 inference.')
15
parser.add_argument(
X
Xinghai Sun 已提交
16 17 18
    "--num_samples",
    default=10,
    type=int,
19
    help="Number of samples for inference. (default: %(default)s)")
20
parser.add_argument(
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    "--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)")
40
parser.add_argument(
41
    "--normalizer_manifest_path",
42
    default='data/manifest.libri.train-clean-100',
43 44
    type=str,
    help="Manifest path for normalizer. (default: %(default)s)")
45
parser.add_argument(
46
    "--decode_manifest_path",
47
    default='data/manifest.libri.test-clean',
48 49
    type=str,
    help="Manifest path for decoding. (default: %(default)s)")
50
parser.add_argument(
51 52 53 54
    "--model_filepath",
    default='./params.tar.gz',
    type=str,
    help="Model filepath. (default: %(default)s)")
55 56 57 58 59
parser.add_argument(
    "--vocab_filepath",
    default='data/eng_vocab.txt',
    type=str,
    help="Vocabulary filepath. (default: %(default)s)")
60 61 62
args = parser.parse_args()


63
def infer():
X
Xinghai Sun 已提交
64 65 66
    """
    Max-ctc-decoding for DeepSpeech2.
    """
67 68
    # initialize data generator
    data_generator = DataGenerator(
69
        vocab_filepath=args.vocab_filepath,
70 71 72 73 74 75
        normalizer_manifest_path=args.normalizer_manifest_path,
        normalizer_num_samples=200,
        max_duration=20.0,
        min_duration=0.0,
        stride_ms=10,
        window_ms=20)
76

77
    # create network config
78 79
    dict_size = data_generator.vocabulary_size()
    vocab_list = data_generator.vocabulary_list()
80 81 82
    audio_data = paddle.layer.data(
        name="audio_spectrogram",
        height=161,
83 84
        width=2000,
        type=paddle.data_type.dense_vector(322000))
85 86 87
    text_data = paddle.layer.data(
        name="transcript_text",
        type=paddle.data_type.integer_value_sequence(dict_size))
88
    output_probs = deep_speech2(
89 90 91 92 93
        audio_data=audio_data,
        text_data=text_data,
        dict_size=dict_size,
        num_conv_layers=args.num_conv_layers,
        num_rnn_layers=args.num_rnn_layers,
94 95
        rnn_size=args.rnn_layer_size,
        is_inference=True)
96 97 98

    # load parameters
    parameters = paddle.parameters.Parameters.from_tar(
99
        gzip.open(args.model_filepath))
100 101

    # prepare infer data
102 103 104 105 106 107 108 109
    feeding = data_generator.data_name_feeding()
    test_batch_reader = data_generator.batch_reader_creator(
        manifest_path=args.decode_manifest_path,
        batch_size=args.num_samples,
        padding_to=2000,
        flatten=True,
        sort_by_duration=False,
        shuffle=False)
110 111
    infer_data = test_batch_reader().next()

112 113 114 115 116 117 118
    # run inference
    infer_results = paddle.infer(
        output_layer=output_probs, parameters=parameters, input=infer_data)
    num_steps = len(infer_results) / len(infer_data)
    probs_split = [
        infer_results[i * num_steps:(i + 1) * num_steps]
        for i in xrange(0, len(infer_data))
119
    ]
120 121 122 123 124 125 126 127 128

    # decode and print
    for i, probs in enumerate(probs_split):
        output_transcription = ctc_decode(
            probs_seq=probs, vocabulary=vocab_list, method="best_path")
        target_transcription = ''.join(
            [vocab_list[index] for index in infer_data[i][1]])
        print("Target Transcription: %s \nOutput Transcription: %s \n" %
              (target_transcription, output_transcription))
129 130 131 132


def main():
    paddle.init(use_gpu=args.use_gpu, trainer_count=1)
133
    infer()
134 135 136 137


if __name__ == '__main__':
    main()