-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_training_loader.py
265 lines (219 loc) · 7.96 KB
/
gen_training_loader.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import torch
from torch.utils.data import Dataset, DataLoader
from text import text_to_sequence
from bert_embedding import get_bert_embedding
import hparams
import numpy as np
from pytorch_pretrained_bert import BertTokenizer, BertModel, BertForMaskedLM
import os
# def get_separator(text):
# total_len = len(text)
# sep_list = list([0])
# for i in range(total_len):
# if ((not text[i].isalpha()) and (not text[i].isdigit()) and (i != 0)):
# sep_list.append(i)
# if sep_list[len(sep_list)-1] != total_len - 1:
# sep_list.append(total_len-1)
# return sep_list
def get_separator(text, tokenized):
# print(text)
# print(tokenized)
total_len = len(text)
tokenized = tokenized[1:len(tokenized)-1]
# start = 0
output_sep = list()
# output_sep.append(start)
cnt = 0
for i in range(len(text)):
# print(tokenized[cnt])
word = text[i:i+len(tokenized[cnt])]
word = word.lower()
# print(word)
if word == tokenized[cnt]:
output_sep.append(i)
cnt = cnt + 1
# print(cnt)
if cnt >= len(tokenized):
break
# for word in tokenized:
# output_sep.append(start+len(word)+1)
# start = start + len(word)
# print(output_sep)
output_sep.append(total_len)
# print(output_sep)
return output_sep
def cut_text(text, sep_list):
for ind in range(len(sep_list) - 1):
print(text[sep_list[ind]:sep_list[ind+1]])
class SpeechData(Dataset):
"""LJSpeech"""
def __init__(self, dataset_path, tokenizer, model_bert):
self.datasetPath = dataset_path
self.textPath = os.path.join(self.datasetPath, "train.txt")
self.text = process_text(self.textPath)
self.model_bert = model_bert
self.tokenizer = tokenizer
# with open(textPath, "r", encoding='utf-8') as f:
# training_text = len(f.read())
# self.trainingText = training_text
def __len__(self):
return len(self.text)
def __getitem__(self, idx):
index = idx + 1
# list_dir = os.listdir(self.datasetPath)
mel_name = os.path.join(
self.datasetPath, "ljspeech-mel-%05d.npy" % index)
spec_name = os.path.join(
self.datasetPath, "ljspeech-spec-%05d.npy" % index)
# print(dir_name)
character = self.text[idx]
embeddings, tokens = get_bert_embedding(
character, self.model_bert, self.tokenizer)
sep_list = get_separator(character, tokens)
em_info = (embeddings, sep_list)
# print(character)
character = text_to_sequence(character, [hparams.cleaners])
character = np.array(character)
# print("#################")
mel_np = np.load(mel_name)
spec_np = np.load(spec_name)
# # print(mel_np)
# print(np.shape(mel_np))
# print(np.shape(spec_np))
# (time, frequency)
return {"text": character, "mel": mel_np, "spec": spec_np, "em_info": em_info}
def process_text(train_text_path):
with open(train_text_path, "r", encoding="utf-8") as f:
inx = 0
txt = []
for line in f.readlines():
cnt = 0
for index, ele in enumerate(line):
if ele == '|':
cnt = cnt + 1
if cnt == 3:
inx = index
end = len(line)
txt.append(line[inx+1:end-1])
break
return txt
def collate_fn(batch):
# # Test
# for d in batch:
# # print(d["mel"])
# print(np.shape(d["mel"]))
texts = [d['text'] for d in batch]
mels = [d['mel'] for d in batch]
specs = [d['spec'] for d in batch]
em_infos = [d['em_info'] for d in batch]
# print(texts)
texts = pad_seq_text(texts)
# print(type(texts))
# print(np.shape(texts))
# mels = pad_sequence(mels)
# specs = pad_sequence(specs)
# print(np.shape(mels[0]))
# print(np.shape(specs[0]))
# for mel in mels:
# print(np.shape(mel))
# print()
mels = pad_seq_spec(mels)
# print(np.shape(mels))
specs = pad_seq_spec(specs)
return {"text": texts, "mel": mels, "spec": specs, "em_infos": em_infos}
def pad_seq_text(inputs):
def pad_data(x, length):
pad = 0
return np.pad(x, (0, length - x.shape[0]), mode='constant', constant_values=pad)
max_len = max((len(x) for x in inputs))
return np.stack([pad_data(x, max_len) for x in inputs])
# def pad(x, max_len):
# # print(type(x))
# if np.shape(x)[0] > max_len:
# print("ERROR!")
# s = np.shape(x)[1]
# # print(s)
# x = np.pad(x, (0, max_len - np.shape(x)
# [0]), mode='constant', constant_values=0)
# return x[:, :s]
def pad_seq_spec(inputs):
def pad(x, max_len):
# print(type(x))
if np.shape(x)[0] > max_len:
# print("ERROR!")
raise ValueError("not max_len")
s = np.shape(x)[1]
# print(s)
x = np.pad(x, (0, max_len - np.shape(x)
[0]), mode='constant', constant_values=0)
return x[:, :s]
max_len = max(np.shape(x)[0] for x in inputs)
# print(max_len)
# for x in inputs:
# x = pad(x, max_len)
# # print(np.shape(x))
# # print(x)
# print(np.stack([pad(x,max_len) for x in inputs]))
# a = np.stack([pad(x,max_len) for x in inputs])
# print(np.shape(a))
# print(type(a))
return np.stack([pad(x, max_len) for x in inputs])
# def pad_sequence(sequences):
# '''
# pad sequence to same length (max length)
# ------------------
# input:
# sequences --- a list of tensor with variable length
# out --- a tensor with max length
# '''
# lengths = [data.size(0) for data in sequences]
# batch_size = len(sequences)
# max_len = max(lengths)
# trailing_dims = sequences[0].size()[1:]
# out_dims = (batch_size, max_len) + trailing_dims
# dtype = sequences[0].data.type()
# out = torch.zeros(*out_dims).type(dtype)
# for i, data in enumerate(sequences):
# out[i, :lengths[i]] = data
# return out
if __name__ == "__main__":
# seq = np.ndarray([])
# seq = np.append(seq, np.array([1, 2, 3]))
# seq = np.append(seq, np.array([1]))
# seq = np.append(seq, np.array([2, 3]))
# seq = np.append(seq, [1])
# seq = np.append(seq, [2, 3])
# seq = [[[1, 2, 3], [1, 2, 3], [1, 2, 3]]]
# # seq = torch.Tensor(seq)
# seq = np.array(seq)
# print(seq)
# print(pad_sequence(seq))
# Test
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
dataset = SpeechData("dataset", tokenizer, model)
training_loader = DataLoader(
dataset, batch_size=2, shuffle=True, collate_fn=collate_fn, drop_last=True, num_workers=6)
for i, data in enumerate(training_loader):
# print(data)
a = 0
# print(data)
text = "I love you, ,,,12,too."
# print(get_separator("I love you, too."))
# sep_list = get_separator("I love you, too.")
# cut_text(text, sep_list)
emb, tokenized_text = get_bert_embedding(text, model, tokenizer)
print(emb.size())
print(tokenized_text)
sep = get_separator(text, tokenized_text)
cut_text(text, sep)
# # Other test
# test_list = [np.array([1, 2, 3]), np.array([1])]
# # print(text_to_sequence("I like", [hparams.cleaners]))
# # print(pad_sequence(test_list))
# # test_list = [np.array([[1, 2, 3], [1, 2, 3]]),
# # np.array([[1, 2, 3], [1, 3]])]
# # print(pad_sequence(test_list))
# a = np.ndarray((2, 2))
# print(a)
# print(pad(a, 3))