forked from Julie-tang00/Point-BERT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
144 lines (120 loc) · 4.52 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
# @Author: Haozhe Xie
# @Date: 2019-08-08 14:31:30
# @Last Modified by: Haozhe Xie
# @Last Modified time: 2020-05-25 09:13:32
# @Email: [email protected]
import logging
import open3d
from extensions.chamfer_dist import ChamferDistanceL1, ChamferDistanceL2
class Metrics(object):
ITEMS = [{
'name': 'F-Score',
'enabled': True,
'eval_func': 'cls._get_f_score',
'is_greater_better': True,
'init_value': 0
}, {
'name': 'CDL1',
'enabled': True,
'eval_func': 'cls._get_chamfer_distancel1',
'eval_object': ChamferDistanceL1(ignore_zeros=True),
'is_greater_better': False,
'init_value': 32767
}, {
'name': 'CDL2',
'enabled': True,
'eval_func': 'cls._get_chamfer_distancel2',
'eval_object': ChamferDistanceL2(ignore_zeros=True),
'is_greater_better': False,
'init_value': 32767
}]
@classmethod
def get(cls, pred, gt):
_items = cls.items()
_values = [0] * len(_items)
for i, item in enumerate(_items):
eval_func = eval(item['eval_func'])
_values[i] = eval_func(pred, gt)
return _values
@classmethod
def items(cls):
return [i for i in cls.ITEMS if i['enabled']]
@classmethod
def names(cls):
_items = cls.items()
return [i['name'] for i in _items]
@classmethod
def _get_f_score(cls, pred, gt, th=0.01):
"""References: https://github.com/lmb-freiburg/what3d/blob/master/util.py"""
b = pred.size(0)
assert pred.size(0) == gt.size(0)
if b != 1:
f_score_list = []
for idx in range(b):
f_score_list.append(cls._get_f_score(pred[idx:idx+1], gt[idx:idx+1]))
return sum(f_score_list)/len(f_score_list)
else:
pred = cls._get_open3d_ptcloud(pred)
gt = cls._get_open3d_ptcloud(gt)
dist1 = pred.compute_point_cloud_distance(gt)
dist2 = gt.compute_point_cloud_distance(pred)
recall = float(sum(d < th for d in dist2)) / float(len(dist2))
precision = float(sum(d < th for d in dist1)) / float(len(dist1))
return 2 * recall * precision / (recall + precision) if recall + precision else 0
@classmethod
def _get_open3d_ptcloud(cls, tensor):
"""pred and gt bs is 1"""
tensor = tensor.squeeze().cpu().numpy()
ptcloud = open3d.geometry.PointCloud()
ptcloud.points = open3d.utility.Vector3dVector(tensor)
return ptcloud
@classmethod
def _get_chamfer_distancel1(cls, pred, gt):
chamfer_distance = cls.ITEMS[1]['eval_object']
return chamfer_distance(pred, gt).item() * 1000
@classmethod
def _get_chamfer_distancel2(cls, pred, gt):
chamfer_distance = cls.ITEMS[2]['eval_object']
return chamfer_distance(pred, gt).item() * 1000
def __init__(self, metric_name, values):
self._items = Metrics.items()
self._values = [item['init_value'] for item in self._items]
self.metric_name = metric_name
if type(values).__name__ == 'list':
self._values = values
elif type(values).__name__ == 'dict':
metric_indexes = {}
for idx, item in enumerate(self._items):
item_name = item['name']
metric_indexes[item_name] = idx
for k, v in values.items():
if k not in metric_indexes:
logging.warn('Ignore Metric[Name=%s] due to disability.' % k)
continue
self._values[metric_indexes[k]] = v
else:
raise Exception('Unsupported value type: %s' % type(values))
def state_dict(self):
_dict = dict()
for i in range(len(self._items)):
item = self._items[i]['name']
value = self._values[i]
_dict[item] = value
return _dict
def __repr__(self):
return str(self.state_dict())
def better_than(self, other):
if other is None:
return True
_index = -1
for i, _item in enumerate(self._items):
if _item['name'] == self.metric_name:
_index = i
break
if _index == -1:
raise Exception('Invalid metric name to compare.')
_metric = self._items[i]
_value = self._values[_index]
other_value = other._values[_index]
return _value > other_value if _metric['is_greater_better'] else _value < other_value