compute_mean_std.py 3.3 KB
Newer Older
H
Hui Zhang 已提交
1
#!/usr/bin/env python3
H
Hui Zhang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.
15
"""Compute mean and std for feature normalizer, and save to file."""
16
import argparse
X
Xinghai Sun 已提交
17
import functools
18

19 20 21 22 23
from paddlespeech.s2t.frontend.augmentor.augmentation import AugmentationPipeline
from paddlespeech.s2t.frontend.featurizer.audio_featurizer import AudioFeaturizer
from paddlespeech.s2t.frontend.normalizer import FeatureNormalizer
from paddlespeech.s2t.utils.utility import add_arguments
from paddlespeech.s2t.utils.utility import print_arguments
24

25
parser = argparse.ArgumentParser(description=__doc__)
X
Xinghai Sun 已提交
26 27
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
H
Hui Zhang 已提交
28
add_arg('num_samples',      int,    2000,    "# of samples to for statistics.")
29

H
Hui Zhang 已提交
30
add_arg('spectrum_type',    str,
31
        'linear',
32 33 34 35
        "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.")
36 37
add_arg('stride_ms', int, 10,  "stride length in ms.")
add_arg('window_ms', int, 20,  "stride length in ms.")
38
add_arg('sample_rate',  int, 16000,  "target sample rate.")
H
Hui Zhang 已提交
39
add_arg('use_dB_normalization', bool, True, "do dB normalization.")
40 41
add_arg('target_dB',   int, -20,  "target dB.")

42
add_arg('manifest_path',    str,
43
        'data/librispeech/manifest.train',
44
        "Filepath of manifest to compute normalizer's mean and stddev.")
45 46 47 48
add_arg('num_workers',
                        default=0,
                        type=int,
                        help='num of subprocess workers for processing')
49
add_arg('output_path',    str,
50
        'data/librispeech/mean_std.npz',
51 52
        "Filepath of write mean and stddev to (.npz).")
# yapf: disable
X
Xinghai Sun 已提交
53
args = parser.parse_args()
54 55 56


def main():
57
    print_arguments(args, globals())
58 59

    augmentation_pipeline = AugmentationPipeline('{}')
60
    audio_featurizer = AudioFeaturizer(
H
Hui Zhang 已提交
61
        spectrum_type=args.spectrum_type,
62 63
        feat_dim=args.feat_dim,
        delta_delta=args.delta_delta,
64 65
        stride_ms=float(args.stride_ms),
        window_ms=float(args.window_ms),
66 67 68 69 70 71
        n_fft=None,
        max_freq=None,
        target_sample_rate=args.sample_rate,
        use_dB_normalization=args.use_dB_normalization,
        target_dB=args.target_dB,
        dither=0.0)
72 73 74 75 76 77 78 79 80

    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,
81 82
        num_samples=args.num_samples,
        num_workers=args.num_workers)
83
    normalizer.write_to_file(args.output_path)
84 85 86 87


if __name__ == '__main__':
    main()