-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathconv_1d_model_aws.py
More file actions
143 lines (108 loc) · 4.1 KB
/
conv_1d_model_aws.py
File metadata and controls
143 lines (108 loc) · 4.1 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
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
from __future__ import print_function
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
np.random.seed(1337) # for reproducibility
from keras.preprocessing import sequence
from keras.layers.noise import GaussianNoise
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Convolution1D, MaxPooling1D, AveragePooling1D
from keras.utils import np_utils
# set parameters:
test_dim = 999
maxlen = 100
batch_size = 50
nb_filter = 512
filter_length_1 = 100
filter_length_2 = 30
filter_length_3 = 15
hidden_dims = 10
nb_epoch = 5
nb_classes = 3
print('Loading data...')
X = np.load('top_3_100_split_mfcc.npy')
y = np.load('top_3_100_split_y.npy')
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15)
# in case the passed in data is 2d and not 3d
'''
xts = X_train.shape
X_train = np.reshape(X_train, (xts[0], xts[1], 1))
xtss = X_test.shape
X_test = np.reshape(X_test, (xtss[0], xtss[1], 1))
yts = y_train.shape
y_train = np.reshape(y_train, (yts[0], 1))
ytss = y_test.shape
y_test = np.reshape(y_test, (ytss[0], 1))
'''
print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
print('Build model...')
model = Sequential()
# we add a Convolution1D, which will learn nb_filter mfcc groups:
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length_1,
input_shape=(test_dim, 13),
init = 'glorot_normal',
border_mode='valid',
activation='relu'
))
# batch normalization to keep weights in the 0 to 1 range
model.add(BatchNormalization())
# add more layers
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length_2,
border_mode='valid',
activation='relu'
))
model.add(BatchNormalization())
# we use standard max pooling (halving the output of the previous layer)
model.add(MaxPooling1D(pool_length=2))
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length_2,
border_mode='valid',
activation='relu'
))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_length=2))
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length_2,
border_mode='valid',
activation='relu'
))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_length=2))
# Dropout reduces overfitting
model.add(Dropout(.1))
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length_2,
border_mode='valid',
activation='relu'
))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_length=2))
model.add(Dropout(.1))
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length_3,
border_mode='valid',
activation='relu'
))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_length=2))
# We flatten the output of the conv layer,
# so that we can add a vanilla dense layer:
model.add(Flatten())
# We project onto a single unit output layer, and squash it with a softmax into 0-1 probability space:
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics = ["accuracy"])
model.fit(X_train, Y_train, batch_size=batch_size,
nb_epoch=nb_epoch, verbose=1,
validation_data=(X_test, Y_test))
# print report of recall, precision, f1 score
y_pred = model.predict_classes(X_test)
print(classification_report(y_test, y_pred))