-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAssignment2.m
504 lines (391 loc) · 16.2 KB
/
Assignment2.m
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
% Image and Visual Computing Assignment 2: Face Verification & Recognition
%==========================================================================
% In this assignment, you are expected to use the previous learned method
% to cope with face recognition and verification problem. The vl_feat,
% libsvm, liblinear and any other classification and feature extraction
% library are allowed to use in this assignment. The built-in matlab
% object-detection functionis not allowed. Good luck and have fun!
%
% Released Date: 31/10/2017
%==========================================================================
% ######################################
% ######################################
% ## ##
% ## Read the README ##
% ## ##
% ######################################
% ######################################
%% Initialisation
%==========================================================================
% Add the path of used library.
% - The function of adding path of liblinear and vlfeat is included.
%==========================================================================
clear all
clc
run ICV_setup
% Hyperparameter of experiments
resize_size=[64 64];
% Setup MatConvNet.
addpath(genpath('./library/matconvnet/matlab'))
vl_setupnn();
% Load the VGG-Face model.
modelPath = fullfile(vl_rootnn,'data','models','vgg-face.mat') ;
if ~exist(modelPath)
fprintf('Downloading the VGG-Face model ... this may take a while\n') ;
mkdir(fileparts(modelPath)) ;
urlwrite(...
'http://www.vlfeat.org/matconvnet/models/vgg-face.mat', ...
modelPath) ;
end
% Load the model and upgrade it to MatConvNet current version.
net = load(modelPath);
net = vl_simplenn_tidy(net);
%% Part I: Face Recognition: Who is it?
%==========================================================================
% The aim of this task is to recognize the person in the image(who is he).
% We train a multiclass classifer to recognize who is the person in this
% image.
% - Propose the patches of the images
% - Recognize the person (multiclass)
%==========================================================================
% Read README.txt for more details
disp('Recognition: Extracting features...')
Xtr = []; Ytr = [];
Xva = []; Yva = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loading the training data
% -tr_img_sample/va_img_sample:
% The data is store in a N-by-3 cell array. The first dimension of the cell
% array is the cropped face images. The second dimension is the name of the
% image and the third dimension is the class label for each image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load('./data/face_recognition/face_recognition_data_tr.mat');
load('./data/face_recognition/face_recognition_data_te.mat');
hog = false;
lbp = false;
bof = false;
nn = true;
pca_ = true;
hog_cellSize = 8;
lbp_cellSize = 8;
vocab_size = 250;
pca_components = 250;
%%
if true(hog)
tr_hog_vectors = zeros(length(tr_img_sample), hog_cellSize * hog_cellSize * 31);
va_hog_vectors = zeros(length(va_img_sample), hog_cellSize * hog_cellSize * 31);
for i =1:length(tr_img_sample)
temp = single(tr_img_sample{i,1})/255;
temp = vl_hog(temp, hog_cellSize);
tr_hog_vectors(i, :) = temp(:)';
end
for i =1:length(va_img_sample)
temp = single(va_img_sample{i,1})/255;
temp = vl_hog(temp, hog_cellSize);
va_hog_vectors(i, :) = temp(:)';
end
end
%%
if true(lbp)
tr_lbp_vectors = zeros(length(tr_img_sample), lbp_cellSize * lbp_cellSize * 58);
va_lbp_vectors = zeros(length(va_img_sample), lbp_cellSize * lbp_cellSize * 58);
for i =1:length(tr_img_sample)
temp = single(tr_img_sample{i,1})/255;
temp = vl_lbp(temp, lbp_cellSize);
tr_lbp_vectors(i, :) = temp(:)';
end
for i =1:length(va_img_sample)
temp = single(va_img_sample{i,1})/255;
temp = vl_lbp(temp, lbp_cellSize);
va_lbp_vectors(i, :) = temp(:)';
end
end
%%
if true(bof)
if ~exist('data/face_recognition/images/training/')
for i =1:length(tr_img_sample)
temp = single(tr_img_sample{i,1})/255;
name = strsplit(tr_img_sample{i,2}, '_');
number = strsplit(name{3}, '.');
foldername = ['data/face_recognition/images/training/', name{1}, '_', name{2}];
w = warning('query','last');
id = w.identifier;
warning('off',id)
mkdir(foldername);
filename = strcat(foldername, '/', name(1), '_', name(2), '_', number{1}, '.png');
imwrite(temp, filename{1});
end
end
if ~exist('data/face_recognition/images/validation/')
for i =1:length(va_img_sample)
temp = single(va_img_sample{i,1})/255;
name = strsplit(va_img_sample{i,2}, '_');
number = strsplit(name{3}, '.');
foldername = ['data/face_recognition/images/validation/', name{1}, '_', name{2}];
mkdir(foldername);
filename = strcat(foldername, '/', name(1), '_', name(2), '_', number{1}, '.png');
imwrite(temp, filename{1});
end
end
tr_imset = imageSet('data/face_recognition/images/training/', 'recursive');
bag = bagOfFeatures(tr_imset, 'VocabularySize', vocab_size);
tr_bof_vectors = zeros(length(tr_img_sample), vocab_size);
va_bof_vectors = zeros(length(va_img_sample), vocab_size);
for i =1:length(tr_img_sample)
temp = single(tr_img_sample{i,1})/255;
temp = encode(bag, temp);
tr_bof_vectors(i, :) = temp(:)';
end
for i =1:length(va_img_sample)
temp = single(va_img_sample{i,1})/255;
temp = encode(bag, temp);
tr_bof_vectors(i, :) = temp(:)';
end
end
%%
if true(nn)
if ~exist('tr_nn_vectors')
if exist(fullfile('data/face_recognition/', 'nn_vectors.mat'), 'file') == 2
nn_vectors = load(fullfile('data/face_recognition/', 'nn_vectors.mat'));
tr_nn_vectors = nn_vectors.tr_nn_vectors;
va_nn_vectors = nn_vectors.va_nn_vectors;
disp('Neural net vectors loaded from storage');
else
nn_vector_size = 2622;
tr_nn_vectors = zeros(length(tr_img_sample), nn_vector_size);
va_nn_vectors = zeros(length(va_img_sample), nn_vector_size);
h = waitbar(0, 'Initializing waitbar...', 'Name', 'Recognition: Extracting features...');
for i =1:length(tr_img_sample)
temp = single(tr_img_sample{i,1}); % 255 range.
temp = imresize(temp, net.meta.normalization.imageSize(1:2));
temp = repmat(temp, [1, 1, 3]);
temp = bsxfun(@minus, temp, net.meta.normalization.averageImage);
temp = vl_simplenn(net, temp);
temp = squeeze(temp(37).x);
temp = temp./norm(temp,2);
tr_nn_vectors(i, :, :) = temp(:)';
perc = i / (length(tr_img_sample) + length(va_img_sample));
waitbar(perc, h, sprintf('%1.3f%% Complete', perc * 100));
end
for i =1:length(va_img_sample)
temp = single(va_img_sample{i,1}); % 255 range.
temp = imresize(temp, net.meta.normalization.imageSize(1:2));
temp = repmat(temp, [1, 1, 3]);
temp = bsxfun(@minus, temp, net.meta.normalization.averageImage);
temp = vl_simplenn(net, temp);
temp = squeeze(temp(37).x);
temp = temp./norm(temp,2);
va_nn_vectors(i, :, :) = temp(:)';
perc = (length(tr_img_sample) + i) / (length(tr_img_sample) + length(va_img_sample));
waitbar(perc, h, sprintf('%1.3f%% Complete', perc * 100));
end
close(h);
% Save output
save('data/face_recognition/nn_vectors.mat', 'tr_nn_vectors', 'va_nn_vectors');
end
end
end
%% Prepare Training set and validation set for required features
if true(hog)
Xtr = cat(2, Xtr, tr_hog_vectors);
Xva = cat(2, Xva, va_hog_vectors);
end
if true(lbp)
Xtr = cat(2, Xtr, tr_lbp_vectors);
Xva = cat(2, Xva, va_lbp_vectors);
end
if true(bof)
Xtr = cat(2, Xtr, tr_bof_vectors);
Xva = cat(2, Xva, va_bof_vectors);
end
if true(nn)
Xtr = cat(2, Xtr, tr_nn_vectors);
Xva = cat(2, Xva, va_nn_vectors);
end
Ytr = zeros(length(tr_img_sample), 1);
for i =1:length(tr_img_sample)
Ytr(i) = tr_img_sample{i, 3};
end
Yva = zeros(length(va_img_sample), 1);
for i =1:length(va_img_sample)
Yva(i) = va_img_sample{i, 3};
end
%% PCA
if true(pca_)
[coeff,score,latent,~,explained] = pca(Xtr, 'NumComponents', pca_components);
Xtr = score;
Xva = bsxfun(@minus ,Xva, mean(Xva));
Xva = Xva * coeff;
disp('Finished feature extraction.')
end
%% Train the recognizer and evaluate the performance
Xtr = double(Xtr);
Xva = double(Xva);
% Train the recognizer
% model = fitcknn(Xtr,Ytr,'NumNeighbors',3);
% [l,prob] = predict(model,Xva);
addpath('library/liblinear-2.1/windows/');
model = train(double(Ytr), sparse(double(Xtr)));
[predicted_label, ~, prob_estimates] = predict(zeros(size(Xva, 1), 1), sparse(Xva), model);
l = predicted_label;
prob = prob_estimates;
% model = fitcecoc(Xtr, Ytr);
% [l,prob] = predict(model, Xva);
% Compute the accuracy
acc = mean(l == Yva) * 100;
fprintf('The accuracy of face recognition is:%.2f \n', acc)
%% Visualization the result of face recognition
data_idx = [1,30,50]; % The index of image in validation set
nSample = 3; % number of visualize data. maximum should be 3
% nPairs = length(data_idx); % unconment to get full size of
visualise_recognition(va_img_sample,prob,Yva,data_idx,nSample)
% Image and Visual Computing Assignment 2 Part 2: Face Verification
%==========================================================================
% In this assignment, you are expected to use the previous learned method
% to cope with face recognition and verification problem. The vl_feat,
% libsvm, liblinear and any other classification and feature extraction
% library are allowed to use in this assignment. The built-in matlab
% object-detection functionis not allowed. Good luck and have fun!
%
% Released Date: 31/10/2017
%==========================================================================
%% Initialisation
%==========================================================================
% Add the path of used library.
% - The function of adding path of liblinear and vlfeat is included.
%==========================================================================
clc
clear all
run ICV_setup
% Hyperparameter of experiments
resize_size=[64 64];
% Setup MatConvNet.
addpath(genpath('./library/matconvnet/matlab'))
vl_setupnn();
% Load the VGG-Face model.
modelPath = fullfile(vl_rootnn,'data','models','vgg-face.mat') ;
if ~exist(modelPath)
fprintf('Downloading the VGG-Face model ... this may take a while\n') ;
mkdir(fileparts(modelPath)) ;
urlwrite(...
'http://www.vlfeat.org/matconvnet/models/vgg-face.mat', ...
modelPath) ;
end
% Load the model and upgrade it to MatConvNet current version.
net = load(modelPath);
net = vl_simplenn_tidy(net);
%% Part II: Face Verification:
%==========================================================================
% The aim of this task is to verify whether the two given people in the
% images are the same person. We train a binary classifier to predict
% whether these two people are actually the same person or not.
% - Extract the features
% - Get a data representation for training
% - Train the verifier and evaluate its performance
%==========================================================================
disp('Verification:Extracting features..')
cellSize = 8;
Xtr = [];
Xva = [];
nn_1_train = [];
nn_2_train = [];
nn_1_val = [];
nn_2_val = [];
lbp_1_train = [];
lbp_2_train = [];
lbp_1_val = [];
lbp_2_val = [];
load('./data/face_verification/face_verification_tr.mat')
% load('./data/face_verification/face_verification_va.mat')
load('./data/face_verification/face_verification_te.mat')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loading the training data
% -tr_img_pair/va_img_pair:
% The data is store in a N-by-4 cell array. The first dimension of the cell
% array is the first cropped face images. The second dimension is the name
% of the image. Similarly, the third dimension is another image and the
% fourth dimension is the name of that image.
% -Ytr/Yva: is the label of 'same' or 'different'
%%%%%%%%%%%%%%%%%
% Ytr2 = zeros(1800,2);
%% Extract Features
h = waitbar(0,'Name','Extracting features...','Initializing waitbar...');
% You should construct the features in here. (read, resize, extract)
for i =1:length(tr_img_pair)
% First Image
im_ = single(tr_img_pair{i,1}) ; % note: 255 range
im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
im_ = bsxfun(@minus,im_,net.meta.normalization.averageImage) ;
res = vl_simplenn(net, im_) ;
output1 = squeeze(res(37).x);
output1 = output1./norm(output1,2);
nn_1_train = [nn_1_train; output1(:)'];
temp = single(tr_img_pair{i,1})/255;
lbp_1 = vl_lbp(temp, cellSize);
lbp_1_train = [lbp_1_train; lbp_1(:)'];
% Second Image
im_ = single(tr_img_pair{i,3}) ; % note: 255 range
im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
im_ = bsxfun(@minus,im_,net.meta.normalization.averageImage) ;
res = vl_simplenn(net, im_) ;
output2 = squeeze(res(37).x);
output2 = output2./norm(output2,2);
nn_2_train = [nn_2_train; output2(:)'];
temp = single(tr_img_pair{i,3})/255;
lbp_2 = vl_lbp(temp, cellSize);
lbp_2_train = [lbp_2_train; lbp_2(:)'];
perc = (i * 100) / (length(tr_img_pair) + length(va_img_pair));
waitbar(perc/100,h,sprintf('%0.5f%% Complete',perc))
end
for i =1:length(va_img_pair)
% First Image
im_ = single(va_img_pair{i,1}) ; % note: 255 range
im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
im_ = bsxfun(@minus,im_,net.meta.normalization.averageImage) ;
res = vl_simplenn(net, im_) ;
output1 = squeeze(res(37).x);
output1 = output1./norm(output1,2);
nn_1_val = [nn_1_val; output1(:)'];
temp = single(va_img_pair{i,1})/255;
lbp_1 = vl_lbp(temp, cellSize);
lbp_1_val = [lbp_1_val; lbp_1(:)'];
% Second Image
im_ = single(va_img_pair{i,3}) ; % note: 255 range
im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
im_ = bsxfun(@minus,im_,net.meta.normalization.averageImage) ;
res = vl_simplenn(net, im_) ;
output2 = squeeze(res(37).x);
output2 = output2./norm(output2,2);
nn_2_val = [nn_2_val; output2(:)'];
temp = single(va_img_pair{i,3})/255;
lbp_2 = vl_lbp(temp, cellSize);
lbp_2_val = [lbp_2_val; lbp_2(:)'];
perc = ((length(tr_img_pair) + i) * 100) / (length(tr_img_pair) + length(va_img_pair));
waitbar(perc/100,h,sprintf('%0.5f%% Complete',perc))
end
%% Build data for training from extracted features
Xtr = [sqrt(sum((lbp_1_train-lbp_2_train)'.^2))' sqrt(sum((nn_1_train-nn_2_train)'.^2))'];
Xva = [sqrt(sum((lbp_1_val-lbp_2_val)'.^2))' sqrt(sum((nn_1_val-nn_2_val)'.^2))'];
Xtr = double(Xtr);
Xva = double(Xva);
%% PCA
pca_components = min(size(Xtr,2));
[coeff,score,latent,~,explained] = pca(Xtr, 'NumComponents', pca_components);
Xtr = score;
Xva = bsxfun(@minus ,Xva, mean(Xva));
Xva = Xva * coeff;
%% Train the verifier and evaluate the performance
% Train the recognizer and evaluate the performance
addpath('library/liblinear-2.1/windows/');
model = train(double(Ytr), sparse(double(Xtr)));
[predicted_label, ~, prob_estimates] = predict(zeros(size(Xva, 1), 1), sparse(Xva), model);
l = predicted_label;
prob = prob_estimates;
% Compute the accuracy
acc = mean(l==Yva)*100;
fprintf('The accuracy of face verification is:%.2f \n', acc)
%% Visualization the result of face verification
data_idx = [100,200,300]; % The index of image in validation set
nPairs = 3; % number of visualize data. maximum is 3
% nPairs = length(data_idx);
visualise_verification(va_img_pair,prob,Yva,data_idx,nPairs )