-
Notifications
You must be signed in to change notification settings - Fork 29
/
datamodule.py
186 lines (155 loc) · 6.45 KB
/
datamodule.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
'''
EfficientSpeech: An On-Device Text to Speech Model
https://ieeexplore.ieee.org/abstract/document/10094639
Rowel Atienza, 2023
Apache 2.0 License
'''
import json
import torch
import os
import numpy as np
from torch.utils.data import Dataset, DataLoader
from text import text_to_sequence
from utils.tools import pad_1D, pad_2D
from lightning import LightningDataModule
from utils.tools import get_mask_from_lengths
class LJSpeechDataModule(LightningDataModule):
def __init__(self, preprocess_config, batch_size=64, num_workers=4):
super(LJSpeechDataModule, self).__init__()
self.preprocess_config = preprocess_config
self.batch_size = batch_size
self.num_workers = num_workers
#self.drop_last = True
self.sort = True
def collate_fn(self, batch):
x, y = zip(*batch)
len_arr = np.array([d["phoneme"].shape[0] for d in x])
idxs = np.argsort(-len_arr).tolist()
phonemes = [x[idx]["phoneme"] for idx in idxs]
texts = [x[idx]["text"] for idx in idxs]
mels = [y[idx]["mel"] for idx in idxs]
pitches = [x[idx]["pitch"] for idx in idxs]
energies = [x[idx]["energy"] for idx in idxs]
durations = [x[idx]["duration"] for idx in idxs]
phoneme_lens = np.array([phoneme.shape[0] for phoneme in phonemes])
mel_lens = np.array([mel.shape[0] for mel in mels])
phonemes = pad_1D(phonemes)
mels = pad_2D(mels)
pitches = pad_1D(pitches)
energies = pad_1D(energies)
durations = pad_1D(durations)
phonemes = torch.from_numpy(phonemes).int()
phoneme_lens = torch.from_numpy(phoneme_lens).int()
max_phoneme_len = torch.max(phoneme_lens).item()
phoneme_mask = get_mask_from_lengths(phoneme_lens, max_phoneme_len)
pitches = torch.from_numpy(pitches).float()
energies = torch.from_numpy(energies).float()
durations = torch.from_numpy(durations).int()
mels = torch.from_numpy(mels).float()
mel_lens = torch.from_numpy(mel_lens).int()
max_mel_len = torch.max(mel_lens).item()
mel_mask = get_mask_from_lengths(mel_lens, max_mel_len)
x = {"phoneme": phonemes,
"phoneme_len": phoneme_lens,
"phoneme_mask": phoneme_mask,
"text": texts,
"mel_len": mel_lens,
"mel_mask": mel_mask,
"pitch": pitches,
"energy": energies,
"duration": durations,}
y = {"mel": mels,}
return x, y
def prepare_data(self):
self.train_dataset = LJSpeechDataset("train.txt",
self.preprocess_config)
#print("Train dataset size: {}".format(len(self.train_dataset)))
self.test_dataset = LJSpeechDataset("val.txt",
self.preprocess_config)
#print("Test dataset size: {}".format(len(self.test_dataset)))
def setup(self, stage=None):
self.prepare_data()
def train_dataloader(self):
self.train_dataloader = DataLoader(self.train_dataset,
shuffle=True,
batch_size=self.batch_size,
collate_fn=self.collate_fn,
num_workers=self.num_workers)
return self.train_dataloader
def test_dataloader(self):
self.test_dataloader = DataLoader(self.test_dataset,
shuffle=False,
batch_size=self.batch_size,
collate_fn=self.collate_fn,
num_workers=self.num_workers)
return self.test_dataloader
def val_dataloader(self):
return self.test_dataloader()
class LJSpeechDataset(Dataset):
def __init__(self, filename, preprocess_config, sort=False, drop_last=False):
self.dataset_name = preprocess_config["dataset"]
self.preprocessed_path = preprocess_config["path"]["preprocessed_path"]
self.cleaners = preprocess_config["preprocessing"]["text"]["text_cleaners"]
#self.batch_size = batch_size
self.max_text_length = preprocess_config["preprocessing"]["text"]["max_length"]
self.basename, self.speaker, self.text, self.raw_text = self.process_meta(filename)
with open(os.path.join(self.preprocessed_path, "speakers.json")) as f:
self.speaker_map = json.load(f)
self.sort = sort
self.drop_last = drop_last
def __len__(self):
return len(self.text)
def __getitem__(self, idx):
basename = self.basename[idx]
speaker = self.speaker[idx]
#speaker_id = self.speaker_map[speaker]
raw_text = self.raw_text[idx]
phoneme = np.array(text_to_sequence(self.text[idx], self.cleaners))
mel_path = os.path.join(
self.preprocessed_path,
"mel",
"{}-mel-{}.npy".format(speaker, basename),
)
mel = np.load(mel_path)
pitch_path = os.path.join(
self.preprocessed_path,
"pitch",
"{}-pitch-{}.npy".format(speaker, basename),
)
pitch = np.load(pitch_path)
energy_path = os.path.join(
self.preprocessed_path,
"energy",
"{}-energy-{}.npy".format(speaker, basename),
)
energy = np.load(energy_path)
duration_path = os.path.join(
self.preprocessed_path,
"duration",
"{}-duration-{}.npy".format(speaker, basename),
)
duration = np.load(duration_path)
x = {"phoneme": phoneme,
"text": raw_text,
"pitch": pitch,
"energy": energy,
"duration": duration}
y = {"mel": mel,}
return x, y
def process_meta(self, filename):
with open(
os.path.join(self.preprocessed_path, filename), "r", encoding="utf-8"
) as f:
name = []
speaker = []
text = []
raw_text = []
for line in f.readlines():
n, s, t, r = line.strip("\n").split("|")
if len(r) > self.max_text_length:
continue
name.append(n)
speaker.append(s)
text.append(t)
raw_text.append(r)
return name, speaker, text, raw_text