-
Notifications
You must be signed in to change notification settings - Fork 155
/
esvm_pool_exemplar_dets.m
184 lines (157 loc) · 5.35 KB
/
esvm_pool_exemplar_dets.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
function final = esvm_pool_exemplar_dets(grid, models, M, params)
% Perform detection post-processing and pool detection boxes
% (which will then be ready to go into the PASCAL evaluation code)
% If there are overlap scores associated with boxes, then they are
% also kept track of propertly, even after NMS.
%
% If M is empty, then just NMS is performed
% If M has neighbor_thresh defined, then we apply the
% calibration-matrix
% If M has betas defined, then do platt-calibration
%
% Copyright (C) 2011-12 by Tomasz Malisiewicz
% All rights reserved.
%
% This file is part of the Exemplar-SVM library and is made
% available under the terms of the MIT license (see COPYING file).
% Project homepage: https://github.com/quantombone/exemplarsvm
%REMOVE FIRINGS ON SELF-IMAGE (these create artificially high
%scores when evaluating on the training set, but no need to set
%this on the testing set as we don't train on testing data)
REMOVE_SELF = 0;
if REMOVE_SELF == 1
curids = cellfun2(@(x)x.curid,models);
end
%cls = models{1}.cls;
%excurids = cellfun2(@(x)x.curid,models);
bboxes = cell(1,length(grid));
maxos = cell(1,length(grid));
try
curcls = find(ismember(params.dataset_params.classes, ...
models{1}.cls));
catch
%dataset_params is missing
end
for i = 1:length(grid)
curid = grid{i}.curid;
bboxes{i} = grid{i}.bboxes;
if size(bboxes{i},1) == 0
continue
end
if ~isempty(grid{i}.extras) && isfield(grid{i}.extras,'maxos')
maxos{i} = grid{i}.extras.maxos;
maxos{i}(grid{i}.extras.maxclass~=curcls) = 0;
end
if REMOVE_SELF == 1
exes = bboxes{i}(:,6);
excurids = curids(exes);
badex = find(ismember(excurids,{curid}));
bboxes{i}(badex,:) = [];
if ~isempty(grid{i}.extras) && isfield(grid{i}.extras,'maxos')
if ~isempty(maxos{i})
maxos{i}(badex) = [];
end
end
end
end
raw_boxes = bboxes;
%Perform score rescaling
%1. no scaling
%2. platt's calibration (sigmoid scaling)
%3. raw score + 1
if (exist('M','var') && (~isempty(M)) && isfield(M,'betas') && ...
~isfield(M,'neighbor_thresh'))
fprintf(1,'Applying betas to %d images:',length(bboxes));
for i = 1:length(bboxes)
%if neighbor thresh is defined, then we are in M-mode boosting
if size(bboxes{i},1) == 0
continue
end
calib_boxes = esvm_calibrate_boxes(bboxes{i},M.betas);
oks = find(calib_boxes(:,end) > params.calibration_threshold);
calib_boxes = calib_boxes(oks,:);
bboxes{i} = calib_boxes;
end
elseif exist('M','var') && ~isempty(M) && isfield(M,'neighbor_thresh')
fprintf(1,'Applying M-matrix to %d images:',length(bboxes));
starter=tic;
nbrlist = cell(length(bboxes),1);
for i = 1:length(bboxes)
fprintf(1,'.');
if size(bboxes{i},1) == 0
continue
end
bboxes{i}(:,end) = bboxes{i}(:,end)+1;
[xraw,nbrlist{i}] = esvm_get_M_features(bboxes{i},length(models), ...
M.neighbor_thresh);
r2 = esvm_apply_M(xraw,bboxes{i},M);
bboxes{i}(:,end) = r2;
end
fprintf(1,'took %.3fsec\n',toc(starter));
else
fprintf(1,'No betas, No M-matrix, no calibration\n');
end
os_thresh = .3;
fprintf(1, 'Applying NMS (OS thresh=%.3f)\n',os_thresh);
for i = 1:length(bboxes)
if size(bboxes{i},1) > 0
bboxes{i}(:,5) = 1:size(bboxes{i},1);
bboxes{i} = esvm_nms(bboxes{i},os_thresh);
if ~isempty(grid{i}.extras) && isfield(grid{i}.extras,'maxos')
maxos{i} = maxos{i}(bboxes{i}(:,5));
end
if exist('nbrlist','var')
nbrlist{i} = nbrlist{i}(bboxes{i}(:,5));
end
bboxes{i}(:,5) = 1:size(bboxes{i},1);
end
end
if params.calibration_propagate_onto_raw && ...
exist('M','var') && length(M)>0 && isfield(M,'betas')
fprintf(1,'Propagating scores onto raw detections\n');
%% propagate scores onto raw boxes
for i = 1:length(bboxes)
if size(bboxes{i},1) > 0
allMscores = bboxes{i}(:,end);
calib_boxes = esvm_calibrate_boxes(raw_boxes{i},M.betas);
beta_scores = calib_boxes(:,end);
osmat = getosmatrix_bb(bboxes{i},raw_boxes{i});
for j = 1:size(osmat,1)
curscores = (osmat(j,:)>.5) .* beta_scores';
[aa,bb] = max(curscores);
bboxes{i}(j,:) = raw_boxes{i}(bb,:);
bboxes{i}(j,end) = aa;
end
bboxes{i}(:,end) = allMscores;
% new_scores = beta_scores;
% for j = 1:length(nbrlist{i})
% new_scores(nbrlist{i}{j}) = max(new_scores(nbrlist{i}{j}),...
% beta_scores(nbrlist{i}{j}).*...
% bboxes{i}(nbrlist{i}{j},end));
% end
% bboxes{i}(:,end) = new_scores;
end
end
end
% Clip boxes to image dimensions since VOC testing annotation
% always fall within the image
unclipped_boxes = bboxes;
for i = 1:length(bboxes)
bboxes{i} = clip_to_image(bboxes{i},grid{i}.imbb);
end
final_boxes = bboxes;
% return unclipped boxes for transfers
final.unclipped_boxes = unclipped_boxes;
final.final_boxes = final_boxes;
final.final_maxos = maxos;
%Create a string which summarizes the pooling type
calib_string = '';
if exist('M','var') && ~isempty(M) && isfield(M,'betas')
calib_string = '-calibrated';
end
if exist('M','var') && ~isempty(M) && isfield(M,'betas') && isfield(M,'w')
calib_string = [calib_string '-M'];
end
final.calib_string = calib_string;
%NOTE: is this necessary anymore?
final.imbb = cellfun2(@(x)x.imbb,grid);