-
Notifications
You must be signed in to change notification settings - Fork 7
/
build_weighted_model.py
56 lines (48 loc) · 1.68 KB
/
build_weighted_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
from common import GENRES
from keras.models import Model
from keras.optimizers import RMSprop
from keras import backend as K
from keras.layers import Input, Dense, Lambda, Dropout, Activation, LSTM, \
TimeDistributed, Conv1D, MaxPooling1D
SEED = 42
N_LAYERS = 3
FILTER_LENGTH = 5
CONV_FILTER_COUNT = 256
LSTM_COUNT = 256
BATCH_SIZE = 32
EPOCH_COUNT = 80
def build_weighted_model(weights_filepath):
print('Building model...')
n_features = 128
input_shape = (None, n_features)
model_input = Input(input_shape, name='input')
model = crnn(model_input)
model.load_weights(weights_filepath)
return model
def crnn(model_input):
layer = model_input
for i in range(N_LAYERS):
# Convolutional layer names are used by extract_filters.py
layer = Conv1D(
nb_filter=CONV_FILTER_COUNT,
filter_length=FILTER_LENGTH,
name='convolution_' + str(i + 1))(layer)
layer = Activation('relu')(layer)
layer = MaxPooling1D(2)(layer)
layer = Dropout(0.5)(layer)
layer = LSTM(LSTM_COUNT, return_sequences=True)(layer)
layer = Dropout(0.5)(layer)
layer = TimeDistributed(Dense(len(GENRES)))(layer)
layer = Activation('softmax', name='output_realtime')(layer)
time_distributed_merge_layer = Lambda(
function=lambda x: K.mean(x, axis=1),
output_shape=lambda shape: (shape[0],) + shape[2:],
name='output_merged')
model_output = time_distributed_merge_layer(layer)
model = Model(model_input, model_output)
opt = RMSprop(lr=0.00001) # Optimizer
model.compile(
loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
return model