-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAssignment1_eval.m
276 lines (220 loc) · 9.05 KB
/
Assignment1_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
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
% Image and Visual Computing Assignment 1: Face Detection-Recognition
%==========================================================================
% In this assignment, you are expected to use the previous learned method
% to cope with face detection 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 function
% is 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.
% - The use image directory is also included in this part.
% - image_dir{1} is the training positive face images(resize).
% - image_dir{2} is the training negative non-face images(resize).
% - val_dir is the validation set of real images
%==========================================================================
clear all
close all
clc
run ICV_setup
% The relevant data directory
images_dir{1} = './data/face_detection/cropped_faces/'; % positive samples directory
images_dir{2} = './data/face_detection/non_faces_images/'; % negative samples directory
face_images_dir = dir(images_dir{1});
face_images_dir(1:2)=[];
val_dir{1} = './data/face_detection/val_face_detection_images/'; % Validation data (For visualization purpose).
val_file = dir(val_dir{1});
val_file(1:2)=[];
val_dir{2} = './data/face_detection/te_raw_images/'; % Validation data (For performance evaluation)
val_file2 = dir(val_dir{2});
val_file2(1:2)=[];
% Hyperparameter of experiments
resize_size=[64 64];
%% Feature Extraction for Face Detection
%==========================================================================
% Use the HoG features for face detection.
% - You should read the images and convert any color images into gray. For
% reading all images in subdirectory, you can use matlab function
% 'imageSet('./your data path', 'recursive'). The former quotes is the
% directory to your saved data and the latter 'recursive' is the
% hyparameter of this function.
% - Extract HoG intesert points for training image. You can use either HoG
% or LBP as features. Generally, using HoG with linear SVM can achieve
% reasonable performance which has already been verified from several
% papers. It is okay to use both vl_feat or your own function. You will get
% bonus points if you are using your own code to get the HoG and LBP
% features.
% (You should finish this part by yourself)
%==========================================================================
% Feature Extraction
disp('Extracting features...')
hog = true;
lbp = true;
nn = false;
pca_ = false;
normalise = false;
hog_cellSize = 8;
lbp_cellSize = 4;
pca_components = 1000;
max_resize = 1.0;
min_resize = 0.8;
threshold = 0.0;
nPosFace = length(face_images_dir);
nNegFace = 0;
imset = imageSet(images_dir{2}, 'recursive');
for i=1:length(imset)
for j = 1:imset(i).Count
nNegFace = nNegFace + 1;
end
end
%% Evaluating your result on the val_datasets
load('face_detector.mat')
% Initialization of the true positive, condition positive and prediction
% positive number collection.
total_TP = zeros(length(val_file2), 100);
total_condi_P = zeros(length(val_file2), 100);
total_Pred_P = zeros(length(val_file2), 100);
imset = imageSet(val_dir{2}, 'recursive');
count = 0;
overall_total = 0;
for k = 1:length(val_file2)
for u = 1:length(imset(k).Count)
overall_total = overall_total + 1;
end
end
h = waitbar(0, 'Initializing waitbar...', 'Name', 'Validation: Extracting features...');
for k = 1:length(val_file2)
for u = 1:length(imset(k).Count)
count = count + 1;
img = read(imset(k), u);
plt_img = img;
if size(img, 3)>1, img = rgb2gray(img); end
window_size=[64 64];
% Sliding window function
[patches, temp_bbox] = sw_detect_face(img, window_size, max_resize, 8);
for p = max_resize - 0.1:-0.1:min_resize
[temp_patches, temp_bbox2] = sw_detect_face(img, window_size, p, 8);
patches = cat(1, patches, temp_patches);
temp_bbox = cat(1, temp_bbox, temp_bbox2);
end
% Extract the features for each patch
total = 0;
for p = 1:length(patches)
for j = 1:size(patches{p}, 3)
total = total + 1;
end
end
if true(hog)
v = resize_size(1) / hog_cellSize;
te_hog_vectors = zeros(total, v * v * 31);
hog_iter = 1;
for p = 1:length(patches)
for j = 1:size(patches{p}, 3)
temp = single(patches{p}(:, :, j))/255;
temp = vl_hog(temp, hog_cellSize);
te_hog_vectors(hog_iter, :) = temp(:)';
hog_iter = hog_iter + 1;
end
end
end
if true(lbp)
v = resize_size(1) / lbp_cellSize;
te_lbp_vectors = zeros(total, v * v * 58);
lbp_iter = 1;
for p = 1:length(patches)
for j = 1:size(patches{p}, 3)
temp = single(patches{p}(:, :, j))/255;
temp = vl_lbp(temp, lbp_cellSize);
te_lbp_vectors(lbp_iter, :) = temp(:)';
lbp_iter = lbp_iter + 1;
end
end
end
if true(nn)
if exist(strcat('data/face_detection/nn_vectors/validation/va_nn_vectors_', int2str(k), '_', int2str(u), '.mat'), 'file') == 2
nn_vectors = load(strcat('data/face_detection/nn_vectors/validation/va_nn_vectors_', int2str(k), '_', int2str(u), '.mat'));
te_nn_vectors = nn_vectors.te_nn_vectors;
disp(strcat('Validation neural net vectors_', int2str(k), '_', int2str(u), '_loaded from storage'));
else
nn_vector_size = 2622;
te_nn_vectors = zeros(total, nn_vector_size);
nn_iter = 1;
for p = 1:length(patches)
for j = 1:size(patches{p}, 3)
temp = single(patches{p}(:, :, j)); % 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);
te_nn_vectors(nn_iter, :, :) = temp(:)';
nn_iter = nn_iter + 1;
end
end
% Save output
save(strcat('data/face_detection/nn_vectors/validation/va_nn_vectors_', int2str(k), '_', int2str(u), '.mat'), 'te_nn_vectors');
end
end
Xte = [];
if true(hog)
Xte = cat(2, Xte, te_hog_vectors);
end
if true(lbp)
Xte = cat(2, Xte, te_lbp_vectors);
end
if true(nn)
Xte = cat(2, Xte, te_nn_vectors);
end
bbox_ms = [];
for p = 1:length(patches)
for j = 1:size(patches{p}, 3)
bbox_ms = [bbox_ms; temp_bbox{p}(j, :)];
end
end
if true(pca_)
Xte = bsxfun(@minus, Xte, mean(Xte));
Xte = Xte * coeff;
end
if true(normalise)
normr(Xte);
end
% Get the positive probability for proposed faces
[predicted_label, ~, prob_estimates] = predict(zeros(size(Xte, 1), 1), sparse(Xte), Mdl);
l = predicted_label;
score = prob_estimates;
prob2 = score(:, 1);
% Getting the True positive, condition positive, predicted positive
% number for evaluating the algorithm performance via Average
[ TP_num, condi_P, Pred_P ] = evaluate_detector( bbox_ms, prob2 );
total_TP(count,:) = TP_num;
total_condi_P(count,:) = condi_P;
total_Pred_P(count,:) = Pred_P;
perc = count / overall_total;
waitbar(perc, h, sprintf('%1.3f%% Complete', perc * 100));
clear Xte Yte
end
end
close(h);
% Summing the statistics over all faces images.
sTP = sum(total_TP);
sCP = sum(total_condi_P);
sPP = sum(total_Pred_P);
% Compute the Precision
% TP is the number of intersection betweem recognized faces and the
% actual faces
Precision = sTP./sPP; % TP/(The number of recognized faces)
Recall = sTP./sCP; % TP/(The number of actual faces)
% Ploting the Precision-Recall curve. Normally, the yaxis is the Precision
% and xaxis is the Recall.
figure
plot(Recall, Precision)
xlabel('Recall');
ylabel('Precision');
% Interpolated Average Precision
AP = VOCap(Recall', Precision');
disp(num2str(AP))