二值化图像分割的评价标准的实现

import numpy as np

# 获得混淆矩阵
def BinaryConfusionMatrix(prediction, groundtruth):
    """Computes scores:
    TP = True Positives    真正例
    FP = False Positives   假正例
    FN = False Negatives   假负例
    TN = True Negatives    真负例
    return: TP, FP, FN, TN"""

    TP = np.float(np.sum((prediction == 1) & (groundtruth == 1)))
    FP = np.float(np.sum((prediction == 1) & (groundtruth == 0)))
    FN = np.float(np.sum((prediction == 0) & (groundtruth == 1)))
    TN = np.float(np.sum((prediction == 0) & (groundtruth == 0)))

    return TN, FP, FN,TP

# 精准率和 或 查准率的计算方法
def get_precision(prediction, groundtruth):
    _, FP, _, TP = BinaryConfusionMatrix(prediction, groundtruth)
    precision = float(TP) / (float(TP+FP)+ 1e-6)
    return precision

# 召回率和 或 查全率的计算方法
def get_recall(prediction, groundtruth):
    TN, FP, FN,TP = BinaryConfusionMatrix(prediction, groundtruth)
    recall =  float(TP)/(float(TP+FN) + 1e-6)
    return recall

# 准确率的计算方法
def get_accuracy(prediction, groundtruth):
    TN, FP, FN,TP = BinaryConfusionMatrix(prediction, groundtruth)
    accuracy = float(TP+TN)/(float(TP + FP + FN + TN) + 1e-6)
    return accuracy

def get_sensitivity(prediction, groundtruth):
    return get_recall(prediction, groundtruth)

def get_specificity(prediction, groundtruth):
    TN, FP, FN,TP = BinaryConfusionMatrix(prediction, groundtruth)
    specificity = float(TN)/(float(TN+FP) + 1e-6) 
    return specificity

def get_f1_score(prediction, groundtruth):
    precision = get_precision(prediction, groundtruth)
    recall =  get_recall(prediction, groundtruth)
    f1_score = 2*precision*recall/(precision+recall)
    return f1_score

# Dice相似度系数,计算两个样本的相似度,取值范围为[0, 1], 分割结果最好为1,最坏为0
def get_dice(prediction, groundtruth):
    TN, FP, FN,TP = BinaryConfusionMatrix(prediction, groundtruth)    
    dice = 2 * float(TP)/(float(FP + 2 * TP + FN) + 1e-6)
    return dice

#交并比 一般都是基于类进行计算, 值为1这一类的iou
def get_iou1(prediction, groundtruth):
    TN, FP, FN,TP = BinaryConfusionMatrix(prediction, groundtruth)
    iou = float(TP)/(float(FP + TP + FN) + 1e-6)
    return iou

#交并比 一般都是基于类进行计算, 值为0这一类的iou
def get_iou0(prediction, groundtruth):
    TN, FP, FN,TP = BinaryConfusionMatrix(prediction, groundtruth)
    iou = float(TN)/(float(FP + TN + FN) + 1e-6)
    return iou
# 基于类进行计算的IoU就是将每一类的IoU计算之后累加,再进行平均,得到的就是基于全局的评价
# 平均交并比
def get_mean_iou(prediction, groundtruth):
    iou0 = get_iou0(prediction, groundtruth)
    iou1 = get_iou1(prediction, groundtruth)
    mean_iou = (iou1 + iou0)/2
    return mean_iou

上述纯numpy实现,基于混淆矩阵

1.测试数据

l = np.array([1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0], dtype=np.uint8)
p = np.array([1,0,0,0,1,1,0,0,1,1,1,0,0,1,1,1], dtype=np.uint8)

2.测试混淆矩阵

from sklearn.metrics.classification import confusion_matrix
cm=confusion_matrix(l, p)
print cm
print BinaryConfusionMatrix(p, l)

[[3 1]
 [4 8]]

3.测试准确率

import torch

def accuracy(logit, target):
    logit = torch.from_numpy(logit).float() 
    target = torch.from_numpy(target).float() 
    return (logit.long() == target.long()).float().mean().item()  

print accuracy(p, l)
print get_accuracy(p, l)

0.6875
0.687499957031

 

4,测试精准率,召回率,准确率

from sklearn.metrics import  recall_score,precision_score, accuracy_score
precision =precision_score(l, p)  # doctest: +ELLIPSIS
recall =recall_score(l, p)
accuracy = accuracy_score(l, p)

print '-------------------------------------'
print precision, get_precision(p, l)
print recall, get_recall(p, l)
print accuracy, get_accuracy(p, l)

-------------------------------------
0.8888888888888888 0.888888790123
0.6666666666666666 0.666666611111
0.6875 0.687499957031

5.测试f1_score

from sklearn.metrics import f1_score
print(f1_score(l, p)) 
print get_f1_score(p, l)

0.761904725624
0.7619047732426298

6.测试dicet系数

def dice_coef(output, target):
    smooth = 1e-6
    intersection = (output * target).sum()

    return (2. * intersection + smooth) / \
        (output.sum() + target.sum() + smooth)
print get_dice(p, l)
print dice_coef(p, l)

0.761904725624
0.7619047732426298

7.测试mean_iou

import tensorflow as tf
predicts = tf.placeholder(tf.int32, shape=[None])
label=tf.placeholder(tf.int32, shape=[None])

iou,conf_mat = tf.metrics.mean_iou(label,predicts,2)
op = tf.confusion_matrix(label, predicts, num_classes=2)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer()) 
    sess.run(tf.local_variables_initializer())
    conf_mat=sess.run(conf_mat,feed_dict={label:l,predicts:p})
    mat=sess.run(op,feed_dict={label:l,predicts:p})
    print conf_mat
    print mat
    mean_iou=sess.run(iou,feed_dict={label:l,predicts:p})
    print mean_iou
    
print get_mean_iou(p,l)

0.49519232
0.495192260586

你可能感兴趣的:(二值化图像分割的评价标准的实现)