-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathloader.py
164 lines (118 loc) · 5.01 KB
/
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
from preprocess import *
import torch
import random
from model.frontend import *
class Data_Loader:
def __init__(self, batch_size, device):
self.batch_size = batch_size
self.device = device
self.char2idx, self.trg = get_vocab()
#print(char2idx)
paths = get_path()
self.data = get_channel_from_pcm(paths)
self.idx = 0
def __len__(self):
return len(self.data)
def get_seed(self, rand = True):
if rand:
seed = random.sample(range(len(self.data)), self.batch_size)
else:
max_idx = min((self.idx + 1) * self.batch_size, len(self.data))
seed = [idx for idx in range(self.idx * self.batch_size, max_idx)]
self.idx += 1
return seed
def get_batch(self, rand = True):
seed = self.get_seed(rand)
batch = [self.data[i] for i in seed]
trg = [self.trg[i] for i in seed]
batch, trg, seq_length, trg_length = self.padding(batch, trg)
ret_dict = {"speech" : batch.to(self.device),
"speech_lengths" : seq_length.to(self.device),
"text" : trg.to(self.device),
"text_lengths" : trg_length.to(self.device)}
if (self.idx * self.batch_size) >= len(self.path):
self.idx = 0
return ret_dict
def padding(self, data, trg):
'''
data : list of data
'''
batch_size = len(data)
channel = data[0].shape[1]
seq_length = torch.LongTensor([len(b) for b in data])
max_length = max(seq_length)
trg_length = torch.LongTensor([len(b) for b in trg])
max_trg = max(trg_length)
torch_batch = torch.zeros(batch_size, max_length, channel).fill_(-1)
trg_batch = torch.zeros(batch_size, max_trg).to(torch.long).fill_(-1)
for idx, (seq, t) in enumerate(zip(data, trg)):
torch_batch[idx, :seq_length[idx], :] = seq
trg_batch[idx, :trg_length[idx]] = torch.Tensor(t)
return torch_batch, trg_batch, seq_length, trg_length
class Batch_Loader:
def __init__(self, batch_size, device, path, trg, char2idx):
self.batch_size = batch_size
self.device = device
self.path, self.trg, self.char2idx = path, trg, char2idx
self.idx = 0
def __len__(self):
return len(self.path)
def get_seed(self, rand = True):
if rand:
seed = random.sample(range(len(self.path)), self.batch_size)
else:
max_idx = min((self.idx + 1) * self.batch_size, len(self.path))
seed = [idx for idx in range(self.idx * self.batch_size, max_idx)]
self.idx += 1
return seed
def get_batch(self, rand = True):
seed = self.get_seed(rand)
batch_paths = [self.path[i] for i in seed]
trg = [self.trg[i] for i in seed]
batch, trg, seq_length, trg_length = self.padding(batch_paths, trg)
ret_dict = {"speech" : batch.to(self.device),
"speech_lengths" : seq_length.to(self.device),
"text" : trg.to(self.device),
"text_lengths" : trg_length.to(self.device)}
if (self.idx * self.batch_size) >= len(self.path):
self.idx = 0
return ret_dict
def get_test_batch(self):
seed = self.get_seed(False)
batch_paths = [self.path[i] for i in seed]
data = get_channel_from_pcm(batch_paths)
batch_size = len(data)
channel = data[0].shape[1]
seq_length = torch.LongTensor([len(b) for b in data])
max_length = max(seq_length)
torch_batch = torch.zeros(batch_size, max_length, channel).fill_(-1)
for idx, seq in enumerate(data):
torch_batch[idx, :seq_length[idx], :] = seq
ret_dict = {"speech" : torch_batch.to(self.device),
"speech_lengths" : seq_length.to(self.device),
"path" : batch_paths
}
if (self.idx * self.batch_size) >= len(self.path):
self.idx = 0
return ret_dict
def padding(self, paths, trg):
'''
data : list of data
'''
data = get_channel_from_pcm(paths)
batch_size = len(data)
channel = data[0].shape[1]
seq_length = torch.LongTensor([len(b) for b in data])
max_length = max(seq_length)
trg_length = torch.LongTensor([len(b) for b in trg])
max_trg = max(trg_length)
torch_batch = torch.zeros(batch_size, max_length, channel).fill_(-1)
trg_batch = torch.zeros(batch_size, max_trg).to(torch.long).fill_(-1)
for idx, (seq, t) in enumerate(zip(data, trg)):
torch_batch[idx, :seq_length[idx], :] = seq
trg_batch[idx, :trg_length[idx]] = torch.Tensor(t)
return torch_batch, trg_batch, seq_length, trg_length
if __name__ == "__main__":
dataloader = Batch_Loader(1000, torch.device("cpu"))
a = dataloader.get_batch()
print(a)