-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathtrain_smic.py
323 lines (241 loc) · 11.7 KB
/
train_smic.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import numpy as np
import sys
import math
import operator
import csv
import glob,os
import xlrd
import cv2
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from collections import Counter
from sklearn.metrics import confusion_matrix
import scipy.io as sio
import pydot, graphviz
from keras.models import Sequential, Model
from keras.layers import LSTM, Dense, TimeDistributed
from keras.utils import np_utils, plot_model
from keras import metrics
from keras import backend as K
from keras.models import model_from_json
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.layers import Conv2D, MaxPooling2D
from keras.preprocessing.sequence import pad_sequences
from keras import optimizers
from keras.applications.vgg16 import VGG16 as keras_vgg16
import keras
from labelling import collectinglabel
from reordering import readinput
from evaluationmatrix import fpr
from utilities import Read_Input_Images, get_subfolders_num, data_loader_with_LOSO, label_matching, duplicate_channel
from utilities import record_scores, loading_smic_labels
from models import VGG_16
def train_smic(batch_size, spatial_epochs, temporal_epochs, train_id):
############################## Loading Labels & Images ##############################
# /media/ice/OS/Datasets/SMIC_TIM10/SMIC_TIM10
root_db_path = "/media/ice/OS/Datasets/"
dB = "SMIC_TIM10"
inputDir = root_db_path + dB + "/" + dB + "/"
workplace = root_db_path + dB + "/"
subject, filename, label, num_frames = loading_smic_labels(root_db_path, dB)
filename = filename.as_matrix()
label = label.as_matrix()
table = np.transpose( np.array( [filename, label] ) )
# os.remove(workplace + "Classification/SMIC_label.txt")
################# Variables #############################
spatial_size = 224
r = w = spatial_size
subjects = 16
samples = 164
n_exp = 3
IgnoredSamples_index = np.empty([0])
VidPerSubject = get_subfolders_num(inputDir, IgnoredSamples_index)
listOfIgnoredSamples = []
timesteps_TIM = 10
data_dim = r * w
pad_sequence = 10
#########################################################
############## Flags ####################
resizedFlag = 1
train_spatial_flag = 1
train_temporal_flag = 1
svm_flag = 0
finetuning_flag = 1
tensorboard_flag = 0
cam_visualizer_flag = 1
#########################################
############## Reading Images and Labels ################
# SubperdB = Read_SMIC_Images(inputDir, listOfIgnoredSamples, dB, resizedFlag, table, workplace, spatial_size)
SubperdB = Read_Input_Images(inputDir, listOfIgnoredSamples, dB, resizedFlag, table, workplace, spatial_size)
labelperSub = label_matching(workplace, dB, subjects, VidPerSubject)
######################################################################################
########### Model #######################
sgd = optimizers.SGD(lr=0.0001, decay=1e-7, momentum=0.9, nesterov=True)
adam = optimizers.Adam(lr=0.00001)
if train_spatial_flag == 0 and train_temporal_flag == 1:
data_dim = spatial_size * spatial_size
else:
data_dim = 4096
temporal_model = Sequential()
temporal_model.add(LSTM(2622, return_sequences=True, input_shape=(10, data_dim)))
temporal_model.add(LSTM(1000, return_sequences=False))
temporal_model.add(Dense(128, activation='relu'))
temporal_model.add(Dense(5, activation='sigmoid'))
temporal_model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=[metrics.categorical_accuracy])
#########################################
################# Pretrained Model ###################
vgg_model = VGG_16('VGG_Face_Deep_16.h5')
# keras_vgg = keras_vgg16(weights='imagenet')
# vgg_model = VGG_16('imagenet')
vgg_model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=[metrics.sparse_categorical_accuracy])
plot_model(vgg_model, to_file='model.png', show_shapes=True)
svm_classifier = SVC(kernel='linear', C=1)
######################################################
# model checkpoint
spatial_weights_name = 'vgg_spatial_17a_smic_'
temporal_weights_name = 'temporal_ID_16_smic_'
# model checkpoint
root = "/home/viprlab/Documents/Micro-Expression/" + spatial_weights_name + "weights.{epoch:02d}-{val_loss:.2f}.hdf5"
root_temporal = "/home/viprlab/Documents/Micro-Expression/" + temporal_weights_name + "weights.{epoch:02d}-{val_loss:.2f}.hdf5"
model_checkpoint = keras.callbacks.ModelCheckpoint(root, monitor='loss', save_best_only=True, save_weights_only=True)
model_checkpoint_temporal = keras.callbacks.ModelCheckpoint(root_temporal, monitor='loss', save_best_only=True, save_weights_only=True)
########### Training Process ############
# Todo:
# 1) LOSO (done)
# 2) call model (done)
# 3) saving model architecture
# 4) Saving Checkpoint
# 5) make prediction (done)
if tensorboard_flag == 1:
tensorboard_path = "/home/ice/Documents/Micro-Expression/tensorboard/"
tot_mat = np.zeros((n_exp,n_exp))
spatial_weights_name = 'vgg_spatial_ID_under_dev_smic.h5'
temporal_weights_name = 'temporal_ID_under_dev_smic.h5'
for sub in range(subjects):
vgg_model = VGG_16('VGG_Face_Deep_16.h5')
vgg_model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=[metrics.sparse_categorical_accuracy])
############ for tensorboard ###############
if tensorboard_flag == 1:
cat_path = tensorboard_path + str(sub) + "/"
os.mkdir(cat_path)
tbCallBack = keras.callbacks.TensorBoard(log_dir=cat_path, write_graph=True)
cat_path2 = tensorboard_path + str(sub) + "spat/"
os.mkdir(cat_path2)
tbCallBack2 = keras.callbacks.TensorBoard(log_dir=cat_path2, write_graph=True)
#############################################
image_label_mapping = np.empty([0])
Train_X, Train_Y, Test_X, Test_Y, Test_Y_gt = data_loader_with_LOSO(sub, SubperdB, labelperSub, subjects)
# Rearrange Training labels into a vector of images, breaking sequence
Train_X_spatial = Train_X.reshape(Train_X.shape[0]*10, r, w, 1)
Test_X_spatial = Test_X.reshape(Test_X.shape[0]* 10, r, w, 1)
# Extend Y labels 10 fold, so that all images have labels
Train_Y_spatial = np.repeat(Train_Y, 10, axis=0)
Test_Y_spatial = np.repeat(Test_Y, 10, axis=0)
# Duplicate channel of input image
Train_X_spatial = duplicate_channel(Train_X_spatial)
Test_X_spatial = duplicate_channel(Test_X_spatial)
# print ("Train_X_shape: " + str(np.shape(Train_X_spatial)))
# print ("Train_Y_shape: " + str(np.shape(Train_Y_spatial)))
# print ("Test_X_shape: " + str(np.shape(Test_X_spatial)))
# print ("Test_Y_shape: " + str(np.shape(Test_Y_spatial)))
# print(Train_X_spatial)
##################### Training & Testing #########################
X = Train_X_spatial.reshape(Train_X_spatial.shape[0], 3, r, w)
y = Train_Y_spatial.reshape(Train_Y_spatial.shape[0], 5)
test_X = Test_X_spatial.reshape(Test_X_spatial.shape[0], 3, r, w)
test_y = Test_Y_spatial.reshape(Test_Y_spatial.shape[0], 5)
###### conv weights must be freezed for transfer learning ######
if finetuning_flag == 1:
for layer in vgg_model.layers[:33]:
layer.trainable = False
if train_spatial_flag == 1 and train_temporal_flag == 1:
# trains encoder until fc, train temporal
# Spatial Training
if tensorboard_flag == 1:
vgg_model.fit(X, y, batch_size=batch_size, epochs=spatial_epochs, shuffle=True, callbacks=[tbCallBack2])
else:
vgg_model.fit(X, y, batch_size=batch_size, epochs=spatial_epochs, shuffle=True, callbacks=[model_checkpoint])
# vgg_model.save_weights(spatial_weights_name)
model = Model(inputs=vgg_model.input, outputs=vgg_model.layers[35].output)
plot_model(model, to_file="spatial_module_FULL_TRAINING.png", show_shapes=True)
# Spatial Encoding
output = model.predict(X, batch_size = batch_size)
features = output.reshape(int(output.shape[0]/10), 10, output.shape[1])
# Temporal Training
if tensorboard_flag == 1:
temporal_model.fit(features, Train_Y, batch_size=batch_size, epochs=temporal_epochs, callbacks=[tbCallBack])
else:
temporal_model.fit(features, Train_Y, batch_size=batch_size, epochs=temporal_epochs, callbacks=[model_checkpoint_temporal])
# temporal_model.save_weights(temporal_weights_name)
# Testing
output = model.predict(test_X, batch_size = batch_size)
features = output.reshape(int(output.shape[0]/10), 10, output.shape[1])
predict = temporal_model.predict_classes(features, batch_size=batch_size)
elif train_spatial_flag == 1 and train_temporal_flag == 0 and cam_visualizer_flag == 0:
# trains spatial module ONLY, no escape
# Spatial Training
if tensorboard_flag == 1:
vgg_model.fit(X, y, batch_size=batch_size, epochs=spatial_epochs, shuffle=True, callbacks=[tbCallBack2])
else:
vgg_model.fit(X, y, batch_size=batch_size, epochs=spatial_epochs, shuffle=True, callbacks=[model_checkpoint])
# vgg_model.save_weights(spatial_weights_name)
plot_model(vgg_model, to_file="spatial_module_ONLY.png", show_shapes=True)
# Testing
predict = vgg_model.predict(test_X, batch_size = batch_size)
Test_Y_gt = np.repeat(Test_Y_gt, 10, axis=0)
elif train_spatial_flag == 0 and train_temporal_flag == 1:
# trains temporal module ONLY.
# Temporal Training
if tensorboard_flag == 1:
temporal_model.fit(Train_X, Train_Y, batch_size=batch_size, epochs=spatial_epochs, callbacks=[tbCallBack])
else:
temporal_model.fit(Train_X, Train_Y, batch_size=batch_size, epochs=spatial_epochs, callbacks=[model_checkpoint_temporal])
# temporal_model.save_weights(temporal_weights_name)
# Testing
predict = temporal_model.predict_classes(Test_X, batch_size = batch_size)
elif svm_flag == 1 and finetuning_flag == 0:
# no finetuning
X = vgg_model.predict(X, batch_size=batch_size)
y_for_svm = np.argmax(y, axis=1)
svm_classifier.fit(X, y_for_svm)
test_X = vgg_model.predict(test_X, batch_size=batch_size)
predict = svm_classifier.predict(test_X)
Test_Y_gt = np.repeat(Test_Y_gt, 10, axis=0)
elif train_spatial_flag == 1 and train_temporal_flag == 0 and cam_visualizer_flag == 1:
# trains spatial module & CAM ONLY
# Spatial Training
if tensorboard_flag == 1:
vgg_model.fit(X, y, batch_size=batch_size, epochs=spatial_epochs, shuffle=True, callbacks=[tbCallBack2])
else:
vgg_model.fit(X, y, batch_size=batch_size, epochs=spatial_epochs, shuffle=True, callbacks=[model_checkpoint])
# vgg_model.save_weights(spatial_weights_name)
plot_model(vgg_model, to_file="spatial_module_CAM_ONLY.png", show_shapes=True)
# Testing
predict = vgg_model.predict(test_X, batch_size = batch_size)
Test_Y_gt = np.repeat(Test_Y_gt, 10, axis=0)
##############################################################
#################### Confusion Matrix Construction #############
print (predict)
print (Test_Y_gt)
ct = confusion_matrix(Test_Y_gt,predict)
# check the order of the CT
order = np.unique(np.concatenate((predict,Test_Y_gt)))
# create an array to hold the CT for each CV
mat = np.zeros((n_exp,n_exp))
# put the order accordingly, in order to form the overall ConfusionMat
for m in range(len(order)):
for n in range(len(order)):
mat[int(order[m]),int(order[n])]=ct[m,n]
tot_mat = mat + tot_mat
################################################################
#################### cumulative f1 plotting ######################
microAcc = np.trace(tot_mat) / np.sum(tot_mat)
[f1,precision,recall] = fpr(tot_mat,n_exp)
file = open(workplace+'Classification/'+ 'Result/' + '/f1.txt', 'a')
file.write(str(f1) + "\n")
file.close()
##################################################################
################# write each CT of each CV into .txt file #####################
record_scores(workplace, dB, ct, sub, order, tot_mat, n_exp, subjects)
###############################################################################