forked from jaywalnut310/MelGAN-Pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.py
170 lines (128 loc) · 5.34 KB
/
data.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
import torch
from torch.utils.data import Dataset
import numpy as np
import os
use_cuda = torch.cuda.is_available()
max_time_steps = 16000
upsample_conditional_features = True
hop_length = 256
class LJspeechDataset(Dataset):
def __init__(self, data_root, train=True, test_size=0.05):
self.data_root = data_root
self.lengths = []
self.train = train
self.test_size = test_size
self.paths = [self.collect_files(0), self.collect_files(1)]
def __len__(self):
return len(self.paths[0])
def __getitem__(self, idx):
wav = np.load(self.paths[0][idx])
mel = np.load(self.paths[1][idx])
return wav, mel
def interest_indices(self, paths):
test_num_samples = int(self.test_size * len(paths))
train_indices, test_indices = range(0, len(paths) - test_num_samples), \
range(len(paths) - test_num_samples, len(paths))
return train_indices if self.train else test_indices
def collect_files(self, col):
meta = os.path.join(self.data_root, "train.txt")
with open(meta, "rb") as f:
lines = f.readlines()
l = lines[0].decode("utf-8").split("|")
assert len(l) == 4
self.lengths = list(
map(lambda l: int(l.decode("utf-8").split("|")[2]), lines))
paths = list(map(lambda l: l.decode("utf-8").split("|")[col], lines))
paths = list(map(lambda f: os.path.join(self.data_root, f), paths))
# Filter by train/test
indices = self.interest_indices(paths)
paths = list(np.array(paths)[indices])
self.lengths = list(np.array(self.lengths)[indices])
self.lengths = list(map(int, self.lengths))
return paths
def _pad(seq, max_len, constant_values=0):
return np.pad(seq, (0, max_len - len(seq)),
mode='constant', constant_values=constant_values)
def _pad_2d(x, max_len, b_pad=0):
x = np.pad(x, [(b_pad, max_len - len(x) - b_pad), (0, 0)],
mode="constant", constant_values=0)
return x
def collate_fn(batch):
"""
Create batch
Args : batch(tuple) : List of tuples / (x, c) x : list of (T,) c : list of (T, D)
Returns : Tuple of batch / Network inputs x (B, C, T), Network targets (B, T, 1)
"""
local_conditioning = len(batch[0]) >= 2
if local_conditioning:
new_batch = []
for idx in range(len(batch)):
x, c = batch[idx]
if upsample_conditional_features:
assert len(x) % len(c) == 0 and len(x) // len(c) == hop_length
max_steps = max_time_steps - max_time_steps % hop_length # To ensure Divisibility
if len(x) > max_steps:
max_time_frames = max_steps // hop_length
s = np.random.randint(0, len(c) - max_time_frames)
ts = s * hop_length
x = x[ts:ts + hop_length * max_time_frames]
c = c[s:s + max_time_frames]
assert len(x) % len(c) == 0 and len(x) // len(c) == hop_length
else:
pass
new_batch.append((x, c))
batch = new_batch
else:
pass
input_lengths = [len(x[0]) for x in batch]
max_input_len = max(input_lengths)
# x_batch : [B, T, 1]
x_batch = np.array([_pad_2d(x[0].reshape(-1, 1), max_input_len) for x in batch], dtype=np.float32)
assert len(x_batch.shape) == 3
if local_conditioning:
max_len = max([len(x[1]) for x in batch])
c_batch = np.array([_pad_2d(x[1], max_len) for x in batch], dtype=np.float32)
assert len(c_batch.shape) == 3
# (B x C x T')
c_batch = torch.tensor(c_batch).transpose(1, 2).contiguous()
del max_len
else:
c_batch = None
# Convert to channel first i.e., (B, C, T) / C = 1
x_batch = torch.tensor(x_batch).transpose(1, 2).contiguous()
input_lengths = torch.tensor(input_lengths)
return x_batch, c_batch, input_lengths
def collate_fn_synthesize(batch):
"""
Create batch
Args : batch(tuple) : List of tuples / (x, c) x : list of (T,) c : list of (T, D)
Returns : Tuple of batch / Network inputs x (B, C, T), Network targets (B, T, 1)
"""
local_conditioning = len(batch[0]) >= 2
if local_conditioning:
new_batch = []
for idx in range(len(batch)):
x, c = batch[idx]
if upsample_conditional_features:
assert len(x) % len(c) == 0 and len(x) // len(c) == hop_length
new_batch.append((x, c))
batch = new_batch
else:
pass
input_lengths = [len(x[0]) for x in batch]
max_input_len = max(input_lengths)
# x_batch : [B, T, 1]
x_batch = np.array([_pad_2d(x[0].reshape(-1, 1), max_input_len) for x in batch], dtype=np.float32)
assert len(x_batch.shape) == 3
if local_conditioning:
max_len = max([len(x[1]) for x in batch])
c_batch = np.array([_pad_2d(x[1], max_len) for x in batch], dtype=np.float32)
assert len(c_batch.shape) == 3
# (B x C x T')
c_batch = torch.tensor(c_batch).transpose(1, 2).contiguous()
else:
c_batch = None
# Convert to channel first i.e., (B, C, T) / C = 1
x_batch = torch.tensor(x_batch).transpose(1, 2).contiguous()
input_lengths = torch.tensor(input_lengths)
return x_batch, c_batch, input_lengths