-
Notifications
You must be signed in to change notification settings - Fork 5
/
cnn1D_training.py
executable file
·281 lines (257 loc) · 11.7 KB
/
cnn1D_training.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: winston lin
"""
import os
import numpy as np
from keras import optimizers
from keras.models import Model
from keras.layers.core import Dense, Activation, Flatten
from keras.layers.normalization import BatchNormalization
from keras.layers import Input, Multiply, Concatenate, Conv1D, Permute
import random
import matplotlib.pyplot as plt
from dataloader import DataGenerator_LLD
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
import keras
from model_utils import crop, reshape, mean, repeat
from model_utils import atten_gated, atten_rnn, atten_selfMH, output_net
from transformer import ScaledDotProductAttention, LayerNormalization
from utils import cc_coef
import time
import argparse
# Ignore warnings & Fix random seed
import warnings
warnings.filterwarnings("ignore")
random.seed(999)
random_seed=99
# for resolving cuDNN failed to allocate CNN algorithm problem
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
class TimeHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.times = []
def on_epoch_begin(self, batch, logs={}):
self.epoch_time_start = time.time()
def on_epoch_end(self, batch, logs={}):
self.times.append(time.time() - self.epoch_time_start)
###############################################################################
# Attention on LSTM chunk output => Weighted Mean of the gated-Attention model
def UttrAtten_GatedVec(atten):
time_step = 62 # same as the number of frames within a chunk (i.e., m)
feat_num = 130 # number of LLDs features
chunk_num = 11 # number of chunks splitted for a sentence (i.e., C)
# Input Layer
inputs = Input((time_step, feat_num))
cnn_inputs = Permute((2, 1))(inputs)
# cnn1: [128, 128]
encode = Conv1D(filters=128, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(cnn_inputs)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
encode = Conv1D(filters=128, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
# cnn2: [64, 64]
encode = Conv1D(filters=64, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
encode = Conv1D(filters=64, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
# cnn3: [32]
encode = Conv1D(filters=32, kernel_size=3, strides=2, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
# cnn flatten output
encode = Flatten()(encode)
encode = Dense(units=feat_num, activation='relu')(encode)
# Uttr Attention Layer
batch_atten_out = []
for uttr_idx in range(0, batch_size*chunk_num, chunk_num):
_start = uttr_idx
_end = uttr_idx+chunk_num
encode_crop = crop(0, _start, _end)(encode)
encode_crop = reshape()(encode_crop)
atten_weights = atten(encode_crop)
atten_out = Multiply()([encode_crop, atten_weights])
atten_out = mean()(atten_out)
batch_atten_out.append(atten_out)
# Output-Layer
concat_atten_out= Concatenate(axis=0)(batch_atten_out)
outputs = output_net(feat_num)(concat_atten_out)
outputs = repeat()(outputs) # for matching the input batch size
model = Model(inputs=inputs, outputs=outputs)
return model
# Attention on LSTM chunk output => RNN-Attention/MultiHead(MH)-Self Attention
def UttrAtten_AttenVec(atten):
time_step = 62 # same as the number of frames within a chunk (i.e., m)
feat_num = 130 # number of LLDs features
chunk_num = 11 # number of chunks splitted for a sentence (i.e., C)
# Input Layer
inputs = Input((time_step, feat_num))
cnn_inputs = Permute((2, 1))(inputs)
# cnn1: [128, 128]
encode = Conv1D(filters=128, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(cnn_inputs)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
encode = Conv1D(filters=128, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
# cnn2: [64, 64]
encode = Conv1D(filters=64, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
encode = Conv1D(filters=64, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
# cnn3: [32]
encode = Conv1D(filters=32, kernel_size=3, strides=2, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
# cnn flatten output
encode = Flatten()(encode)
encode = Dense(units=feat_num, activation='relu')(encode)
# Uttr Attention Layer
batch_atten_out = []
for uttr_idx in range(0, batch_size*chunk_num, chunk_num):
_start = uttr_idx
_end = uttr_idx+chunk_num
encode_crop = crop(0, _start, _end)(encode)
encode_crop = reshape()(encode_crop)
atten_out = atten(encode_crop)
batch_atten_out.append(atten_out)
# Output-Layer
concat_atten_out= Concatenate(axis=0)(batch_atten_out)
outputs = output_net(feat_num)(concat_atten_out)
outputs = repeat()(outputs) # for matching the input batch size
model = Model(inputs=inputs, outputs=outputs)
return model
# Attention on LSTM chunk output => directly average without Attention
def UttrAtten_NonAtten():
time_step = 62 # same as the number of frames within a chunk (i.e., m)
feat_num = 130 # number of LLDs features
chunk_num = 11 # number of chunks splitted for a sentence (i.e., C)
# Input Layer
inputs = Input((time_step, feat_num))
cnn_inputs = Permute((2, 1))(inputs)
# cnn1: [128, 128]
encode = Conv1D(filters=128, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(cnn_inputs)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
encode = Conv1D(filters=128, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
# cnn2: [64, 64]
encode = Conv1D(filters=64, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
encode = Conv1D(filters=64, kernel_size=3, strides=1, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
# cnn3: [32]
encode = Conv1D(filters=32, kernel_size=3, strides=2, dilation_rate=1, data_format='channels_first')(encode)
encode = BatchNormalization()(encode)
encode = Activation('relu')(encode)
# cnn flatten output
encode = Flatten()(encode)
encode = Dense(units=feat_num, activation='relu')(encode)
# Uttr Attention Layer
batch_out = []
for uttr_idx in range(0, batch_size*chunk_num, chunk_num):
_start = uttr_idx
_end = uttr_idx+chunk_num
encode_crop = crop(0, _start, _end)(encode)
encode_crop = reshape()(encode_crop)
encode_out = mean()(encode_crop)
batch_out.append(encode_out)
# Output-Layer
concat_out= Concatenate(axis=0)(batch_out)
outputs = output_net(feat_num)(concat_out)
outputs = repeat()(outputs) # for matching the input batch size
model = Model(inputs=inputs, outputs=outputs)
return model
###############################################################################
argparse = argparse.ArgumentParser()
argparse.add_argument("-ep", "--epoch", required=True)
argparse.add_argument("-batch", "--batch_size", required=True)
argparse.add_argument("-emo", "--emo_attr", required=True)
argparse.add_argument("-atten", "--atten_type", required=True)
args = vars(argparse.parse_args())
# Parameters
batch_size = int(args['batch_size'])
epochs = int(args['epoch'])
emo_attr = args['emo_attr']
atten_type = args['atten_type']
# Paths Setting
root_dir = '/media/winston/UTD-MSP/Speech_Datasets/MSP-PODCAST-Publish-1.6/Features/OpenSmile_lld_IS13ComParE/feat_mat/'
label_dir = '/media/winston/UTD-MSP/Speech_Datasets/MSP-PODCAST-Publish-1.6/Labels/labels_concensus.csv'
params_train = {'batch_size': batch_size,
'split_set': 'Train',
'emo_attr': emo_attr,
'shuffle': True}
params_valid = {'batch_size': batch_size,
'split_set': 'Validation',
'emo_attr': emo_attr,
'shuffle': False}
# Generators
training_generator = DataGenerator_LLD(root_dir, label_dir, **params_train)
validation_generator = DataGenerator_LLD(root_dir, label_dir, **params_valid)
# Optimizer
adam = optimizers.Adam(lr=0.0001)
# Model Saving Settings
if os.path.exists('./Models'):
pass
else:
os.mkdir('./Models/')
filepath='./Models/CNN_model[epoch'+str(epochs)+'-batch'+str(batch_size)+']_'+atten_type+'_'+emo_attr+'.hdf5'
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
time_callback = TimeHistory()
callbacks_list = [checkpoint, time_callback]
# Model Architecture
if atten_type == 'GatedVec':
model = UttrAtten_GatedVec(atten_gated(feat_num=130, C=11))
elif atten_type == 'RnnAttenVec':
model = UttrAtten_AttenVec(atten_rnn(feat_num=130, C=11))
elif atten_type == 'SelfAttenVec':
model = UttrAtten_AttenVec(atten_selfMH(feat_num=130, C=11))
elif atten_type == 'NonAtten':
model = UttrAtten_NonAtten()
#print(model.summary())
# Model Compile Settings
model.compile(optimizer=adam, loss=cc_coef)
model.fit_generator(generator=training_generator,
validation_data=validation_generator,
use_multiprocessing=False,
workers=12,
epochs=epochs,
verbose=1,
callbacks=callbacks_list)
# Show training & validation loss
v_loss = model.history.history['val_loss']
t_loss = model.history.history['loss']
plt.plot(t_loss,'b')
plt.plot(v_loss,'r')
plt.savefig('./Models/CNN_model[epoch'+str(epochs)+'-batch'+str(batch_size)+']_'+atten_type+'_'+emo_attr+'.png')
# Record training time cost per epoch
print('Epochs: '+str(epochs)+', ')
print('Batch_size: '+str(batch_size)+', ')
print('Emotion: '+emo_attr+', ')
print('Chunk_type: dynamicOverlap, ')
print('Model_type: CNN, ')
print('Atten_type: '+atten_type+', ')
print('Avg. Training Time(s/epoch): '+str(np.mean(time_callback.times))+', ')
print('Std. Training Time(s/epoch): '+str(np.std(time_callback.times)))
####### Saving Model Weights/Bias seperately due to different info-flow in the testing stage
model = None # clean gpu-memory
if atten_type=='SelfAttenVec':
best_model = load_model(filepath, custom_objects={'cc_coef':cc_coef,
'ScaledDotProductAttention':ScaledDotProductAttention,
'LayerNormalization':LayerNormalization})
else:
best_model = load_model(filepath, custom_objects={'cc_coef':cc_coef})
# Saving trained model weights only
best_model.save_weights('./Models/CNN_model[epoch'+str(epochs)+'-batch'+str(batch_size)+']_'+atten_type+'_'+emo_attr+'_weights.h5')