-
Notifications
You must be signed in to change notification settings - Fork 36
/
models_time_invariant.py
238 lines (175 loc) · 7.58 KB
/
models_time_invariant.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
"""
This module contains several time-invariant models.
I'm assuming a raw-audio input, which is converted to melspectrogram using Kapre.
"""
from __future__ import print_function
from keras.models import Sequential, Model
from keras.layers import Activation, Dense, Flatten, Input, Reshape, Dropout, Permute
from keras.layers.convolutional import Conv2D
from keras.layers.normalization import BatchNormalization
from keras.layers.recurrent import GRU
from keras.layers.pooling import MaxPooling2D, GlobalAveragePooling2D
from keras.layers.merge import Concatenate
from keras import backend as K
from kapre.time_frequency import Melspectrogram
from global_config import *
def model_multi_kernel_shape(n_out, input_shape=INPUT_SHAPE,
out_activation='softmax'):
"""
Symbolic summary:
> c2' - p2 - c2 - p2 - c2 - p2 - c2 - p3 - d1
where c2' -> multiple kernel shapes
Parameters
----------
n_out: integer, number of output nodes
input_shape: tuple, an input shape, which doesn't include batch-axis.
out_activation: activation function on the output
"""
audio_input = Input(shape=input_shape)
x = Melspectrogram(sr=SR, n_mels=64, power_melgram=2.0, return_decibel_melgram=True)(audio_input)
x = BatchNormalization(axis=channel_axis)(x)
x1 = Conv2D(7, (20, 3), padding='same')(x)
x2 = Conv2D(7, (3, 3), padding='same')(x)
x3 = Conv2D(7, (3, 20), padding='same')(x)
x = Concatenate(axis=channel_axis)([x1, x2, x3])
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(21, (3, 3), padding='same')(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(21, (3, 3), padding='same')(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(21, (3, 3), padding='same')(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((4, 4), padding='same')(x)
x = GlobalAveragePooling2D()(x)
out = Dense(n_out, activation=out_activation)(x)
model = Model(audio_input, out)
return model
def model_crnn_icassp2017_choi(n_out, input_shape=INPUT_SHAPE,
out_activation='softmax'):
"""A simplified model of
Convolutional Recurrent Neural Networks for Music Classification,
K Choi, G Fazekas, M Sandler, K Choi, ICASSP, 2017, New Orleans, USA
Symbolic summary:
> c2 - p2 - c2 - p2 - c2 - p2 - c2 - p2 - r1 - r2 - d1
Parameters
----------
n_out: integer, number of output nodes
input_shape: tuple, an input shape, which doesn't include batch-axis.
out_activation: activation function on the output
"""
audio_input = Input(shape=input_shape)
x = Melspectrogram(sr=SR, n_mels=64, power_melgram=2.0, return_decibel_melgram=True)(audio_input)
x = BatchNormalization(axis=channel_axis)(x)
x = Conv2D(21, (3, 3), padding='same')(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(21, (3, 3), padding='same')(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(21, (3, 3), padding='same')(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(21, (3, 3), padding='same')(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((4, 4), padding='same')(x)
if K.image_dim_ordering() == 'channels_first':
x = Permute((3, 1, 2))(x)
x = Reshape((-1, 21))(x)
# GRU block 1, 2, output
x = GRU(41, return_sequences=True, name='gru1')(x)
x = GRU(41, return_sequences=False, name='gru2')(x)
x = Dropout(0.3)(x)
out = Dense(n_out, activation=out_activation)(x)
model = Model(audio_input, out)
return model
def model_conv3x3_ismir2016_choi(n_out, input_shape=INPUT_SHAPE,
out_activation='softmax'):
""" A simplified model of
Automatic Tagging Using Deep Convolutional Neural Networks,
K Choi, G Fazekas, M Sandler, ISMIR, 2016, New York, USA
Symbolic summary:
> c2 - p2 - c2 - p2 - c2 - p2 - c2 - p2 - c2 - p3 - d1
Modifications:
* n_mels (96 -> 32)
* n_channels (many -> [16, 24, 32, 40, 48])
* remove dropout
* maxpooling (irregular to fit the size -> all (2, 2))
* add GlobalAveragePooling2D
"""
model = Sequential()
model.add(Melspectrogram(sr=SR, n_mels=64, power_melgram=2.0, return_decibel_melgram=True,
input_shape=input_shape))
model.add(BatchNormalization(axis=channel_axis))
model.add(Conv2D(10, (3, 3), padding='same'))
model.add(BatchNormalization(axis=channel_axis))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Conv2D(15, (3, 3), padding='same'))
model.add(BatchNormalization(axis=channel_axis))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Conv2D(15, (3, 3), padding='same'))
model.add(BatchNormalization(axis=channel_axis))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Conv2D(20, (3, 3), padding='same'))
model.add(BatchNormalization(axis=channel_axis))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Conv2D(20, (3, 3), padding='same'))
model.add(BatchNormalization(axis=channel_axis))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(GlobalAveragePooling2D())
model.add(Dense(n_out, activation=out_activation))
return model
def model_conv1d_icassp2014_sander(n_out, input_shape=INPUT_SHAPE,
out_activation='softmax'):
"""A simplified model of
End-to-end learning for music audio,
Sander Dieleman and Benjamin Schrauwen, ICASSP, 2014
Symbolic summary:
> c1 - p1 - c1 - p1 - c1 - p1 - p3 - d1
Modifications:
* Add BatchNormalization
* n_mels (128 -> 32)
* n_layers (2 -> 3)
* add GlobalAveragePooling2D
Parameters
----------
n_out: integer, number of output nodes
input_shape: tuple, an input shape, which doesn't include batch-axis.
out_activation: activation function on the output
"""
model = Sequential()
model.add(Melspectrogram(sr=SR, n_mels=64, power_melgram=2.0, return_decibel_melgram=True,
input_shape=input_shape))
model.add(Conv2D(30, (32, 4), padding='valid')) # (None, 16, 1, N)
model.add(BatchNormalization(axis=channel_axis))
model.add(Activation('relu'))
model.add(MaxPooling2D((1, 4), padding='same'))
model.add(Conv2D(30, (1, 4), padding='same'))
model.add(BatchNormalization(axis=channel_axis))
model.add(Activation('relu'))
model.add(MaxPooling2D((1, 4), padding='same'))
model.add(Conv2D(30, (1, 4), padding='same'))
model.add(BatchNormalization(axis=channel_axis))
model.add(Activation('relu'))
model.add(MaxPooling2D((1, 4), padding='same'))
model.add(GlobalAveragePooling2D())
model.add(Dense(n_out, activation=out_activation))
return model
if __name__ == "__main__":
model = model_multi_kernel_shape(8)
model.summary()