-
Notifications
You must be signed in to change notification settings - Fork 5
/
Assignment2_Part1_eval.m
126 lines (98 loc) · 4.1 KB
/
Assignment2_Part1_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
% 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.
%==========================================================================
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)
%==========================================================================
disp('Recognition: Extracting features...')
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('./models/fr_model.mat');
load('./data/face_recognition/face_recognition_data_te.mat');
lbp_cellSize = 8;
pca_components = 250;
va_lbp_vectors = zeros(length(va_img_sample), lbp_cellSize * lbp_cellSize * 58);
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
nn_vector_size = 2622;
va_nn_vectors = zeros(length(va_img_sample), nn_vector_size);
h = waitbar(0, 'Initializing waitbar...', 'Name', 'Recognition: Extracting features...');
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 = i / length(va_img_sample);
waitbar(perc, h, sprintf('%1.3f%% Complete', perc * 100));
end
close(h);
Yva = zeros(length(va_img_sample), 1);
for i =1:length(va_img_sample)
Yva(i) = va_img_sample{i, 3};
end
%% Build data for training from extracted features
Xva = [Xva va_nn_vectors];
% PCA
Xva = bsxfun(@minus ,Xva, mean(Xva));
Xva = Xva * coeff;
Xva = double(Xva);
%% 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 recognition is:%.2f \n', acc)