-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprepare_dataset.py
223 lines (195 loc) · 10.6 KB
/
prepare_dataset.py
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
217
218
219
220
221
222
223
# *****************************************************************************
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the NVIDIA CORPORATION nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# *****************************************************************************
import argparse
import csv
import json
import os
import torch
import dllogger as DLLogger
import numpy as np
from dllogger import StdOutBackend, Verbosity
from torch.utils.data import DataLoader
from tqdm import tqdm
from fastpitch.data_function import TextMelAliLoader
def parse_args(parser):
"""
Parse commandline arguments.
"""
parser.add_argument('-d', '--dataset-path', type=str,
default='./', help='Path to dataset')
parser.add_argument('--wav-text-filelists', required=True, nargs='+',
type=str, help='Path to file with audio paths and text')
parser.add_argument('--extract-mels', action=argparse.BooleanOptionalAction,
default=True, help='Save mel spectrograms to disk')
parser.add_argument('--extract-durs', action=argparse.BooleanOptionalAction,
default=True, help='Save input symbol durations to disk')
parser.add_argument('--extract-pitch', action=argparse.BooleanOptionalAction,
default=True, help='Save framewise, unnormalized pitch values to disk')
parser.add_argument('--n-workers', default=4, type=int,
help='Number of parallel threads for data processing')
parser.add_argument('--write-meta', action='store_true',
help='Write metadata file pointing to extracted features')
parser.add_argument('--input-type', type=str, default='char',
choices=['char', 'phone', 'unit'],
help='Input symbols used, either char (text), phone '
'or quantized unit symbols.')
parser.add_argument('--symbol-set', type=str, default='english_basic',
help='Define symbol set for input text')
parser.add_argument('--text-cleaners', nargs='*',
default=[], type=str,
help='Type of text cleaners for input text')
parser.add_argument('--max-wav-value', default=32768.0, type=float,
help='Maximum audiowave value')
parser.add_argument('--peak-norm', action='store_true',
help='Apply peak normalization to audio')
parser.add_argument('--sampling-rate', default=22050, type=int,
help='Sampling rate')
parser.add_argument('--filter-length', default=512, type=int,
help='Filter length')
parser.add_argument('--hop-length', default=256, type=int,
help='Hop (stride) length')
parser.add_argument('--win-length', default=512, type=int,
help='Window length')
parser.add_argument('--n-mel-channels', default=80, type=int,
help='Number of bins in mel-spectrograms')
parser.add_argument('--mel-fmin', default=0.0, type=float,
help='Minimum mel frequency')
parser.add_argument('--mel-fmax', default=8000.0, type=float,
help='Maximum mel frequency')
parser.add_argument('--pitch-fmin', default=40.0, type=float,
help='Minimum frequency for pitch extraction')
parser.add_argument('--pitch-fmax', default=600.0, type=float,
help='Maximum frequency for pitch extraction')
parser.add_argument('--pitch-method', default='yin', choices=['yin', 'pyin'],
help='Method to use for pitch extraction. Probabilistic YIN '
'(pyin) is more accurate but also slower')
parser.add_argument('--durations-from', default=None, type=str,
choices=['textgrid', 'unit_rle', 'attn_prior'],
help='Source of input symbol durations. Either Praat TextGrids, '
'run-length encoding quantized unit sequences, or attention '
'priors derived from text and mel lengths')
parser.add_argument('--trim-silence-dur', default=None, type=float,
help='Trim leading and trailing silences from audio using TextGrids. '
'Specify desired silence duration to leave in seconds (0 to trim '
'completely)')
return parser
def passthrough_collate(batch):
return batch
def calculate_pitch_mean_std(fname_pitch):
nonzeros = np.concatenate([v[np.where(v != 0.0)[0]]
for v in fname_pitch.values()])
mean = np.mean(nonzeros)
std = np.std(nonzeros)
return mean, std
def save_stats(dataset_path, wav_text_filelist, feature_name, mean, std):
fpath = stats_filename(dataset_path, wav_text_filelist, feature_name)
with open(fpath, 'w') as f:
json.dump({'mean': mean, 'std': std}, f, indent=4)
def stats_filename(dataset_path, filelist_path, feature_name):
stem = os.path.splitext(os.path.basename(filelist_path))[0]
return os.path.join(dataset_path, f'{feature_name}_stats__{stem}.json')
def main():
parser = argparse.ArgumentParser(
description='PyTorch TTS Data Pre-processing',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser = parse_args(parser)
args, unk_args = parser.parse_known_args()
if len(unk_args) > 0:
raise ValueError(f'Invalid options {unk_args}')
DLLogger.init(backends=[StdOutBackend(Verbosity.VERBOSE,
prefix_format=lambda t: "[{}] ".format(t.strftime('%Y-%m-%d %H:%M:%S')))])
for k,v in vars(args).items():
DLLogger.log(step="PARAMETER", data={k:v})
for datum in ('mels', 'durations', 'pitches'):
os.makedirs(os.path.join(args.dataset_path, datum), exist_ok=True)
for filelist in args.wav_text_filelists:
# store all utterance metadata and pitches for normalization
fname_text = {}
fname_pitch = {}
fname_spkr = {}
fname_lang = {}
load_mel_from_disk = False
speaker_ids = None
lang_ids = None
load_durs_from_disk = False
load_pitch_from_disk = False
dataset = TextMelAliLoader(
args.dataset_path, filelist, args.text_cleaners, args.n_mel_channels,
args.input_type, args.symbol_set, speaker_ids, lang_ids,
load_mel_from_disk, load_durs_from_disk, load_pitch_from_disk,
args.max_wav_value, args.sampling_rate,
args.filter_length, args.hop_length, args.win_length,
args.mel_fmin, args.mel_fmax, args.peak_norm,
args.durations_from, args.trim_silence_dur,
args.pitch_fmin, args.pitch_fmax, args.pitch_method,
pitch_mean=None, pitch_std=None, pitch_mean_std_file=None)
data_loader = DataLoader(dataset, collate_fn=passthrough_collate,
num_workers=args.n_workers,
batch_size=1) # no need to worry about padding
label = os.path.splitext(os.path.basename(filelist))[0]
for i, batch in enumerate(tqdm(data_loader, label)):
text, mel, text_len, durations, pitch, speaker, lang, fname = batch[0]
fname_text[fname] = text
fname_pitch[fname] = pitch
fname_spkr[fname] = speaker
fname_lang[fname] = lang
if args.extract_mels:
fpath = os.path.join(args.dataset_path, 'mels', fname + '.pt')
torch.save(mel, fpath)
if args.extract_durs:
fpath = os.path.join(args.dataset_path, 'durations', fname + '.pt')
torch.save(torch.tensor(durations).squeeze(), fpath)
if args.extract_pitch:
fpath = os.path.join(args.dataset_path, 'pitches', fname + '.pt')
torch.save(torch.from_numpy(pitch), fpath)
# TODO: consider normalizing per speaker
mean, std = calculate_pitch_mean_std(fname_pitch)
save_stats(args.dataset_path, filelist, 'pitches', mean, std)
if args.write_meta:
meta_fields = ['audio', 'duration', 'pitch', 'text']
if None not in fname_spkr.values():
meta_fields.append('speaker')
if None not in fname_lang.values():
meta_fields.append('language')
meta_file = os.path.join(args.dataset_path, label + '.meta.txt')
with open(meta_file, 'w') as meta_out:
meta_csv = csv.DictWriter(
meta_out, fieldnames=meta_fields, extrasaction='ignore', delimiter='|')
meta_csv.writeheader()
for fname, text in fname_text.items():
meta_row = {
'audio': os.path.join('mels', fname + '.pt'),
'duration': os.path.join('durations', fname + '.pt'),
'pitch': os.path.join('pitches', fname + '.pt'),
'text': text,
'speaker': fname_spkr[fname],
'language': fname_lang[fname],
}
meta_csv.writerow(meta_row)
DLLogger.flush()
if __name__ == '__main__':
main()