-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathdataset.py
122 lines (90 loc) · 3.5 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
import torch
from torch.nn import functional as F
from torch.utils.data import Dataset, DataLoader
import numpy as np
import math
import time
import os
import hparams
import audio
from utils import process_text, pad_1D, pad_2D
from utils import pad_1D_tensor, pad_2D_tensor
from text import text_to_sequence
from tqdm import tqdm
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def get_data_to_buffer():
buffer = list()
text = process_text(os.path.join("data", "train.txt"))
start = time.perf_counter()
for i in tqdm(range(len(text))):
mel_gt_name = os.path.join(
hparams.mel_ground_truth, "ljspeech-mel-%05d.npy" % (i+1))
mel_gt_target = np.load(mel_gt_name)
duration = np.load(os.path.join(
hparams.alignment_path, str(i)+".npy"))
character = text[i][0:len(text[i])-1]
character = np.array(
text_to_sequence(character, hparams.text_cleaners))
character = torch.from_numpy(character)
duration = torch.from_numpy(duration)
mel_gt_target = torch.from_numpy(mel_gt_target)
buffer.append({"text": character, "duration": duration,
"mel_target": mel_gt_target})
end = time.perf_counter()
print("cost {:.2f}s to load all data into buffer.".format(end-start))
return buffer
class BufferDataset(Dataset):
def __init__(self, buffer):
self.buffer = buffer
self.length_dataset = len(self.buffer)
def __len__(self):
return self.length_dataset
def __getitem__(self, idx):
return self.buffer[idx]
def reprocess_tensor(batch, cut_list):
texts = [batch[ind]["text"] for ind in cut_list]
mel_targets = [batch[ind]["mel_target"] for ind in cut_list]
durations = [batch[ind]["duration"] for ind in cut_list]
length_text = np.array([])
for text in texts:
length_text = np.append(length_text, text.size(0))
src_pos = list()
max_len = int(max(length_text))
for length_src_row in length_text:
src_pos.append(np.pad([i+1 for i in range(int(length_src_row))],
(0, max_len-int(length_src_row)), 'constant'))
src_pos = torch.from_numpy(np.array(src_pos))
length_mel = np.array(list())
for mel in mel_targets:
length_mel = np.append(length_mel, mel.size(0))
mel_pos = list()
max_mel_len = int(max(length_mel))
for length_mel_row in length_mel:
mel_pos.append(np.pad([i+1 for i in range(int(length_mel_row))],
(0, max_mel_len-int(length_mel_row)), 'constant'))
mel_pos = torch.from_numpy(np.array(mel_pos))
texts = pad_1D_tensor(texts)
durations = pad_1D_tensor(durations)
mel_targets = pad_2D_tensor(mel_targets)
out = {"text": texts,
"mel_target": mel_targets,
"duration": durations,
"mel_pos": mel_pos,
"src_pos": src_pos,
"mel_max_len": max_mel_len}
return out
def collate_fn_tensor(batch):
len_arr = np.array([d["text"].size(0) for d in batch])
index_arr = np.argsort(-len_arr)
batchsize = len(batch)
real_batchsize = batchsize // hparams.batch_expand_size
cut_list = list()
for i in range(hparams.batch_expand_size):
cut_list.append(index_arr[i*real_batchsize:(i+1)*real_batchsize])
output = list()
for i in range(hparams.batch_expand_size):
output.append(reprocess_tensor(batch, cut_list[i]))
return output
if __name__ == "__main__":
# TEST
get_data_to_buffer()