/
data_process.py
343 lines (299 loc) · 15 KB
/
data_process.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
##########################################################################
# These functions is used to get the minibatch of the mix training data #
# and load the training data from the given path #
# 1. get_batch(Path, Batch_idx, Step_size) #
# input: #
# Path : path of location of your data #
# Batch_idx : index of batch #
# Step_size : the step size of back propagation through time #
# output: #
# mix_train: the batch of the mix training data #
# target1 : the batch of the first clean data #
# target2 : the batch of the second clean data #
# 2.
##########################################################################
import scipy.io as sio
import numpy as np
import os
import math
def get_data_train(path_mix, path_t1, path_t2, num_speaker, batch_size, time_step, longest=200):
filelist = os.listdir(path_mix)
filelist.sort() # sort it in order
filelist = filelist[0:num_speaker]
mix = {}
t1 = {}
t2 = {}
sequence_length = {} # store the sequence length for the last batch of each speaker
for speaker in filelist: # for each speaker
mix.update({speaker:np.zeros((513, 1))})
t1.update({speaker:np.zeros((513, 1))})
t2.update({speaker:np.zeros((513, 1))})
sentlist = os.listdir(path_mix+speaker+'/')
sentlist.sort()
sequence_length.update({speaker:[]})
for sentence in sentlist: # for each sentence in each speaker file
if sentence.endswith('_phase.mat') or not sentence.endswith('.mat'):
continue
# append mixed signal
tmp = sio.loadmat(path_mix+speaker+'/'+sentence)
tmp_out = tmp.get('mix_train')
mix[speaker], sequence = data_append(mix[speaker], tmp_out, longest)
# append target 1
tmp = sio.loadmat(path_t1+speaker+'/'+sentence)
tmp_out = tmp.get('target1')
t1[speaker], _ = data_append(t1[speaker], tmp_out, longest)
# append target 2
tmp = sio.loadmat(path_t2+speaker+'/'+sentence)
tmp_out = tmp.get('target2')
t2[speaker], _ = data_append(t2[speaker], tmp_out, longest)
# append sequence_length
sequence_length[speaker].extend(sequence)
mix[speaker] = mix[speaker][:, 1:]
t1[speaker] = t1[speaker][:, 1:]
t2[speaker] = t2[speaker][:, 1:]
# size of mix, t1, t2 : N x D
mix[speaker] = np.transpose(mix[speaker])
t1[speaker] = np.transpose(t1[speaker])
t2[speaker] = np.transpose(t2[speaker])
input_size = mix[speaker].shape[1]
# reshape
mix[speaker] = np.reshape(mix[speaker], [-1, longest, input_size])
t1[speaker] = np.reshape(t1[speaker], [-1, longest, input_size])
t2[speaker] = np.reshape(t2[speaker], [-1, longest, input_size])
if mix[speaker].shape[0]<batch_size:
zero_pad_size = batch_size - mix[speaker].shape[0]
mix[speaker] = np.lib.pad(mix[speaker], ((0, zero_pad_size), (0, 0), (0, 0)), 'constant')
t1[speaker] = np.lib.pad(t1[speaker], ((0, zero_pad_size), (0, 0), (0, 0)), 'constant')
t2[speaker] = np.lib.pad(t2[speaker], ((0, zero_pad_size), (0, 0), (0, 0)), 'constant')
sequence = [0]*zero_pad_size
sequence_length[speaker].extend(sequence)
return mix, t1, t2, sequence_length
def data_append(old, new, longest):
sequence = []
if new.shape[1]>longest:
to_append_size = int(math.ceil(float(new.shape[1])/longest))
start_idx=0
for idx in range(to_append_size):
if idx is to_append_size-1:
old = np.append(old, new[:, start_idx:], axis=1)
sequence.append((new.shape[1] - (to_append_size-1)*longest))
zero_pad_size = longest - sequence[-1]
old = np.lib.pad(old, ((0, 0), (0, zero_pad_size)), 'constant')
break
old = np.append(old, new[:, start_idx:start_idx+longest], axis=1)
sequence.append(longest)
start_idx += longest
else:
old = np.append(old, new, axis=1)
sequence.append(new.shape[1])
zero_pad_size = longest - sequence[-1]
old = np.lib.pad(old, ((0, 0), (0, zero_pad_size)), 'constant')
return old, sequence
def get_data_test(path_mix, path_t1, path_t2, num_speaker, batch_size, time_step):
filelist = os.listdir(path_mix)
filelist.sort() # sort it in order
filelist = filelist[0:num_speaker]
mix = {}
t1 = {}
t2 = {}
order = np.zeros((1, 1))
sequence_length = {} # store the sequence length for the last batch of each speaker
for speaker in filelist: # for each speaker
mix.update({speaker:np.zeros((513, 1))})
t1.update({speaker:np.zeros((513, 1))})
t2.update({speaker:np.zeros((513, 1))})
sentlist = os.listdir(path_mix+speaker+'/')
sentlist.sort()
for sentence in sentlist: # for each sentence in each speaker file
if sentence.endswith('_phase.mat') or not sentence.endswith('.mat'):
continue
# append mixed signal
tmp = sio.loadmat(path_mix+speaker+'/'+sentence)
tmp_out = tmp.get('mix_train')
mix[speaker] = np.append(mix[speaker], tmp_out, axis=1)
# append target 1
tmp = sio.loadmat(path_t1+speaker+'/'+sentence)
tmp_out = tmp.get('target1')
t1[speaker] = np.append(t1[speaker], tmp_out, axis=1)
# append target 2
tmp = sio.loadmat(path_t2+speaker+'/'+sentence)
tmp_out = tmp.get('target2')
t2[speaker] = np.append(t2[speaker], tmp_out, axis=1)
# append order
order = np.append(order, [[int(sentence.replace('.mat', ''))]], axis=0)
mix[speaker] = mix[speaker][:, 1:]
t1[speaker] = t1[speaker][:, 1:]
t2[speaker] = t2[speaker][:, 1:]
# zeros padding
mix[speaker], t1[speaker], t2[speaker], sequence_length[speaker] = \
zeros_padding(mix[speaker], t1[speaker], t2[speaker], batch_size, time_step)
# size of mix, t1, t2 : N x D
mix[speaker] = np.transpose(mix[speaker])
t1[speaker] = np.transpose(t1[speaker])
t2[speaker] = np.transpose(t2[speaker])
order = order[1:, 0]
return mix, t1, t2, order, sequence_length
def zeros_padding(mix, t1, t2, batch_size, time_step):
input_size, data_size = mix.shape
padding_size = batch_size * time_step - ( data_size%(batch_size * time_step) )
mix_out = np.lib.pad(mix, ((0, 0), (0, padding_size)), 'constant')
t1_out = np.lib.pad(t1, ((0, 0), (0, padding_size)), 'constant')
t2_out = np.lib.pad(t2, ((0, 0), (0, padding_size)), 'constant')
num_zeros = math.floor(padding_size/time_step)
last_len = time_step - (padding_size % time_step)
sequence_length = []
for idx in range(0, batch_size):
if idx < batch_size-(num_zeros+1):
sequence_length.append(time_step)
elif idx == batch_size-(num_zeros+1):
sequence_length.append(last_len)
else:
sequence_length.append(0)
return mix_out, t1_out, t2_out, sequence_length
def get_batch_test(mix, t1, t2, batch_start, batch_size, step_size, input_size, dim, window=1):
# get data from batch_start and extract data for number of step_size
if dim is True:
mix_out = np.reshape(mix[batch_start:batch_start+batch_size*step_size, :], [batch_size*step_size, -1])
t1_out = np.reshape(t1[batch_start:batch_start+batch_size*step_size, :], [batch_size*step_size, -1])
t2_out = np.reshape(t2[batch_start:batch_start+batch_size*step_size, :], [batch_size*step_size, -1])
else:
mix_out = np.reshape(mix[:, batch_start:batch_start+batch_size*step_size].T, [batch_size*step_size, -1])
t1_out = np.reshape(t1[:, batch_start:batch_start+batch_size*step_size].T, [batch_size*step_size, -1])
t2_out = np.reshape(t2[:, batch_start:batch_start+batch_size*step_size].T, [batch_size*step_size, -1])
if window > 1:
mix_out = turn_to_map(mix_out, window, batch_size)
return mix_out, t1_out, t2_out
def get_batch_train(mix, t1, t2, batch_idx, time_idx, sp_idx,
batch_size, time_step, input_size, sp_list,
sequence_length, longest=200, window=1):
cross_batch = batch_size - (mix[mix.keys()[sp_list[sp_idx]]].shape[0] - batch_idx)
sequence = []
if cross_batch is batch_size:
sp_idx += 1
time_idx = 0
batch_idx = 0
cross_batch = 0
if sp_idx == len(sp_list):
return [], [], [], sequence, sp_idx, batch_idx, time_idx
elif sp_idx+1 < len(sp_list):
if cross_batch > 0:
mix_out = np.append(
mix[mix.keys()[sp_list[sp_idx]]][batch_idx:, time_idx:time_idx+time_step, :],
mix[mix.keys()[sp_list[sp_idx+1]]][0:cross_batch, time_idx:time_idx+time_step, :], axis=0)
t1_out = np.append(
t1[mix.keys()[sp_list[sp_idx]]][batch_idx:, time_idx:time_idx+time_step, :],
t1[mix.keys()[sp_list[sp_idx+1]]][0:cross_batch, time_idx:time_idx+time_step, :], axis=0)
t2_out = np.append(
t2[mix.keys()[sp_list[sp_idx]]][batch_idx:, time_idx:time_idx+time_step, :],
t2[mix.keys()[sp_list[sp_idx+1]]][0:cross_batch, time_idx:time_idx+time_step, :], axis=0)
for seq in sequence_length[mix.keys()[sp_list[sp_idx]]][batch_idx:]:
if time_idx<seq-1:
if time_idx+time_step<seq:
sequence.append(time_step)
else:
sequence.append(seq-time_idx)
else:
sequence.append(0)
for seq in sequence_length[mix.keys()[sp_list[sp_idx+1]]][0:cross_batch]:
if time_idx<seq-1:
if time_idx+time_step<seq:
sequence.append(time_step)
else:
sequence.append(seq-time_idx)
else:
sequence.append(0)
if time_idx+time_step >= longest:
time_idx = 0;
batch_idx = cross_batch
sp_idx += 1
else:
time_idx += time_step
else:
mix_out = mix[mix.keys()[sp_list[sp_idx]]][batch_idx:batch_idx+batch_size, time_idx:time_idx+time_step, :]
t1_out = t1[mix.keys()[sp_list[sp_idx]]][batch_idx:batch_idx+batch_size, time_idx:time_idx+time_step, :]
t2_out = t2[mix.keys()[sp_list[sp_idx]]][batch_idx:batch_idx+batch_size, time_idx:time_idx+time_step, :]
for seq in sequence_length[mix.keys()[sp_list[sp_idx]]][batch_idx:batch_idx+batch_size]:
if time_idx<seq-1:
if time_idx+time_step<seq:
sequence.append(time_step)
else:
sequence.append(seq-time_idx)
else:
sequence.append(0)
if time_idx+time_step >= longest:
time_idx = 0;
batch_idx += batch_size
else:
time_idx += time_step
else:
if cross_batch > 0:
mix_out = np.append(
mix[mix.keys()[sp_list[sp_idx]]][batch_idx:, time_idx:time_idx+time_step, :],
np.zeros((cross_batch, time_step, input_size)), axis=0)
t1_out = np.append(
t1[mix.keys()[sp_list[sp_idx]]][batch_idx:, time_idx:time_idx+time_step, :],
np.zeros((cross_batch, time_step, input_size)), axis=0)
t2_out = np.append(
t2[mix.keys()[sp_list[sp_idx]]][batch_idx:, time_idx:time_idx+time_step, :],
np.zeros((cross_batch, time_step, input_size)), axis=0)
for seq in sequence_length[mix.keys()[sp_list[sp_idx]]][batch_idx:]:
if time_idx<seq-1:
if time_idx+time_step<seq:
sequence.append(time_step)
else:
sequence.append(seq-time_idx)
else:
sequence.append(0)
for seq in range(cross_batch):
sequence.append(0)
if time_idx+time_step >= longest:
time_idx = 0;
batch_idx = cross_batch
sp_idx += 1
else:
time_idx += time_step
else:
mix_out = mix[mix.keys()[sp_list[sp_idx]]][batch_idx:batch_idx+batch_size, time_idx:time_idx+time_step, :]
t1_out = t1[mix.keys()[sp_list[sp_idx]]][batch_idx:batch_idx+batch_size, time_idx:time_idx+time_step, :]
t2_out = t2[mix.keys()[sp_list[sp_idx]]][batch_idx:batch_idx+batch_size, time_idx:time_idx+time_step, :]
for seq in sequence_length[mix.keys()[sp_list[sp_idx]]][batch_idx:batch_idx+batch_size]:
if time_idx<seq-1:
if time_idx+time_step<seq:
sequence.append(time_step)
else:
sequence.append(seq-time_idx)
else:
sequence.append(0)
if time_idx+time_step >= longest:
time_idx = 0;
batch_idx += batch_size
else:
time_idx += time_step
mix_out = np.reshape(mix_out, (-1, input_size))
t1_out = np.reshape(t1_out, (-1, input_size))
t2_out = np.reshape(t2_out, (-1, input_size))
if window > 1:
mix_out = turn_to_map(mix_out, window, batch_size)
return mix_out, t1_out, t2_out, sequence, sp_idx, batch_idx, time_idx
def turn_to_map(mix, window, batch_size):
"""
mix : [(batch_size x time_step) x input_size]
mix_new: [(batch_size x time_step) x (input_size x window)]
"""
batch_time, input_size = mix.shape
time_step = batch_time / batch_size
mix_new = np.zeros((batch_size*time_step, input_size*window))
for idx in xrange(batch_size):
time_idx = idx*time_step
mix_new[time_idx:time_idx+time_step, :] = circular_shift(mix[time_idx:time_idx+time_step, :], window)
return mix_new
def circular_shift(x, window):
out = x
left = x
right = x
for win in xrange((window-1)/2):
left = np.roll(left, 1, axis=0)
out = np.concatenate((left, out), axis=1)
right = np.roll(right, -1, axis=0)
out = np.concatenate((out, right), axis=1)
return out