-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathtestCoreRank.m
51 lines (41 loc) · 1.5 KB
/
testCoreRank.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
function [rankloss, losses]= testCoreRank(db, qFeat, dbFeat, margin, nNegChoice, varargin)
opts= struct(...
'nTestSample', 1000, ...
'recallNs', [1:5, 10:5:100], ...
'printN', 10 ...
);
opts= vl_argparse(opts, varargin);
assert(~isinf(opts.nTestSample));
rngState= rng;
nQueries= size(qFeat, 2);
if opts.nTestSample < nQueries
rng(43);
toTest= randsample(nQueries, opts.nTestSample);
else
toTest= 1:nQueries;
opts.nTestSample= nQueries;
end
losses= [];
rng(43);
evalProg= tic;
for iTestSample= 1:opts.nTestSample
relja_progress(iTestSample, ...
opts.nTestSample, ...
sprintf('%.4f', mean(losses)), evalProg);
qID= toTest(iTestSample);
potPosIDs= db.nontrivialPosQ(qID);
if isempty(potPosIDs), continue; end
negIDs= db.sampleNegsQ(qID, nNegChoice);
dsSq= sum( bsxfun(@minus, qFeat(:, qID), dbFeat(:, potPosIDs)) .^2, 1 );
dPos= min(dsSq);
dsSq= sum( bsxfun(@minus, qFeat(:, qID), dbFeat(:, negIDs)) .^2, 1 );
losses(end+1)= mean( max(dPos + margin - dsSq, 0) );
end
t= toc(evalProg);
if isempty(losses)
error('No positives in the test');
end
rankloss= mean(losses);
relja_display('\n\tloss= %.4f, margin= %.4f, time= %.4f s, avgTime= %.4f ms\n', rankloss, margin, t, t*1000/length(toTest));
rng(rngState);
end