-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevalution_segmentaion.py
More file actions
70 lines (53 loc) · 2.59 KB
/
evalution_segmentaion.py
File metadata and controls
70 lines (53 loc) · 2.59 KB
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
from __future__ import division
import numpy as np
import six
def calc_semantic_segmentation_confusion(pred_labels, gt_labels):
pred_labels = iter(pred_labels) # (352, 480)
gt_labels = iter(gt_labels) # (352, 480)
n_class = 4
confusion = np.zeros((n_class, n_class), dtype=np.int64) # (12, 12)
for pred_label, gt_label in six.moves.zip(pred_labels, gt_labels):
if pred_label.ndim != 2 or gt_label.ndim != 2:
raise ValueError('ndim of labels should be two.')
if pred_label.shape != gt_label.shape:
raise ValueError('Shape of ground truth and prediction should'
' be same.')
pred_label = pred_label.flatten() # (168960, )
gt_label = gt_label.flatten() # (168960, )
# Dynamically expand the confusion matrix if necessary.
lb_max = np.max((pred_label, gt_label))
# print(lb_max)
if lb_max >= n_class:
expanded_confusion = np.zeros(
(lb_max + 1, lb_max + 1), dtype=np.int64)
expanded_confusion[0:n_class, 0:n_class] = confusion
n_class = lb_max + 1
confusion = expanded_confusion
# Count statistics from valid pixels. 极度巧妙 × class_nums 正好使得每个ij能够对应.
mask = gt_label >= 0
confusion += np.bincount(
n_class * gt_label[mask].astype(int) + pred_label[mask],
minlength=n_class ** 2)\
.reshape((n_class, n_class))
for iter_ in (pred_labels, gt_labels):
# This code assumes any iterator does not contain None as its items.
if next(iter_, None) is not None:
raise ValueError('Length of input iterables need to be same')
return confusion
def calc_semantic_segmentation_iou(confusion):
iou_denominator = (confusion.sum(axis=1) + confusion.sum(axis=0)
- np.diag(confusion))
iou = np.diag(confusion) / iou_denominator
return iou[:-1]
# return iou
def eval_semantic_segmentation(pred_labels, gt_labels):
confusion = calc_semantic_segmentation_confusion(
pred_labels, gt_labels)
iou = calc_semantic_segmentation_iou(confusion) # (11, )
pixel_accuracy = np.diag(confusion).sum() / confusion.sum()
class_accuracy = np.diag(confusion) / (np.sum(confusion, axis=1) + 1e-10)
return {'iou': iou, 'miou': np.nanmean(iou),
'pixel_accuracy': pixel_accuracy,
'class_accuracy': class_accuracy,
'mean_class_accuracy': np.nanmean(class_accuracy[:-1])}
# 'mean_class_accuracy': np.nanmean(class_accuracy)}