-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
85 lines (58 loc) · 2.09 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
import numpy as np
import numpy.linalg as LA
class BaseMetric:
def __init__(self):
self.name = self.__class__.__name__
@staticmethod
def _check_input(X, Xref):
assert X.shape == Xref.shape
assert type(X) == type(Xref)
return X, Xref
def __call__(self, X, Xref):
raise NotImplementedError
def __repr__(self):
return f"{self.name}"
class MeanAbsoluteError(BaseMetric):
def __init__(self):
super().__init__()
def __call__(self, E, Eref):
E, Eref = self._check_input(E, Eref)
normE = LA.norm(E, axis=0, keepdims=True)
normEref = LA.norm(Eref, axis=0, keepdims=True)
return 100 * (1 - np.abs((E / normE).T @ (Eref / normEref)))
class SpectralAngleDistance(BaseMetric):
def __init__(self):
super().__init__()
def __call__(self, E, Eref):
E, Eref = self._check_input(E, Eref)
normE = LA.norm(E, axis=0, keepdims=True)
normEref = LA.norm(Eref, axis=0, keepdims=True)
return np.arccos((E / normE).T @ (Eref / normEref))
class SADDegrees(SpectralAngleDistance):
def __init__(self):
super().__init__()
def __call__(self, E, Eref):
tmp = super().__call__(E, Eref)
return (np.diag(tmp) * (180 / np.pi)).mean()
class SRE(BaseMetric):
def __init__(self):
super().__init__()
def __call__(self, X, Xref):
X, Xref = self._check_input(X, Xref)
return 20 * np.log10(
LA.norm(Xref, "fro") / LA.norm((Xref - np.clip(X, 0, 1)), "fro")
)
class MeanSquareError(BaseMetric):
def __init__(self):
super().__init__()
def __call__(self, E, Eref):
E, Eref = self._check_input(E, Eref)
normE = LA.norm(E, axis=0, keepdims=True)
normEref = LA.norm(Eref, axis=0, keepdims=True)
return np.sqrt(normE.T ** 2 + normEref ** 2 - 2 * (E.T @ Eref))
class aRMSE(BaseMetric):
def __init__(self):
super().__init__()
def __call__(self, A, Aref):
A, Aref = self._check_input(A, Aref)
return 100 * np.sqrt(((A - Aref) ** 2).mean())