forked from xuanjihe/speech-emotion-recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
289 lines (262 loc) · 13.8 KB
/
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 18 16:34:21 2017
@author: hxj
"""
from attention import attention
import cPickle
import tensorflow as tf
import math
import numpy as np
import os
from tensorflow.contrib.layers import batch_norm
from tensorflow.contrib.framework import arg_scope
epsilon = 1e-3
def Batch_Normalization(x, training, scope):
with arg_scope([batch_norm],
scope=scope,
updates_collections=None,
decay=0.9,
center=True,
scale=True,
zero_debias_moving_mean=True) :
return tf.cond(training,
lambda : batch_norm(inputs=x, is_training=training, reuse=None),
lambda : batch_norm(inputs=x, is_training=training, reuse=True))
def leaky_relu(x, leakiness=0.0):
return tf.where(tf.less(x, 0.0), leakiness * x, x, name='leaky_relu')
def load_data():
f = open('./CASIA_40_delta.pkl','rb')
train_data,train_label,test_data,test_label,valid_data,valid_label = cPickle.load(f)
return train_data,train_label,test_data,test_label,valid_data,valid_label
def batch_norm_wrapper(inputs, is_training, decay = 0.999):
scale = tf.Variable(tf.ones([inputs.get_shape()[-1]]))
beta = tf.Variable(tf.zeros([inputs.get_shape()[-1]]))
pop_mean = tf.Variable(tf.zeros([inputs.get_shape()[-1]]), trainable=False)
pop_var = tf.Variable(tf.ones([inputs.get_shape()[-1]]), trainable=False)
if is_training is not None:
batch_mean, batch_var = tf.nn.moments(inputs,[0])
train_mean = tf.assign(pop_mean,
pop_mean * decay + batch_mean * (1 - decay))
train_var = tf.assign(pop_var,
pop_var * decay + batch_var * (1 - decay))
with tf.control_dependencies([train_mean, train_var]):
return tf.nn.batch_normalization(inputs,
batch_mean, batch_var, beta, scale, epsilon)
else:
return tf.nn.batch_normalization(inputs,
pop_mean, pop_var, beta, scale, epsilon)
def batchnorm(Ylogits, is_test, iteration, offset, convolutional=False):
exp_moving_avg = tf.train.ExponentialMovingAverage(0.999, iteration) # adding the iteration prevents from averaging across non-existing iterations
bnepsilon = 1e-5
if convolutional:
mean, variance = tf.nn.moments(Ylogits, [0, 1, 2])
else:
mean, variance = tf.nn.moments(Ylogits, [0])
update_moving_averages = exp_moving_avg.apply([mean, variance])
m = tf.cond(is_test, lambda: exp_moving_avg.average(mean), lambda: mean)
v = tf.cond(is_test, lambda: exp_moving_avg.average(variance), lambda: variance)
Ybn = tf.nn.batch_normalization(Ylogits, m, v, offset, None, bnepsilon)
return Ybn, update_moving_averages
def dense_to_one_hot(labels_dense, num_classes):
"""Convert class labels from scalars to one-hot vectors."""
num_labels = labels_dense.shape[0]
index_offset = np.arange(num_labels) * num_classes
labels_one_hot = np.zeros((num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
return labels_one_hot
def build_model(inputX, is_training,keep_prob):
# 3 2-D convolution layers
L1 = 256
L2 = 512
L3 = 512
Li1 = 768
F1 = 64
F2 = 6
p = 5
cell_units1 = 128
timesteps = 200
ATTENTION_SIZE = 1
layer1_filter = tf.get_variable('layer1_filter', shape=[5, 3, 3, L1], dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1))
layer1_bias = tf.get_variable('layer1_bias', shape=[L1], dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
layer1_stride = [1, 1, 1, 1]
layer2_filter = tf.get_variable('layer2_filter', shape=[5, 3, L1, L2], dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1))
layer2_bias = tf.get_variable('layer2_bias', shape=[L2], dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
layer2_stride = [1, 1, 1, 1]
layer3_filter = tf.get_variable('layer3_filter', shape=[5, 3, L2, L3], dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1))
layer3_bias = tf.get_variable('layer3_bias', shape=[L3], dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
layer3_stride = [1, 1, 1, 1]
linear1_weight = tf.get_variable('linear1_weight', shape=[p*L2,Li1], dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1))
linear1_bias = tf.get_variable('linear1_bias', shape=[Li1], dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
fully1_weight = tf.get_variable('fully1_weight', shape=[2*cell_units1,F1], dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1))
fully1_bias = tf.get_variable('fully1_bias', shape=[F1], dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
fully2_weight = tf.get_variable('fully2_weight', shape=[F1,F2], dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1))
fully2_bias = tf.get_variable('fully2_bias', shape=[F2], dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
layer1 = tf.nn.conv2d(inputX, layer1_filter, layer1_stride, padding='SAME')
layer1 = tf.nn.bias_add(layer1,layer1_bias)
#layer1 = tf.layers.batch_normalization(layer1, training=is_training)
#layer1 = Batch_Normalization(layer1, training=is_training, scope='layer1_batch')
layer1 = leaky_relu(layer1, 0.01)
#layer1 = Batch_Normalization(layer1, training=is_training, scope='layer1_batch')
#print layer1.get_shape()
layer1 = tf.nn.max_pool(layer1,ksize=[1, 1, 4, 1], strides=[1, 1, 4, 1], padding='VALID', name='max_pool')
#print layer1.get_shape()
layer1 = tf.contrib.layers.dropout(layer1, keep_prob=keep_prob, is_training=is_training)
#layer1 = tf.reshape(layer1,[-1,timesteps,L1*p])
layer2 = tf.nn.conv2d(layer1, layer2_filter, layer2_stride, padding='SAME')
layer2 = tf.nn.bias_add(layer2,layer2_bias)
#layer1 = tf.layers.batch_normalization(layer1, training=is_training)
layer2 = leaky_relu(layer2, 0.01)
#print layer2.get_shape()
#layer2 = Batch_Normalization(layer2, training=is_training, scope='layer1_batch')
layer2 = tf.nn.max_pool(layer2,ksize=[1, 1, 2, 1], strides=[1, 1, 2, 1], padding='VALID', name='max_pool')
#print layer2.get_shape()
layer2 = tf.contrib.layers.dropout(layer2, keep_prob=keep_prob, is_training=is_training)
layer2 = tf.reshape(layer2,[-1,timesteps,L2*p])
layer2 = tf.reshape(layer2, [-1,p*L2])
#layer1 = tf.reshape(layer1,[-1,p*L1])
linear1 = tf.matmul(layer2,linear1_weight) + linear1_bias
linear1 = batch_norm_wrapper(linear1,is_training)
linear1 = leaky_relu(linear1, 0.01)
#linear1 = batch_norm_wrapper(linear1,is_training)
linear1 = tf.reshape(linear1, [-1, timesteps, Li1])
'''
#adding gru cell
gru_bw_cell1 = tf.nn.rnn_cell.GRUCell(cell_units)
#if is_training is not None:
# gru_bw_cell1 = tf.contrib.rnn.DropoutWrapper(cell=gru_bw_cell1, output_keep_prob=keep_prob)
# Forward direction cell: (if else required for TF 1.0 and 1.1 compat)
gru_fw_cell1 = tf.nn.rnn_cell.GRUCell(cell_units)
#if is_training is not None:
# gru_fw_cell1 = tf.contrib.rnn.DropoutWrapper(cell=gru_fw_cell1, output_keep_prob=keep_prob)
'''
# Define lstm cells with tensorflow
# Forward direction cell
gru_fw_cell1 = tf.contrib.rnn.BasicLSTMCell(cell_units1, forget_bias=1.0)
# Backward direction cell
gru_bw_cell1 = tf.contrib.rnn.BasicLSTMCell(cell_units1, forget_bias=1.0)
'''
# Define lstm cells with tensorflow
# Forward direction cell
gru_fw_cell1 = tf.contrib.rnn.BasicLSTMCell(cell_units, forget_bias=1.0)
if is_training is not None:
gru_fw_cell1 = tf.contrib.rnn.DropoutWrapper(cell=gru_fw_cell1, output_keep_prob=keep_prob)
# Backward direction cell
gru_bw_cell1 = tf.contrib.rnn.BasicLSTMCell(cell_units, forget_bias=1.0)
if is_training is not None:
gru_bw_cell1 = tf.contrib.rnn.DropoutWrapper(cell=gru_bw_cell1, output_keep_prob=keep_prob)
'''
# Now we feed `layer_3` into the LSTM BRNN cell and obtain the LSTM BRNN output.
outputs1, output_states1 = tf.nn.bidirectional_dynamic_rnn(cell_fw=gru_fw_cell1,
cell_bw=gru_bw_cell1,
inputs= linear1,
dtype=tf.float32,
time_major=False,
scope='LSTM1')
'''
outputs1 = tf.concat(outputs1,2)
# Forward direction cell
gru_fw_cell2 = tf.contrib.rnn.BasicLSTMCell(cell_units2, forget_bias=1.0)
# Backward direction cell
gru_bw_cell2 = tf.contrib.rnn.BasicLSTMCell(cell_units2, forget_bias=1.0)
# Now we feed `layer_3` into the LSTM BRNN cell and obtain the LSTM BRNN output.
outputs, output_states2 = tf.nn.bidirectional_dynamic_rnn(cell_fw=gru_fw_cell2,
cell_bw=gru_bw_cell2,
inputs= outputs1,
dtype=tf.float32,
time_major=False,
scope='LSTM2')
'''
#time_major=false,tensor的shape为[batch_size, max_time, depth]。实验中使用tf.concat(outputs, 2)将其拼接
outputs = tf.concat(outputs1,2)
outputs = tf.reshape(outputs, [-1, timesteps,2*cell_units1, 1])
gru = tf.nn.max_pool(outputs,ksize=[1,timesteps,1,1], strides=[1,timesteps,1,1], padding='VALID', name='max_pool')
gru = tf.reshape(gru, [-1,2*cell_units1])
'''
# Attention layer
gru, alphas = attention(outputs1, ATTENTION_SIZE, return_alphas=True)
'''
fully1 = tf.matmul(gru,fully1_weight) + fully1_bias
#fully1 = batch_norm_wrapper(fully1,is_training)
fully1 = leaky_relu(fully1, 0.01)
#fully1 = batch_norm_wrapper(fully1,is_training)
fully1 = tf.nn.dropout(fully1, keep_prob)
Ylogits = tf.matmul(fully1, fully2_weight) + fully2_bias
Ylogits = tf.nn.softmax(Ylogits)
'''
fully2 = tf.matmul(fully1,fully2_weight) + fully2_bias
fully2 = leaky_relu(fully2, 0.01)
#fully2 = batch_norm_wrapper(fully2,is_training)
Ylogits = tf.matmul(fully2, fully3_weight) + fully3_bias
Ylogits = tf.nn.softmax(Ylogits)
'''
return Ylogits
def train_op(norm):
STEPS = 50000
batch_size = 60
grad_clip = 5
MODEL_SAVE_PATH = "./model2/"
MODEL_NAME = "model.ckpt"
X = tf.placeholder(tf.float32, shape=[None, 200,40,3])
Y = tf.placeholder(tf.int32, shape=[None, 6])
is_training = tf.placeholder(tf.bool)
# variable learning rate
lr = tf.placeholder(tf.float32)
keep_prob = tf.placeholder(tf.float32)
Ylogits = build_model(X, is_training, keep_prob)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels = Y, logits = Ylogits)
cost = tf.reduce_mean(cross_entropy)
#train_op = tf.train.AdamOptimizer(lr).minimize(cost)
var_trainable_op = tf.trainable_variables()
if norm == -1:
# not apply gradient clipping
train_op = tf.train.AdamOptimizer(lr).minimize(cost)
else:
# apply gradient clipping
grads, _ = tf.clip_by_global_norm(tf.gradients(cost, var_trainable_op), grad_clip)
opti = tf.train.AdamOptimizer(lr)
train_op = opti.apply_gradients(zip(grads, var_trainable_op))
correct_pred = tf.equal(tf.argmax(Ylogits, 1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
saver=tf.train.Saver(tf.global_variables())
train_data,train_label,test_data,test_label,valid_data,valid_label = load_data()
train_label = dense_to_one_hot(train_label,6)
test_label = dense_to_one_hot(test_label,6)
valid_label = dense_to_one_hot(valid_label,6)
max_learning_rate = 0.0001
min_learning_rate = 0.000001
decay_speed = 1600
dataset_size = train_data.shape[0]
# init
init = tf.global_variables_initializer()
best_acc = 0
with tf.Session() as sess:
sess.run(init)
for i in range(STEPS):
learning_rate = min_learning_rate + (max_learning_rate - min_learning_rate) * math.exp(-i/decay_speed)
start = (i * batch_size) % dataset_size
end = min(start+batch_size, dataset_size)
if i % 5 == 0:
loss, train_acc = sess.run([cost,accuracy],feed_dict = {X:valid_data, Y:valid_label,is_training:False, keep_prob:1})
test_acc = sess.run(accuracy, feed_dict = {X:test_data, Y:test_label, is_training:False, keep_prob:1})
if test_acc > best_acc:
best_acc = test_acc
print "After %5d trainging step(s), validation cross entropy is %2.2g, validation accuracy is %3.2g, test accuracy is %3.2g, the best accuracy is %3.2g" %(i, loss, train_acc, test_acc, best_acc)
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME),global_step = i)
sess.run(train_op, feed_dict={X:train_data[start:end,:,:,:], Y:train_label[start:end,:],
is_training:True, keep_prob:1, lr:learning_rate})
if __name__=='__main__':
train_op(1)