forked from LSimon95/megatts2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_ds.py
290 lines (226 loc) · 9.79 KB
/
prepare_ds.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
'''
wavs dir
├── speaker1
│ ├── s1wav1.wav
│ ├── s1wav1.txt
│ ├── s1wav2.wav
│ ├── s1wav2.txt
│ ├── ...
├── speaker2
│ ├── s2wav1.wav
│ ├── s2wav1.txt
│ ├── ...
cautions: stage 0 will delete all txt files in wavs dir
'''
import os
import glob
from modules.tokenizer import TextTokenizer
from multiprocessing import Pool
from tqdm.auto import tqdm
from utils.textgrid import read_textgrid
import argparse
from lhotse import validate_recordings_and_supervisions, CutSet, NumpyHdf5Writer, load_manifest_lazy, load_manifest
from lhotse.audio import Recording, RecordingSet
from lhotse.supervision import SupervisionSegment, SupervisionSet
from lhotse.recipes.utils import read_manifests_if_cached
from lhotse.utils import Seconds, compute_num_frames
from functools import partial
from modules.tokenizer import (
HIFIGAN_SR,
HIFIGAN_HOP_LENGTH,
MelSpecExtractor,
AudioFeatExtraConfig
)
from models.megatts2 import MegaG
from modules.datamodule import TTSDataset, make_spk_cutset
from utils.symbol_table import SymbolTable
import soundfile as sf
import librosa
import torch
import numpy as np
def make_lab(tt, wav):
id = wav.split('/')[-1].split('.')[0]
folder = '/'.join(wav.split('/')[:-1])
# Create lab files
with open(f'{folder}/{id}.txt', 'r') as f:
txt = f.read()
with open(f'{folder}/{id}.lab', 'w') as f:
f.write(' '.join(tt.tokenize(txt)))
class DatasetMaker:
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument('--stage', type=int, default=0,
help='Stage to start from')
parser.add_argument('--wavtxt_path', type=str,
default='data/wavs/', help='Path to wav and txt files')
parser.add_argument('--text_grid_path', type=str,
default='data/textgrids/', help='Path to textgrid files')
parser.add_argument('--ds_path', type=str,
default='data/ds/', help='Path to save dataset')
parser.add_argument('--num_workers', type=int,
default=4, help='Number of workers')
parser.add_argument('--test_set_ratio', type=float,
default=0.03, help='Test set ratio')
parser.add_argument('--trim_wav', type=bool,
default=False, help='Trim wav by textgrid')
parser.add_argument('--generator_ckpt', type=str,
default='generator.ckpt', help='Load generator checkpoint')
parser.add_argument('--generator_config', type=str,
default='configs/config_gan.yaml', help='Load generator config')
self.args = parser.parse_args()
self.test_set_interval = int(1 / self.args.test_set_ratio)
def make_labs(self):
wavs = glob.glob(f'{self.args.wavtxt_path}/**/*.wav', recursive=True)
tt = TextTokenizer()
with Pool(self.args.num_workers) as p:
for _ in tqdm(p.imap(partial(make_lab, tt), wavs), total=len(wavs)):
pass
def make_ds(self):
tgs = glob.glob(
f'{self.args.text_grid_path}/**/*.TextGrid', recursive=True)
recordings = [[], []] # train, test
supervisions = [[], []]
set_name = ['train', 'valid']
max_duration_token = 0
for i, tg in tqdm(enumerate(tgs)):
id = tg.split('/')[-1].split('.')[0]
speaker = tg.split('/')[-2]
intervals = [i for i in read_textgrid(tg) if (i[3] == 'phones')]
y, sr = librosa.load(
f'{self.args.wavtxt_path}/{speaker}/{id}.wav', sr=HIFIGAN_SR)
if intervals[0][2] == '':
intervals = intervals[1:]
if intervals[-1][2] == '':
intervals = intervals[:-1]
if self.args.trim_wav:
start = intervals[0][0]*sr
stop = intervals[-1][1]*sr
y = y[int(start):int(stop)]
y = librosa.util.normalize(y)
sf.write(
f'{self.args.wavtxt_path}/{speaker}/{id}.wav', y, HIFIGAN_SR)
start = intervals[0][0]
stop = intervals[-1][1]
frame_shift=HIFIGAN_HOP_LENGTH / HIFIGAN_SR
duration = round(y.shape[-1] / HIFIGAN_SR, ndigits=12)
n_frames = compute_num_frames(
duration=duration,
frame_shift=frame_shift,
sampling_rate=HIFIGAN_SR,
)
duration_tokens = []
phone_tokens = []
for i, interval in enumerate(intervals):
phone_stop = (interval[1] - start)
n_frame_interval = int(phone_stop / frame_shift)
duration_tokens.append(n_frame_interval - sum(duration_tokens))
phone_tokens.append(interval[2] if interval[2] != '' else '<sil>')
if sum(duration_tokens) > n_frames:
print(f'{id} duration_tokens: {sum(duration_tokens)} must <= n_frames: {n_frames}')
assert False
recording = Recording.from_file(
f'{self.args.wavtxt_path}/{speaker}/{id}.wav')
text = open(
f'{self.args.wavtxt_path}/{speaker}/{id}.txt', 'r').read()
segment = SupervisionSegment(
id=id,
recording_id=id,
start=0,
duration=recording.duration,
channel=0,
language="CN",
speaker=speaker,
text=text,
)
if abs(recording.duration - (stop - start)) > 0.01:
print(f'{id} recording duration: {recording.duration} != {stop - start}')
assert False
set_id = 0 if i % self.test_set_interval else 1
recordings[set_id].append(recording)
supervisions[set_id].append(segment)
segment.custom = {}
segment.custom['duration_tokens'] = duration_tokens
segment.custom['phone_tokens'] = phone_tokens
max_duration_token = max(max_duration_token, len(duration_tokens))
assert len(duration_tokens) == len(phone_tokens)
for i in range(2):
recording_set = RecordingSet.from_recordings(recordings[i])
supervision_set = SupervisionSet.from_segments(supervisions[i])
validate_recordings_and_supervisions(
recording_set, supervision_set)
supervision_set.to_file(
f"{self.args.ds_path}/supervisions_{set_name[i]}.jsonl.gz")
recording_set.to_file(
f"{self.args.ds_path}/recordings_{set_name[i]}.jsonl.gz")
# Extract features
manifests = read_manifests_if_cached(
dataset_parts=['train', 'valid'],
output_dir=self.args.ds_path,
prefix="",
suffix='jsonl.gz',
types=["recordings", "supervisions"],
)
for partition, m in manifests.items():
cut_set = CutSet.from_manifests(
recordings=m["recordings"],
supervisions=m["supervisions"],
)
# extract
cut_set = cut_set.compute_and_store_features(
extractor=MelSpecExtractor(AudioFeatExtraConfig()),
storage_path=f"{self.args.ds_path}/cuts_{partition}",
storage_type=NumpyHdf5Writer,
num_jobs=self.args.num_workers,
)
cut_set.to_file(
f"{self.args.ds_path}/cuts_{partition}.jsonl.gz")
print(f'max_duration_token: {max_duration_token}')
def extract_latent(self):
os.system(f'mkdir -p {self.args.ds_path}/latents')
G = MegaG.from_pretrained(dm.args.generator_ckpt, dm.args.generator_config)
G = G.cuda()
G.eval()
cs_all = load_manifest(f'{dm.args.ds_path}/cuts_train.jsonl.gz') + load_manifest(f'{dm.args.ds_path}/cuts_valid.jsonl.gz')
spk_cs = make_spk_cutset(cs_all)
for spk in spk_cs.keys():
os.system(f'mkdir -p {self.args.ds_path}/latents/{spk}')
ttsds = TTSDataset(spk_cs, f'{dm.args.ds_path}', 10)
for c in tqdm(cs_all):
id = c.recording_id
spk = c.supervisions[0].speaker
batch = ttsds.__getitem__(CutSet.from_cuts([c]))
s2_latent = {}
with torch.no_grad():
tc_latent, p_code = G.s2_latent(
batch['phone_tokens'].cuda(),
batch['tokens_lens'].cuda(),
batch['mel_timbres'].cuda(),
batch['mel_targets'].cuda()
)
s2_latent['tc_latent'] = tc_latent.cpu().numpy()
s2_latent['p_code'] = p_code.cpu().numpy()
np.save(f'{self.args.ds_path}/latents/{spk}/{id}.npy', s2_latent)
if __name__ == '__main__':
dm = DatasetMaker()
# Create lab files
if dm.args.stage == 0:
dm.make_labs()
elif dm.args.stage == 1:
dm.make_ds()
# Test
cs_train = load_manifest_lazy(
f'{dm.args.ds_path}/cuts_train.jsonl.gz')
cs_valid = load_manifest_lazy(
f'{dm.args.ds_path}/cuts_valid.jsonl.gz')
cs = cs_train + cs_valid
unique_symbols = set()
for c in tqdm(cs):
unique_symbols.update(c.supervisions[0].custom["phone_tokens"])
unique_phonemes = SymbolTable()
for s in sorted(list(unique_symbols)):
unique_phonemes.add(s)
unique_phonemes_file = f"unique_text_tokens.k2symbols"
unique_phonemes.to_file(f'{dm.args.ds_path}/{unique_phonemes_file}')
print(cs.describe())
elif dm.args.stage == 2:
dm.extract_latent()