File size: 3,118 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
# 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 takes a list of TTS manifests and creates a JSON mapping the input speaker names to
unique indices for multi-speaker TTS training.

To ensure that speaker names are unique across datasets, it is recommended that you prepend the speaker
names in your manifest with the name of the dataset.

$ python <nemo_root_path>/scripts/dataset_processing/tts/create_speaker_map.py \
    --manifest_path=manifest1.json \
    --manifest_path=manifest2.json \
    --speaker_map_path=speakers.json

Example output:

{
    "vctk_p225": 0,
    "vctk_p226": 1,
    "vctk_p227": 2,
    ...
}

"""

import argparse
import json
from pathlib import Path

from nemo.collections.asr.parts.utils.manifest_utils import read_manifest


def get_args():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description="Create mapping from speaker names to numerical speaker indices.",
    )
    parser.add_argument(
        "--manifest_path", required=True, type=Path, action="append", help="Path to training manifest(s).",
    )
    parser.add_argument(
        "--speaker_map_path", required=True, type=Path, help="Path for output speaker index JSON",
    )
    parser.add_argument(
        "--overwrite",
        action=argparse.BooleanOptionalAction,
        help="Whether to overwrite the output speaker file if it exists.",
    )
    args = parser.parse_args()
    return args


def main():
    args = get_args()
    manifest_paths = args.manifest_path
    speaker_map_path = args.speaker_map_path
    overwrite = args.overwrite

    for manifest_path in manifest_paths:
        if not manifest_path.exists():
            raise ValueError(f"Manifest {manifest_path} does not exist.")

    if speaker_map_path.exists():
        if overwrite:
            print(f"Will overwrite existing speaker path: {speaker_map_path}")
        else:
            raise ValueError(f"Speaker path already exists: {speaker_map_path}")

    speaker_set = set()
    for manifest_path in manifest_paths:
        entries = read_manifest(manifest_path)
        for entry in entries:
            speaker = str(entry["speaker"])
            speaker_set.add(speaker)

    speaker_list = list(speaker_set)
    speaker_list.sort()
    speaker_index_map = {speaker_list[i]: i for i in range(len(speaker_list))}

    with open(speaker_map_path, 'w', encoding="utf-8") as stats_f:
        json.dump(speaker_index_map, stats_f, indent=4)


if __name__ == "__main__":
    main()