-
Notifications
You must be signed in to change notification settings - Fork 37
/
metrics.py
129 lines (94 loc) · 3.12 KB
/
metrics.py
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
import numpy as np
from shapely.geometry import Polygon
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def estimateAccuracy(box_a, box_b, dim=3):
if dim == 3:
return np.linalg.norm(box_a.center - box_b.center, ord=2)
elif dim == 2:
return np.linalg.norm(
box_a.center[[0, 2]] - box_b.center[[0, 2]], ord=2)
def fromBoxToPoly(box):
return Polygon(tuple(box.corners()[[0, 2]].T[[0, 1, 5, 4]]))
def estimateOverlap(box_a, box_b, dim=2):
# if box_a == box_b:
# return 1.0
Poly_anno = fromBoxToPoly(box_a)
Poly_subm = fromBoxToPoly(box_b)
box_inter = Poly_anno.intersection(Poly_subm)
box_union = Poly_anno.union(Poly_subm)
if dim == 2:
return box_inter.area / box_union.area
else:
ymax = min(box_a.center[1], box_b.center[1])
ymin = max(box_a.center[1] - box_a.wlh[2],
box_b.center[1] - box_b.wlh[2])
inter_vol = box_inter.area * max(0, ymax - ymin)
anno_vol = box_a.wlh[0] * box_a.wlh[1] * box_a.wlh[2]
subm_vol = box_b.wlh[0] * box_b.wlh[1] * box_b.wlh[2]
overlap = inter_vol * 1.0 / (anno_vol + subm_vol - inter_vol)
return overlap
class Success(object):
"""Computes and stores the Success"""
def __init__(self, n=21, max_overlap=1):
self.max_overlap = max_overlap
self.Xaxis = np.linspace(0, self.max_overlap, n)
self.reset()
def reset(self):
self.overlaps = []
def add_overlap(self, val):
self.overlaps.append(val)
@property
def count(self):
return len(self.overlaps)
@property
def value(self):
succ = [
np.sum(i >= thres
for i in self.overlaps).astype(float) / self.count
for thres in self.Xaxis
]
return np.array(succ)
@property
def average(self):
if len(self.overlaps) == 0:
return 0
return np.trapz(self.value, x=self.Xaxis) * 100 / self.max_overlap
class Precision(object):
"""Computes and stores the Precision"""
def __init__(self, n=21, max_accuracy=2):
self.max_accuracy = max_accuracy
self.Xaxis = np.linspace(0, self.max_accuracy, n)
self.reset()
def reset(self):
self.accuracies = []
def add_accuracy(self, val):
self.accuracies.append(val)
@property
def count(self):
return len(self.accuracies)
@property
def value(self):
prec = [
np.sum(i <= thres
for i in self.accuracies).astype(float) / self.count
for thres in self.Xaxis
]
return np.array(prec)
@property
def average(self):
if len(self.accuracies) == 0:
return 0
return np.trapz(self.value, x=self.Xaxis) * 100 / self.max_accuracy