compute_mean_std.py 3.1 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
import argparse
X
Xinghai Sun 已提交
16
import functools
H
Hui Zhang 已提交
17

H
Hui Zhang 已提交
18 19
from deepspeech.frontend.augmentor.augmentation import AugmentationPipeline
from deepspeech.frontend.featurizer.audio_featurizer import AudioFeaturizer
H
Hui Zhang 已提交
20 21 22
from deepspeech.frontend.normalizer import FeatureNormalizer
from deepspeech.utils.utility import add_arguments
from deepspeech.utils.utility import print_arguments
23

24
parser = argparse.ArgumentParser(description=__doc__)
X
Xinghai Sun 已提交
25 26
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
H
Hui Zhang 已提交
27
add_arg('num_samples',      int,    -1,    "# of samples to for statistics.")
28 29
add_arg('specgram_type',    str,
        'linear',
30 31
        "Audio feature type. Options: linear, mfcc, fbank.",
        choices=['linear', 'mfcc', 'fbank'])
H
Hui Zhang 已提交
32
add_arg('feat_dim',    int, 13, "Audio feature dim.")
33 34 35
add_arg('delta_delta',    bool,
        False,
        "Audio feature with delta delta.")
H
Hui Zhang 已提交
36 37 38
add_arg('stride_ms',    float, 10.0,  "stride length in ms.")
add_arg('window_ms',    float, 20.0,  "stride length in ms.")
add_arg('sample_rate',    int, 16000,  "target sample rate.")
39
add_arg('manifest_path',    str,
40
        'data/librispeech/manifest.train',
41
        "Filepath of manifest to compute normalizer's mean and stddev.")
H
Hui Zhang 已提交
42 43 44 45
add_arg('num_workers',
                        default=0,
                        type=int,
                        help='num of subprocess workers for processing')
46
add_arg('output_path',    str,
47
        'data/librispeech/mean_std.npz',
48 49
        "Filepath of write mean and stddev to (.npz).")
# yapf: disable
X
Xinghai Sun 已提交
50
args = parser.parse_args()
51 52 53


def main():
H
Hui Zhang 已提交
54
    print_arguments(args, globals())
55 56

    augmentation_pipeline = AugmentationPipeline('{}')
57 58 59
    audio_featurizer = AudioFeaturizer(
        specgram_type=args.specgram_type,
        feat_dim=args.feat_dim,
H
Hui Zhang 已提交
60 61 62 63 64 65 66 67
        delta_delta=args.delta_delta,
        stride_ms=args.stride_ms,
        window_ms=args.window_ms,
        n_fft=None,
        max_freq=None,
        target_sample_rate=args.sample_rate,
        use_dB_normalization=True,
        target_dB=-20)
68 69 70 71 72 73 74 75 76

    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,
H
Hui Zhang 已提交
77 78
        num_samples=args.num_samples,
        num_workers=args.num_workers)
79
    normalizer.write_to_file(args.output_path)
80 81 82 83


if __name__ == '__main__':
    main()