-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathtest_samm_cross.py
402 lines (313 loc) · 15 KB
/
test_samm_cross.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
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 PIL import Image
from keras.models import Sequential, Model
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, GlobalAveragePooling2D
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
from keras.preprocessing.image import ImageDataGenerator, array_to_img
import keras
from keras.callbacks import EarlyStopping
from labelling import collectinglabel
from reordering import readinput
from evaluationmatrix import fpr, weighted_average_recall, unweighted_average_recall
from utilities import Read_Input_Images, get_subfolders_num, standard_data_loader, label_matching, duplicate_channel
from utilities import record_scores, loading_smic_table, loading_casme_table, ignore_casme_samples, ignore_casmergb_samples, LossHistory
from utilities import loading_samm_table, loading_casme_objective_table, filter_objective_samples
from samm_utilitis import get_subfolders_num_crossdb, Read_Input_Images_SAMM_CASME, loading_samm_labels
from models import VGG_16, temporal_module, VGG_16_4_channels, convolutional_autoencoder
def test_samm_cross(batch_size, spatial_epochs, temporal_epochs, train_id, dB, spatial_size, flag, tensorboard):
############## Path Preparation ######################
root_db_path = "/media/ice/OS/Datasets/"
workplace = root_db_path + dB + "/"
inputDir = root_db_path + dB + "/" + dB + "/"
######################################################
classes = 5
if dB == 'CASME2_TIM':
table = loading_casme_table(workplace + 'CASME2-ObjectiveClasses.xlsx')
listOfIgnoredSamples, IgnoredSamples_index = ignore_casme_samples(inputDir)
############## Variables ###################
r = w = spatial_size
subjects=2
n_exp = 5
# VidPerSubject = get_subfolders_num(inputDir, IgnoredSamples_index)
listOfIgnoredSamples = []
VidPerSubject = [2,1]
timesteps_TIM = 10
data_dim = r * w
pad_sequence = 10
channel = 3
############################################
os.remove(workplace + "Classification/CASME2_TIM_label.txt")
elif dB == 'CASME2_Optical':
table = loading_casme_table(workplace + 'CASME2-ObjectiveClasses.xlsx')
listOfIgnoredSamples, IgnoredSamples_index = ignore_casme_samples(inputDir)
############## Variables ###################
r = w = spatial_size
subjects=26
n_exp = 5
VidPerSubject = get_subfolders_num(inputDir, IgnoredSamples_index)
timesteps_TIM = 9
data_dim = r * w
pad_sequence = 9
channel = 3
############################################
# os.remove(workplace + "Classification/CASME2_TIM_label.txt")
elif dB == 'SAMM_TIM10':
table, table_objective = loading_samm_table(root_db_path, dB)
listOfIgnoredSamples = []
IgnoredSamples_index = np.empty([0])
################# Variables #############################
r = w = spatial_size
subjects = 29
n_exp = 8
VidPerSubject = get_subfolders_num(inputDir, IgnoredSamples_index)
timesteps_TIM = 10
data_dim = r * w
pad_sequence = 10
channel = 3
classes = 8
#########################################################
elif dB == 'SAMM_CASME_Optical':
# total amount of videos 253
# table, table_objective = loading_samm_table(root_db_path, dB)
# table = table_objective
table = loading_casme_objective_table(root_db_path, dB)
# merge samm and casme tables
# table = np.concatenate((table, table2), axis=1)
# print(table.shape)
# listOfIgnoredSamples, IgnoredSamples_index, sub_items = ignore_casme_samples(inputDir)
listOfIgnoredSamples = []
IgnoredSamples_index = np.empty([0])
sub_items = np.empty([0])
list_samples = filter_objective_samples(table)
r = w = spatial_size
subjects = 26 # some subjects were removed because of objective classes and ignore samples: 47
n_exp = 5
# TODO:
# 1) Further decrease the video amount, the one with objective classes >= 6
# list samples: samples with wanted objective class
VidPerSubject, list_samples = get_subfolders_num_crossdb(inputDir, IgnoredSamples_index, sub_items, table, list_samples)
# print(VidPerSubject)
# print(len(VidPerSubject))
# print(sum(VidPerSubject))
timesteps_TIM = 9
data_dim = r * w
channel = 3
classes = 5
# if os.path.isfile(workplace + "Classification/SAMM_CASME_Optical_label.txt"):
# os.remove(workplace + "Classification/SAMM_CASME_Optical_label.txt")
##################### Variables ######################
######################################################
############## Flags ####################
tensorboard_flag = tensorboard
resizedFlag = 1
train_spatial_flag = 0
train_temporal_flag = 0
svm_flag = 0
finetuning_flag = 0
cam_visualizer_flag = 0
channel_flag = 0
if flag == 'st':
train_spatial_flag = 1
train_temporal_flag = 1
finetuning_flag = 1
elif flag == 's':
train_spatial_flag = 1
finetuning_flag = 1
elif flag == 't':
train_temporal_flag = 1
elif flag == 'nofine':
svm_flag = 1
elif flag == 'scratch':
train_spatial_flag = 1
train_temporal_flag = 1
elif flag == 'st4':
train_spatial_flag = 1
train_temporal_flag = 1
channel_flag = 1
elif flag == 'st7':
train_spatial_flag = 1
train_temporal_flag = 1
channel_flag = 2
#########################################
############ Reading Images and Labels ################
SubperdB = Read_Input_Images_SAMM_CASME(inputDir, list_samples, listOfIgnoredSamples, dB, resizedFlag, table, workplace, spatial_size, channel)
print("Loaded Images into the tray...")
labelperSub = label_matching(workplace, dB, subjects, VidPerSubject)
print("Loaded Labels into the tray...")
if channel_flag == 1:
SubperdB_strain = Read_Input_Images(inputDir, listOfIgnoredSamples, 'CASME2_Strain_TIM10', resizedFlag, table, workplace, spatial_size, 1)
elif channel_flag == 2:
SubperdB_strain = Read_Input_Images(inputDir, listOfIgnoredSamples, 'CASME2_Strain_TIM10', resizedFlag, table, workplace, spatial_size, 1)
SubperdB_gray = Read_Input_Images(inputDir, listOfIgnoredSamples, 'CASME2_TIM', resizedFlag, table, workplace, spatial_size, 3)
#######################################################
########### Model Configurations #######################
sgd = optimizers.SGD(lr=0.0001, decay=1e-7, momentum=0.9, nesterov=True)
adam = optimizers.Adam(lr=0.00001, decay=0.000001)
adam2 = optimizers.Adam(lr= 0.00075, decay= 0.0001)
# Different Conditions for Temporal Learning ONLY
if train_spatial_flag == 0 and train_temporal_flag == 1 and dB != 'CASME2_Optical':
data_dim = spatial_size * spatial_size
elif train_spatial_flag == 0 and train_temporal_flag == 1 and dB == 'CASME2_Optical':
data_dim = spatial_size * spatial_size * 3
else:
data_dim = 4096
########################################################
########### Training Process ############
# total confusion matrix to be used in the computation of f1 score
tot_mat = np.zeros((n_exp,n_exp))
# model checkpoint
spatial_weights_name = 'vgg_spatial_'+ str(train_id) + '_casme2_'
temporal_weights_name = 'temporal_ID_' + str(train_id) + '_casme2_'
history = LossHistory()
stopping = EarlyStopping(monitor='loss', min_delta = 0, mode = 'min')
############### Reinitialization & weights reset of models ########################
vgg_model_cam = VGG_16(spatial_size=spatial_size, classes=classes, weights_path='vgg_spatial_2_SAMM_CASME_Optical.h5')
temporal_model = temporal_module(data_dim=data_dim, classes=classes, timesteps_TIM=timesteps_TIM, weights_path='temporal_ID_2_SAMM_CASME_Optical.h5')
temporal_model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=[metrics.categorical_accuracy])
conv_ae = convolutional_autoencoder(spatial_size = spatial_size, classes = classes)
conv_ae.compile(loss='binary_crossentropy', optimizer=adam)
if channel_flag == 1 or channel_flag == 2:
vgg_model = VGG_16_4_channels(classes=classes, spatial_size = spatial_size)
vgg_model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=[metrics.categorical_accuracy])
else:
vgg_model = VGG_16(spatial_size = spatial_size, classes=classes, weights_path='vgg_spatial_2_SAMM_CASME_Optical.h5')
vgg_model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=[metrics.categorical_accuracy])
svm_classifier = SVC(kernel='linear', C=1)
####################################################################################
############ 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_Y_gt = standard_data_loader(SubperdB, labelperSub, subjects, classes)
# Rearrange Training labels into a vector of images, breaking sequence
Train_X_spatial = Train_X.reshape(Train_X.shape[0]*timesteps_TIM, r, w, channel)
# Test_X_spatial = Test_X.reshape(Test_X.shape[0]* timesteps_TIM, r, w, channel)
# Special Loading for 4-Channel
if channel_flag == 1:
Train_X_Strain, _, Test_X_Strain, _, _ = data_loader_with_LOSO(sub, SubperdB_strain, labelperSub, subjects, classes)
Train_X_Strain = Train_X_Strain.reshape(Train_X_Strain.shape[0]*timesteps_TIM, r, w, 1)
Test_X_Strain = Test_X_Strain.reshape(Test_X.shape[0]*timesteps_TIM, r, w, 1)
# Concatenate Train X & Train_X_Strain
Train_X_spatial = np.concatenate((Train_X_spatial, Train_X_Strain), axis=3)
Test_X_spatial = np.concatenate((Test_X_spatial, Test_X_Strain), axis=3)
channel = 4
elif channel_flag == 2:
Train_X_Strain, _, Test_X_Strain, _, _ = data_loader_with_LOSO(sub, SubperdB_strain, labelperSub, subjects, classes)
Train_X_gray, _, Test_X_gray, _, _ = data_loader_with_LOSO(sub, SubperdB_gray, labelperSub, subjects)
Train_X_Strain = Train_X_Strain.reshape(Train_X_Strain.shape[0]*timesteps_TIM, r, w, 1)
Test_X_Strain = Test_X_Strain.reshape(Test_X_Strain.shape[0]*timesteps_TIM, r, w, 1)
Train_X_gray = Train_X_gray.reshape(Train_X_gray.shape[0]*timesteps_TIM, r, w, 3)
Test_X_gray = Test_X_gray.reshape(Test_X_gray.shape[0]*timesteps_TIM, r, w, 3)
# Concatenate Train_X_Strain & Train_X & Train_X_gray
Train_X_spatial = np.concatenate((Train_X_spatial, Train_X_Strain, Train_X_gray), axis=3)
Test_X_spatial = np.concatenate((Test_X_spatial, Test_X_Strain, Test_X_gray), axis=3)
channel = 7
if channel == 1:
# Duplicate channel of input image
Train_X_spatial = duplicate_channel(Train_X_spatial)
# Test_X_spatial = duplicate_channel(Test_X_spatial)
# Extend Y labels 10 fold, so that all images have labels
Train_Y_spatial = np.repeat(Train_Y, timesteps_TIM, axis=0)
# Test_Y_spatial = np.repeat(Test_Y, timesteps_TIM, axis=0)
# 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], channel, r, w)
y = Train_Y_spatial.reshape(Train_Y_spatial.shape[0], classes)
normalized_X = X.astype('float32') / 255.
# test_X = Test_X_spatial.reshape(Test_X_spatial.shape[0], channel, r, w)
# test_y = Test_Y_spatial.reshape(Test_Y_spatial.shape[0], classes)
# normalized_test_X = test_X.astype('float32') / 255.
print(X.shape)
###### conv weights must be freezed for transfer learning ######
if finetuning_flag == 1:
for layer in vgg_model.layers[:33]:
layer.trainable = False
for layer in vgg_model_cam.layers[:31]:
layer.trainable = False
if train_spatial_flag == 1 and train_temporal_flag == 1:
# Spatial Encoding
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)
output = model.predict(X, batch_size = batch_size)
# Testing
# output = model.predict(test_X, batch_size = batch_size)
# output_ae = model_ae.predict(normalized_test_X, batch_size = batch_size)
# for i in range(batch_size):
# visual_ae = output_ae[i].reshape(224,224,channel)
# # de-normalize
# visual_ae = ( ( visual_ae - min(visual_ae) ) / ( max(visual_ae) - min(visual_ae) ) ) * 255
# fname = '{prefix}_{index}_{hash}.{format}'.format(prefix='AE_output', index=str(sub),
# hash=np.random.randint(1e7), format='png')
# cv2.imwrite(workplace+'Classification/Result/ae_train/'+fname, visual_ae)
# output_ae = model.predict(output_ae, batch_size = batch_size)
# output = np.concatenate((output, output_ae), axis=1)
features = output.reshape(int(Train_X.shape[0]), timesteps_TIM, output.shape[1])
predict = temporal_model.predict_classes(features, batch_size=batch_size)
##############################################################
#################### 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)
war = weighted_average_recall(tot_mat, n_exp, int(Train_X.shape[0]))
uar = unweighted_average_recall(tot_mat, n_exp)
print(tot_mat)
print("WAR: " + str(war))
print("UAR: " + str(uar))
print("F1-Score: " + str(f1))
file = open(workplace+'Classification/'+ 'Result/'+dB+'/f1_' + str(train_id) + '.txt', 'a')
file.write(str(f1) + "\n")
file.close()
file = open(workplace+'Classification/'+ 'Result/'+dB+'/WAR_' + str(train_id) + '.txt', 'a')
file.write(str(war) + "\n")
file.close()
file = open(workplace+'Classification/'+ 'Result/'+dB+'/UAR_' + str(train_id) + '.txt', 'a')
file.write(str(uar) + "\n")
file.close()
##################################################################
################# write each CT of each CV into .txt file #####################
# record_scores(workplace, dB, ct, sub, order, tot_mat, n_exp, subjects)
###############################################################################