forked from sharathadavanne/seld-dcase2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keras_model.py
385 lines (321 loc) · 15.6 KB
/
keras_model.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
#
# The SELDnet architecture
#
from keras.layers import Bidirectional, Conv2D, MaxPooling2D, Input, Concatenate
from keras.layers.core import Dense, Activation, Dropout, Reshape, Permute
from keras.layers import add, multiply, GlobalAveragePooling2D, ELU
import keras.backend as K
from keras.layers.recurrent import GRU
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.layers.wrappers import TimeDistributed
from keras.optimizers import Adam
from keras.models import load_model
import keras
keras.backend.set_image_data_format('channels_first')
# from IPython import embed
# import numpy as np
import warnings
def _obtain_input_shape(input_shape,
default_size,
min_size,
data_format,
require_flatten,
weights=None):
"""Internal utility to compute/validate a model's tensor shape.
# Arguments
input_shape: Either None (will return the default network input shape),
or a user-provided shape to be validated.
default_size: Default input width/height for the model.
min_size: Minimum input width/height accepted by the model.
data_format: Image data format to use.
require_flatten: Whether the model is expected to
be linked to a classifier via a Flatten layer.
weights: One of `None` (random initialization)
or 'imagenet' (pre-training on ImageNet).
If weights='imagenet' input channels must be equal to 3.
# Returns
An integer shape tuple (may include None entries).
# Raises
ValueError: In case of invalid argument values.
"""
if weights != 'imagenet' and input_shape and len(input_shape) == 3:
if data_format == 'channels_first':
if input_shape[0] not in {1, 3}:
warnings.warn(
'This model usually expects 1 or 3 input channels. '
'However, it was passed an input_shape with {input_shape}'
' input channels.'.format(input_shape=input_shape[0]))
default_shape = (input_shape[0], default_size, default_size)
else:
if input_shape[-1] not in {1, 3}:
warnings.warn(
'This model usually expects 1 or 3 input channels. '
'However, it was passed an input_shape with {n_input_channels}'
' input channels.'.format(n_input_channels=input_shape[-1]))
default_shape = (default_size, default_size, input_shape[-1])
else:
if data_format == 'channels_first':
default_shape = (3, default_size, default_size)
else:
default_shape = (default_size, default_size, 3)
if weights == 'imagenet' and require_flatten:
if input_shape is not None:
if input_shape != default_shape:
raise ValueError('When setting `include_top=True` '
'and loading `imagenet` weights, '
'`input_shape` should be {default_shape}.'.format(default_shape=default_shape))
return default_shape
if input_shape:
if data_format == 'channels_first':
if input_shape is not None:
if len(input_shape) != 3:
raise ValueError(
'`input_shape` must be a tuple of three integers.')
if input_shape[0] != 3 and weights == 'imagenet':
raise ValueError('The input must have 3 channels; got '
'`input_shape={input_shape}`'.format(input_shape=input_shape))
if ((input_shape[1] is not None and input_shape[1] < min_size) or
(input_shape[2] is not None and input_shape[2] < min_size)):
raise ValueError('Input size must be at least {min_size}x{min_size};'
' got `input_shape={input_shape}`'.format(min_size=min_size,
input_shape=input_shape))
else:
if input_shape is not None:
if len(input_shape) != 3:
raise ValueError(
'`input_shape` must be a tuple of three integers.')
if input_shape[-1] != 3 and weights == 'imagenet':
raise ValueError('The input must have 3 channels; got '
'`input_shape={input_shape}`'.format(input_shape=input_shape))
if ((input_shape[0] is not None and input_shape[0] < min_size) or
(input_shape[1] is not None and input_shape[1] < min_size)):
raise ValueError('Input size must be at least {min_size}x{min_size};'
' got `input_shape={input_shape}`'.format(min_size=min_size,
input_shape=input_shape))
else:
if require_flatten:
input_shape = default_shape
else:
if data_format == 'channels_first':
input_shape = (3, None, None)
else:
input_shape = (None, None, 3)
if require_flatten:
if None in input_shape:
raise ValueError('If `include_top` is True, '
'you should specify a static `input_shape`. '
'Got `input_shape={input_shape}`'.format(input_shape=input_shape))
return input_shape
def squeeze_excite_block(input_tensor, ratio=16):
""" Create a channel-wise squeeze-excite block
Args:
input_tensor: input Keras tensor
ratio: number of output filters
Returns: a Keras tensor
References
- [Squeeze and Excitation Networks](https://arxiv.org/abs/1709.01507)
"""
init = input_tensor
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
filters = _tensor_shape(init)[channel_axis]
se_shape = (1, 1, filters)
se = GlobalAveragePooling2D()(init)
se = Reshape(se_shape)(se)
se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)
if K.image_data_format() == 'channels_first':
se = Permute((3, 1, 2))(se)
x = multiply([init, se])
return x
def spatial_squeeze_excite_block(input_tensor):
""" Create a spatial squeeze-excite block
Args:
input_tensor: input Keras tensor
Returns: a Keras tensor
References
- [Concurrent Spatial and Channel Squeeze & Excitation in Fully Convolutional Networks](https://arxiv.org/abs/1803.02579)
"""
se = Conv2D(1, (1, 1), activation='sigmoid', use_bias=False,
kernel_initializer='he_normal')(input_tensor)
x = multiply([input_tensor, se])
return x
def channel_spatial_squeeze_excite(input_tensor, ratio=16):
""" Create a spatial squeeze-excite block
Args:
input_tensor: input Keras tensor
ratio: number of output filters
Returns: a Keras tensor
References
- [Squeeze and Excitation Networks](https://arxiv.org/abs/1709.01507)
- [Concurrent Spatial and Channel Squeeze & Excitation in Fully Convolutional Networks](https://arxiv.org/abs/1803.02579)
"""
cse = squeeze_excite_block(input_tensor, ratio)
sse = spatial_squeeze_excite_block(input_tensor)
x = add([cse, sse])
return x
def _tensor_shape(tensor):
return getattr(tensor, '_keras_shape')
def get_model(data_in, data_out, dropout_rate, nb_cnn2d_filt, f_pool_size, t_pool_size,
rnn_size, fnn_size, weights, doa_objective, is_accdoa, is_baseline, ratio, is_tcn):
# model definition
spec_start = Input(shape=(data_in[-3], data_in[-2], data_in[-1]))
# CNN
spec_cnn = spec_start
spec_cnn = spec_start
for i, convCnt in enumerate(f_pool_size):
if is_baseline:
spec_cnn = Conv2D(filters=nb_cnn2d_filt, kernel_size=(3, 3), padding='same')(spec_cnn)
spec_cnn = BatchNormalization()(spec_cnn)
spec_cnn = Activation('relu')(spec_cnn)
else:
spec_aux = spec_cnn
spec_cnn = Conv2D(nb_cnn2d_filt, 3, padding='same')(spec_cnn)
spec_cnn = BatchNormalization()(spec_cnn)
spec_cnn = ELU()(spec_cnn)
spec_cnn = Conv2D(nb_cnn2d_filt, 3, padding='same')(spec_cnn)
spec_cnn = BatchNormalization()(spec_cnn)
spec_aux = Conv2D(nb_cnn2d_filt, 1, padding='same')(spec_aux)
spec_aux = BatchNormalization()(spec_aux)
spec_cnn = add([spec_cnn, spec_aux])
spec_cnn = ELU()(spec_cnn)
spec_cnn = channel_spatial_squeeze_excite(spec_cnn, ratio=ratio)
spec_cnn = add([spec_cnn, spec_aux])
spec_cnn = MaxPooling2D(pool_size=(t_pool_size[i], f_pool_size[i]))(spec_cnn)
spec_cnn = Dropout(dropout_rate)(spec_cnn)
spec_cnn = Permute((2, 1, 3))(spec_cnn)
# RNN
spec_rnn = Reshape((data_out[-2] if is_accdoa else data_out[0][-2], -1))(spec_cnn)
for nb_rnn_filt in rnn_size:
spec_rnn = Bidirectional(
GRU(nb_rnn_filt, activation='tanh', dropout=dropout_rate, recurrent_dropout=dropout_rate,
return_sequences=True),
merge_mode='mul'
)(spec_rnn)
# FC - DOA
doa = spec_rnn
for nb_fnn_filt in fnn_size:
doa = TimeDistributed(Dense(nb_fnn_filt))(doa)
doa = Dropout(dropout_rate)(doa)
doa = TimeDistributed(Dense(data_out[-1] if is_accdoa else data_out[1][-1]))(doa)
doa = Activation('tanh', name='doa_out')(doa)
model = None
if is_accdoa:
model = Model(inputs=spec_start, outputs=doa)
model.compile(optimizer=Adam(), loss='mse')
else:
# FC - SED
sed = spec_rnn
for nb_fnn_filt in fnn_size:
sed = TimeDistributed(Dense(nb_fnn_filt))(sed)
sed = Dropout(dropout_rate)(sed)
sed = TimeDistributed(Dense(data_out[0][-1]))(sed)
sed = Activation('sigmoid', name='sed_out')(sed)
if doa_objective is 'mse':
model = Model(inputs=spec_start, outputs=[sed, doa])
model.compile(optimizer=Adam(), loss=['binary_crossentropy', 'mse'], loss_weights=weights)
elif doa_objective is 'masked_mse':
doa_concat = Concatenate(axis=-1, name='doa_concat')([sed, doa])
model = Model(inputs=spec_start, outputs=[sed, doa_concat])
model.compile(optimizer=Adam(), loss=['binary_crossentropy', masked_mse], loss_weights=weights)
else:
print('ERROR: Unknown doa_objective: {}'.format(doa_objective))
exit()
model.summary()
return model
def get_seldtcn_model(data_in, data_out, dropout_rate, nb_cnn2d_filt, pool_size, fnn_size, weights, ratio):
# model definition
spec_start = Input(shape=(data_in[-3], data_in[-2], data_in[-1]))
spec_cnn = spec_start
# CONVOLUTIONAL LAYERS =========================================================
for i, convCnt in enumerate(pool_size):
spec_aux = spec_cnn
spec_cnn = Conv2D(nb_cnn2d_filt, 3, padding='same')(spec_cnn)
spec_cnn = BatchNormalization()(spec_cnn)
spec_cnn = ELU()(spec_cnn)
spec_cnn = Conv2D(nb_cnn2d_filt, 3, padding='same')(spec_cnn)
spec_cnn = BatchNormalization()(spec_cnn)
spec_aux = Conv2D(nb_cnn2d_filt, 1, padding='same')(spec_aux)
spec_aux = BatchNormalization()(spec_aux)
spec_cnn = add([spec_cnn, spec_aux])
spec_cnn = ELU()(spec_cnn)
spec_cnn = channel_spatial_squeeze_excite(spec_cnn, ratio=ratio)
spec_cnn = add([spec_cnn, spec_aux])
spec_cnn = MaxPooling2D(pool_size=(1, pool_size[i]))(spec_cnn)
spec_cnn = Dropout(dropout_rate)(spec_cnn)
spec_cnn = Permute((2, 1, 3))(spec_cnn)
resblock_input = Reshape((data_in[-2], -1))(spec_cnn)
# TCN layer ===================================================================
# residual blocks ------------------------
skip_connections = []
for d in range(10):
# 1D convolution
spec_conv1d = keras.layers.Convolution1D(filters=256,
kernel_size=(3),
padding='same',
dilation_rate=2 ** d)(resblock_input)
spec_conv1d = BatchNormalization()(spec_conv1d)
# activations
tanh_out = keras.layers.Activation('tanh')(spec_conv1d)
sigm_out = keras.layers.Activation('sigmoid')(spec_conv1d)
spec_act = keras.layers.Multiply()([tanh_out, sigm_out])
# spatial dropout
spec_drop = keras.layers.SpatialDropout1D(rate=0.5)(spec_act)
# 1D convolution
skip_output = keras.layers.Convolution1D(filters=128,
kernel_size=(1),
padding='same')(spec_drop)
res_output = keras.layers.Add()([resblock_input, skip_output])
if skip_output is not None:
skip_connections.append(skip_output)
resblock_input = res_output
# ---------------------------------------
# Residual blocks sum
spec_sum = keras.layers.Add()(skip_connections)
spec_sum = keras.layers.Activation('relu')(spec_sum)
# 1D convolution
spec_conv1d_2 = keras.layers.Convolution1D(filters=128,
kernel_size=(1),
padding='same')(spec_sum)
spec_conv1d_2 = keras.layers.Activation('relu')(spec_conv1d_2)
# 1D convolution
spec_tcn = keras.layers.Convolution1D(filters=128,
kernel_size=(1),
padding='same')(spec_conv1d_2)
spec_tcn = keras.layers.Activation('tanh')(spec_tcn)
# SED ==================================================================
sed = spec_tcn
for nb_fnn_filt in fnn_size:
sed = TimeDistributed(Dense(nb_fnn_filt))(sed)
sed = Dropout(dropout_rate)(sed)
sed = TimeDistributed(Dense(data_out[0][-1]))(sed)
sed = Activation('sigmoid', name='sed_out')(sed)
# DOA ==================================================================
doa = spec_tcn
for nb_fnn_filt in fnn_size:
doa = TimeDistributed(Dense(nb_fnn_filt))(doa)
doa = Dropout(dropout_rate)(doa)
doa = TimeDistributed(Dense(data_out[1][-1]))(doa)
doa = Activation('tanh', name='doa_out')(doa)
model = Model(inputs=spec_start, outputs=[sed, doa])
model.compile(optimizer=Adam(), loss=['binary_crossentropy', 'mse'], loss_weights=weights)
model.summary()
return model
def masked_mse(y_gt, model_out):
nb_classes = 12 # TODO fix this hardcoded value of number of classes
# SED mask: Use only the predicted DOAs when gt SED > 0.5
sed_out = y_gt[:, :, :nb_classes] >= 0.5
sed_out = keras.backend.repeat_elements(sed_out, 3, -1)
sed_out = keras.backend.cast(sed_out, 'float32')
# Use the mask to computed mse now. Normalize with the mask weights
return keras.backend.sqrt(keras.backend.sum(
keras.backend.square(y_gt[:, :, nb_classes:] - model_out[:, :, nb_classes:]) * sed_out)) / keras.backend.sum(
sed_out)
def load_seld_model(model_file, doa_objective):
if doa_objective is 'mse':
return load_model(model_file)
elif doa_objective is 'masked_mse':
return load_model(model_file, custom_objects={'masked_mse': masked_mse})
else:
print('ERROR: Unknown doa objective: {}'.format(doa_objective))
exit()