-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
346 lines (306 loc) · 12 KB
/
dataloader.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
337
338
339
340
341
342
343
344
345
346
import h5py
import numpy as np
from pathlib import Path
import random
import librosa
import pandas as pd
import os
import torch
from torch.utils.data import Dataset, DataLoader
class AudioBaseDataset(Dataset):
def __init__(self, audio_file: str):
with h5py.File(audio_file, 'r') as input:
if 'waveform' in input:
self.is_index = False
else:
self.is_index = True
self.files = input['file'][:]
self.indices_in_file = input['index'][:]
self.base_dir = os.path.dirname(audio_file)
self.labels = input['label'][:]
self.audio_names = input['audio_name'][:]
self.audio_file = audio_file
def _get_waveform(self, idx):
"""
Read waveform from index h5 file
index h5 file contains:
- file: list of file names
- index: list of index in file
- label: list of label name (encoded string)
- audio_name: list of audio name (encoded string)
data file (indicated in <file> and indexed by <index> above) contains:
- waveform: numpy array of N x T
"""
if not self.is_index:
with h5py.File(self.audio_file) as input:
waveform = input['waveform'][idx]
else:
file = os.path.join(self.base_dir, self.files[idx].decode())
index_in_file = self.indices_in_file[idx]
with h5py.File(file, 'r') as input:
waveform = input['waveform'][index_in_file]
label = self.labels[idx]
audio_name = self.audio_names[idx]
waveform = waveform.astype(np.float32)
return {
"waveform": waveform,
"label": label.decode(),
"audio_name": audio_name.decode()
}
class AudioDataset(AudioBaseDataset):
"""
Dataset of classifying audios
========================================
Parameters:
audio_file: h5 file containing waveform
- waveform: numpy array of N x T
- label: numpy array of label name (encoded string)
"""
def __init__(self,
audio_file: str,
label2int: dict,
audio_transform,
indices: list):
super(AudioDataset, self).__init__(audio_file)
self.label2int = label2int
self.indices = indices
self.audio_transform = audio_transform
def __getitem__(self, i):
index = self.indices[i]
data = self._get_waveform(index)
waveform, label = data['waveform'], data['label']
audio_name = data['audio_name']
target = self.label2int[label]
waveform = self.audio_transform(waveform)
return waveform, target, label, audio_name
def __len__(self):
return len(self.indices)
class RandomAttributeVectorDataset(Dataset):
"""
Dataset of audio embeddings and RAW text
Generate random attributes for each audio
========================================
Initialization Parameters:
attr_lists (List[List[str]]): list of attributes
targets (List[int]): list of target
audio_embeddings (torch.tensor): audio embeddings
random_strategy (str):
- all: all attributes
- random_with_class: random attributes including class name
- random: random attributes
========================================
Returns:
audio_embedding (torch.tensor): D
desc (str): description text (randomly picked)
target (int): target index
"""
def __init__(self,
audio_embeddings,
attr_list: list,
targets: list,
random_strategy: str = 'all'):
self.audio_embeddings = audio_embeddings
self.targets = targets
self.attr_list = attr_list
assert random_strategy in ['all', 'random_with_class', 'random']
self.random_strategy = random_strategy
def __getitem__(self, idx):
target = self.targets[idx]
attr_list = self.attr_list[target]
audio_embedding = self.audio_embeddings[idx]
if self.random_strategy == 'all':
desc = '; '.join(attr_list)
elif self.random_strategy == 'random_with_class':
selected_attrs = [attr_list[0]]
num_attr = len(attr_list) - 1 # first element already selected
if num_attr:
num_elements = torch.randint(0, num_attr + 1, (1,)).item()
indices = torch.randperm(num_attr)[0: num_elements] + 1
selected_attrs.extend([attr_list[i] for i in indices])
desc = '; '.join(selected_attrs)
else:
selected_attrs = []
num_attr = len(attr_list)
if num_attr:
num_elements = torch.randint(1, num_attr + 1, (1,)).item()
indices = torch.randperm(num_attr)[0: num_elements]
selected_attrs.extend([attr_list[i] for i in indices])
desc = '; '.join(selected_attrs)
# print(desc)
return audio_embedding, desc, target
def __len__(self):
return len(self.targets)
class VectorDataset(Dataset):
"""
Bilinear Vector Dataset of audio embeddings
========================================
Parameters:
audio_embeddings (torch.tensor): B x D
targets (List[int]): B
========================================
Returns:
audio_embed (torch.tensor): D
target (int)
"""
def __init__(self,
audio_embeddings,
targets):
self.audio_embeddings = audio_embeddings
self.targets = targets
def __getitem__(self, idx):
return self.audio_embeddings[idx], self.targets[idx]
def __len__(self):
return len(self.targets)
def get_collate_fn(task, **kwargs):
if task == 'vector':
def vector_collate_fn(batch):
"""
collate_fn for bilinear vector dataset
================
Returns:
Batch dict:
- audio_embed (torch.tensor): B x D
- target (torch.tensor): B
"""
embeddings, targets = zip(*batch)
embeddings = torch.stack(embeddings, dim=0) # B x D
targets = torch.tensor(targets)
return {
'audio_embed': embeddings,
'target': targets
}
return vector_collate_fn
if task == 'audio':
def audio_collate_fn(batch):
"""
collate_fn for single audio task
================
Returns:
Batch dict:
- waveform (torch.tensor): B x T
- target (torch.tensor): B
- label (List[str]): B
- audio_name (List[str]): B
"""
waveform_list, targets, labels, audio_names = zip(*batch)
waveforms = torch.stack(waveform_list, dim=0) # B x T
targets = torch.tensor(targets)
return {
'waveform': waveforms,
'target': targets,
'label': labels,
'audio_name': audio_names
}
return audio_collate_fn
if task == 'audio-text':
tokenize_fn = kwargs['tokenize_fn']
def audio_text_collate_fn(batch):
"""
collate_fn for audio-text task (random attribute)
Use tokenizer to generate token
================
Returns:
Batch dict:
- waveform (torch.tensor): B x T
- target (torch.tensor): B
- input_ids (torch.tensor): token IDs
- attention_mask (torch.tensor): real tokens (1) and padding tokens (0)
"""
audio_data_list, desc_list, targets = zip(*batch)
audio_data = torch.stack(audio_data_list, dim=0) # B x T
targets = torch.tensor(targets)
desc_tokens = tokenize_fn(desc_list)
return {
'audio_data': audio_data,
'target': targets,
**desc_tokens}
return audio_text_collate_fn
def create_random_attr_dataloader(audio_embeddings,
attr_list,
targets,
tokenize_fn,
random_strategy: str = 'all',
is_train: bool = True,
**kwargs):
kwargs.setdefault('batch_size', 64)
kwargs.setdefault('num_workers', 8)
print(kwargs)
if not is_train:
random_strategy = 'all'
dataset = RandomAttributeVectorDataset(audio_embeddings=audio_embeddings,
attr_list=attr_list,
random_strategy=random_strategy,
targets=targets)
collate_fn = get_collate_fn(task='audio-text',
tokenize_fn=tokenize_fn)
if is_train:
dataloader = DataLoader(dataset=dataset,
collate_fn=collate_fn,
drop_last=True,
shuffle=True,
**kwargs)
else:
dataloader = DataLoader(dataset=dataset,
collate_fn=collate_fn,
drop_last=False,
shuffle=False,
**kwargs)
return dataloader
def create_bilinear_vector_dataloader(audio_embeddings,
targets,
is_train: bool = True,
**kwargs):
kwargs.setdefault('batch_size', len(targets))
kwargs.setdefault('num_workers', 8)
collate_fn = get_collate_fn(task='vector')
dataset = VectorDataset(audio_embeddings=audio_embeddings,
targets=targets)
if is_train:
dataloader = DataLoader(dataset=dataset,
collate_fn=collate_fn,
drop_last=True,
shuffle=True,
**kwargs)
else:
dataloader = DataLoader(dataset=dataset,
collate_fn=collate_fn,
drop_last=False,
shuffle=False,
**kwargs)
return dataloader
def create_train_cls_dataloader(audio_file: str,
label2int: dict,
indices: list,
audio_transform,
**kwargs):
kwargs.setdefault('batch_size', 64)
kwargs.setdefault('num_workers', 8)
collate_fn = get_collate_fn(task='audio')
dataset = AudioDataset(audio_file=audio_file,
label2int=label2int,
audio_transform=audio_transform,
indices=indices)
dataloader = DataLoader(dataset=dataset,
collate_fn=collate_fn,
drop_last=True,
shuffle=True,
**kwargs)
return dataloader
def create_val_cls_dataloader(audio_file: str,
label2int: dict,
indices: list,
audio_transform,
**kwargs):
kwargs.setdefault('batch_size', 64)
kwargs.setdefault('num_workers', 8)
dataset = AudioDataset(audio_file=audio_file,
label2int=label2int,
audio_transform=audio_transform,
indices=indices)
collate_fn = get_collate_fn(task='audio')
dataloader = DataLoader(dataset=dataset,
collate_fn=collate_fn,
drop_last=False,
shuffle=False,
**kwargs)
return dataloader