-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAssignment2_Part2_eval.m
128 lines (106 loc) · 4.57 KB
/
Assignment2_Part2_eval.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
% 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
%==========================================================================
%% Initialisation
%==========================================================================
% Add the path of used library.
% - The function of adding path of liblinear and vlfeat is included.
%==========================================================================
clc
clear all
run ICV_setup
% 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;
nn_1_val = [];
nn_2_val = [];
lbp_1_val = [];
lbp_2_val = [];
load('./models/fv_model.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...');
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 = (i*100/ length(va_img_pair));
waitbar(perc/100,h,sprintf('%0.3f%% Complete',perc))
end
%% Build data for training from extracted features
Xva = [sqrt(sum((lbp_1_val-lbp_2_val)'.^2))' sqrt(sum((nn_1_val-nn_2_val)'.^2))'];
Xva = double(Xva);
% PCA
Xva = bsxfun(@minus ,Xva, mean(Xva));
Xva = Xva * coeff;
%% Train the verifier and evaluate the performance
[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 )