-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataset_generator.py
235 lines (198 loc) · 7.19 KB
/
dataset_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
"""Code for the dataset_generator for task1."""
import itertools
import os
import numpy as np
import tensorflow as tf
@tf.function
def default_batch_equalizer_fn(*args):
"""Batch equalizer.
Prepares the inputs for a model to be trained in
match-mismatch task. It makes sure that match_env
and mismatch_env are equally presented as a first
envelope in match-mismatch task.
Parameters
----------
args : Sequence[tf.Tensor]
List of tensors representing feature data
Returns
-------
Tuple[Tuple[tf.Tensor], tf.Tensor]
Tuple of the EEG/speech features serving as the input to the model and
the labels for the match/mismatch task
Notes
-----
This function will also double the batch size. E.g. if the batch size of
the elements in each of the args was 32, the output features will have
a batch size of 64.
"""
eeg = args[0]
new_eeg = tf.concat([eeg, eeg], axis=0)
all_features = [new_eeg]
for match, mismatch in zip(args[1::2], args[2::2]):
stimulus_feature1 = tf.concat([match, mismatch], axis=0)
stimulus_feature2 = tf.concat([mismatch, match], axis=0)
all_features += [stimulus_feature1, stimulus_feature2]
labels = tf.concat(
[
tf.tile(tf.constant([[0]]), [tf.shape(eeg)[0], 1]),
tf.tile(tf.constant([[1]]), [tf.shape(eeg)[0], 1]),
],
axis=0,
)
# print(new_eeg.shape, env1.shape, env2.shape, labels.shape)
return tuple(all_features), labels
def create_tf_dataset(
data_generator,
window_length,
batch_equalizer_fn=None,
hop_length=64,
batch_size=64,
data_types=(tf.float32, tf.float32, tf.float32),
feature_dims=(64, 28, 28) # Change to (64,1,1) for using speech envelopes and (64,28,28) for using mel-spectrograms
):
"""Creates a tf.data.Dataset.
This will be used to create a dataset generator that will
pass windowed data to a model in both tasks.
Parameters
---------
data_generator: DataGenerator
A data generator.
window_length: int
Length of the decision window in samples.
batch_equalizer_fn: Callable
Function that will be applied on the data after batching (using
the `map` method from tf.data.Dataset). In the match/mismatch task,
this function creates the imposter segments and labels.
hop_length: int
Hop length between two consecutive decision windows.
batch_size: Optional[int]
If not None, specifies the batch size. In the match/mismatch task,
this amount will be doubled by the default_batch_equalizer_fn
data_types: Union[Sequence[tf.dtype], tf.dtype]
The data types that the individual features of data_generator should
be cast to. If you only specify a single datatype, it will be chosen
for all EEG/speech features.
Returns
-------
tf.data.Dataset
A Dataset object that generates data to train/evaluate models
efficiently
"""
# create tf dataset from generator
dataset = tf.data.Dataset.from_generator(
data_generator,
output_signature=tuple(
tf.TensorSpec(shape=(None, x), dtype=data_types[index])
for index, x in enumerate(feature_dims)
),
)
# window dataset
dataset = dataset.map(
lambda *args: [
tf.signal.frame(arg, window_length, hop_length, axis=0)
for arg in args
]
)
# batch data
dataset = dataset.interleave(
lambda *args: tf.data.Dataset.from_tensor_slices(args),
cycle_length=4,
block_length=16,
)
if batch_size is not None:
dataset = dataset.batch(batch_size, drop_remainder=True)
if batch_equalizer_fn is not None:
# Create the labels and make sure classes are balanced
dataset = dataset.map(batch_equalizer_fn)
return dataset
class MatchMismatchDataGenerator:
"""Generate data for the Match/Mismatch task."""
def __init__(
self,
files,
window_length,
spacing
):
"""Initialize the DataGenerator.
Parameters
----------
files: Sequence[Union[str, pathlib.Path]]
Files to load.
window_length: int
Length of the decision window.
spacing: int
Spacing between matched and mismatched samples
"""
self.window_length = window_length
self.files = self.group_recordings(files)
self.spacing = spacing
def group_recordings(self, files):
"""Group recordings and corresponding stimuli.
Parameters
----------
files : Sequence[Union[str, pathlib.Path]]
List of filepaths to preprocessed and split EEG and speech features
Returns
-------
list
Files grouped by the self.group_key_fn and subsequently sorted
by the self.feature_sort_fn.
"""
new_files = []
grouped = itertools.groupby(sorted(files), lambda x: "_-_".join(os.path.basename(x).split("_-_")[:3]))
for recording_name, feature_paths in grouped:
new_files += [sorted(feature_paths, key=lambda x: "0" if x == "eeg" else x)]
return new_files
def __len__(self):
return len(self.files)
def __getitem__(self, recording_index):
"""Get data for a certain recording.
Parameters
----------
recording_index: int
Index of the recording in this dataset
Returns
-------
Union[Tuple[tf.Tensor,...], Tuple[np.ndarray,...]]
The features corresponding to the recording_index recording
"""
data = []
for feature in self.files[recording_index]:
data += [np.load(feature).astype(np.float32)]
data = self.prepare_data(data)
return tuple(tf.constant(x) for x in data)
def __call__(self):
"""Load data for the next recording.
Yields
-------
Union[Tuple[tf.Tensor,...], Tuple[np.ndarray,...]]
The features corresponding to the recording_index recording
"""
for idx in range(self.__len__()):
yield self.__getitem__(idx)
if idx == self.__len__() - 1:
self.on_epoch_end()
def on_epoch_end(self):
"""Change state at the end of an epoch."""
np.random.shuffle(self.files)
def prepare_data(self, data):
"""Creates mismatch (imposter) envelope.
Parameters
----------
data: Sequence[numpy.ndarray]
Data to create an imposter for.
Returns
-------
tuple (numpy.ndarray, numpy.ndarray, numpy.ndarray, ...)
(EEG, matched stimulus feature, mismatched stimulus feature, ...).
"""
eeg = data[0]
new_length = eeg.shape[0] - self.window_length - self.spacing
resulting_data = [eeg[:new_length, ...]]
for stimulus_feature in data[1:]:
match_feature = stimulus_feature[:new_length, ...]
mismatch_feature = stimulus_feature[
self.spacing + self.window_length:, ...
]
resulting_data += [match_feature, mismatch_feature]
return resulting_data