-
Notifications
You must be signed in to change notification settings - Fork 15
/
metrics.py
76 lines (59 loc) · 2.82 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
import numpy as np
import torch
def rmse(estimate, target):
return np.sqrt(np.mean((estimate - target) ** 2))
def mae(estimate, target):
return np.mean(np.abs(estimate - target))
def absrel(estimate, target):
return np.mean(np.abs(estimate - target) / target)
def inv_rmse(estimate, target):
return np.sqrt(np.mean((1.0/estimate - 1.0/target) ** 2))
def inv_mae(estimate, target):
return np.mean(np.abs(1.0/estimate - 1.0/target))
def inv_absrel(estimate, target):
return np.mean((np.abs(1.0/estimate - 1.0/target)) / (1.0/target))
class ErrorMetrics(object):
def __init__(self):
# initialize by setting to worst values
self.rmse, self.mae, self.absrel = np.inf, np.inf, np.inf
self.inv_rmse, self.inv_mae, self.inv_absrel = np.inf, np.inf, np.inf
def compute(self, estimate, target, valid):
# apply valid masks
estimate = estimate[valid]
target = target[valid]
# estimate and target will be in inverse space, convert to regular
estimate = 1.0/estimate
target = 1.0/target
# depth error, estimate in meters, convert units to mm
self.rmse = rmse(1000.0*estimate, 1000.0*target)
self.mae = mae(1000.0*estimate, 1000.0*target)
self.absrel = absrel(1000.0*estimate, 1000.0*target)
# inverse depth error, estimate in meters, convert units to 1/km
self.inv_rmse = inv_rmse(0.001*estimate, 0.001*target)
self.inv_mae = inv_mae(0.001*estimate, 0.001*target)
self.inv_absrel = inv_absrel(0.001*estimate, 0.001*target)
class ErrorMetricsAverager(object):
def __init__(self):
# initialize avg accumulators to zero
self.rmse_avg, self.mae_avg, self.absrel_avg = 0, 0, 0
self.inv_rmse_avg, self.inv_mae_avg, self.inv_absrel_avg = 0, 0, 0
self.total_count = 0
def accumulate(self, error_metrics):
# adds to accumulators from ErrorMetrics object
assert isinstance(error_metrics, ErrorMetrics)
self.rmse_avg += error_metrics.rmse
self.mae_avg += error_metrics.mae
self.absrel_avg += error_metrics.absrel
self.inv_rmse_avg += error_metrics.inv_rmse
self.inv_mae_avg += error_metrics.inv_mae
self.inv_absrel_avg += error_metrics.inv_absrel
self.total_count += 1
def average(self):
# print(f"Averaging depth metrics over {self.total_count} samples")
self.rmse_avg = self.rmse_avg / self.total_count
self.mae_avg = self.mae_avg / self.total_count
self.absrel_avg = self.absrel_avg / self.total_count
# print(f"Averaging inv depth metrics over {self.total_count} samples")
self.inv_rmse_avg = self.inv_rmse_avg / self.total_count
self.inv_mae_avg = self.inv_mae_avg / self.total_count
self.inv_absrel_avg = self.inv_absrel_avg / self.total_count