-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexperiment_models.py
404 lines (350 loc) · 13.5 KB
/
experiment_models.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""Different model architectures."""
import tensorflow as tf
# Multi-Head Attention block
class TransformerBlock(tf.keras.layers.Layer):
def __init__(self, embed_dim, num_heads, ff_dim):
super(TransformerBlock, self).__init__()
self.att = tf.keras.layers.MultiHeadAttention(num_heads=num_heads, key_dim=embed_dim)
self.ffn = tf.keras.Sequential([tf.keras.layers.Dense(ff_dim, activation="relu"), tf.keras.layers.Dense(embed_dim), ])
self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.dropout1 = tf.keras.layers.Dropout(0.5)
self.dropout2 = tf.keras.layers.Dropout(0.5)
def build(self, input_shape):
super(TransformerBlock, self).build(input_shape)
def call(self, inputs, training):
attn_output = self.att(inputs, inputs)
attn_output = self.dropout1(attn_output, training=training)
out1 = self.layernorm1(inputs + attn_output)
ffn_output = self.ffn(out1)
ffn_output = self.dropout2(ffn_output, training=training)
out = self.layernorm2(out1 + ffn_output)
return out
#### Baseline model ####
def dilation_model(
time_window=None,
eeg_input_dimension=64,
env_input_dimension=1,
layers=3,
kernel_size=3,
spatial_filters=8,
dilation_filters=16,
activation="relu",
compile=True,
inputs=tuple(),
):
"""Convolutional dilation model.
Code was taken and adapted from
https://github.com/exporl/eeg-matching-eusipco2020
Parameters
----------
time_window : int or None
Segment length. If None, the model will accept every time window input
length.
eeg_input_dimension : int
number of channels of the EEG
env_input_dimension : int
dimemsion of the stimulus representation.
if stimulus == envelope, env_input_dimension =1
if stimulus == mel, env_input_dimension =28
layers : int
Depth of the network/Number of layers
kernel_size : int
Size of the kernel for the dilation convolutions
spatial_filters : int
Number of parallel filters to use in the spatial layer
dilation_filters : int
Number of parallel filters to use in the dilation layers
activation : str or list or tuple
Name of the non-linearity to apply after the dilation layers
or list/tuple of different non-linearities
compile : bool
If model should be compiled
inputs : tuple
Alternative inputs
Returns
-------
tf.Model
The dilation model
References
----------
Accou, B., Jalilpour Monesi, M., Montoya, J., Van hamme, H. & Francart, T.
Modeling the relationship between acoustic stimulus and EEG with a dilated
convolutional neural network. In 2020 28th European Signal Processing
Conference (EUSIPCO), 1175–1179, DOI: 10.23919/Eusipco47968.2020.9287417
(2021). ISSN: 2076-1465.
Accou, B., Monesi, M. J., hamme, H. V. & Francart, T.
Predicting speech intelligibility from EEG in a non-linear classification
paradigm. J. Neural Eng. 18, 066008, DOI: 10.1088/1741-2552/ac33e9 (2021).
Publisher: IOP Publishing
"""
# If different inputs are required
if len(inputs) == 3:
eeg, env1, env2 = inputs[0], inputs[1], inputs[2]
else:
eeg = tf.keras.layers.Input(shape=[time_window, eeg_input_dimension])
env1 = tf.keras.layers.Input(shape=[time_window, env_input_dimension])
env2 = tf.keras.layers.Input(shape=[time_window, env_input_dimension])
# Activations to apply
if isinstance(activation, str):
activations = [activation] * layers
else:
activations = activation
env_proj_1 = env1
env_proj_2 = env2
# Spatial convolution
eeg_proj_1 = tf.keras.layers.Conv1D(spatial_filters, kernel_size=1)(eeg)
# Construct dilation layers
for layer_index in range(layers):
# dilation on EEG
eeg_proj_1 = tf.keras.layers.Conv1D(
dilation_filters,
kernel_size=kernel_size,
dilation_rate=kernel_size**layer_index,
strides=1,
activation=activations[layer_index],
)(eeg_proj_1)
# Dilation on envelope data, share weights
env_proj_layer = tf.keras.layers.Conv1D(
dilation_filters,
kernel_size=kernel_size,
dilation_rate=kernel_size**layer_index,
strides=1,
activation=activations[layer_index],
)
env_proj_1 = env_proj_layer(env_proj_1)
env_proj_2 = env_proj_layer(env_proj_2)
# Comparison
cos1 = tf.keras.layers.Dot(1, normalize=True)([eeg_proj_1, env_proj_1])
cos2 = tf.keras.layers.Dot(1, normalize=True)([eeg_proj_1, env_proj_2])
# Classification
out = tf.keras.layers.Dense(1, activation="sigmoid")(
tf.keras.layers.Flatten()(tf.keras.layers.Concatenate()([cos1, cos2]))
)
model = tf.keras.Model(inputs=[eeg, env1, env2], outputs=[out])
if compile:
model.compile(
optimizer=tf.keras.optimizers.Adam(),
metrics=["acc"],
loss=["binary_crossentropy"],
)
print(model.summary())
return model
#### Model: MHA+DC for EEG and DC for speech stimulus ####
def eeg_mha_dc_speech_dc_model(
time_window=None,
eeg_input_dimension=64,
env_input_dimension=1,
layers=3,
kernel_size=3,
dilation_filters=16,
activation="relu",
compile=True,
inputs=tuple(),
):
"""Convolutional dilation model.
Code was taken and adapted from
https://github.com/exporl/eeg-matching-eusipco2020
Parameters
----------
time_window : int or None
Segment length. If None, the model will accept every time window input
length.
eeg_input_dimension : int
number of channels of the EEG
env_input_dimension : int
dimemsion of the stimulus representation.
if stimulus == envelope, env_input_dimension =1
if stimulus == mel, env_input_dimension =28
layers : int
Depth of the network/Number of layers
kernel_size : int
Size of the kernel for the dilation convolutions
dilation_filters : int
Number of parallel filters to use in the dilation layers
activation : str or list or tuple
Name of the non-linearity to apply after the dilation layers
or list/tuple of different non-linearities
compile : bool
If model should be compiled
inputs : tuple
Alternative inputs
Returns
-------
tf.Model
The dilation model
References
----------
Accou, B., Jalilpour Monesi, M., Montoya, J., Van hamme, H. & Francart, T.
Modeling the relationship between acoustic stimulus and EEG with a dilated
convolutional neural network. In 2020 28th European Signal Processing
Conference (EUSIPCO), 1175–1179, DOI: 10.23919/Eusipco47968.2020.9287417
(2021). ISSN: 2076-1465.
Accou, B., Monesi, M. J., hamme, H. V. & Francart, T.
Predicting speech intelligibility from EEG in a non-linear classification
paradigm. J. Neural Eng. 18, 066008, DOI: 10.1088/1741-2552/ac33e9 (2021).
Publisher: IOP Publishing
"""
# If different inputs are required
if len(inputs) == 3:
eeg, env1, env2 = inputs[0], inputs[1], inputs[2]
else:
eeg = tf.keras.layers.Input(shape=[time_window, eeg_input_dimension])
env1 = tf.keras.layers.Input(shape=[time_window, env_input_dimension])
env2 = tf.keras.layers.Input(shape=[time_window, env_input_dimension])
# Activations to apply
if isinstance(activation, str):
activations = [activation] * layers
else:
activations = activation
env_proj_1 = env1
env_proj_2 = env2
# Multi-Head Attention
transformer_block_1 = TransformerBlock(embed_dim=eeg_input_dimension, num_heads=2, ff_dim=32)
eeg_proj_1 = transformer_block_1(eeg)
# Construct dilation layers
for layer_index in range(layers):
# dilation on EEG
eeg_proj_1 = tf.keras.layers.Conv1D(
dilation_filters,
kernel_size=kernel_size,
dilation_rate=kernel_size**layer_index,
strides=1,
activation=activations[layer_index],
)(eeg_proj_1)
# Dilation on envelope data, share weights
env_proj_layer = tf.keras.layers.Conv1D(
dilation_filters,
kernel_size=kernel_size,
dilation_rate=kernel_size**layer_index,
strides=1,
activation=activations[layer_index],
)
env_proj_1 = env_proj_layer(env_proj_1)
env_proj_2 = env_proj_layer(env_proj_2)
# Comparison
cos1 = tf.keras.layers.Dot(1, normalize=True)([eeg_proj_1, env_proj_1])
cos2 = tf.keras.layers.Dot(1, normalize=True)([eeg_proj_1, env_proj_2])
# Classification
out = tf.keras.layers.Dense(1, activation="sigmoid")(
tf.keras.layers.Flatten()(tf.keras.layers.Concatenate()([cos1, cos2]))
)
model = tf.keras.Model(inputs=[eeg, env1, env2], outputs=[out])
if compile:
model.compile(
optimizer=tf.keras.optimizers.Adam(),
metrics=["acc"],
loss=["binary_crossentropy"],
)
print(model.summary())
return model
#### Model: MHA+DC for EEG and GRU+DC for speech stimulus ####
def eeg_mha_dc_speech_gru_dc_model(
time_window=None,
eeg_input_dimension=64,
env_input_dimension=1,
layers=3,
kernel_size=3,
dilation_filters=16,
activation="relu",
compile=True,
inputs=tuple(),
):
"""Convolutional dilation model.
Code was taken and adapted from
https://github.com/exporl/eeg-matching-eusipco2020
Parameters
----------
time_window : int or None
Segment length. If None, the model will accept every time window input
length.
eeg_input_dimension : int
number of channels of the EEG
env_input_dimension : int
dimemsion of the stimulus representation.
if stimulus == envelope, env_input_dimension =1
if stimulus == mel, env_input_dimension =28
layers : int
Depth of the network/Number of layers
kernel_size : int
Size of the kernel for the dilation convolutions
dilation_filters : int
Number of parallel filters to use in the dilation layers
activation : str or list or tuple
Name of the non-linearity to apply after the dilation layers
or list/tuple of different non-linearities
compile : bool
If model should be compiled
inputs : tuple
Alternative inputs
Returns
-------
tf.Model
The dilation model
References
----------
Accou, B., Jalilpour Monesi, M., Montoya, J., Van hamme, H. & Francart, T.
Modeling the relationship between acoustic stimulus and EEG with a dilated
convolutional neural network. In 2020 28th European Signal Processing
Conference (EUSIPCO), 1175–1179, DOI: 10.23919/Eusipco47968.2020.9287417
(2021). ISSN: 2076-1465.
Accou, B., Monesi, M. J., hamme, H. V. & Francart, T.
Predicting speech intelligibility from EEG in a non-linear classification
paradigm. J. Neural Eng. 18, 066008, DOI: 10.1088/1741-2552/ac33e9 (2021).
Publisher: IOP Publishing
"""
# If different inputs are required
if len(inputs) == 3:
eeg, env1, env2 = inputs[0], inputs[1], inputs[2]
else:
eeg = tf.keras.layers.Input(shape=[time_window, eeg_input_dimension])
env1 = tf.keras.layers.Input(shape=[time_window, env_input_dimension])
env2 = tf.keras.layers.Input(shape=[time_window, env_input_dimension])
# Activations to apply
if isinstance(activation, str):
activations = [activation] * layers
else:
activations = activation
# Multi-Head Attention
transformer_block_1 = TransformerBlock(embed_dim=eeg_input_dimension, num_heads=2, ff_dim=32)
eeg_proj_1 = transformer_block_1(eeg)
# Gated Recurrent Unit
gru_model = tf.keras.layers.GRU(env_input_dimension, return_sequences=True)
env_proj_1 = gru_model(env1)
env_proj_2 = gru_model(env2)
# Construct dilation layers
for layer_index in range(layers):
# dilation on EEG
eeg_proj_1 = tf.keras.layers.Conv1D(
dilation_filters,
kernel_size=kernel_size,
dilation_rate=kernel_size**layer_index,
strides=1,
activation=activations[layer_index],
)(eeg_proj_1)
# Dilation on envelope data, share weights
env_proj_layer = tf.keras.layers.Conv1D(
dilation_filters,
kernel_size=kernel_size,
dilation_rate=kernel_size**layer_index,
strides=1,
activation=activations[layer_index],
)
env_proj_1 = env_proj_layer(env_proj_1)
env_proj_2 = env_proj_layer(env_proj_2)
# Comparison
cos1 = tf.keras.layers.Dot(1, normalize=True)([eeg_proj_1, env_proj_1])
cos2 = tf.keras.layers.Dot(1, normalize=True)([eeg_proj_1, env_proj_2])
# Classification
out = tf.keras.layers.Dense(1, activation="sigmoid")(
tf.keras.layers.Flatten()(tf.keras.layers.Concatenate()([cos1, cos2]))
)
model = tf.keras.Model(inputs=[eeg, env1, env2], outputs=[out])
if compile:
model.compile(
optimizer=tf.keras.optimizers.Adam(),
metrics=["acc"],
loss=["binary_crossentropy"],
)
print(model.summary())
return model