-
Notifications
You must be signed in to change notification settings - Fork 698
/
Copy pathdataset.py
222 lines (153 loc) · 6.56 KB
/
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
import pickle
import random
import torch
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.sampler import Sampler
from utils.dsp import *
from utils import hparams as hp
from utils.text import text_to_sequence
from utils.paths import Paths
from pathlib import Path
###################################################################################
# WaveRNN/Vocoder Dataset #########################################################
###################################################################################
class VocoderDataset(Dataset):
def __init__(self, path: Path, dataset_ids, train_gta=False):
self.metadata = dataset_ids
self.mel_path = path/'gta' if train_gta else path/'mel'
self.quant_path = path/'quant'
def __getitem__(self, index):
item_id = self.metadata[index]
m = np.load(self.mel_path/f'{item_id}.npy')
x = np.load(self.quant_path/f'{item_id}.npy')
return m, x
def __len__(self):
return len(self.metadata)
def get_vocoder_datasets(path: Path, batch_size, train_gta):
with open(path/'dataset.pkl', 'rb') as f:
dataset = pickle.load(f)
dataset_ids = [x[0] for x in dataset]
random.seed(1234)
random.shuffle(dataset_ids)
test_ids = dataset_ids[-hp.voc_test_samples:]
train_ids = dataset_ids[:-hp.voc_test_samples]
train_dataset = VocoderDataset(path, train_ids, train_gta)
test_dataset = VocoderDataset(path, test_ids, train_gta)
train_set = DataLoader(train_dataset,
collate_fn=collate_vocoder,
batch_size=batch_size,
num_workers=2,
shuffle=True,
pin_memory=True)
test_set = DataLoader(test_dataset,
batch_size=1,
num_workers=1,
shuffle=False,
pin_memory=True)
return train_set, test_set
def collate_vocoder(batch):
mel_win = hp.voc_seq_len // hp.hop_length + 2 * hp.voc_pad
max_offsets = [x[0].shape[-1] -2 - (mel_win + 2 * hp.voc_pad) for x in batch]
mel_offsets = [np.random.randint(0, offset) for offset in max_offsets]
sig_offsets = [(offset + hp.voc_pad) * hp.hop_length for offset in mel_offsets]
mels = [x[0][:, mel_offsets[i]:mel_offsets[i] + mel_win] for i, x in enumerate(batch)]
labels = [x[1][sig_offsets[i]:sig_offsets[i] + hp.voc_seq_len + 1] for i, x in enumerate(batch)]
mels = np.stack(mels).astype(np.float32)
labels = np.stack(labels).astype(np.int64)
mels = torch.tensor(mels)
labels = torch.tensor(labels).long()
x = labels[:, :hp.voc_seq_len]
y = labels[:, 1:]
bits = 16 if hp.voc_mode == 'MOL' else hp.bits
x = label_2_float(x.float(), bits)
if hp.voc_mode == 'MOL':
y = label_2_float(y.float(), bits)
return x, y, mels
###################################################################################
# Tacotron/TTS Dataset ############################################################
###################################################################################
def get_tts_datasets(path: Path, batch_size, r):
with open(path/'dataset.pkl', 'rb') as f:
dataset = pickle.load(f)
dataset_ids = []
mel_lengths = []
for (item_id, len) in dataset:
if len <= hp.tts_max_mel_len:
dataset_ids += [item_id]
mel_lengths += [len]
with open(path/'text_dict.pkl', 'rb') as f:
text_dict = pickle.load(f)
train_dataset = TTSDataset(path, dataset_ids, text_dict)
sampler = None
if hp.tts_bin_lengths:
sampler = BinnedLengthSampler(mel_lengths, batch_size, batch_size * 3)
train_set = DataLoader(train_dataset,
collate_fn=lambda batch: collate_tts(batch, r),
batch_size=batch_size,
sampler=sampler,
num_workers=1,
pin_memory=True)
longest = mel_lengths.index(max(mel_lengths))
# Used to evaluate attention during training process
attn_example = dataset_ids[longest]
# print(attn_example)
return train_set, attn_example
class TTSDataset(Dataset):
def __init__(self, path: Path, dataset_ids, text_dict):
self.path = path
self.metadata = dataset_ids
self.text_dict = text_dict
def __getitem__(self, index):
item_id = self.metadata[index]
x = text_to_sequence(self.text_dict[item_id], hp.tts_cleaner_names)
mel = np.load(self.path/'mel'/f'{item_id}.npy')
mel_len = mel.shape[-1]
return x, mel, item_id, mel_len
def __len__(self):
return len(self.metadata)
def pad1d(x, max_len):
return np.pad(x, (0, max_len - len(x)), mode='constant')
def pad2d(x, max_len):
return np.pad(x, ((0, 0), (0, max_len - x.shape[-1])), mode='constant')
def collate_tts(batch, r):
x_lens = [len(x[0]) for x in batch]
max_x_len = max(x_lens)
chars = [pad1d(x[0], max_x_len) for x in batch]
chars = np.stack(chars)
spec_lens = [x[1].shape[-1] for x in batch]
max_spec_len = max(spec_lens) + 1
if max_spec_len % r != 0:
max_spec_len += r - max_spec_len % r
mel = [pad2d(x[1], max_spec_len) for x in batch]
mel = np.stack(mel)
ids = [x[2] for x in batch]
mel_lens = [x[3] for x in batch]
chars = torch.tensor(chars).long()
mel = torch.tensor(mel)
# scale spectrograms to -4 <--> 4
mel = (mel * 8.) - 4.
return chars, mel, ids, mel_lens
class BinnedLengthSampler(Sampler):
def __init__(self, lengths, batch_size, bin_size):
_, self.idx = torch.sort(torch.tensor(lengths).long())
self.batch_size = batch_size
self.bin_size = bin_size
assert self.bin_size % self.batch_size == 0
def __iter__(self):
# Need to change to numpy since there's a bug in random.shuffle(tensor)
# TODO: Post an issue on pytorch repo
idx = self.idx.numpy()
bins = []
for i in range(len(idx) // self.bin_size):
this_bin = idx[i * self.bin_size:(i + 1) * self.bin_size]
random.shuffle(this_bin)
bins += [this_bin]
random.shuffle(bins)
binned_idx = np.stack(bins).reshape(-1)
if len(binned_idx) < len(idx):
last_bin = idx[len(binned_idx):]
random.shuffle(last_bin)
binned_idx = np.concatenate([binned_idx, last_bin])
return iter(torch.tensor(binned_idx).long())
def __len__(self):
return len(self.idx)