normalizer.py 7.2 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
"""Contains feature normalizers."""
H
Hui Zhang 已提交
15
import random
16 17

import numpy as np
H
Hui Zhang 已提交
18 19 20
import paddle
from paddle.io import DataLoader
from paddle.io import Dataset
H
Hui Zhang 已提交
21

H
Hui Zhang 已提交
22
from deepspeech.frontend.audio import AudioSegment
H
Hui Zhang 已提交
23 24
from deepspeech.frontend.utility import load_cmvn
from deepspeech.frontend.utility import read_manifest
H
Hui Zhang 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
from deepspeech.utils.log import Log

__all__ = ["FeatureNormalizer"]

logger = Log(__name__).getlog()


class CollateFunc(object):
    ''' Collate function for AudioDataset
    '''

    def __init__(self):
        pass

    def __call__(self, batch):
        mean_stat = None
        var_stat = None
        number = 0
        for feat in batch:
            sums = np.sum(feat, axis=1)
            if mean_stat is None:
                mean_stat = sums
            else:
                mean_stat += sums

            square_sums = np.sum(np.square(feat), axis=1)
            if var_stat is None:
                var_stat = square_sums
            else:
                var_stat += square_sums

            number += feat.shape[1]
        return paddle.to_tensor(number), paddle.to_tensor(
            mean_stat), paddle.to_tensor(var_stat)
        #return number, mean_stat, var_stat


class AudioDataset(Dataset):
    def __init__(self, manifest_path, feature_func, num_samples=-1, rng=None):
        self.feature_func = feature_func
        self._rng = rng
        manifest = read_manifest(manifest_path)
        if num_samples == -1:
            sampled_manifest = manifest
        else:
            sampled_manifest = self._rng.sample(manifest, num_samples)
        self.items = sampled_manifest

    def __len__(self):
        return len(self.items)

    def __getitem__(self, idx):
        key = self.items[idx]['feat']
        audioseg = AudioSegment.from_file(key)
        feat = self.feature_func(audioseg)  #(D, T)
        return feat
81 82 83


class FeatureNormalizer(object):
84 85 86 87 88 89
    """Feature normalizer. Normalize features to be of zero mean and unit
    stddev.

    if mean_std_filepath is provided (not None), the normalizer will directly
    initilize from the file. Otherwise, both manifest_path and featurize_func
    should be given for on-the-fly mean and stddev computing.
Y
Yibing Liu 已提交
90

91
    :param mean_std_filepath: File containing the pre-computed mean and stddev.
H
Hui Zhang 已提交
92
    :type mean_std_filepath: None|str
93
    :param manifest_path: Manifest of instances for computing mean and stddev.
H
Hui Zhang 已提交
94
    :type meanifest_path: None|str
95 96 97 98 99 100 101 102 103 104 105
    :param featurize_func: Function to extract features. It should be callable
                           with ``featurize_func(audio_segment)``.
    :type featurize_func: None|callable
    :param num_samples: Number of random samples for computing mean and stddev.
    :type num_samples: int
    :param random_seed: Random seed for sampling instances.
    :type random_seed: int
    :raises ValueError: If both mean_std_filepath and manifest_path
                        (or both mean_std_filepath and featurize_func) are None.
    """

106 107 108 109 110
    def __init__(self,
                 mean_std_filepath,
                 manifest_path=None,
                 featurize_func=None,
                 num_samples=500,
H
Hui Zhang 已提交
111
                 num_workers=0,
112 113 114 115 116 117
                 random_seed=0):
        if not mean_std_filepath:
            if not (manifest_path and featurize_func):
                raise ValueError("If mean_std_filepath is None, meanifest_path "
                                 "and featurize_func should not be None.")
            self._rng = random.Random(random_seed)
H
Hui Zhang 已提交
118 119
            self._compute_mean_std(manifest_path, featurize_func, num_samples,
                                   num_workers)
120 121 122
        else:
            self._read_mean_std_from_file(mean_std_filepath)

H
Hui Zhang 已提交
123
    def apply(self, features):
124 125 126
        """Normalize features to be of zero mean and unit stddev.

        :param features: Input features to be normalized.
H
Hui Zhang 已提交
127
        :type features: ndarray, shape (D, T)
128 129 130 131 132
        :param eps:  added to stddev to provide numerical stablibity.
        :type eps: float
        :return: Normalized features.
        :rtype: ndarray
        """
H
Hui Zhang 已提交
133
        return (features - self._mean) * self._istd
134

H
Hui Zhang 已提交
135 136 137 138 139 140
    def _read_mean_std_from_file(self, filepath, eps=1e-20):
        """Load mean and std from file."""
        mean, istd = load_cmvn(filepath, filetype='json')
        self._mean = mean
        self._istd = istd

141
    def write_to_file(self, filepath):
142 143 144
        """Write the mean and stddev to the file.

        :param filepath: File to write mean and stddev.
H
Hui Zhang 已提交
145
        :type filepath: str
146
        """
H
Hui Zhang 已提交
147 148
        with open(filepath, 'w') as fout:
            fout.write(json.dumps(self.cmvn_info))
149

H
Hui Zhang 已提交
150 151 152 153
    def _compute_mean_std(self,
                          manifest_path,
                          featurize_func,
                          num_samples,
H
Hui Zhang 已提交
154
                          num_workers,
H
Hui Zhang 已提交
155
                          eps=1e-20):
156
        """Compute mean and std from randomly sampled instances."""
H
Hui Zhang 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        # manifest = read_manifest(manifest_path)
        # if num_samples == -1:
        #     sampled_manifest = manifest
        # else:
        #     sampled_manifest = self._rng.sample(manifest, num_samples)
        # features = []
        # for instance in sampled_manifest:
        #     features.append(
        #         featurize_func(AudioSegment.from_file(instance["feat"])))
        # features = np.hstack(features)  #(D, T)
        # self._mean = np.mean(features, axis=1)  #(D,)
        # std = np.std(features, axis=1)  #(D,)
        # std = np.clip(std, eps, None)
        # self._istd = 1.0 / std

        collate_func = CollateFunc()

        dataset = AudioDataset(manifest_path, featurize_func, num_samples)

        batch_size = 20
        data_loader = DataLoader(
            dataset,
            batch_size=batch_size,
            shuffle=False,
            num_workers=num_workers,
            collate_fn=collate_func)

        with paddle.no_grad():
            all_mean_stat = None
            all_var_stat = None
            all_number = 0
            wav_number = 0
            for batch in data_loader():
                number, mean_stat, var_stat = batch
                if all_mean_stat is None:
                    all_mean_stat = mean_stat
                    all_var_stat = var_stat
                else:
                    all_mean_stat += mean_stat
                    all_var_stat += var_stat
                all_number += number
                wav_number += batch_size

                if wav_number % 1000 == 0:
                    logger.info('process {} wavs,{} frames'.format(
                        wav_number, int(all_number)))

        self.cmvn_info = {
            'mean_stat': list(all_mean_stat.tolist()),
            'var_stat': list(all_var_stat.tolist()),
            'frame_num': int(all_number),
        }

        return self.cmvn_info