e
v
a
l
u
t
i
o
n
−
s
e
g
m
e
n
t
a
i
o
n
evalution-segmentaion
e v a l u t i o n − s e g m e n t a i o n
from __future__ import division
import numpy as np
import six
import major_config
def calc_semantic_segmentation_confusion ( pred_labels, gt_labels) :
pred_labels = iter ( pred_labels)
gt_labels = iter ( gt_labels)
n_class = major_config. num_classes
confusion = np. zeros( ( n_class, n_class) , dtype= np. int64)
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( )
gt_label = gt_label. flatten( )
lb_max = np. max ( ( pred_label, gt_label) )
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
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) :
if next ( iter_, None ) is not None :
raise ValueError( 'Length of input iterables need to be same' )
return confusion
def Pixel_Accuracy ( confusion_matrix) :
Acc = np. diag( confusion_matrix) . sum ( ) / confusion_matrix. sum ( )
return Acc
def Pixel_Accuracy_Class ( confusion_matrix) :
Acc = np. diag( confusion_matrix) / confusion_matrix. sum ( axis= 1 )
Acc = np. nanmean( Acc)
return Acc
def Mean_Intersection_over_Union ( confusion_matrix) :
MIoU = np. diag( confusion_matrix) / (
np. sum ( confusion_matrix, axis= 1 ) + np. sum ( confusion_matrix, axis= 0 ) -
np. diag( confusion_matrix) )
MIoU = np. nanmean( MIoU)
return MIoU
def Frequency_Weighted_Intersection_over_Union ( confusion_matrix) :
freq = np. sum ( confusion_matrix, axis= 1 ) / np. sum ( confusion_matrix)
iu = np. diag( confusion_matrix) / (
np. sum ( confusion_matrix, axis= 1 ) + np. sum ( confusion_matrix, axis= 0 ) -
np. diag( confusion_matrix) )
FWIoU = ( freq[ freq > 0 ] * iu[ freq > 0 ] ) . sum ( )
return FWIoU
def eval_semantic_segmentation ( pred_labels, gt_labels) :
confusion = calc_semantic_segmentation_confusion( pred_labels, gt_labels)
pa = Pixel_Accuracy( confusion)
mpa = Pixel_Accuracy_Class( confusion)
miou = Mean_Intersection_over_Union( confusion)
fwiou = Frequency_Weighted_Intersection_over_Union( confusion)
return {
'pa' : pa,
"mpa" : mpa,
'miou' : miou,
'fwiou' : fwiou,
}