forked from G-Wang/WaveRNN-Pytorch
-
Notifications
You must be signed in to change notification settings - Fork 37
/
dataset.py
237 lines (182 loc) · 7.86 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import numpy as np
import os
import torch
from torch.utils.data import DataLoader, Dataset
from hparams import hparams as hp
from utils import mulaw_quantize, inv_mulaw_quantize
import pickle
import csv
from audio import quantize
class AudiobookDataset(Dataset):
def __init__(self, data_path):
self.path = os.path.join(data_path, "")
with open(os.path.join(self.path,'dataset_ids.pkl'), 'rb') as f:
self.metadata = pickle.load(f)
self.mel_path = os.path.join(data_path, "mel")
self.wav_path = os.path.join(data_path, "wav")
self.test_path = os.path.join(data_path, "test")
def __getitem__(self, index):
file = self.metadata[index]
m = np.load(os.path.join(self.mel_path,'{}.npy'.format(file)))
x = np.load(os.path.join(self.wav_path,'{}.npy'.format(file)))
return m, x
def __len__(self):
return len(self.metadata)
class TacotronDataset(Dataset):
def __init__(self, data_path):
self.metadata=[]
self.path = os.path.join(data_path, "")
with open(os.path.join(self.path,'train.txt'), 'r', newline='') as f:
csvreader = csv.reader(f, delimiter='|')
for row in csvreader:
self.metadata.append(row)
self.mel_path = os.path.join(data_path, "mels")
self.wav_path = os.path.join(data_path, "audio")
self.test_path = os.path.join(data_path, "mels")
def __getitem__(self, index):
entry = self.metadata[index]
m = np.load(os.path.join(self.mel_path, entry[1])).T
wav = np.load(os.path.join(self.wav_path, entry[0]))
if hp.input_type == 'raw' or hp.input_type=='mixture':
wav = wav.astype(np.float32)
elif hp.input_type == 'mulaw':
wav = mulaw_quantize(wav, hp.mulaw_quantize_channels).astype(np.int)
elif hp.input_type == 'bits':
wav = quantize(wav).astype(np.int)
else:
raise ValueError("hp.input_type {} not recognized".format(hp.input_type))
return m, wav
def __len__(self):
return len(self.metadata)
class Tacotron2Dataset(Dataset):
def __init__(self, data_path):
self.metadata=[]
self.path = os.path.join(data_path, "")
with open(os.path.join(self.path,'train.txt'), 'r', newline='') as f:
csvreader = csv.reader(f, delimiter='|')
for row in csvreader:
self.metadata.append(row)
self.mel_path = os.path.join(data_path, "mels")
self.wav_path = os.path.join(data_path, "audio")
self.test_path = os.path.join(data_path, "mels")
def __getitem__(self, index):
entry = self.metadata[index]
m = np.load(os.path.join(self.mel_path, entry[1])).T
wav = np.load(os.path.join(self.wav_path, entry[0]))
if hp.input_type == 'raw' or hp.input_type=='mixture':
wav = wav.astype(np.float32)
elif hp.input_type == 'mulaw':
wav = mulaw_quantize(wav, hp.mulaw_quantize_channels).astype(np.int)
elif hp.input_type == 'bits':
wav = quantize(wav).astype(np.int)
else:
raise ValueError("hp.input_type {} not recognized".format(hp.input_type))
return m, wav
def __len__(self):
return len(self.metadata)
class MozillaTTS(Dataset):
def __init__(self, data_path):
self.metadata=[]
self.path = os.path.join(data_path, "")
with open(os.path.join(self.path,'tts_metadata.csv'), 'r', newline='') as f:
csvreader = csv.reader(f, delimiter='|')
for row in csvreader:
self.metadata.append(row)
self.mel_path = os.path.join(data_path, "mel")
self.wav_path = os.path.join(data_path, "audio")
self.test_path = os.path.join(data_path, "mel")
def __getitem__(self, index):
entry = self.metadata[index]
m = np.load(entry[2].strip())
wav = np.load(entry[1].strip())
if hp.input_type == 'raw' or hp.input_type=='mixture':
wav = wav.astype(np.float32)
elif hp.input_type == 'mulaw':
wav = mulaw_quantize(wav, hp.mulaw_quantize_channels).astype(np.int)
elif hp.input_type == 'bits':
wav = quantize(wav).astype(np.int)
else:
raise ValueError("hp.input_type {} not recognized".format(hp.input_type))
return m, wav
def __len__(self):
return len(self.metadata)
def raw_collate(batch) :
"""collate function used for raw wav forms, such as using beta/guassian/mixture of logistic
"""
pad = 2
mel_win = hp.seq_len // hp.hop_size + 2 * pad
max_offsets = [x[0].shape[-1] - (mel_win + 2 * pad) for x in batch]
mel_offsets = [np.random.randint(0, offset) for offset in max_offsets]
sig_offsets = [(offset + pad) * hp.hop_size for offset in mel_offsets]
mels = [x[0][:, mel_offsets[i]:mel_offsets[i] + mel_win] \
for i, x in enumerate(batch)]
coarse = [x[1][sig_offsets[i]:sig_offsets[i] + hp.seq_len + 1] \
for i, x in enumerate(batch)]
mels = np.stack(mels).astype(np.float32)
coarse = np.stack(coarse).astype(np.float32)
mels = torch.FloatTensor(mels)
coarse = torch.FloatTensor(coarse)
x_input = coarse[:,:hp.seq_len]
y_coarse = coarse[:, 1:]
return x_input, mels, y_coarse
def discrete_collate(batch) :
"""collate function used for discrete wav output, such as 9-bit, mulaw-discrete, etc.
"""
pad = 2
mel_win = hp.seq_len // hp.hop_size + 2 * pad
max_offsets = [x[0].shape[-1] - (mel_win + 2 * pad) for x in batch]
mel_offsets = [np.random.randint(0, offset) for offset in max_offsets]
sig_offsets = [(offset + pad) * hp.hop_size for offset in mel_offsets]
mels = [x[0][:, mel_offsets[i]:mel_offsets[i] + mel_win] \
for i, x in enumerate(batch)]
coarse = [x[1][sig_offsets[i]:sig_offsets[i] + hp.seq_len + 1] \
for i, x in enumerate(batch)]
mels = np.stack(mels).astype(np.float32)
coarse = np.stack(coarse).astype(np.int64)
mels = torch.FloatTensor(mels)
coarse = torch.LongTensor(coarse)
if hp.input_type == 'bits':
x_input = 2 * coarse[:, :hp.seq_len].float() / (2**hp.bits - 1.) - 1.
elif hp.input_type == 'mulaw':
x_input = inv_mulaw_quantize(coarse[:, :hp.seq_len], hp.mulaw_quantize_channels)
y_coarse = coarse[:, 1:]
return x_input, mels, y_coarse
def no_test_raw_collate():
import matplotlib.pyplot as plt
from test_utils import plot, plot_spec
data_id_path = "data_dir/"
data_path = "data_dir/"
print(hp.seq_len)
with open('{}dataset_ids.pkl'.format(data_id_path), 'rb') as f:
dataset_ids = pickle.load(f)
dataset = AudiobookDataset(data_path)
print(len(dataset))
data_loader = DataLoader(dataset, collate_fn=raw_collate, batch_size=32,
num_workers=0, shuffle=True)
x, m, y = next(iter(data_loader))
print(x.shape, m.shape, y.shape)
plot(x.numpy()[0])
plot(y.numpy()[0])
plot_spec(m.numpy()[0])
def test_discrete_collate():
import matplotlib.pyplot as plt
from test_utils import plot, plot_spec
data_id_path = "data_dir/"
data_path = "data_dir/"
print(hp.seq_len)
with open('{}dataset_ids.pkl'.format(data_id_path), 'rb') as f:
dataset_ids = pickle.load(f)
dataset = AudiobookDataset(data_path)
print(len(dataset))
data_loader = DataLoader(dataset, collate_fn=discrete_collate, batch_size=32,
num_workers=0, shuffle=True)
x, m, y = next(iter(data_loader))
print(x.shape, m.shape, y.shape)
plot(x.numpy()[0])
plot(y.numpy()[0])
plot_spec(m.numpy()[0])
def no_test_dataset():
data_id_path = "data_dir/"
data_path = "data_dir/"
print(hp.seq_len)
dataset = AudiobookDataset(data_path)