File size: 7,408 Bytes
2d8da09 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 211 212 213 214 215 216 |
# Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. 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.
"""
This script is to compute global and speaker-level feature statistics for a given TTS training manifest.
This script should be run after compute_features.py as it loads the precomputed feature data.
$ python <nemo_root_path>/scripts/dataset_processing/tts/compute_feature_stats.py \
--feature_config_path=<nemo_root_path>/examples/tts/conf/features/feature_22050.yaml
--manifest_path=<data_root_path>/manifest1.json \
--manifest_path=<data_root_path>/manifest2.json \
--audio_dir=<data_root_path>/audio1 \
--audio_dir=<data_root_path>/audio2 \
--feature_dir=<data_root_path>/features1 \
--feature_dir=<data_root_path>/features2 \
--stats_path=<data_root_path>/feature_stats.json
The output dictionary will contain the feature statistics for every speaker, as well as a "default" entry
with the global statistics.
For example:
{
"default": {
"pitch_mean": 100.0,
"pitch_std": 50.0,
"energy_mean": 7.5,
"energy_std": 4.5
},
"speaker1": {
"pitch_mean": 105.0,
"pitch_std": 45.0,
"energy_mean": 7.0,
"energy_std": 5.0
},
"speaker2": {
"pitch_mean": 110.0,
"pitch_std": 30.0,
"energy_mean": 5.0,
"energy_std": 2.5
}
}
"""
import argparse
import json
from collections import defaultdict
from pathlib import Path
from typing import List, Tuple
import torch
from hydra.utils import instantiate
from omegaconf import OmegaConf
from tqdm import tqdm
from nemo.collections.asr.parts.utils.manifest_utils import read_manifest
def get_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter, description="Compute TTS feature statistics.",
)
parser.add_argument(
"--feature_config_path", required=True, type=Path, help="Path to feature config file.",
)
parser.add_argument(
"--manifest_path", required=True, type=Path, action="append", help="Path(s) to training manifest.",
)
parser.add_argument(
"--audio_dir", required=True, type=Path, action="append", help="Path(s) to base directory with audio data.",
)
parser.add_argument(
"--feature_dir",
required=True,
type=Path,
action="append",
help="Path(s) to directory where feature data was stored.",
)
parser.add_argument(
"--feature_names", default="pitch,energy", type=str, help="Comma separated list of features to process.",
)
parser.add_argument(
"--mask_field",
default="voiced_mask",
type=str,
help="If provided, stat computation will ignore non-masked frames.",
)
parser.add_argument(
"--stats_path",
default=Path("feature_stats.json"),
type=Path,
help="Path to output JSON file with dataset feature statistics.",
)
parser.add_argument(
"--overwrite",
action=argparse.BooleanOptionalAction,
help="Whether to overwrite the output stats file if it exists.",
)
args = parser.parse_args()
return args
def _compute_stats(values: List[torch.Tensor]) -> Tuple[float, float]:
values_tensor = torch.cat(values, dim=0)
mean = values_tensor.mean().item()
std = values_tensor.std(dim=0).item()
return mean, std
def main():
args = get_args()
feature_config_path = args.feature_config_path
manifest_paths = args.manifest_path
audio_dirs = args.audio_dir
feature_dirs = args.feature_dir
feature_name_str = args.feature_names
mask_field = args.mask_field
stats_path = args.stats_path
overwrite = args.overwrite
if not (len(manifest_paths) == len(audio_dirs) == len(feature_dirs)):
raise ValueError(
f"Need same number of manifest, audio_dir, and feature_dir. Received: "
f"{len(manifest_paths)}, "
f"{len(audio_dirs)}, "
f"{len(feature_dirs)}"
)
for (manifest_path, audio_dir, feature_dir) in zip(manifest_paths, audio_dirs, feature_dirs):
if not manifest_path.exists():
raise ValueError(f"Manifest {manifest_path} does not exist.")
if not audio_dir.exists():
raise ValueError(f"Audio directory {audio_dir} does not exist.")
if not feature_dir.exists():
raise ValueError(
f"Feature directory {feature_dir} does not exist. "
f"Please check that the path is correct and that you ran compute_features.py"
)
if stats_path.exists():
if overwrite:
print(f"Will overwrite existing stats path: {stats_path}")
else:
raise ValueError(f"Stats path already exists: {stats_path}")
feature_config = OmegaConf.load(feature_config_path)
feature_config = instantiate(feature_config)
featurizer_dict = feature_config.featurizers
print(f"Found featurizers for {list(featurizer_dict.keys())}.")
featurizers = featurizer_dict.values()
feature_names = feature_name_str.split(",")
# For each feature, we have a dictionary mapping speaker IDs to a list containing all features
# for that speaker
feature_stats = {name: defaultdict(list) for name in feature_names}
for (manifest_path, audio_dir, feature_dir) in zip(manifest_paths, audio_dirs, feature_dirs):
entries = read_manifest(manifest_path)
for entry in tqdm(entries):
speaker = entry["speaker"]
entry_dict = {}
for featurizer in featurizers:
feature_dict = featurizer.load(manifest_entry=entry, audio_dir=audio_dir, feature_dir=feature_dir)
entry_dict.update(feature_dict)
if mask_field:
mask = entry_dict[mask_field]
else:
mask = None
for feature_name in feature_names:
values = entry_dict[feature_name]
if mask is not None:
values = values[mask]
feature_stat_dict = feature_stats[feature_name]
feature_stat_dict["default"].append(values)
feature_stat_dict[speaker].append(values)
stat_dict = defaultdict(dict)
for feature_name in feature_names:
mean_key = f"{feature_name}_mean"
std_key = f"{feature_name}_std"
feature_stat_dict = feature_stats[feature_name]
for speaker_id, values in feature_stat_dict.items():
speaker_mean, speaker_std = _compute_stats(values)
stat_dict[speaker_id][mean_key] = speaker_mean
stat_dict[speaker_id][std_key] = speaker_std
with open(stats_path, 'w', encoding="utf-8") as stats_f:
json.dump(stat_dict, stats_f, indent=4)
if __name__ == "__main__":
main()
|