-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.py
61 lines (49 loc) · 1.69 KB
/
metric.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
import numpy as np
class Metric:
def __init__(self, num_classes, ignore_indexes=[]):
self.num_classes = num_classes
self.ignore_indexes = ignore_indexes
self.matrix = np.zeros((self.num_classes, self.num_classes))
self.count = 0
def reset(self):
self.matrix.fill(0)
self.count = 0
def add(self, pred, label):
mask = (label >= 0) & (label < self.num_classes)
for ignore_index in self.ignore_indexes:
mask &= (label != ignore_index)
count = np.bincount(self.num_classes * label[mask] + pred[mask], minlength=self.num_classes ** 2)
self.matrix += count.reshape((self.num_classes, self.num_classes))
if len(pred.shape) >= 1:
self.count += pred.shape[0]
else:
self.count += 1
def PA(self):
# pixel accuracy
acc = np.diag(self.matrix).sum() / self.matrix.sum()
return acc
def mPA(self):
# mean PA
accs = self.Ps()
acc = accs.mean()
return acc
def Ps(self):
# precision of each class
accs = np.diag(self.matrix) / self.matrix.sum(axis=0)
return accs
def Rs(self):
# recall of each class
rs = np.diag(self.matrix) / self.matrix.sum(axis=1) # 沿着1求和,相当于excel求到最右侧的单元格
return rs
def F1s(self):
# F1 score
ps = self.Ps()
rs = self.Ps()
f1s = 2 * ps * rs / (ps + rs)
return f1s
def KC(self):
# Kappa Coefficient
p0 = self.PA()
pe = np.sum(np.diag(self.matrix) * self.matrix.sum(axis=1)) / np.sum(self.matrix)**2
kc = (p0 - pe) / (1 - pe)
return kc