-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdataset.py
321 lines (276 loc) · 12.8 KB
/
dataset.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
import torch
import numpy as np
from loguru import logger
from typing import Optional, Tuple, List,Any
import random
from pathlib import Path
from typing import Sequence
import pandas as pd
from typing import Dict, Sequence
from torch.multiprocessing import Queue
from h5py import File
INT_MAX = np.iinfo(np.int16).max
class WeakHDF5Dataset(torch.utils.data.Dataset):
"""
HDF5 dataset indexed by a labels dataframe.
Indexing is done via the dataframe since we want to preserve some storage
in cases where oversampling is needed ( pretty likely )
"""
def __init__(
self,
data_frame: pd.DataFrame,
num_classes: int,
):
super(WeakHDF5Dataset, self).__init__()
self._dataframe = data_frame
self._datasetcache = {}
self._len = len(self._dataframe)
self._num_classes = num_classes
def __len__(self) -> int:
return self._len
def __del__(self):
for k, cache in self._datasetcache.items():
cache.close()
def _readdata(self, hdf5path: str, fname: str) -> torch.Tensor:
if not hdf5path in self._datasetcache:
self._datasetcache[hdf5path] = File(hdf5path, 'r')
data = self._datasetcache[hdf5path][f"{fname}"][:]
if np.issubdtype(data.dtype, np.integer):
data = (data/32768.).astype('float32')
return torch.as_tensor(data, dtype=torch.float32)
def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor, str]:
fname, label_idxs, hdf5path = self._dataframe.iloc[index][[
'filename', 'labels', 'hdf5path'
]]
# Init with all-Zeros classes i.e., nothing present
target = torch.zeros(self._num_classes, dtype=torch.float32).scatter_(
0, torch.as_tensor(label_idxs), 1)
data = self._readdata(hdf5path, fname)
return data, target, fname
class WeakRandomCropHDF5Dataset(WeakHDF5Dataset):
def __init__(self,
data_frame,
chunk_length: float,
num_classes: int,
sample_rate: int = 16000,
smooth: bool = False):
super(WeakRandomCropHDF5Dataset,
self).__init__(data_frame, num_classes=num_classes)
self._sr = sample_rate
self.target_transform = self._target_transform_multilabel
self._num_classes = num_classes
self.chunk_length = int(chunk_length * sample_rate)
self.smooth = smooth
def _target_transform_multilabel(self, label_idx: List) -> torch.Tensor:
target = torch.zeros(self._num_classes, dtype=torch.float32)
target = target.scatter_(0, torch.as_tensor(label_idx), 1)
return target
def _readdata(self, hdf5path: str, fname: str) -> torch.Tensor:
if not hdf5path in self._datasetcache:
self._datasetcache[hdf5path] = File(hdf5path, 'r')
data_shape = self._datasetcache[hdf5path][f"{fname}"].shape[-1]
if data_shape > self.chunk_length:
start_idx = random.randint(0, data_shape - self.chunk_length - 1)
data = self._datasetcache[hdf5path][f"{fname}"][
start_idx:start_idx + self.chunk_length]
else:
load_data = self._datasetcache[hdf5path][f"{fname}"][:]
data = np.zeros(self.chunk_length, dtype=load_data.dtype)
data_length = load_data.shape[-1]
start_idx = 0
#Randomly insert into array if longer
if self.chunk_length - data_length > 0:
start_idx = random.randint(0,
self.chunk_length - data_length - 1)
data[start_idx:start_idx + data_length] = load_data
if np.issubdtype(data.dtype, np.integer):
data = (data/32768.).astype('float32')
return torch.as_tensor(data, dtype=torch.float32)
def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor, str]:
fname, label_idxs, hdf5path = self._dataframe.iloc[index][[
'filename', 'labels','hdf5path'
]]
#Generate target from int list [1,5,7] --> [0,1,0,0,0,1,0,1]
target= self.target_transform(label_idxs)
data = self._readdata(hdf5path, fname)
return data, target, fname
class WeakChunkedHDF5Dataset(WeakHDF5Dataset):
def __init__(self, data_frame, num_classes:int, sample_rate:int =16000):
super(WeakChunkedHDF5Dataset, self).__init__(data_frame, num_classes)
self._sr = sample_rate
self.target_transform = self._target_transform_multilabel
self._num_classes = num_classes
def _target_transform_singlelabel(self, label_idx:int) -> torch.Tensor:
return torch.as_tensor(label_idx)
def _target_transform_multilabel(self, label_idx:List) -> torch.Tensor:
target = torch.zeros(self._num_classes,
dtype=torch.float32)
if -1 in label_idx: label_idx.remove(-1)
if len(label_idx) > 0:
target = target.scatter_(
0, torch.as_tensor(label_idx), 1)
return target
def _readdata(self, hdf5path: str, fname: str, from_time:int, to_time:int) -> torch.Tensor:
if not hdf5path in self._datasetcache:
self._datasetcache[hdf5path] = File(hdf5path, 'r')
data = self._datasetcache[hdf5path][f"{fname}"][from_time:to_time]
if np.issubdtype(data.dtype, np.integer):
data = (data/32768.).astype('float32')
return torch.as_tensor(data, dtype=torch.float32)
def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor, str]:
fname, label_idxs, from_time, to_time, hdf5path = self._dataframe.iloc[index][[
'filename', 'labels','from', 'to', 'hdf5path'
]]
#Generate target from int list [1,5,7] --> [0,1,0,0,0,1,0,1]
target= self.target_transform(label_idxs)
from_time = int(from_time * self._sr)
to_time = int(to_time * self._sr)
data = self._readdata(hdf5path, fname, from_time, to_time)
return data, target, fname
class UnlabeledRandomChunkedHDF5Dataset(torch.utils.data.Dataset):
def __init__(
self,
data_df,
chunk_length: float = 2.0,
sample_rate: int = 16000,
num_classes=527,
):
super(UnlabeledRandomChunkedHDF5Dataset, self).__init__()
self._dataframe = data_df
self.sample_rate = sample_rate
self.chunk_length = chunk_length
self.chunk_length_in_samples = int(self.sample_rate * self.chunk_length)
self.num_classes = num_classes
self._datasetcache = {}
logger.info(
f"Using random chunks of length {self.chunk_length_in_samples}")
def _readdata(self, hdf5path: str, fname: str) -> torch.Tensor:
if not hdf5path in self._datasetcache:
self._datasetcache[hdf5path] = File(hdf5path, 'r')
data_shape = self._datasetcache[hdf5path][f"{fname}"].shape[-1]
if data_shape > self.chunk_length_in_samples:
start_idx = random.randint(
0, data_shape - self.chunk_length_in_samples - 1)
data = self._datasetcache[hdf5path][f"{fname}"][
start_idx:start_idx + self.chunk_length_in_samples]
else:
load_data = self._datasetcache[hdf5path][f"{fname}"][:]
data = np.zeros(self.chunk_length_in_samples,
dtype=load_data.dtype)
data[:load_data.shape[0]] = load_data
if np.issubdtype(data.dtype, np.integer):
data = (data / 32768.).astype('float32')
return torch.as_tensor(data, dtype=torch.float32)
def __getitem__(self,
index: int) -> Tuple[torch.Tensor, torch.Tensor, str]:
fname, hdf5path = self._dataframe.iloc[index][['filename', 'hdf5path']]
#Generate target from int list [1,5,7] --> [0,1,0,0,0,1,0,1]
data = self._readdata(hdf5path, fname)
return data, torch.zeros(self.num_classes), fname
def __len__(self):
return len(self._dataframe)
class UnlabeledHDF5Dataset(UnlabeledRandomChunkedHDF5Dataset):
def __init__(self,*args,**kwrgs):
super().__init__(*args, **kwrgs)
def _readdata(self, hdf5path: str, fname: str) -> torch.Tensor:
if not hdf5path in self._datasetcache:
self._datasetcache[hdf5path] = File(hdf5path, 'r')
data_shape = self._datasetcache[hdf5path][f"{fname}"].shape[-1]
if data_shape > self.chunk_length_in_samples:
start_idx = 0
data = self._datasetcache[hdf5path][f"{fname}"][
start_idx:start_idx + self.chunk_length_in_samples]
else:
load_data = self._datasetcache[hdf5path][f"{fname}"][:]
data = np.zeros(self.chunk_length_in_samples,
dtype=load_data.dtype)
data[:load_data.shape[0]] = load_data
if np.issubdtype(data.dtype, np.integer):
data = (data / 32768.).astype('float32')
return torch.as_tensor(data, dtype=torch.float32)
class WeakHDF5DatasetTextLogits(torch.utils.data.Dataset):
"""
HDF5 dataset indexed by a labels dataframe.
Indexing is done via the dataframe since we want to preserve some storage
in cases where oversampling is needed ( pretty likely )
"""
def __init__(
self,
data_frame: pd.DataFrame,
num_classes: int = 527,
sr: int = 16000,
):
super().__init__()
self._datasetcache = {}
self._dataframe = data_frame
self._sr = sr
self._num_classes = num_classes
def _readdata(self, hdf5path: str, fname: str, start: int,
end: int) -> torch.Tensor:
if not hdf5path in self._datasetcache:
self._datasetcache[hdf5path] = File(hdf5path, 'r')
start_idx = int(start * self._sr)
end_idx = int(end * self._sr)
data = self._datasetcache[hdf5path][f"{fname}"][start_idx:end_idx]
if np.issubdtype(data.dtype, np.integer):
data = (data / 32768.).astype('float32')
return torch.as_tensor(data, dtype=torch.float32)
def __getitem__(
self, index: int
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, str]:
fname, probs, idxs, start, end, hdf5path = self._dataframe.iloc[index][
['filename', 'prob', 'idxs', 'start', 'end', 'hdf5path']]
soft_target = torch.zeros(self._num_classes,
dtype=torch.float32).scatter_(
0, torch.as_tensor(idxs),
torch.as_tensor(probs,
dtype=torch.float32))
target = torch.zeros(self._num_classes, dtype=torch.float32)
data = self._readdata(hdf5path, fname, start, end)
return data, target, soft_target, fname
def __len__(self):
return len(self._dataframe)
def pad(tensorlist: Sequence[torch.Tensor], padding_value: float = 0.):
# Tensors are expected to be B, ..., T
lengths = [f.shape[-1] for f in tensorlist]
dims = tensorlist[0].shape
trailing_dims = dims[:-1]
batch_dim = len(lengths)
num_raw_samples = max(lengths)
out_dims = (batch_dim, ) + trailing_dims + (num_raw_samples, )
out_tensor = torch.full(out_dims,
fill_value=padding_value,
dtype=torch.float32)
for i, tensor in enumerate(tensorlist):
length = tensor.shape[-1]
out_tensor[i, ..., :length] = tensor[..., :length]
return out_tensor, lengths
def sequential_pad(batches):
datas, *targets, fnames = zip(*batches)
targets = tuple(map(lambda x: torch.stack(x), targets))
datas, lengths = pad(datas)
return datas, *targets, torch.as_tensor(lengths), fnames
class BalancedSampler(torch.utils.data.WeightedRandomSampler):
def __init__(self,
labels_df: pd.Series,
replacement:bool = True,
num_samples: Optional[int] = None,
offset: int = 100,
random_state=None):
#labels_df is ideally a dataframe with some keys and labels as:
#[0,11]
#[5,100,200]
self._random_state = np.random.RandomState(seed=random_state)
single_labels_df = labels_df.copy().explode().reset_index()
single_labels_df.columns = ['index', 'label']
occurances = single_labels_df.groupby('label')['index'].apply(len)
occurances = occurances.sort_index()
weights = 1000. / (occurances + offset) # Offset classes with low prob
weights = weights.to_dict()
sample_weights = labels_df.apply(
lambda x: sum([weights[class_id] for class_id in x])).values
num_samples = len(sample_weights) if num_samples is None else num_samples
super().__init__(sample_weights, num_samples=num_samples, replacement=replacement)
if __name__ == "__main__":
from tqdm import tqdm
import utils