-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathlbptop.py
327 lines (256 loc) · 9.22 KB
/
lbptop.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
324
325
326
import sklearn
import os
import pandas as pd
import numpy as np
from sklearn import svm
from sklearn.metrics import confusion_matrix
from sklearn.externals import joblib
from sklearn.model_selection import GridSearchCV
import pickle
from keras.utils import np_utils
from evaluationmatrix import fpr, weighted_average_recall, unweighted_average_recall
def data_loader_LOSO(data, y_labels, subject, list_subjects):
# lbp dim
lbp_dim = 177
class_dim = 5
# loading data
test_X = np.empty([0])
X = np.empty([0])
test_y = np.empty([0])
y = np.empty([0])
#### Load test set ####
test_X = np.array(data[subject])
test_y = y_labels[subject]
# print(len(data))
# test_y = np_utils.to_categorical(y_labels[subject], 5)
#######################
# print(len(data))
########### Leave-One-Subject-Out ###############
if subject==0:
for i in range(1,list_subjects):
X = np.append(X, data[i])
y = np.append(y, y_labels[i])
elif subject==list_subjects-1:
for i in range(list_subjects-1):
X = np.append(X, data[i])
y = np.append(y, y_labels[i])
else:
for i in range(list_subjects):
if subject == i:
continue
else:
X = np.append(X, data[i])
y = np.append(y, y_labels[i])
##################################################
############ Conversion to numpy and stacking ###############
X = X.reshape(int(len(X)/lbp_dim), lbp_dim)
test_X = test_X.reshape(int(len(test_X)/lbp_dim), lbp_dim)
return X, y, test_X, test_y
def standard_data_loader(SubjectPerDatabase, y_labels, subjects, classes):
lbp_dim = 177
Train_X = np.empty([0])
Train_Y = np.empty([0])
Test_Y_gt = np.empty([0])
for subject in range((subjects)):
Train_X = np.append(Train_X, data[subject])
Train_Y = np.append(Train_Y, y_labels[subject])
Test_Y_gt = np.append(Test_Y_gt, y_labels[subject])
Train_X = Train_X.reshape(int(len(Train_X)/lbp_dim), lbp_dim)
return Train_X, Train_Y, Test_Y_gt
def load_labels_and_LBP_values(file_casme, file_samm, lbp_path, flag):
# get files that should be ignored due to objective class 6, 7
file_casme = file_casme
file_samm = file_samm
lbp_path = lbp_path
table_casme = pd.read_excel(file_casme, converters={'Subject': lambda x: str(x)})
table_samm = pd.read_excel(file_samm, converters={'Subject': lambda x: str(x)})
# remove class 6, 7
table_casme = table_casme.ix[table_casme['Objective Class'] < 6]
table_samm = table_samm.ix[table_samm['Objective Classes'] < 6]
table_casme = table_casme[['Subject', 'Filename', 'Objective Class']]
table_casme['Subject'] = 'sub' + table_casme['Subject'].astype(str)
table_samm = table_samm[['Subject', 'Filename', 'Objective Classes']]
table_casme = table_casme.as_matrix()
table_samm = table_samm.as_matrix()
table_casme[:, 2] -= 1
table_samm[:, 2] -= 1
table = np.concatenate((table_casme, table_samm)) #filtered table
if flag == 'samm_only':
table = table_samm
elif flag == 'casme_only':
table = table_casme
else:
table = table
# table = table_casme
# table = table_samm
# print(table)
# print(table.shape)
reference_table = np.empty([0])
help_y = np.empty([0])
help_x = np.empty([0])
y_labels = []
list_data = []
current_sub = str(table[0, 0]) # initial subject
for item in range(len(table)):
item_name = lbp_path + str(table[item, 0]) + '_' + table[item, 1] + '.txt'
data = np.loadtxt(item_name)
test_data = sum(data)
# print(test_data)
# normalize the value
# way 1
# data = ( data - min(data) ) / ( max(data) - min(data) )
# way 2
# data = data * 255
# data = data.astype(int)
help_y = np.append(help_y, table[item, 2])
help_x = np.append(help_x, data)
# print(help_y)
if current_sub != str(table[item, 0]) or (item + 1)==len(table):
# print(current_sub)
y_labels += [help_y]
list_data += [help_x]
current_sub = str(table[item, 0])
help_y = np.empty([0])
help_x = np.empty([0])
# print(len(list_data))
# print(len(y_labels))
# print(sum(len(y_labels[])))
return list_data, y_labels
def load_labels_and_LBP_values_emotion(file_casme, lbp_path, flag):
table_casme = pd.read_excel(file_casme, converters={'Subject': lambda x: str(x)})
table_casme = table_casme[['Subject', 'Filename', 'Estimated Emotion']]
table_casme['Subject'] = 'sub' + table_casme['Subject'].astype(str)
table_casme = table_casme.as_matrix()
print(table_casme)
table = table_casme
reference_table = np.empty([0])
help_y = np.empty([0])
help_x = np.empty([0])
y_labels = []
list_data = []
current_sub = str(table[0, 0]) # initial subject
IgnoredSamples = ['sub09_EP13_02','sub09_EP02_02f','sub10_EP13_01','sub17_EP15_01',
'sub17_EP15_03','sub19_EP19_04', 'sub19_EP06_02f', 'sub24_EP10_03','sub24_EP07_01',
'sub24_EP07_04f','sub24_EP02_07','sub26_EP15_01' ]
for item in range(len(table)):
pass_flag = 0
item_name = lbp_path + str(table[item, 0]) + '_' + table[item, 1] + '.txt'
for stuff in IgnoredSamples:
if stuff in item_name:
pass_flag = 1
print(item_name)
if pass_flag == 0:
data = np.loadtxt(item_name)
test_data = sum(data)
if table[item, 2] == 'happiness':
label = 0
if table[item, 2] == 'disgust':
label = 1
if table[item, 2] == 'repression':
label = 2
if table[item, 2] == 'surprise':
label = 3
if table[item, 2] == 'others':
label = 4
help_y = np.append(help_y, label)
help_x = np.append(help_x, data)
# print(help_y)
if current_sub != str(table[item, 0]) or (item + 1)==len(table):
# print(current_sub)
y_labels += [help_y]
list_data += [help_x]
current_sub = str(table[item, 0])
help_y = np.empty([0])
help_x = np.empty([0])
# print(len(list_data))
# print(len(y_labels))
# print(sum(len(y_labels[])))
return list_data, y_labels
# some tunable parameters
subjects = 26 # 46 for samm-casme # 26 for casme
samples = 247 # 253 for samm-casme # 185 casme # 68 samm
# casme_path = "/media/ice/OS/Datasets/SAMM_CASME_Optical/CASME2-ObjectiveClasses.xlsx"
samm_path = "/media/ice/OS/Datasets/SAMM_CASME_Optical/SAMM_Micro_FACS_Codes_v2.xlsx"
casme_path = "/media/ice/OS/Datasets/CASME2_Magnified/CASME2_label_Ver_2.xls"
lbp_path = '/home/ice/Documents/Micro-Expression/LBP_Magnified_denoised/'
image_path = '/media/ice/OS/Datasets/CASME2_Optical/CASME2_Optical/'
path = '/home/ice/Documents/Micro-Expression/'
n_exp = 5
flag = 'cde'
flag2 = 'casme_only'
# load labels and LBP values
# data, y_labels = load_labels_and_LBP_values(casme_path, samm_path, lbp_path, flag2)
data, y_labels = load_labels_and_LBP_values_emotion(casme_path, lbp_path, flag)
# print(len(data))
# print(data)
tot_mat = np.zeros((n_exp,n_exp))# loading lbptop features
# model savings
casme_weights = path + 'casme_samm.pickle'
samm_weights = path + 'samm_casme.pickle'
if flag == 'cde':
for sub in range(subjects):
# X, y, test_X, test_y = data_loader_with_LOSO(sub, data, y_labels, subjects, 5)
X, y, test_X, test_y = data_loader_LOSO(data, y_labels, sub, subjects)
# print(X)
# clf = svm.SVC(kernel = 'linear', C = 0.1, decision_function_shape='ovr')
# lin_clf = svm.LinearSVC()
# lin_clf.fit(X, y)
# parameters = {'kernel':('linear', 'linear', 'linear', 'linear', 'linear'), 'C':[1, 10, 100, 1000, 10000]}
# svc = svm.SVC()
clf = svm.SVC(kernel = 'linear', C = 10000, decision_function_shape='ovr')
# clf = GridSearchCV(svc, parameters)
clf.fit(X, y)
prediction = clf.predict(test_X)
print(test_y)
print(prediction)
ct = confusion_matrix(test_y, prediction)
# check the order of the CT
order = np.unique(np.concatenate((prediction,test_y)))
# create an array to hold the CT for each CV
mat = np.zeros((n_exp,n_exp))
# print(mat.shape)
# print(ct.shape)
# 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
microAcc = np.trace(tot_mat) / np.sum(tot_mat)
[f1,precision,recall] = fpr(tot_mat,n_exp)
print("f1: " + str(f1))
print(tot_mat)
print("f1: " + str(f1))
war = weighted_average_recall(tot_mat, n_exp, samples)
uar = unweighted_average_recall(tot_mat, n_exp)
print("war: " + str(war))
print("uar: " + str(uar))
elif flag == 'hde_train':
X, y, Test_Y_gt = standard_data_loader(data, y_labels, subjects, n_exp)
clf = svm.SVC(kernel = 'linear', C = 10000, decision_function_shape='ovr')
clf.fit(X, y)
joblib.dump(clf, samm_weights)
print('done')
elif flag == 'hde_test':
X, y, Test_Y_gt = standard_data_loader(data, y_labels, subjects, n_exp)
clf = joblib.load(samm_weights)
prediction = clf.predict(X)
ct = confusion_matrix(Test_Y_gt, prediction)
# check the order of the CT
order = np.unique(np.concatenate((prediction, Test_Y_gt)))
# create an array to hold the CT for each CV
mat = np.zeros((n_exp,n_exp))
# print(mat.shape)
# print(ct.shape)
# 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
microAcc = np.trace(tot_mat) / np.sum(tot_mat)
[f1,precision,recall] = fpr(tot_mat,n_exp)
print(tot_mat)
print("f1: " + str(f1))
war = weighted_average_recall(tot_mat, n_exp, samples)
uar = unweighted_average_recall(tot_mat, n_exp)
print("war: " + str(war))
print("uar: " + str(uar))