-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathreproduceIjcv05.m
431 lines (358 loc) · 14.4 KB
/
reproduceIjcv05.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
function reproduceIjcv05(varargin)
% REPRODUCEIJCV05 Reproduce results from the IJCV05 article
% REPRODUCEIJCV05('OptionName',OptionValue) computes results presented
% in [1] and stores them as graphs and data files (*.mat + *.csv).
%
% This function does not reproduce figures which require tuning of
% detector parameters (21c, 22a and 22b) as some of the available
% binaries does not allow to affect a number of detected frames.
%
% Supported Options:
%
% ResultsDir:: 'ijcv05_res'
% Path where to store computed data.
%
% UseIjcvOriginalBenchmark:: true
% Compute the results with the original IJCV05 code. When true, the
% test takes several minutes.
%
% REFERENCES
% [1] K. Mikolajczyk, T. Tuytelaars, C. Schmid, A. Zisserman,
% J. Matas, F. Schaffalitzky, T. Kadir, and L. Van Gool. A
% comparison of affine region detectors. IJCV, 1(65):43–72, 2005.
% AUTORIGHTS
import datasets.*;
import localFeatures.*;
import benchmarks.*;
opts.resultsDir = 'ijcv05_res'; % Directory to store generated files
opts.useIjcvOriginalBenchmark = true; % Set false if you want to skip KM benchm.
opts = helpers.vl_argparse(opts, varargin);
%% Define Local features extractors
% Create local features extractor such that each of them uses the same
% algorithm and parameters for computing SIFT descriptors.
descDet = VggDescriptor('CropFrames',true,'Magnification',3); % Descriptor calc.
detectors{1} = DescriptorAdapter(...
VggAffine('Detector','haraff','Threshold',1000), descDet);
detectors{2} = DescriptorAdapter(...
VggAffine('Detector','hesaff','Threshold',500), descDet);
detectors{3} = DescriptorAdapter(VggMser('es',1),descDet);
detectors{4} = DescriptorAdapter(Ibr('ScaleFactor',1),descDet);
detectors{5} = DescriptorAdapter(Ebr(),descDet);
detNames = {'Harris-Affine','Hessian-Affine','MSER','IBR','EBR'};
numDetectors = numel(detectors);
%% Define benchmarks
repBenchmark = RepeatabilityBenchmark('Mode','Repeatability');
matchBenchmark = RepeatabilityBenchmark('Mode','MatchingScore');
kmBenchmark = IjcvOriginalBenchmark('CommonPart',1);
%% Define Figure
fig = figure('Visible','off');
detColorMap = hsv(numDetectors);
%% Repeatability vs. overlap error
fprintf('\n######## REPEATABILITY VS. OVERLAP ERR (Fig. 21a) #######\n');
dataset = VggAffineDataset('category','graf');
overlapErrValues = 0.1:0.1:0.6;
imageBIdx = 4;
oeScores = zeros(numDetectors,numel(overlapErrValues));
confFig(fig);
for oei = 1:numel(overlapErrValues)
rBenchm = RepeatabilityBenchmark('Mode','Repeatability',...
'OverlapError',overlapErrValues(oei));
imageAPath = dataset.getImagePath(1);
imageBPath = dataset.getImagePath(imageBIdx);
H = dataset.getTransformation(imageBIdx);
parfor detectorIdx = 1:numDetectors
detector = detectors{detectorIdx};
[oeScores(detectorIdx,oei) tmp] = ...
rBenchm.testFeatureExtractor(detector, H, imageAPath,imageBPath);
end
end
saveResults(oeScores, fullfile(opts.resultsDir,'rep_vs_overlap'));
subplot(2,2,1);
plot(overlapErrValues.*100,oeScores.*100,'+-'); grid on;
xlabel('Overlap error %'); ylabel('Repeatability %');
axis([5 65 0 100]);
legend(detNames,'Location','NorthWest');
%% Repeatability vs. normalised region size
fprintf('\n######## REPEATABILITY VS. NORM. REG. SIZE (Fig. 21b) #######\n');
normRegSizes = [15 30 50 75 90 110];
nrsScores = zeros(numDetectors,numel(normRegSizes));
for nrsi = 1:numel(normRegSizes)
rBenchm = RepeatabilityBenchmark('Mode','Repeatability',...
'NormalisedScale',normRegSizes(nrsi));
imageAPath = dataset.getImagePath(1);
imageBPath = dataset.getImagePath(imageBIdx);
H = dataset.getTransformation(imageBIdx);
parfor detectorIdx = 1:numDetectors
detector = detectors{detectorIdx};
nrsScores(detectorIdx,nrsi) = ...
rBenchm.testFeatureExtractor(detector, H, imageAPath,imageBPath);
end
end
saveResults(oeScores, fullfile(opts.resultsDir,'rep_vs_norm_reg_size'));
subplot(2,2,2);
plot(normRegSizes,nrsScores.*100,'+-'); grid on;
xlabel('Normalised region size'); ylabel('Repeatability %');
axis([10 120 0 100]);
legend(detNames,'Location','SouthEast');
%% Repeatability vs. region sizes
fprintf('\n######## REPEATABILITY VS. REGION SIZE (Fig. 21d) #######\n');
regSizeBenchm = RepeatabilityBenchmark('Mode','Repeatability',...
'Magnification',1);
numBins = 10;
imageBIdx = 4;
dataset = VggAffineDataset('category','graf');
rsScores = zeros(numDetectors,numBins);
binAvgs = zeros(numDetectors,numBins); % Centres of frame scales bins
numFramesInBin = zeros(numDetectors,numBins);
framesA = cell(numDetectors,1);
framesB = cell(numDetectors,1);
imageAPath = dataset.getImagePath(1);
imageBPath = dataset.getImagePath(imageBIdx);
imageASize = helpers.imageSize(imageAPath);
imageBSize = helpers.imageSize(imageBPath);
H = dataset.getTransformation(imageBIdx);
% Detect the frames
parfor di = 1:numDetectors
detector = detectors{di};
framesA{di} = detector.extractFeatures(imageAPath);
framesB{di} = detector.extractFeatures(imageBPath);
end
subplot(2,2,4); hold on;
% Process the results
for di = 1:numDetectors
% Divide the frames based on scales into equaly distributed ones
scalesA = localFeatures.helpers.getFrameScale(framesA{di});
binA = ceil(numBins * tiedrank(scalesA) / length(scalesA));
for nrsi = 1:numBins
sFramesA = framesA{di}(:,binA == nrsi);
sFramesB = framesB{di};
numFramesInBin(di,nrsi) = size(sFramesA,2);
binAvgs(di,nrsi) = mean(scalesA(binA == nrsi));
rsScores(di,nrsi)= rsScores(di,nrsi) +...
regSizeBenchm.testFeatures(H, imageASize, imageBSize, ...
sFramesA,sFramesB);
end
plot(binAvgs(di,:),rsScores(di,:).*100,'+-',...
'Color',detColorMap(di,:));
end
saveResults(rsScores, fullfile(opts.resultsDir,'rep_vs_reg_size'));
grid on;
xlabel('Region size'); ylabel('Repeatability %');
axis([0 max(binAvgs(:)) 0 100]);
legend(detNames,'Location','SouthEast');
% Plot the number of frames per bin for each detector
subplot(2,2,3);
bar(mean(numFramesInBin,2));
set(gca,'XTick',1:numDetectors)
set(gca,'XTickLabel',detNames);
ylabel('Number of frames per region size bin');
print(fig,fullfile(opts.resultsDir, 'fig_rep_graf.eps'),'-depsc');
%% Matching vs. magnification factor
fprintf('\n######## MATCHING SCORE VS. REGION MAGNIF. (Fig. 22c) #######\n');
dataset = VggAffineDataset('category','graf');
imageBIdx = 4;
magFactors = 1:5;
magnifScores = zeros(numDetectors,numel(magFactors));
confFig(fig);
for mf = 1:numel(magFactors)
magFactor = magFactors(mf);
imageAPath = dataset.getImagePath(1);
imageBPath = dataset.getImagePath(imageBIdx);
H = dataset.getTransformation(imageBIdx);
for detectorIdx = 1:numDetectors
descrExtr = VggDescriptor('Magnification',magFactor);
detector = DescriptorAdapter(detectors{detectorIdx},descrExtr);
magnifScores(detectorIdx,mf) = ...
matchBenchmark.testFeatureExtractor(detector, H, imageAPath,imageBPath);
end
end
saveResults(magnifScores, fullfile(opts.resultsDir,'matching_vs_mag'));
subplot(2,2,4);
plot(magFactors,magnifScores'.*100,'+-'); grid on;
xlabel('Magnification factor'); ylabel('Matching %');
axis([0.5 5.5 0 100]);
legend(detNames,'Location','NorthEast');
print(fig,fullfile(opts.resultsDir, 'fig_matching_graf.eps'),'-depsc');
%% Regions sizes histograms
fprintf('\n######## REGION SIZE HISTOGRAMS (Fig. 10) #######\n');
if (0)
dataset = VggAffineDataset('category','graf');
refImgPath = dataset.getImagePath(1);
numFrames = zeros(numDetectors,1);
runTime = zeros(numDetectors,1);
detFrames = cell(1,numDetectors);
confFig(fig);
% Detect the frames
parfor di = 1:numDetectors
% Disable caching in order to force computation
detectors{di}.disableCaching();
startTime = tic;
detFrames{di} = detectors{di}.extractFeatures(refImgPath);
runTime(di) = toc(startTime);
detectors{di}.enableCaching();
end
% Process the results
for di = 1:numDetectors
numFrames(di) = size(detFrames{di},2);
scales = getFrameScale(detFrames{di});
subplot(2,3,di);
scalesHist = hist(scales,0:100);
bar(scalesHist);
axis([0 100 0 ceil(max(scalesHist)/10)*10]);
grid on;
title(detNames{di});
xlabel('Average region size');
ylabel('Number of detected regions');
end
saveResults(runTime, fullfile(opts.resultsDir,'det_run_time_graf_img1ppm'));
saveResults(numFrames, fullfile(opts.resultsDir,'det_num_frames_graf_img1ppm'));
print(fig,fullfile(opts.resultsDir, 'fig_hist_graf.eps'),'-depsc');
end
%% Repeatability and Matching scores
fprintf('\n######## REPEATABILITY AND MATCHING SCORES (Fig. 13-20) #######\n');
datasetNum = 1;
categories = VggAffineDataset.AllCategories;
for category=categories
fprintf('\n######## TESTING DATASET %s #######\n',category{:});
dataset = VggAffineDataset('category',category{:});
%% Run the new benchmarks in parallel
numImages = dataset.NumImages;
repeatability = zeros(numDetectors, numImages);
numCorresp = zeros(numDetectors, numImages);
matchingScore = zeros(numDetectors, numImages);
numMatches = zeros(numDetectors, numImages);
% Test all detectors
for di = 1:numDetectors
detector = detectors{di};
imageAPath = dataset.getImagePath(1);
parfor imageIdx = 2:numImages
imageBPath = dataset.getImagePath(imageIdx);
H = dataset.getTransformation(imageIdx);
[repeatability(di,imageIdx) numCorresp(di,imageIdx)] = ...
repBenchmark.testFeatureExtractor(detector, H, imageAPath,imageBPath);
[matchingScore(di,imageIdx) numMatches(di,imageIdx)] = ...
matchBenchmark.testFeatureExtractor(detector, H, imageAPath,imageBPath);
end
end
%% Show scores
confFig(fig);
titleText = ['Detectors Repeatability [%%] (',category{:},')'];
printScores(repeatability.*100, detNames, titleText,...
fullfile(opts.resultsDir,[category{:} '_rep']));
subplot(2,2,1); plotScores(repeatability.*100, detNames, dataset,...
titleText);
printScores(repeatability.*100, detNames, titleText,...
fullfile(opts.resultsDir,[category{:} '_rep']));
subplot(2,2,1); plotScores(repeatability.*100, detNames,...
dataset, titleText);
titleText = ['Detectors Num. Correspondences (',category{:},')'];
printScores(numCorresp, detNames, titleText,...
fullfile(opts.resultsDir,[category{:} '_ncorresp']));
subplot(2,2,2); plotScores(numCorresp, detNames, dataset, titleText);
titleText = ['Detectors Matching Score [%%] (',category{:},')'];
printScores(matchingScore.*100, detNames, titleText,...
fullfile(opts.resultsDir,[category{:} '_matching']));
subplot(2,2,3); plotScores(matchingScore.*100, detNames, dataset,...
titleText);
titleText = ['Detectors Num. Matches (',category{:},')'];
printScores(numMatches, detNames, titleText,...
fullfile(opts.resultsDir,[category{:} '_nmatches']));
subplot(2,2,4); plotScores(numMatches, detNames, dataset, titleText);
print(fig,fullfile(opts.resultsDir, ['fig' num2str(datasetNum) '_rm_' ...
dataset.Category '.eps']),'-depsc');
%% For comparison, run IJCV05 original Benchmark
if opts.useIjcvOriginalBenchmark
% Test all detectors
for di = 1:numDetectors
detector = detectors{di};
imageAPath = dataset.getImagePath(1);
parfor imageIdx = 2:numImages
imageBPath = dataset.getImagePath(imageIdx);
H = dataset.getTransformation(imageIdx);
% Repeatability must be computed separately in order to be able to
% compare it with the previous results as the number of frames when
% computed with and without descriptors differs.
[repeatability(di,imageIdx) numCorresp(di,imageIdx)] = ...
kmBenchmark.testFeatureExtractor(detector, H, imageAPath,imageBPath);
[tmp tmp2 matchingScore(di,imageIdx) numMatches(di,imageIdx)] = ...
kmBenchmark.testFeatureExtractor(detector, H, imageAPath,imageBPath);
end
end
confFig(fig);
titleText = 'Detectors Repeatability [%%]';
printScores(repeatability.*100, detNames, titleText,...
fullfile(opts.resultsDir,['km_' category{:} '_rep']));
subplot(2,2,1); plotScores(repeatability.*100, detNames, dataset,...
titleText);
titleText = ['KM Detectors Num. Correspondences (',category{:},')'];
printScores(numCorresp, detNames, titleText,...
fullfile(opts.resultsDir,['km_' category{:} '_ncorresp']));
subplot(2,2,2); plotScores(numCorresp, detNames, dataset, titleText);
titleText = ['KM Detectors Matching Score [%%] (',category{:},')'];
printScores(matchingScore.*100, detNames, titleText,...
fullfile(opts.resultsDir,['km_' category{:} '_matching']));
subplot(2,2,3); plotScores(matchingScore.*100, detNames, dataset, ...
titleText);
titleText = ['KM Detectors Num. Matches (',category{:},')'];
printScores(numMatches, detNames, titleText,...
fullfile(opts.resultsDir,['km_' category{:} '_nmatches']));
subplot(2,2,4); plotScores(numMatches, detNames, dataset, titleText);
print(fig,fullfile(opts.resultsDir, ['km_fig' num2str(datasetNum) '_rm_' ...
dataset.Category '.eps']),'-depsc');
end
datasetNum = datasetNum + 1;
end
%% Helper functions
function printScores(scores, scoreLineNames, name, fileName)
numScores = size(scores,1);
maxNameLen = 0;
for k = 1:numScores
maxNameLen = max(maxNameLen,length(scoreLineNames{k}));
end
maxNameLen = max(length('Method name'),maxNameLen);
fprintf(['\n', name,':\n']);
formatString = ['%' sprintf('%d',maxNameLen) 's:'];
fprintf(formatString,'Method name');
for k = 1:size(scores,2)
fprintf('\tImg#%02d',k);
end
fprintf('\n');
for k = 1:numScores
fprintf(formatString,scoreLineNames{k});
for l = 2:size(scores,2)
fprintf('\t%6s',sprintf('%.2f',scores(k,l)));
end
fprintf('\n');
end
if exist('fileName','var');
saveResults(scores,fileName);
end
end
function saveResults(scores, fileName)
[dir name] = fileparts(fileName);
vl_xmkdir(fullfile(pwd,dir,''));
save(fullfile(dir,name),'scores');
csvwrite(fullfile(dir, [name '.csv']), scores);
end
function plotScores(scores, detNames, dataset, titleText)
% PLOTSCORES
import helpres.*;
titleText = sprintf(titleText);
xLabel = dataset.ImageNamesLabel;
xVals = dataset.ImageNames;
plot(xVals,scores(:,2:6)','+-','linewidth', 1) ; hold on ;
ylabel(titleText) ;
xlabel(xLabel);
title(titleText);
maxScore = ceil(max([max(max(scores)) 100])/10)*10;
legend(detNames,'Location','NorthEast');
grid on ;
axis([min(xVals)*0.9 max(xVals)*1.05 0 maxScore]);
end
function confFig(fig)
clf(fig);
set(fig,'PaperPositionMode','auto')
set(fig,'PaperType','A4');
set(fig, 'Position', [0, 0, 900,700]);
end
end