compute_mean_std.py 2.5 KB
Newer Older
H
Hui Zhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
14
"""Compute mean and std for feature normalizer, and save to file."""
15 16

import argparse
X
Xinghai Sun 已提交
17
import functools
H
Hui Zhang 已提交
18 19 20 21
from deepspeech.frontend.normalizer import FeatureNormalizer
from deepspeech.frontend.augmentor.augmentation import AugmentationPipeline
from deepspeech.frontend.featurizer.audio_featurizer import AudioFeaturizer
from deepspeech.utils.utility import add_arguments, print_arguments
22

23
parser = argparse.ArgumentParser(description=__doc__)
X
Xinghai Sun 已提交
24 25
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
26 27 28
add_arg('num_samples',      int,    2000,    "# of samples to for statistics.")
add_arg('specgram_type',    str,
        'linear',
29 30 31 32 33 34 35 36
        "Audio feature type. Options: linear, mfcc, fbank.",
        choices=['linear', 'mfcc', 'fbank'])
add_arg('feat_dim',    int,
        13,
        "Audio feature dim.")
add_arg('delta_delta',    bool,
        False,
        "Audio feature with delta delta.")
37
add_arg('manifest_path',    str,
38
        'data/librispeech/manifest.train',
39 40
        "Filepath of manifest to compute normalizer's mean and stddev.")
add_arg('output_path',    str,
41
        'data/librispeech/mean_std.npz',
42 43
        "Filepath of write mean and stddev to (.npz).")
# yapf: disable
X
Xinghai Sun 已提交
44
args = parser.parse_args()
45 46 47


def main():
48 49 50
    print_arguments(args)

    augmentation_pipeline = AugmentationPipeline('{}')
51 52 53 54
    audio_featurizer = AudioFeaturizer(
        specgram_type=args.specgram_type,
        feat_dim=args.feat_dim,
        delta_delta=args.delta_delta)
55 56 57 58 59 60 61 62 63 64

    def augment_and_featurize(audio_segment):
        augmentation_pipeline.transform_audio(audio_segment)
        return audio_featurizer.featurize(audio_segment)

    normalizer = FeatureNormalizer(
        mean_std_filepath=None,
        manifest_path=args.manifest_path,
        featurize_func=augment_and_featurize,
        num_samples=args.num_samples)
65
    normalizer.write_to_file(args.output_path)
66 67 68 69


if __name__ == '__main__':
    main()