-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_feature.py
359 lines (263 loc) · 14.2 KB
/
single_feature.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
import tensorflow as tf
import numpy as np
import util
class PhonemeEmbeddedLayer(tf.keras.layers.Layer):
def __init__(self, units_embedded=64, units_lstm=32):
# layer
self.max_pool = tf.keras.layers.MaxPool2D(pool_size=(3, 1), strides=(3, 1), padding='same')
self.layer_exp1 = tf.keras.layers.Lambda(lambda x: tf.keras.backend.expand_dims(x, axis=3))
self.embedding_layer = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(units_embedded, use_bias=False))
# self.lstm_layer = tf.keras.layers.LSTM(units_lstm, return_sequences=True)
self.lstm_layer = tf.keras.layers.CuDNNLSTM(units_lstm, return_sequences=True)
self.squeeze_layer = util.SqueezeLayer()
def get_config(self):
config = super().get_config().copy()
config.update({
'units_embedded': self.units_embedded,
'units_lstm': self.units_lstm,
})
return config
def __call__(self, input_phoneme):
phoneme_proj = input_phoneme
phoneme_out = self.layer_exp1(phoneme_proj)
# layer
phoneme_out = self.max_pool(phoneme_out)
phoneme_out = self.squeeze_layer(phoneme_out)
# layer
phoneme_out = self.embedding_layer(phoneme_out)
# layer
phoneme_out = self.lstm_layer(phoneme_out) # size = (210,16)
return phoneme_out
# LSTM-based model which uses mel spectrogram as speech representation in match/mismatch task
def lstm_mel(shape_eeg, shape_spch, units_lstm=32, filters_cnn_eeg=16, filters_cnn_env=16,
units_hidden=128,
stride_temporal=3, kerSize_temporal=9, spatial_filters_eeg=32,
spatial_filters_mel=8, fun_act='tanh'):
"""
Return an LSTM based model where batch normalization is applied to input of each layer.
:param shape_eeg: a numpy array, shape of EEG signal (time, channel)
:param shape_spch: a numpy array, shape of speech signal (time, feature_dim)
:param units_lstm: an int, number of units in LSTM
:param filters_cnn_eeg: an int, number of CNN filters applied on EEG
:param filters_cnn_env: an int, number of CNN filters applied on envelope
:param units_hidden: an int, number of units in the first time_distributed layer
:param stride_temporal: an int, amount of stride in the temporal direction
:param kerSize_temporal: an int, size of CNN filter kernel in the temporal direction
:param fun_act: activation function used in layers
:return: LSTM-based model
"""
############
input_eeg = tf.keras.layers.Input(shape=shape_eeg)
input_spch1 = tf.keras.layers.Input(shape=shape_spch)
input_spch2 = tf.keras.layers.Input(shape=shape_spch)
############
#### upper part of network dealing with EEG.
layer_exp1 = tf.keras.layers.Lambda(lambda x: tf.keras.backend.expand_dims(x, axis=3))
eeg_proj = input_eeg
# layer
output_eeg = tf.keras.layers.BatchNormalization()(eeg_proj) # batch normalization
output_eeg = tf.keras.layers.Conv1D(spatial_filters_eeg, kernel_size=1)(output_eeg)
# layer
output_eeg = tf.keras.layers.BatchNormalization()(output_eeg)
output_eeg = layer_exp1(output_eeg)
output_eeg = tf.keras.layers.Convolution2D(filters_cnn_eeg, (kerSize_temporal, 1),
strides=(stride_temporal, 1), activation="relu")(output_eeg)
# layer
layer_permute = tf.keras.layers.Permute((1, 3, 2))
output_eeg = layer_permute(output_eeg)
layer_reshape = tf.keras.layers.Reshape((tf.keras.backend.int_shape(output_eeg)[1],
tf.keras.backend.int_shape(output_eeg)[2] *
tf.keras.backend.int_shape(output_eeg)[3]))
output_eeg = layer_reshape(output_eeg)
layer2_timeDis = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(units_hidden, activation=fun_act))
output_eeg = layer2_timeDis(output_eeg)
# layer
output_eeg = tf.keras.layers.BatchNormalization()(output_eeg)
layer3_timeDis = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(units_lstm, activation=fun_act))
output_eeg = layer3_timeDis(output_eeg)
##############
#### Bottom part of the network dealing with Speech.
spch1_proj = input_spch1
spch2_proj = input_spch2
# layer
BN_layer = tf.keras.layers.BatchNormalization()
output_spch1 = BN_layer(spch1_proj)
output_spch2 = BN_layer(spch2_proj)
env_spatial_layer = tf.keras.layers.Conv1D(spatial_filters_mel, kernel_size=1)
output_spch1 = env_spatial_layer(output_spch1)
output_spch2 = env_spatial_layer(output_spch2)
# layer
BN_layer1 = tf.keras.layers.BatchNormalization()
output_spch1 = BN_layer1(output_spch1)
output_spch2 = BN_layer1(output_spch2)
output_spch1 = layer_exp1(output_spch1)
output_spch2 = layer_exp1(output_spch2)
conv_env_layer = tf.keras.layers.Convolution2D(filters_cnn_env, (kerSize_temporal, 1),
strides=(stride_temporal, 1), activation="relu")
output_spch1 = conv_env_layer(output_spch1)
output_spch2 = conv_env_layer(output_spch2)
# layer
BN_layer2 = tf.keras.layers.BatchNormalization()
output_spch1 = BN_layer2(output_spch1)
output_spch2 = BN_layer2(output_spch2)
output_spch1 = layer_permute(output_spch1)
output_spch2 = layer_permute(output_spch2)
layer_reshape = tf.keras.layers.Reshape((tf.keras.backend.int_shape(output_spch1)[1],
tf.keras.backend.int_shape(output_spch1)[2] *
tf.keras.backend.int_shape(output_spch1)[3]))
output_spch1 = layer_reshape(output_spch1) # size = (210,32)
output_spch2 = layer_reshape(output_spch2)
# lstm_spch = tf.keras.layers.LSTM(units_lstm, return_sequences=True, activation= fun_act)
lstm_spch = tf.compat.v1.keras.layers.CuDNNLSTM(units_lstm, return_sequences=True)
output_spch1 = lstm_spch(output_spch1)
output_spch2 = lstm_spch(output_spch2)
##############
#### last common layers
# layer
layer_dot = util.DotLayer()
cos_scores = layer_dot([output_eeg, output_spch1])
cos_scores2 = layer_dot([output_eeg, output_spch2])
# layer
layer_expand = tf.keras.layers.Lambda(lambda x: tf.keras.backend.expand_dims(x, axis=2))
layer_sigmoid = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(1, activation='sigmoid'))
cos_scores_mix = tf.keras.layers.Concatenate()([layer_expand(cos_scores), layer_expand(cos_scores2)])
cos_scores_sig = layer_sigmoid(cos_scores_mix)
# layer
layer_ave = tf.keras.layers.Lambda(lambda x: tf.reduce_mean(x, axis=1, keepdims=True))
cos_scores_sig = util.SqueezeLayer()(cos_scores_sig, axis=2)
y_out = layer_ave(cos_scores_sig)
model = tf.keras.Model(inputs=[input_eeg, input_spch1, input_spch2], outputs=[y_out, cos_scores_sig])
return model
# LSTM-based model that uses phonemes as speech representation in match/mismatch task
def lstm_phoneme(shape_eeg, shape_spch, units_lstm=32, filters_cnn_eeg=16,
units_hidden=128,
stride_temporal=3, kerSize_temporal=9, spatial_filters_eeg=32,
units_embedded= 64, fun_act='tanh'):
############
input_eeg = tf.keras.layers.Input(shape=shape_eeg)
phoneme1 = tf.keras.layers.Input(shape=shape_spch)
phoneme2 = tf.keras.layers.Input(shape=shape_spch)
############
#### upper part of network dealing with EEG.
layer_exp1 = tf.keras.layers.Lambda(lambda x: tf.keras.backend.expand_dims(x, axis=3))
eeg_proj = input_eeg
# layer
output_eeg = tf.keras.layers.BatchNormalization()(eeg_proj) # batch normalization
output_eeg = tf.keras.layers.Conv1D(spatial_filters_eeg, kernel_size=1)(output_eeg)
# layer
output_eeg = tf.keras.layers.BatchNormalization()(output_eeg)
output_eeg = layer_exp1(output_eeg)
output_eeg = tf.keras.layers.Convolution2D(filters_cnn_eeg, (kerSize_temporal, 1),
strides=(stride_temporal, 1), activation="relu", padding='same')(output_eeg)
# layer
layer_permute = tf.keras.layers.Permute((1, 3, 2))
output_eeg = layer_permute(output_eeg)
layer_reshape = tf.keras.layers.Reshape((tf.keras.backend.int_shape(output_eeg)[1],
tf.keras.backend.int_shape(output_eeg)[2] *
tf.keras.backend.int_shape(output_eeg)[3]))
output_eeg = layer_reshape(output_eeg)
layer2_timeDis = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(units_hidden, activation=fun_act))
output_eeg = layer2_timeDis(output_eeg)
# layer
output_eeg = tf.keras.layers.BatchNormalization()(output_eeg)
layer3_timeDis = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(units_lstm, activation=fun_act))
output_eeg = layer3_timeDis(output_eeg)
##############
#### Bottom part of the network dealing with Envelope.
phoneme1_proj = phoneme1
phoneme2_proj = phoneme2
phoneme_layer = PhonemeEmbeddedLayer(units_embedded=units_embedded, units_lstm=units_lstm)
phoneme1_out = phoneme_layer(phoneme1_proj)
phoneme2_out = phoneme_layer(phoneme2_proj)
##############
#### last common layers
# layer
layer_dot = util.DotLayer()
cos_scores = layer_dot([output_eeg, phoneme1_out])
cos_scores2 = layer_dot([output_eeg, phoneme2_out])
# layer
layer_expand = tf.keras.layers.Lambda(lambda x: tf.keras.backend.expand_dims(x, axis=2))
layer_sigmoid = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(1, activation='sigmoid'))
cos_scores_mix = tf.keras.layers.Concatenate()([layer_expand(cos_scores), layer_expand(cos_scores2)])
cos_scores_sig = layer_sigmoid(cos_scores_mix)
# layer
layer_ave = tf.keras.layers.Lambda(lambda x: tf.reduce_mean(x, axis=1, keepdims=True))
cos_scores_sig = util.SqueezeLayer()(cos_scores_sig, axis=2)
y_out = layer_ave(cos_scores_sig)
model = tf.keras.Model(inputs=[input_eeg, phoneme1, phoneme2], outputs=[y_out, cos_scores_sig])
return model
# LSTM-based model which uses word embedding as speech representation in match/mismatch task
def lstm_spch_pooling(shape_eeg, shape_spch, units_lstm=32, filters_cnn_eeg=16, filters_cnn_env=16,
units_hidden=128,
stride_temporal=3, kerSize_temporal=9, spatial_filters_eeg=32, fun_act='tanh'):
############
input_eeg = tf.keras.layers.Input(shape=shape_eeg)
input_spch1 = tf.keras.layers.Input(shape=shape_spch)
input_spch2 = tf.keras.layers.Input(shape=shape_spch)
############
#### upper part of network dealing with EEG.
layer_exp1 = tf.keras.layers.Lambda(lambda x: tf.keras.backend.expand_dims(x, axis=3))
eeg_proj = input_eeg
# layer
output_eeg = tf.keras.layers.BatchNormalization()(eeg_proj) # batch normalization
output_eeg = tf.keras.layers.Conv1D(spatial_filters_eeg, kernel_size=1)(output_eeg)
# layer
output_eeg = tf.keras.layers.BatchNormalization()(output_eeg)
output_eeg = layer_exp1(output_eeg)
output_eeg = tf.keras.layers.Convolution2D(filters_cnn_eeg, (kerSize_temporal, 1),
strides=(stride_temporal, 1), activation="relu", padding='same')(output_eeg)
# layer
layer_permute = tf.keras.layers.Permute((1, 3, 2))
output_eeg = layer_permute(output_eeg)
layer_reshape = tf.keras.layers.Reshape((tf.keras.backend.int_shape(output_eeg)[1],
tf.keras.backend.int_shape(output_eeg)[2] *
tf.keras.backend.int_shape(output_eeg)[3]))
output_eeg = layer_reshape(output_eeg)
layer2_timeDis = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(units_hidden, activation=fun_act))
output_eeg = layer2_timeDis(output_eeg)
# layer
output_eeg = tf.keras.layers.BatchNormalization()(output_eeg)
layer3_timeDis = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(units_lstm, activation=fun_act))
output_eeg = layer3_timeDis(output_eeg)
##############
#### Bottom part of the network dealing with Speech.
spch1_proj = input_spch1
spch2_proj = input_spch2
# layer
output_spch1 = layer_exp1(spch1_proj)
output_spch2 = layer_exp1(spch2_proj)
max_pool = tf.keras.layers.MaxPool2D(pool_size=(3, 1), strides=(3, 1), padding='same')
output_spch1 = max_pool(output_spch1)
output_spch2 = max_pool(output_spch2)
# layer
BN_layer2 = tf.keras.layers.BatchNormalization()
output_spch1 = BN_layer2(output_spch1)
output_spch2 = BN_layer2(output_spch2)
output_spch1 = layer_permute(output_spch1)
output_spch2 = layer_permute(output_spch2)
layer_reshape = tf.keras.layers.Reshape((tf.keras.backend.int_shape(output_spch1)[1],
tf.keras.backend.int_shape(output_spch1)[2] *
tf.keras.backend.int_shape(output_spch1)[3]))
output_spch1 = layer_reshape(output_spch1)
output_spch2 = layer_reshape(output_spch2)
# lstm_spch = tf.keras.layers.LSTM(units_lstm, return_sequences=True, activation= fun_act)
lstm_spch = tf.compat.v1.keras.layers.CuDNNLSTM(units_lstm, return_sequences=True)
output_spch1 = lstm_spch(output_spch1)
output_spch2 = lstm_spch(output_spch2)
##############
#### last common layers
# layer
layer_dot = util.DotLayer()
cos_scores = layer_dot([output_eeg, output_spch1])
cos_scores2 = layer_dot([output_eeg, output_spch2])
# layer
layer_expand = tf.keras.layers.Lambda(lambda x: tf.keras.backend.expand_dims(x, axis=2))
layer_sigmoid = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(1, activation='sigmoid'))
cos_scores_mix = tf.keras.layers.Concatenate()([layer_expand(cos_scores), layer_expand(cos_scores2)])
cos_scores_sig = layer_sigmoid(cos_scores_mix)
# layer
layer_ave = tf.keras.layers.Lambda(lambda x: tf.reduce_mean(x, axis=1, keepdims=True))
cos_scores_sig = util.SqueezeLayer()(cos_scores_sig, axis=2)
y_out = layer_ave(cos_scores_sig)
model = tf.keras.Model(inputs=[input_eeg, input_spch1, input_spch2], outputs=[y_out, cos_scores_sig])
return model