-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathtrain_model.py
More file actions
94 lines (84 loc) · 3.14 KB
/
train_model.py
File metadata and controls
94 lines (84 loc) · 3.14 KB
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
from common import GENRES
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Input, Dense, Lambda, Dropout, Activation, \
TimeDistributed, Convolution1D, MaxPooling1D, BatchNormalization
from sklearn.model_selection import train_test_split
import numpy as np
import pickle
from optparse import OptionParser
from sys import stderr, argv
import os
SEED = 42
N_LAYERS = 3
FILTER_LENGTH = 5
CONV_FILTER_COUNT = 256
BATCH_SIZE = 32
EPOCH_COUNT = 100
def train_model(data, model_path):
x = data['x']
y = data['y']
(x_train, x_val, y_train, y_val) = train_test_split(x, y, test_size=0.3,
random_state=SEED)
print('Building model...')
n_features = x_train.shape[2]
input_shape = (None, n_features)
model_input = Input(input_shape, name='input')
layer = model_input
for i in range(N_LAYERS):
# second convolutional layer names are used by extract_filters.py
layer = Convolution1D(
filters=CONV_FILTER_COUNT,
kernel_size=FILTER_LENGTH,
name='convolution_' + str(i + 1)
)(layer)
layer = BatchNormalization(momentum=0.9)(layer)
layer = Activation('relu')(layer)
layer = MaxPooling1D(2)(layer)
layer = Dropout(0.5)(layer)
layer = TimeDistributed(Dense(len(GENRES)))(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'
)
layer = time_distributed_merge_layer(layer)
layer = Activation('softmax', name='output_realtime')(layer)
model_output = layer
model = Model(model_input, model_output)
opt = Adam(lr=0.001)
model.compile(
loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy']
)
print('Training...')
model.fit(
x_train, y_train, batch_size=BATCH_SIZE, nb_epoch=EPOCH_COUNT,
validation_data=(x_val, y_val), verbose=1, callbacks=[
ModelCheckpoint(
model_path, save_best_only=True, monitor='val_acc', verbose=1
),
ReduceLROnPlateau(
monitor='val_acc', factor=0.5, patience=10, min_delta=0.01,
verbose=1
)
]
)
return model
if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-d', '--data_path', dest='data_path',
default=os.path.join(os.path.dirname(__file__),
'data/data.pkl'),
help='path to the data pickle', metavar='DATA_PATH')
parser.add_option('-m', '--model_path', dest='model_path',
default=os.path.join(os.path.dirname(__file__),
'models/model.h5'),
help='path to the output model HDF5 file', metavar='MODEL_PATH')
options, args = parser.parse_args()
with open(options.data_path, 'rb') as f:
data = pickle.load(f)
train_model(data, options.model_path)