forked from sharathadavanne/seld-dcase2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cls_data_generator.py
282 lines (231 loc) · 12.1 KB
/
cls_data_generator.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
#
# Data generator for training the SELDnet
#
import os
import numpy as np
import cls_feature_class
from IPython import embed
from collections import deque
import random
class DataGenerator(object):
def __init__(
self, params, split=1, shuffle=True, per_file=False, is_eval=False
):
self._per_file = per_file
self._is_eval = is_eval
self._splits = np.array(split)
self._batch_size = params['batch_size']
self._feature_seq_len = params['feature_sequence_length']
self._label_seq_len = params['label_sequence_length']
self._is_accdoa = params['is_accdoa']
self._doa_objective = params['doa_objective']
self._shuffle = shuffle
self._feat_cls = cls_feature_class.FeatureClass(params=params, is_eval=self._is_eval)
self._label_dir = self._feat_cls.get_label_dir()
self._feat_dir = self._feat_cls.get_normalized_feat_dir()
self._filenames_list = list()
self._nb_frames_file = 0 # Using a fixed number of frames in feat files. Updated in _get_label_filenames_sizes()
self._nb_mel_bins = self._feat_cls.get_nb_mel_bins()
self._nb_ch = None
self._label_len = None # total length of label - DOA + SED
self._doa_len = None # DOA label length
self._class_dict = self._feat_cls.get_classes()
self._nb_classes = self._feat_cls.get_nb_classes()
self._get_filenames_list_and_feat_label_sizes()
self._feature_batch_seq_len = self._batch_size*self._feature_seq_len
self._label_batch_seq_len = self._batch_size*self._label_seq_len
self._circ_buf_feat = None
self._circ_buf_label = None
if self._per_file:
self._nb_total_batches = len(self._filenames_list)
else:
self._nb_total_batches = int(np.floor((len(self._filenames_list) * self._nb_frames_file /
float(self._feature_batch_seq_len))))
# self._dummy_feat_vec = np.ones(self._feat_len.shape) *
print(
'\tDatagen_mode: {}, nb_files: {}, nb_classes:{}\n'
'\tnb_frames_file: {}, feat_len: {}, nb_ch: {}, label_len:{}\n'.format(
'eval' if self._is_eval else 'dev', len(self._filenames_list), self._nb_classes,
self._nb_frames_file, self._nb_mel_bins, self._nb_ch, self._label_len
)
)
print(
'\tDataset: {}, split: {}\n'
'\tbatch_size: {}, feat_seq_len: {}, label_seq_len: {}, shuffle: {}\n'
'\tTotal batches in dataset: {}\n'
'\tlabel_dir: {}\n '
'\tfeat_dir: {}\n'.format(
params['dataset'], split,
self._batch_size, self._feature_seq_len, self._label_seq_len, self._shuffle,
self._nb_total_batches,
self._label_dir, self._feat_dir
)
)
def get_data_sizes(self):
feat_shape = (self._batch_size, self._nb_ch, self._feature_seq_len, self._nb_mel_bins)
if self._is_eval:
label_shape = None
else:
if self._is_accdoa:
label_shape = (self._batch_size, self._label_seq_len, self._nb_classes*3)
else:
label_shape = [
(self._batch_size, self._label_seq_len, self._nb_classes),
(self._batch_size, self._label_seq_len, self._nb_classes*3)
]
return feat_shape, label_shape
def get_total_batches_in_data(self):
return self._nb_total_batches
def _get_filenames_list_and_feat_label_sizes(self):
for filename in os.listdir(self._feat_dir):
if self._is_eval:
self._filenames_list.append(filename)
else:
if int(filename[4]) in self._splits: # check which split the file belongs to
self._filenames_list.append(filename)
temp_feat = np.load(os.path.join(self._feat_dir, self._filenames_list[0]))
self._nb_frames_file = temp_feat.shape[0]
self._nb_ch = temp_feat.shape[1] // self._nb_mel_bins
if not self._is_eval:
temp_label = np.load(os.path.join(self._label_dir, self._filenames_list[0]))
self._label_len = temp_label.shape[-1]
self._doa_len = (self._label_len - self._nb_classes)//self._nb_classes
if self._per_file:
self._batch_size = int(np.ceil(temp_feat.shape[0]/float(self._feature_seq_len)))
return
def generate(self):
"""
Generates batches of samples
:return:
"""
while 1:
if self._shuffle:
random.shuffle(self._filenames_list)
# Ideally this should have been outside the while loop. But while generating the test data we want the data
# to be the same exactly for all epoch's hence we keep it here.
self._circ_buf_feat = deque()
self._circ_buf_label = deque()
file_cnt = 0
if self._is_eval:
for i in range(self._nb_total_batches):
# load feat and label to circular buffer. Always maintain atleast one batch worth feat and label in the
# circular buffer. If not keep refilling it.
while len(self._circ_buf_feat) < self._feature_batch_seq_len:
temp_feat = np.load(os.path.join(self._feat_dir, self._filenames_list[file_cnt]))
for row_cnt, row in enumerate(temp_feat):
self._circ_buf_feat.append(row)
# If self._per_file is True, this returns the sequences belonging to a single audio recording
if self._per_file:
extra_frames = self._feature_batch_seq_len - temp_feat.shape[0]
extra_feat = np.ones((extra_frames, temp_feat.shape[1])) * 1e-6
for row_cnt, row in enumerate(extra_feat):
self._circ_buf_feat.append(row)
file_cnt = file_cnt + 1
# Read one batch size from the circular buffer
feat = np.zeros((self._feature_batch_seq_len, self._nb_mel_bins * self._nb_ch))
for j in range(self._feature_batch_seq_len):
feat[j, :] = self._circ_buf_feat.popleft()
feat = np.reshape(feat, (self._feature_batch_seq_len, self._nb_ch, self._nb_mel_bins)).transpose((0, 2, 1))
# Split to sequences
feat = self._split_in_seqs(feat, self._feature_seq_len)
feat = np.transpose(feat, (0, 3, 1, 2))
yield feat
else:
for i in range(self._nb_total_batches):
# load feat and label to circular buffer. Always maintain atleast one batch worth feat and label in the
# circular buffer. If not keep refilling it.
while len(self._circ_buf_feat) < self._feature_batch_seq_len:
temp_feat = np.load(os.path.join(self._feat_dir, self._filenames_list[file_cnt]))
temp_label = np.load(os.path.join(self._label_dir, self._filenames_list[file_cnt]))
for f_row in temp_feat:
self._circ_buf_feat.append(f_row)
for l_row in temp_label:
self._circ_buf_label.append(l_row)
# If self._per_file is True, this returns the sequences belonging to a single audio recording
if self._per_file:
feat_extra_frames = self._feature_batch_seq_len - temp_feat.shape[0]
extra_feat = np.ones((feat_extra_frames, temp_feat.shape[1])) * 1e-6
label_extra_frames = self._label_batch_seq_len - temp_label.shape[0]
extra_labels = np.zeros((label_extra_frames, temp_label.shape[1]))
for f_row in extra_feat:
self._circ_buf_feat.append(f_row)
for l_row in extra_labels:
self._circ_buf_label.append(l_row)
file_cnt = file_cnt + 1
# Read one batch size from the circular buffer
feat = np.zeros((self._feature_batch_seq_len, self._nb_mel_bins * self._nb_ch))
label = np.zeros((self._label_batch_seq_len, self._label_len))
for j in range(self._feature_batch_seq_len):
feat[j, :] = self._circ_buf_feat.popleft()
for j in range(self._label_batch_seq_len):
label[j, :] = self._circ_buf_label.popleft()
feat = np.reshape(feat, (self._feature_batch_seq_len, self._nb_ch, self._nb_mel_bins)).transpose((0, 2, 1))
# Split to sequences
feat = self._split_in_seqs(feat, self._feature_seq_len)
feat = np.transpose(feat, (0, 3, 1, 2))
label = self._split_in_seqs(label, self._label_seq_len)
if self._is_accdoa:
mask = label[:, :, :self._nb_classes]
mask = np.tile(mask, 3)
label = mask * label[:, :, self._nb_classes:]
else:
label = [
label[:, :, :self._nb_classes], # SED labels
label[:, :, self._nb_classes:] if self._doa_objective is 'mse' else label # SED + DOA labels
]
yield feat, label
def _split_in_seqs(self, data, _seq_len):
if len(data.shape) == 1:
if data.shape[0] % _seq_len:
data = data[:-(data.shape[0] % _seq_len), :]
data = data.reshape((data.shape[0] // _seq_len, _seq_len, 1))
elif len(data.shape) == 2:
if data.shape[0] % _seq_len:
data = data[:-(data.shape[0] % _seq_len), :]
data = data.reshape((data.shape[0] // _seq_len, _seq_len, data.shape[1]))
elif len(data.shape) == 3:
if data.shape[0] % _seq_len:
data = data[:-(data.shape[0] % _seq_len), :, :]
data = data.reshape((data.shape[0] // _seq_len, _seq_len, data.shape[1], data.shape[2]))
else:
print('ERROR: Unknown data dimensions: {}'.format(data.shape))
exit()
return data
@staticmethod
def split_multi_channels(data, num_channels):
tmp = None
in_shape = data.shape
if len(in_shape) == 3:
hop = in_shape[2] / num_channels
tmp = np.zeros((in_shape[0], num_channels, in_shape[1], hop))
for i in range(num_channels):
tmp[:, i, :, :] = data[:, :, i * hop:(i + 1) * hop]
elif len(in_shape) == 4 and num_channels == 1:
tmp = np.zeros((in_shape[0], 1, in_shape[1], in_shape[2], in_shape[3]))
tmp[:, 0, :, :, :] = data
else:
print('ERROR: The input should be a 3D matrix but it seems to have dimensions: {}'.format(in_shape))
exit()
return tmp
def get_default_elevation(self):
return self._default_ele
def get_azi_ele_list(self):
return self._feat_cls.get_azi_ele_list()
def get_nb_classes(self):
return self._nb_classes
def nb_frames_1s(self):
return self._feat_cls.nb_frames_1s()
def get_hop_len_sec(self):
return self._feat_cls.get_hop_len_sec()
def get_classes(self):
return self._feat_cls.get_classes()
def get_filelist(self):
return self._filenames_list
def get_frame_per_file(self):
return self._label_batch_seq_len
def get_nb_frames(self):
return self._feat_cls.get_nb_frames()
def get_data_gen_mode(self):
return self._is_eval
def write_output_format_file(self, _out_file, _out_dict):
return self._feat_cls.write_output_format_file(_out_file, _out_dict)