-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_svm.m
executable file
·328 lines (262 loc) · 10.8 KB
/
test_svm.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
function statistics = test_svm(model,paths)
% TEST_SVM Tests a (lib)SVM classifier from the specified images paths
%
% INPUT:
% model: SVMmodel to use
% threshold: positive confidence threshold
% paths: positive / negative images_path to test
% //
% windows, descriptor and test parameter configuration is read from their
% corresponding paramteter files. If not found a window prompts for them.
%
% OUTPUT:
% statistics: ok, ko, false_pos, false_neg, true_pos, true_neg
% fppw and miss_rate metrics
%
%$ Author: Jose Marcos Rodriguez $
%$ Date: 2013/11/09 $
%$ Revision: 1.05 $
%% svm testing parameters
get_test_params();
% path stuff
if nargin < 2
positive_images_path = uigetdir('images','Select positive image folder');
negative_images_path = uigetdir('images','Select negative image folder');
if safe
images_path = uigetdir('images','Select base image path');
end
if isa(positive_images_path,'double') || ...
isa(negative_images_path,'double')
cprintf('Errors','Invalid paths...\nexiting...\n\n')
return
end
else
positive_images_path = paths{1};
negative_images_path = paths{2};
if safe
images_path = paths{3};
end
end
%% getting images to test from the specified folders
paths = {positive_images_path,negative_images_path};
[positive_images, negative_images] = get_files(pos_instances,neg_instances, paths);
% ====================================================================
%% Reading all POSITIVE images & computing the descriptor
% (64x128 images)
% ====================================================================
%% Computing HOG descriptor for all images (in chunks)
pos_start_time = tic;
false_negatives = 0;
true_positives = 0;
i = 0;
while i < numel(positive_images)
%% window obtainment
this_chunk = min(pos_chunk_size,numel(positive_images)-i);
windows = uint8(zeros(height,width,depth,this_chunk));
hogs = zeros(this_chunk, descriptor_size);
labels = ones(size(hogs,1),1);
for l=1:this_chunk
I = imread(positive_images(i+1).name);
windows(:,:,:,l) = get_window(I,width,height, 'center');
hogs(l,:) = compute_HOG(windows(:,:,:,l),cell_size,block_size,n_bins);
i = i+1;
end
% just for fixing GUI freezing due to unic thread MatLab issue
drawnow;
%% prediction
[predict_labels, ~, probs] = ...
svmpredict(labels, hogs, model, '-b 1');
%% counting and copying
for l=1:size(predict_labels)
predict_label = predict_labels(l);
if probs(l,1) >= 0.1
ok = ok + 1;
true_positives = true_positives + 1;
else
ko = ko + 1;
false_negatives = false_negatives + 1;
% saving hard image for further retrain
if safe
[~, name, ext] = fileparts(positive_images(i).name);
saving_path = strcat(images_path,'/hard_examples/false_neg/',...
name,...
'_n_wind_',num2str(l), ext);
% writting image
imwrite(windows(:,:,:,l), saving_path);
end
end
end
end
% hog extraction elapsed time
pos_elapsed_time = toc(pos_start_time);
fprintf('Elapsed time to classify positive images: %f seconds.\n',pos_elapsed_time);
% ====================================================================
%% Reading all NEGATIVE images & computing the descriptor
% Exhaustive search for hard examples
% (space-scaled 64x128 windows)
% ====================================================================
num_neg_images = size(negative_images,1);
if strcmp(neg_method, 'pyramid')
num_neg_windows = ...
get_negative_windows_count(negative_images);
elseif strcmp(neg_method, 'windows')
num_neg_windows = num_neg_images*neg_chunk_size;
end
fprintf('testing with %d negative images and %d negative windows\n', num_neg_images,num_neg_windows);
%% Computing HOG descriptor for all images (in chunks)
neg_start_time = tic;
false_positives = 0;
true_negatives = 0;
i = 0;
while i < numel(negative_images)
%% window obtaintion
% All pyramid HOGS
if strcmp(neg_method, 'pyramid')
I = imread(negative_images(i+1).name);
%% temporal
[h,w,~] = size(I);
if max(h,w) >= 160
ratio = max(96/w,160/h);
I = imresize(I,ratio);
end
%% fin temporal
[hogs, windows, wxl] = get_pyramid_hogs(I, descriptor_size, scale, stride);
labels = ones(size(hogs,1),1).*(-1);
i = i+1;
% random window HOG
elseif strcmp(neg_method,'windows')
this_chunk = min(neg_chunk_size, numel(negative_images)-i);
windows = uint8(zeros(height,width,depth,this_chunk));
hogs = zeros(this_chunk, descriptor_size);
labels = ones(size(hogs,1),1).*(-1);
for l=1:this_chunk
I = imread(negative_images(i+1).name);
windows(:,:,:,l) = get_window(I,width,height, 'center');
hogs(l,:) = compute_HOG(windows(:,:,:,l),cell_size,block_size,n_bins);
i = i+1;
end
end
% just for fixing GUI freezing due to unic thread MatLab issue
drawnow;
%% prediction
[predict_labels, ~, probs] = ...
svmpredict(labels, hogs, model, '-b 1');
%% updating statistics
for l=1:size(predict_labels)
predict_label = predict_labels(l);
if probs(l,1) < 0.1
ok = ok + 1;
true_negatives = true_negatives + 1;
else
ko = ko + 1;
false_positives = false_positives + 1;
if safe
% saving hard image for further retrain
[~, name, ext] = fileparts(negative_images(i).name);
if strcmp(neg_method, 'pyramid')
[level, num_image] = get_window_indices(wxl, l);
saving_path = strcat(images_path,'/hard_examples/false_pos/',...
name,...
'_l',num2str(level),...
'_w',num2str(num_image),ext);
else
saving_path = strcat(images_path,'/hard_examples/false_pos/',...
name,...
'_n_wind_',num2str(l), ext);
end
% writting image
imwrite(windows(:,:,:,l), saving_path);
end
end
end
end
% hog extraction elapsed time
neg_elapsed_time = toc(neg_start_time);
fprintf('Elapsed time to classify negative images: %f seconds.\n',neg_elapsed_time);
%% Printing gloabl results
precision = true_positives/(true_positives+false_positives);
recall = true_positives/(true_positives+false_negatives);
fprintf('oks: %d \n',ok)
fprintf('kos: %d \n',ko)
fprintf('false positives: %d \n',false_positives)
fprintf('false negatives: %d \n',false_negatives)
fprintf('true positives: %d \n',true_positives)
fprintf('true negatives: %d \n',true_negatives)
fprintf('mis rate: %d \n',false_negatives / (true_positives + false_negatives))
fprintf('fppw: %d \n',false_positives / (ok + ko))
fprintf('Precision: %d \n',precision)
fprintf('Recall: %d \n',recall)
fprintf('F score: %d \n',2*((precision*recall)/(precision+recall)))
% preparing values to return
statistics = containers.Map;
statistics('oks') = ok;
statistics('kos') = ok;
statistics('fp') = false_positives;
statistics('tp') = true_positives;
statistics('fn') = false_negatives;
statistics('tn') = true_negatives;
statistics('miss_rate') = false_negatives / (true_positives + false_negatives);
statistics('fppw') = false_positives / (ok + ko);
statistics('precision') = precision;
statistics('recall') = recall;
statistics('fscore') = 2*((precision*recall)/(precision+recall));
% ---------------------------------------------------------------------
%% Aux function to obtain the test parameters
% ---------------------------------------------------------------------
function get_test_params()
test_params = get_params('test_svm_params');
pos_chunk_size = test_params.pos_chunk_size;
neg_chunk_size = test_params.neg_chunk_size;
scale = test_params.scale;
stride = test_params.stride;
threshold = test_params.threshold;
neg_method = test_params.neg_window_method;
safe = test_params.safe;
neg_instances = test_params.neg_instances;
pos_instances = test_params.pos_instances;
w_params = get_params('window_params');
depth = w_params.color_depth;
width = w_params.width;
height = w_params.height;
desc_params = get_params('desc_params');
cell_size = desc_params.cell_size;
block_size = desc_params.block_size;
n_bins = desc_params.n_bins;
desp = 1;
n_v_cells = floor(height/cell_size);
n_h_cells = floor(width/cell_size);
hist_size = block_size*block_size*n_bins;
descriptor_size = hist_size*(n_v_cells-block_size+desp)*(n_h_cells-block_size+desp);
ok = 0;
ko = 0;
end
end
%% Aux function to know how many windows we'll have...
function count = get_negative_windows_count(negative_images)
% computing number of levels in the pyramid
count = 0;
for i=1:numel(negative_images)
I = imread(negative_images(i).name);
%% temporal
[h,w,~] = size(I);
if max(h,w) >= 160
ratio = max(96/w,160/h);
I = imresize(I,ratio);
end
%% fin temporal
[~, windows] = get_pyramid_dimensions(I);
count = count + windows;
end
end
%% Aux function to know how the windows indices...
function [level, num_window] = get_window_indices(wxl, w_linear_index)
accum_windows = 0;
for i=1:size(wxl,2)
accum_windows = accum_windows + wxl(i);
if w_linear_index <= accum_windows
level = i;
num_window = accum_windows - w_linear_index;
break
end
end
end