forked from quantombone/exemplarsvm
/
esvm_estimate_M.m
212 lines (169 loc) · 4.95 KB
/
esvm_estimate_M.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
function M = esvm_estimate_M(grid, models, params, ...
CACHE_FILES)
% Given a bunch of detections, learn the M boosting matrix, which
% makes a final boxes's score depend on the co-occurrence of certain
% "friendly" detections
%
% 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
neighbor_thresh = params.calibration_neighbor_thresh;
count_thresh = params.calibration_count_thresh;
if ~exist('CACHE_FILES','var')
CACHE_FILES = 0;
end
final_dir = ...
sprintf('%s/models',params.dataset_params.localdir);
final_file = ...
sprintf('%s/%s-M.mat',...
final_dir, models{1}.models_name);
if CACHE_FILES == 1
lockfile = [final_file '.lock'];
if fileexists(final_file) || (mymkdir_dist(lockfile)==0)
%wait until lockfiles are gone
wait_until_all_present({lockfile},5,1);
fprintf(1,'Loading final file %s\n',final_file);
res = load_keep_trying(final_file);
M = res.M;
return;
end
end
if length(grid) == 0
error(sprintf('Found no images of type %s\n',results_directory))
end
%REMOVE FIRINGS ON SELF-IMAGE (these create artificially high scores)
REMOVE_SELF = 1;
cls = models{1}.cls;
excurids = cellfun2(@(x)x.curid,models);
boxes = cell(1,length(grid));
maxos = cell(1,length(grid));
if REMOVE_SELF == 0
fprintf(1,'Warning: Not removing self-hits\n');
end
curcls = models{1}.cls;
fprintf(1,' -Computing Box Features:');
starter=tic;
for i = 1:length(grid)
curid = grid{i}.curid;
boxes{i} = grid{i}.bboxes;
if size(boxes{i},1) == 0
if length(grid{i}.extras)>0
maxos{i} = [];
end
continue
end
%old-method: use raw SVM scores + 1 (works better!) NOTE: this
%works better than using calibrated scores (doesn't work too well)
calib_boxes = boxes{i};
calib_boxes(:,end) = calib_boxes(:,end)+1;
%Threshold at the target value specified in parameters
oks = find(calib_boxes(:,end) >= params.calibration_threshold);
boxes{i} = calib_boxes(oks,:);
if length(grid{i}.extras)>0
maxos{i} = grid{i}.extras.maxos;
maxos{i}(find(ismember(grid{i}.extras.maxclass,curcls)==0)) = 0;
maxos{i} = maxos{i}(oks);
else
maxos{i} = zeros(size(boxes{i},1),1);
end
if REMOVE_SELF == 1
%% remove self from this detection image!!! LOO stuff!
%fprintf(1,'hack not removing self!\n');
badex = find(ismember(excurids,curid));
badones = ismember(boxes{i}(:,6),badex);
boxes{i}(badones,:) = [];
if length(maxos{i})>0
maxos{i}(badones) = [];
end
end
end
lens = cellfun(@(x)size(x,1),boxes);
boxes(lens==0) = [];
maxos(lens==0) = [];
K = length(models);
N = sum(cellfun(@(x)size(x,2),maxos));
y = cat(1,maxos{:});
os = cat(1,maxos{:})';
scores = cellfun2(@(x)x(:,end)',boxes);
scores = [scores{:}];
xraw = cell(length(boxes),1);
allboxes = cat(1,boxes{:});
for i = 1:length(boxes)
fprintf(1,'.');
xraw{i} = esvm_get_M_features(boxes{i}, K, neighbor_thresh);
end
x = [xraw{:}];
exids = allboxes(:,6);
exids(allboxes(:,7)==1)= exids(allboxes(:,7)==1) + length(models);
imids = allboxes(:,5);
fprintf(1,'took %.3fsec\n',toc(starter));
fprintf(1,' -Learning M by counting: ');
starter=tic;
%This one works best so far
M = learn_M_counting(x, exids, os, count_thresh);
fprintf(1,'took %.3fsec\n',toc(starter));
M.neighbor_thresh = neighbor_thresh;
M.count_thresh = count_thresh;
r = cell(length(xraw),1);
fprintf(1,' -Applying M to %d images: ',length(xraw));
starter=tic;
for j = 1:length(xraw)
r{j} = esvm_apply_M(xraw{j},boxes{j},M);
end
r = [r{:}];
[aa,bb] = sort(r,'descend');
goods = os>.5;
res = (cumsum(goods(bb))./(1:length(bb)));
M.score = mean(res);
fprintf(1,'took %.3fsec\n',toc(starter));
if params.dataset_params.display == 1
figure(4)
subplot(1,2,1)
plot(scores,os,'r.')
xlabel('Detection Score')
ylabel('OS wrt gt')
subplot(1,2,2)
plot(r,os,'r.')
xlabel('Detection Score')
ylabel('OS wrt gt')
title('w/ M-matrx')
drawnow
snapnow
figure(5)
clf
[aa,bb] = sort(scores,'descend');
plot(cumsum(os(bb)>.5)./(1:length(os)),'r-','LineWidth',3)
hold on;
[aa,bb] = sort(r,'descend');
plot(cumsum(os(bb)>.5)./(1:length(os)),'b--','LineWidth',3)
xlabel('#instances Recalled')
ylabel('Precision')
title('M-matrix estimation Precision-Recall');
legend('no matrix','matrix')
drawnow
snapnow
end
if CACHE_FILES == 1
fprintf(1,'Computed M, saving to %s\n',final_file);
save(final_file,'M');
rmdir(lockfile);
end
function M = learn_M_counting(x, exids, os, count_thresh)
%Learn the matrix by counting activations on positives
N = size(x,2);
K = size(x,1);
C = zeros(K,K);
for i = 1:N
cur = find(x(:,i)>0);
%old way: works better!
C(cur,exids(i)) = C(cur,exids(i)) + os(i)*(os(i) >= count_thresh) / ...
length(cur);
end
for i = 1:K
M.w{i} = C(:,i);
M.b{i} = 0;
end
M.C = sparse(C);