-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathevalAP.m
49 lines (37 loc) · 1.03 KB
/
evalAP.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
function [ ap ] = evalAP( scores, gt_labels )
%EVALAP Average Precision using Score Matrix
% Using a score matrix and the ground truth labels computes the average
% precision used in the Pascal VOC
confidence = scores';
labels = gt_labels;
assert(size(confidence, 1)==length(labels));
remove_idx = labels<0;
confidence(remove_idx, :)=[];
labels(remove_idx) = [];
unique_cls = unique(labels);
ap = zeros(length(unique_cls), 1);
for i=1:length(unique_cls)
cls_idx = (labels==unique_cls(i));
gt = zeros(length(labels), 1);
gt(cls_idx) = 1;
gt(~cls_idx) = -1;
[~,si]=sort(-confidence(:, i));
tp=gt(si)>0;
fp=gt(si)<0;
fp=cumsum(fp);
tp=cumsum(tp);
rec=tp/sum(gt>0);
prec=tp./(fp+tp);
ap(i)=VOCap(rec,prec);
end
end
% from pascal voc
function ap = VOCap(rec,prec)
mrec=[0 ; rec ; 1];
mpre=[0 ; prec ; 0];
for i=numel(mpre)-1:-1:1
mpre(i)=max(mpre(i),mpre(i+1));
end
i=find(mrec(2:end)~=mrec(1:end-1))+1;
ap=sum((mrec(i)-mrec(i-1)).*mpre(i));
end