-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.py
58 lines (50 loc) · 1.18 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
import numpy as np
import torch
import settings
def fast_hist(label_true, label_pred):
n_class = settings.N_CLASSES
mask = (label_true >= 0) & (label_true < n_class)
hist = torch.bincount(
n_class * label_true[mask].int() + label_pred[mask].int(),
minlength=n_class ** 2,
).reshape(n_class, n_class)
return hist
label_names = [
'background',
'aeroplane',
'bicycle',
'bird',
'boat',
'bottle',
'bus',
'car',
'cat',
'chair',
'cow',
'diningtable',
'dog',
'horse',
'motorbike',
'person',
'pottedplant',
'sheep',
'sofa',
'train',
'tvmonitor',
]
def cal_scores(hist):
n_class = settings.N_CLASSES
acc = np.diag(hist).sum() / hist.sum()
acc_cls = np.diag(hist) / hist.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
mean_iu = np.nanmean(iu)
freq = hist.sum(axis=1) / hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
cls_iu = dict(zip(label_names, iu))
return {
'pAcc': acc,
'mAcc': acc_cls,
'fIoU': fwavacc,
'mIoU': mean_iu,
}, cls_iu