-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
349 lines (241 loc) · 12.2 KB
/
main.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
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import skimage.color as skc
import argparse
import imutils
import time
import json
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from extract_features import *
from detect_faces import *
from utils import load_dataset, plot_single_img, compute_AUC
from training_models import train_model
from tuning import tuning_fn, tuning_fn_random, remove_correlated_values
from testing_models import test_model
# gpu libraries for numpy (not implemented)
from numba import cuda
#cupy library
def main_feature_extraction(img_list, gab_filters):
"""_Extract the features of the cropped faces_
Parameters:
----------
...
gab_filters: (list) batch of gabor filters
return:
------
features_vector: array with the extracted features
"""
global gf_lambda
# initialize features vector
features_vector = np.zeros([len(img_list), 53])
for i in range(len(img_list)):
# gray scale
img_gray = cv2.cvtColor(img_list[i], cv2.COLOR_BGR2GRAY)
# Features based on gradients
features_vector[i, 0:5] = compute_gradient(img_gray)
# features based on gray level co-ocurrence matrix
features_vector[i, 5:10] = features_coomatrix(img_gray)
# features based on wavelet transform
features_vector[i, 10:19] = wavelet_tranform(img_gray, color=False)
#features based on wavelet transform (multiple levels)
features_vector[i, 19:35] = wavelet_tranform2(img_gray, levels=5, w_type = 'haar', color=False)
# features based on gabor filters
features_vector[i, 35:53] = gabor_filter(img_gray, gab_filters, n_batches=len(gf_lambda))
return features_vector
def main_feature_extraction_blocks(img_list, gab_filters):
"""_Extract the features of the cropped faces (many of them from the blocks in which the faces are divided)_
Parameters:
----------
...
gab_filters: (list) batch of gabor filters
return:
------
features_vector: array with the extracted features
"""
global gf_lambda
# initialize features vector
features_vector = np.zeros([len(img_list), 205])
for i in range(len(img_list)):
# gray scale
img_gray = cv2.cvtColor(img_list[i], cv2.COLOR_BGR2GRAY)
# divide img in slices (grayscale)
real_gray_slices = divide_image(img_gray, n=3, color=False)
# divide img in slices (color)
real_color_slices = divide_image(img_list[i], n=3, color=False)
for j in range(len(real_gray_slices)):
# Features based on gradients
features_vector[i, 0+(j*5):5+(j*5)] = compute_gradient(real_gray_slices[j])
# features based on gray level co-ocurrence matrix
features_vector[i, 45+(j*5):50+(j*5)] = features_coomatrix(real_gray_slices[j])
# features based on wavelet transform
features_vector[i, 90+(j*9):99+(j*9)] = wavelet_tranform(real_color_slices[j], color=True)
# features based on wavelet transform
features_vector[i, 171:187] = wavelet_tranform2(img_gray, levels=5, w_type = 'haar', color=False)
# features based on gabor filters
features_vector[i, 187:205] = gabor_filter(img_gray, gab_filters, n_batches=len(gf_lambda))
return features_vector
## PARAMETERS
task_folder = ["Task_1/", "Task_2_3/"]
folder_path = ["development", "evaluation"]
options = ['real/', 'fake/']
width_img = 200
height_img = 200
gf_lambda = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
gf_theta = 16
F_BLOCKS = False
F_RANDOM_SPLIT = False
# PROCESS
F_EXTRACT_FEATURES = True
F_NORMALIZATION = True
F_SAVE_FEATURES = True
F_LOAD_FEATURES = False
F_TRAIN_MODEL = True
F_TUNE_MODEL = True
F_TEST_MODEL = True
F_AUC = True
# TUNING MODES
# 0 = Features splitted by half and acc checked adding feature by feature
# 1 = Choose de features that maximize at the same time the acc of the training and validation set
# 2 = Eliminate highly correlated features
F_TUNING_MODE = 1
# TASK SELECTION
# 1 = TASK 1
# 2 = TASK 2
F_TASK = 1
if __name__ == '__main__':
"""_MAIN_
Parameters:
----------
...
F_BLOCKS _____________ (bool) if true divide the cropped faces into blocks or slices (9, computational cost *9)
F_RANDOM_SPLIT _______ (bool) if true, randomly split the training and validation set
F_EXTRACT_FEATURES ___ (bool) if true, extract the features for the training, validation and test set
F_NORMALIZATION ______ (bool) if true, normalize the extracted features
F_SAVE_FEATURES ______ (bool) if true, save the extracted features of the input data
F_LOAD_FEATURES ______ (bool) if true, load the saved features and the idxs of the selected features
F_TRAIN_MODEL ________ (bool) if true, train the model and plot results for different classifiers (acc and confusion matrices)
F_TUNE_MODEL _________ (bool) if true, tune the model selecting the best parameters
F_TEST_MODEL _________ (bool) if true, test the model with the selected parameters
F_AUC ________________ (bool) if true, comput the ROC curve and calculate the AUC
F_TUNING_MODE ________ (int) select the tuning mode for the model
0 _ Features splitted by half and acc checked adding feature by feature
1 _ Choose the features that maximize at the same time the acc of the training and validation set
2 _ Eliminate highly correlated features
F_TASK _______________ (int) select the task
1 _ Task 1
2 _ Task 2 (only for evaluation of the task 1 model)
width_img ____________ (int) resize width
height_img ___________ (int) resize height
gf_lambda ____________ (list(floats)) coefficients of lambda of the gabor filters
gf_theta _____________ (int) number of orientations considered for the gabor filters within {0, 180}
"""
if F_EXTRACT_FEATURES:
#0. GABOR FILTERS
gab_filters = get_gabor_filters(n_theta=gf_theta, lambda_val=gf_lambda)
for n_main in range(2):
# 1. LOAD DATASET
print('1. Loading dataset...')
real_faces = load_dataset(folder_path = (task_folder[n_main*(F_TASK-1)] + folder_path[n_main] + "/"), type_face = options[0])
fake_faces = load_dataset(folder_path = (task_folder[n_main*(F_TASK-1)] + folder_path[n_main] + "/"), type_face = options[1])
# 2. CROP THE FACE REGION
print('2. Cropping faces')
real_faces_cropped = []
fake_faces_cropped = []
for i in range(len(real_faces)):
img = face_detector_cvlib(real_faces[i], folder_path[n_main], resize=True, x=width_img, y=height_img)
if isinstance(img, np.ndarray):
real_faces_cropped.append(img)
for i in range(len(fake_faces)):
img = face_detector_cvlib(fake_faces[i], folder_path[n_main], resize=True, x=width_img, y=height_img)
if isinstance(img, np.ndarray):
fake_faces_cropped.append(img)
# 3. FEATURE EXTRACTION
print('3. Extracting features')
## get the resolution of the images
# get_resolution(real_faces_cropped, fake_faces_cropped, save=True)
start = time.time()
if F_BLOCKS:
features_real = main_feature_extraction_blocks(real_faces_cropped, gab_filters)
else:
features_real = main_feature_extraction(real_faces_cropped, gab_filters)
end = time.time()
print('Time: %8.10f' % (end-start))
start = time.time()
if F_BLOCKS:
features_fake = main_feature_extraction_blocks(fake_faces_cropped, gab_filters)
else:
features_fake = main_feature_extraction(fake_faces_cropped, gab_filters)
end = time.time()
print('Time: %8.10f' % (end-start))
# 4. ADD LABELS (Real=0 ; Fake=1)
print('4. Adding labels')
features_real = np.append(features_real, np.zeros([features_real.shape[0],1]), axis=1)
features_fake = np.append(features_fake, np.ones([features_fake.shape[0],1]), axis=1)
# 5. SPLIT TRAIN, VALIDATION AND TEST
print('5. Splitting data')
if n_main==0:
if F_RANDOM_SPLIT:
features_real = shuffle(features_real)
features_fake = shuffle(features_fake)
n_split = 300
train_features_real = features_real[:n_split,:]
train_features_fake = features_fake[:n_split,:]
val_features_real = features_real[n_split:,:]
val_features_fake = features_fake[n_split:,:]
train_features = np.concatenate((train_features_real, train_features_fake), axis=0)
val_features = np.concatenate((val_features_real,val_features_fake), axis=0)
else:
test_features = np.concatenate((features_real,features_fake), axis=0)
# 6. NORMALIZATION
if F_NORMALIZATION:
print("6. Normalization")
fnorm_train, fnorm_val, fnorm_test = features_normalization(train_features, val_features, test_features, zero_mean=True)
# 7. SAVE FEATURES
if F_SAVE_FEATURES:
print("7. Saving features")
np.save('fnrom_train.npy', fnorm_train)
np.save('fnrom_val.npy', fnorm_val)
np.save('fnrom_test.npy', fnorm_test)
if F_LOAD_FEATURES:
fnorm_train = np.load('fnrom_train.npy')
fnorm_val = np.load('fnrom_val.npy')
fnorm_test = np.load('fnrom_test.npy')
## load array with the index of the selected features (MODE 0 or 2)
# idx_features = np.load('selected_features.npy')
## load array with the index of the selected features (MODE 1)
with open("idx_list", "r") as fp:
idx_features = json.load(fp)
## CREATE array with the idx of ALL the features, regardless of saved tunning
# idx_features = np.linspace(0,fnorm_train.shape[1]-2,fnorm_train.shape[1]-1,dtype=int)
# 8. TRAIN MODEL
if F_TRAIN_MODEL:
print("8. Training model")
train_model(fnorm_train, fnorm_val, fnorm_test, mode='test')
# 9. TUNE MODEL
if F_TUNE_MODEL:
print("9. Tunning model")
if F_TUNING_MODE == 0:
idx_features = tuning_fn(fnorm_train, fnorm_val, LINEAR_MODEL=False, POLY_MODEL=False, GAUSSIAN_MDOEL=True, RF_MODEL=False, save_idx=True, merge_data=False, shuffle_data=False)
if F_TUNING_MODE == 1:
idx_features = tuning_fn_random(fnorm_train, fnorm_val, LINEAR_MODEL=True, POLY_MODEL=False, GAUSSIAN_MDOEL=True, RF_MODEL=False, save_idx=True,
merge_data=True, shuffle_data = True, min_n = 3, max_n = 205, n_iterations=25, weight_train=0.5, weigth_val=0.5)
with open("idx_list", "w") as fp:
json.dump(idx_features, fp)
if F_TUNING_MODE == 2:
idx_features = remove_correlated_values(fnorm_train, thres_corr=0.95, save_idx=True)
# 10. TEST MODEL
if F_TEST_MODEL:
print("10. Testing model")
if F_TUNING_MODE == 1:
for idfe in idx_features:
prob_predctions, y_true = test_model(fnorm_train, fnorm_val, fnorm_test, idfe, mode='train', merge_dataset=False)
if F_AUC:
compute_AUC(pred_prob=prob_predctions, y_true=y_true, plot_results=True)
else:
prob_predctions, y_true = test_model(fnorm_train, fnorm_val, fnorm_test, idx_features, mode='test', merge_dataset=False)
# 11. AUC
if F_AUC:
compute_AUC(pred_prob=prob_predctions, y_true=y_true, plot_results=True)