-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdataset.py
More file actions
618 lines (520 loc) · 19.8 KB
/
Copy pathdataset.py
File metadata and controls
618 lines (520 loc) · 19.8 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
import random
import shutil
import string
from concurrent.futures import ThreadPoolExecutor
from dataclasses import asdict, dataclass
from os import PathLike
from pathlib import Path
from typing import Callable, Generator
import numpy as np
import torch
import yaml
from torch.utils.data import DataLoader, Dataset, random_split
from tqdm import tqdm
from config import Tacotron2AudioConfig
from preprocess import Tacotron2Preprocessor, Tacotron2Sample
RANDOM_STRINGS = string.ascii_lowercase + string.digits
@dataclass
class Tacotron2Batch:
tokens: torch.Tensor
token_lengths: torch.Tensor
texts: list[str]
mels: torch.Tensor
mel_lengths: torch.Tensor
mel_max_length: int
mel_pad: int
gates: torch.Tensor
embeds: torch.Tensor
def to(self, device: torch.device | str) -> "Tacotron2Batch":
self.tokens = self.tokens.to(device)
self.token_lengths = self.token_lengths.to(device)
self.mels = self.mels.to(device)
self.mel_lengths = self.mel_lengths.to(device)
self.gates = self.gates.to(device)
self.embeds = self.embeds.to(device)
return self
@classmethod
def make_batch(
cls,
items: Generator[tuple[np.ndarray, np.ndarray, str, np.ndarray], None, None],
frames_per_step: int = 2,
mel_stats: tuple[float, float] | None = None,
):
mels: list[np.ndarray]
tokens: list[np.ndarray]
texts: list[str]
embeds: list[np.ndarray]
mels, tokens, texts, embeds = zip(*items) # type: ignore
token_lengths = np.array([len(token) for token in tokens])
mel_lengths = np.array([mel.shape[0] for mel in mels])
token_max_length = int(max(token_lengths))
mel_max_length = int(max(mel_lengths))
mel_pad = mel_max_length % frames_per_step
if mel_pad != 0:
mel_max_length += frames_per_step - mel_pad
assert mel_max_length % frames_per_step == 0
gates = np.zeros([mel_lengths.shape[0], mel_max_length])
for i, mel_len in enumerate(mel_lengths):
gates[i, mel_len - 1 :] = 1
mels = torch.from_numpy(
np.stack(
[
np.pad( # type: ignore
x,
(0, mel_max_length - np.shape(x)[0]), # type: ignore
mode="constant",
constant_values=0,
)[
:, : np.shape(x)[1] # type: ignore
]
for x in mels
]
)
).float()
if mel_stats:
mel_min, mel_max = mel_stats
mels = 2 * (mels - mel_min) / (mel_max - mel_min) - 1 # type: ignore
return Tacotron2Batch(
tokens=torch.from_numpy(
np.stack(
[
np.pad( # type: ignore
x,
(0, token_max_length - x.shape[0]),
mode="constant",
constant_values=0,
)
for x in tokens
]
)
).long(),
token_lengths=torch.from_numpy(token_lengths),
texts=texts,
mels=mels, # type: ignore
mel_lengths=torch.from_numpy(mel_lengths),
mel_max_length=mel_max_length,
mel_pad=mel_pad,
gates=torch.from_numpy(gates).float(),
embeds=torch.from_numpy(np.concatenate(embeds, axis=0)).float(),
)
@dataclass
class Tacotron2SpeakerEmbeds:
count: int = 0
embed: np.ndarray | None = None
def __post_init__(self):
self.count = int(self.count)
def update(self, embed: np.ndarray):
if self.embed is None:
self.embed = embed
else:
assert (
self.embed.shape == embed.shape
), f"Embed shape must match ({self.embed.shape} != {embed.shape})"
self.count += 1
self.embed += (embed - self.embed) / (self.count)
def __call__(self) -> np.ndarray:
assert self.embed is not None, "No embeds"
return self.embed
class Tacotron2SpeakerDataset:
path: Path
config: Tacotron2AudioConfig | None
audios: list[str]
embeds: Tacotron2SpeakerEmbeds
def __init__(
self,
path: str | PathLike,
config: Tacotron2AudioConfig | None = None,
ensure_config: bool = False,
):
self.path = Path(path)
embeds_path = self.embeds_path
config_path = self.config_path
if ensure_config:
assert embeds_path.exists(), f"{embeds_path} does not exist"
assert config_path.exists(), f"{config_path} does not exist"
self.audios = list(f.name for f in self.path.glob("*.npz") if f.resolve() != embeds_path)
self.embeds = Tacotron2SpeakerEmbeds(**(np.load(embeds_path) if embeds_path.exists() else {})) # type: ignore
if config_path.exists():
with open(config_path, "r", encoding="utf-8") as f:
self.config = Tacotron2AudioConfig(**yaml.safe_load(f))
if config:
assert self.config == config, f"Config must match ({self.config} != {config})"
else:
self.config = config
@property
def embeds_path(self):
return (self.path / "embeds.npz").resolve()
@property
def config_path(self):
return (self.path / "data.yaml").resolve()
def __len__(self):
return len(self.audios)
def __getitem__(self, index: int, text_index: int | None = None):
sample = Tacotron2Sample.load(self.path / self.audios[index])
text_index = text_index or random.randrange(len(sample.tokens))
return (
sample.mel,
sample.tokens[text_index].astype(np.int64),
sample.decode(text_index),
self.embeds(),
)
def save(self):
assert self.config is not None, "No config"
np.savez(self.embeds_path, **asdict(self.embeds))
with open(self.config_path, "w", encoding="utf-8") as f:
yaml.safe_dump(asdict(self.config), f)
def extend(
self,
preprocessor: Tacotron2Preprocessor,
audios: str | PathLike | list[str | PathLike],
labels: str
| Path
| list[str | Path | None]
| Callable[[Path], str | list[str] | None]
| None = None,
*,
max_workers: int | None = None,
compress: bool = False,
test_count: int = 2,
audio_pattern: str = "*.wav",
label_suffix: str = ".txt",
encoding: str | None = "utf-8",
ensure_sampling_rate: bool = False,
skip_not_found: bool = False,
speaker_name: str | None = "",
verbose: bool = True,
) -> float:
if self.config:
assert self.config == preprocessor.config, (
"Preprocessor config must match dataset config "
f"({self.config} != {preprocessor.config})"
)
else:
self.config = preprocessor.config
tests_path = self.path / "tests"
embeds_path = self.embeds_path.resolve()
self.path.mkdir(parents=True, exist_ok=True)
tests_path.mkdir(parents=True, exist_ok=True)
def process(args: tuple[int, str | PathLike]):
i, audio_path = args
audio_path = Path(audio_path)
if not audio_path.exists():
assert skip_not_found, f"{audio_path} does not exist"
if verbose:
print(f"Skipping {audio_path}: Does not exist")
return None
# Get label
if callable(labels):
label = labels(audio_path)
elif isinstance(labels, (list, tuple)):
label = labels[i]
else:
label = labels
if not label:
label = audio_path.with_suffix(label_suffix)
if isinstance(label, Path):
if label.exists():
label = label.read_text(encoding=encoding)
else:
assert skip_not_found, f"{label} does not exist"
label = None
if not label:
if verbose:
print(f"Skipping {audio_path}: No label")
return None
# Process
sample = preprocessor.process(
label,
audio_path,
ensure_sampling_rate=ensure_sampling_rate,
)
# Save
file_path = self.path / f"{audio_path.stem}.npz"
if embeds_path == file_path.resolve():
file_path = self.path / f"{audio_path.stem}_.npz"
while file_path.exists():
file_path = (
self.path / f"{audio_path.stem}_{''.join(random.sample(RANDOM_STRINGS, 6))}.npz"
)
sample.save(file_path, compress=compress)
# Test
if i in tests:
sample.save_test(tests_path / audio_path.stem, preprocessor.config.sampling_rate)
return (
file_path,
sample.embed,
sample.duration(preprocessor.config.hop_length, preprocessor.config.sampling_rate),
)
with ThreadPoolExecutor(max_workers=max_workers) as executor:
if not isinstance(audios, list):
audios = list(Path(audios).glob(audio_pattern))
audio_list = []
avg = len(audios) // (test_count or 1)
tests = set(int(avg * i + avg / 2) for i in range(test_count))
total_time = 0
for result in tqdm(
executor.map(process, enumerate(audios)),
desc=f"Speaker {speaker_name}",
disable=not verbose,
leave=False,
total=len(audios),
):
if not result:
continue
sample, embed, duration = result
assert embed is not None, "No embed"
total_time += duration
self.embeds.update(embed)
audio_list.append(sample.name)
self.audios = list(set(self.audios) | set(audio_list))
self.save()
print(f"\nSpeaker {speaker_name}: {len(audios)} audios, {total_time / 60 / 60:.2f} hours")
return total_time
class Tacotron2Dataset(Dataset):
path: Path
freezn: bool = False
speakers: dict[str, Tacotron2SpeakerDataset]
cached_length: int | None = None
cached_indices: dict[str, int] | None = None
_config: Tacotron2AudioConfig | None
def __init__(
self,
path: str | PathLike,
config: Tacotron2AudioConfig | None = None,
exclude: set[str] | None = None,
ensure_config: bool = True,
skip_incomplete: bool = True,
delete_incomplete: bool = False,
):
self.path = Path(path)
self.speakers = {}
if self.path.exists():
for speaker_path in self.path.iterdir():
if not speaker_path.is_dir() or (exclude and speaker_path.name in exclude):
continue
try:
dataset = self.speakers[speaker_path.name] = Tacotron2SpeakerDataset(
speaker_path,
ensure_config=ensure_config or skip_incomplete or delete_incomplete,
)
except AssertionError as e:
if delete_incomplete:
print(f"Deleting incomplete dataset {speaker_path}: {e}")
shutil.rmtree(speaker_path)
continue
if skip_incomplete:
print(f"Skipping incomplete dataset {speaker_path}: {e}")
continue
else:
raise e
if config:
assert dataset.config == config, (
"Dataset config must match config " f"({dataset.config} != {config})"
)
config = dataset.config
self._config = config
@property
def config(self):
assert self._config is not None, "Config is not set"
return self._config
def freeze(self):
self.freezn = True
self.cached_length = None
self.cached_length = self.__len__()
indices = {}
total = 0
for key, speaker in self.speakers.items():
indices[key] = total
total += len(speaker)
self.cached_indices = indices
def unfreeze(self):
self.freezn = False
self.cached_length = None
self.cached_indices = None
def remove(self, speaker_name: str, delete: bool = False):
dataset = self.speakers.pop(speaker_name, None)
assert dataset is not None, f"Speaker {speaker_name} does not exist"
if delete:
shutil.rmtree(dataset.path)
def clear(self, delete: bool = False):
for speaker_name in list(self.speakers.keys()):
self.remove(speaker_name, delete=delete)
def append(
self,
preprocessor: Tacotron2Preprocessor,
audios: str | PathLike | list[str | PathLike],
labels: str
| Path
| list[str | Path | None]
| Callable[[Path], str | list[str] | None]
| None = None,
speaker_name: str | None = None,
*,
max_workers: int | None = None,
compress: bool = False,
test_count: int = 2,
audio_pattern: str = "*.wav",
label_suffix: str = ".txt",
encoding: str | None = "utf-8",
ensure_sampling_rate: bool = False,
skip_not_found: bool = False,
verbose: bool = True,
) -> float:
assert not self.freezn, "Dataset is frozen"
if self._config:
assert self._config == preprocessor.config, (
"Preprocessor config must match dataset config "
f"({self._config} != {preprocessor.config})"
)
else:
self._config = preprocessor.config
if isinstance(audios, list):
assert speaker_name is not None, "Speaker name must be provided"
elif speaker_name is None:
speaker_name = Path(audios).name
speaker_path = self.path / speaker_name
dataset = Tacotron2SpeakerDataset(speaker_path, config=self._config)
total_time = dataset.extend(
preprocessor,
audios,
labels,
max_workers=max_workers,
compress=compress,
test_count=test_count,
audio_pattern=audio_pattern,
label_suffix=label_suffix,
encoding=encoding,
ensure_sampling_rate=ensure_sampling_rate,
skip_not_found=skip_not_found,
speaker_name=speaker_name,
verbose=verbose,
)
self.speakers[speaker_name] = dataset
return total_time
def extend(
self,
preprocessor: Tacotron2Preprocessor,
speakers: str
| PathLike
| list[str | PathLike]
| dict[str, str | PathLike | list[str | PathLike]],
labels: str
| Path
| list[str | Path | None]
| Callable[[Path], str | list[str] | None]
| dict[
str | None,
str | Path | list[str | Path | None] | Callable[[Path], str | list[str] | None] | None,
]
| None = None,
*,
max_workers: int | None = None,
compress: bool = False,
test_count: int = 2,
audio_pattern: str = "*.wav",
label_suffix: str = ".txt",
encoding: str | None = "utf-8",
ensure_sampling_rate: bool = False,
skip_not_found: bool = False,
verbose: bool = True,
) -> float:
if isinstance(speakers, list):
speakers = {speaker.name: speaker for speaker in map(Path, speakers) if speaker.is_dir()}
elif not isinstance(speakers, dict):
speakers = {
speaker.name: speaker for speaker in Path(speakers).iterdir() if speaker.is_dir()
}
if isinstance(labels, list):
labels = {speaker: labels[i] for i, speaker in enumerate(speakers.keys())}
elif callable(labels):
labels = {speaker: labels for speaker in speakers.keys()}
elif not isinstance(labels, dict) and labels is not None:
labels = Path(labels)
labels = {speaker: labels / speaker for speaker in speakers.keys()}
total_time = 0
for result in tqdm(
(
self.append(
preprocessor,
speaker_name=name,
audios=audios,
labels=labels.get(name, labels[None]) if labels else None, # type: ignore
max_workers=max_workers,
compress=compress,
test_count=test_count,
audio_pattern=audio_pattern,
label_suffix=label_suffix,
encoding=encoding,
ensure_sampling_rate=ensure_sampling_rate,
skip_not_found=skip_not_found,
verbose=verbose,
)
for name, audios in speakers.items()
),
desc="Speakers",
disable=not verbose,
leave=False,
total=len(speakers),
):
total_time += result
if verbose:
print(f"Total time: {total_time / 60 / 60:.2f} hours")
return total_time
def __len__(self):
if self.freezn and self.cached_length is not None:
return self.cached_length
length = sum(len(s) for s in self.speakers.values())
if self.freezn:
self.cached_length = length
return length
def __getitem__(self, index: int):
if self.freezn and self.cached_indices is not None:
for key, start_index in self.cached_indices.items():
if start_index <= index < start_index + len(self.speakers[key]):
return self.speakers[key][index - start_index]
else:
for speaker in self.speakers.values():
if index < len(speaker):
return speaker[index]
index -= len(speaker)
raise IndexError(f"Index {index} out of range")
def loaders(
self,
batch_size: int,
num_workers: int = 0,
shuffle: bool = True,
validation_split: float | int = 0.05,
) -> tuple[DataLoader, DataLoader]:
total_size = len(self)
assert total_size > 0, "Dataset is empty"
validation_size = (
int(total_size * validation_split) if validation_split < 1 else int(validation_split)
)
assert (
batch_size < validation_size and batch_size < total_size - validation_size
), "Batch size is too large"
train_dataset, validation_dataset = random_split(
self, [total_size - validation_size, validation_size]
)
return (
DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=shuffle,
collate_fn=_collate_fn,
num_workers=num_workers,
),
DataLoader(
validation_dataset,
batch_size=batch_size,
shuffle=False,
collate_fn=_collate_fn,
num_workers=num_workers,
),
)
def _collate_fn(batch: list[tuple[np.ndarray, np.ndarray, str, np.ndarray]]):
lengths = np.array([i[1].shape[0] for i in batch]) # token length
indexs = np.argsort(-lengths) # sort by token length
return Tacotron2Batch.make_batch(
(batch[i] for i in indexs), # type: ignore
)