-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.m
executable file
·55 lines (47 loc) · 1.23 KB
/
evaluate.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
function results = evaluate(Z, Y)
P = size(Z,2);
ygt = Y;
% getting the predicted classes and the associated confidence
% [~, ygt] = max(Y, [], 2);
[s, z] = max(Z, [], 2);
[s, idx] = sort(s, 'descend');
z = z(idx, :);
ygt = ygt(idx, :);
% computing the accuracy
acc = sum(z==ygt)/length(z);
% computing precision and recall
p = zeros(length(s), 1);
r = zeros(length(s), 1);
eval = ones(length(ygt), 1);
for j = 1:length(s)
p(j) = sum(z(1:j) == ygt(1:j) & eval(1:j)) / j;
r(j) = sum(eval(1:j)) / sum(eval);
end
% computing the average precision
ap = 0;
for i = 2:length(r)
ap = ap + (p(i)+p(i-1)) * (r(i)-r(i-1)) / 2;
end
% computing the per-class performance
class_p = cell(P, 1);
class_r = cell(P, 1);
class_ap = zeros(P, 1);
class_acc = zeros(P, 1);
for i = 1:P
[class_r{i}, class_p{i}, class_ap(i)] = binary_pr(Z(:,i), Y==i);
idx = ygt==i;
class_acc(i) = sum(z(idx) == ygt(idx)) / sum(idx);
end
% storing everything in a structure
results = struct();
results.ap = ap;
results.p = p;
results.r = r;
results.class_r = class_r;
results.class_p = class_p;
results.class_ap = class_ap;
results.Z = Z;
results.Y = Y;
results.acc = acc;
results.class_acc = class_acc;
end