-
Notifications
You must be signed in to change notification settings - Fork 4
/
retrieval.m
92 lines (73 loc) · 2.36 KB
/
retrieval.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
function [info] = retrieval(q,gt_q,retriev,gt_r,performancemeasure);
%
% [info] = retrieval(quer,gt_q,retr,gt_r,opt)
%
% q : query set (one sample per row)
% gt_q : groundtruth for the query set
%
% retriev : retrieval set (one sample per row)
% gt_r : groundtruth for the retrieval set
%
%performancemeasure = 'PrecisionatK' / 'NDCGatK'
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X = q;
Y = retriev;
% DIST =1-pdist2(X,Y,'euclidean');
% DIST = X*Y.';
DIST =1-pdist2(X,Y,'hamming');
DIST(isnan(DIST)) = -1000000000;
if(strcmp(performancemeasure,'PrecisionatK'))
pk = [];
tp = 0;
fp = 0;
K = 10;
for query = 1:size(X,1)
idx = gt_q(query,:);
d = DIST(query,:);
[c2,id2]=sort(d,'descend'); %descend as cosine similarity
for i=1:K
idy = gt_r(id2(i),:);
for subquery = 1:size(idx,2)
if(idy(1,subquery)>0)
if(idx(1,subquery)>0)
tp = tp + 1;
end
if(idx(1,subquery)==0)
fp = fp + 1;
end
end
end
end
end
PK = tp/(tp+fp);
info.result= PK;
end
if(strcmp(performancemeasure,'NDCGatK'))
K = 30;
catte1 = gt_r;
NDCG = [];
%***********************************************
D = 1-pdist2(catte1,catte1,'cosine');
%***********************************************
for query = 1:size(X,1)
idx = gt_q(query,:);
if(nnz(idx)>0)
d = DIST(query,:);
[c2,id2]=sort(d,'descend'); %descend as cosine similarity
%***********************************************
% d1 = 1-pdist2(catte1(query,:),catte1,'cosine');
d1 = D(query,:);
%***********************************************
d2 = sort(d1,'descend');
dcg = d1(id2(1));
idcg = d2(1);
for i=2:K
dcg = dcg+(d1(id2(i))/log2(i));
idcg = idcg + (d2(i)/log2(i));
end
NDCG = vertcat(NDCG,dcg/idcg);
end
end
info.result= mean(NDCG);
end