compute_mean_std.py 1.7 KB
Newer Older
1
"""Compute mean and std for feature normalizer, and save to file."""
2 3 4 5 6
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
X
Xinghai Sun 已提交
7
import functools
8
import _init_paths
9 10 11
from data_utils.normalizer import FeatureNormalizer
from data_utils.augmentor.augmentation import AugmentationPipeline
from data_utils.featurizer.audio_featurizer import AudioFeaturizer
12
from utils.utility import add_arguments, print_arguments
13

14
parser = argparse.ArgumentParser(description=__doc__)
X
Xinghai Sun 已提交
15 16
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
17 18 19 20 21 22
add_arg('num_samples',      int,    2000,    "# of samples to for statistics.")
add_arg('specgram_type',    str,
        'linear',
        "Audio feature type. Options: linear, mfcc.",
        choices=['linear', 'mfcc'])
add_arg('manifest_path',    str,
23
        'data/librispeech/manifest.train',
24 25
        "Filepath of manifest to compute normalizer's mean and stddev.")
add_arg('output_path',    str,
26
        'data/librispeech/mean_std.npz',
27 28
        "Filepath of write mean and stddev to (.npz).")
# yapf: disable
X
Xinghai Sun 已提交
29
args = parser.parse_args()
30 31 32


def main():
33 34 35
    print_arguments(args)

    augmentation_pipeline = AugmentationPipeline('{}')
Y
Yibing Liu 已提交
36
    audio_featurizer = AudioFeaturizer(specgram_type=args.specgram_type)
37 38 39 40 41 42 43 44 45 46

    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)
47
    normalizer.write_to_file(args.output_path)
48 49 50 51


if __name__ == '__main__':
    main()