分割常用评价指标及代码

分割常用评价指标及代码

来源:知乎
来源标题:分割常用评价指标及代码
来源链接:https://zhuanlan.zhihu.com/p/117435908?from_voters_page=true
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Dice

对于分割过程中的评价标准主要采用Dice相似系数(Dice Similariy Coefficient,DSC),Dice系数是一种集合相似度度量指标,通常用于计算两个样本的相似度,值的范围 [公式] ,分割结果最好时值为 [公式] ,最差时值为 [公式]

[公式]

img
def dice_coef(output, target):#output为预测结果 target为真实结果
    smooth = 1e-5 #防止0除

    if torch.is_tensor(output):
        output = torch.sigmoid(output).data.cpu().numpy()
    if torch.is_tensor(target):
        target = target.data.cpu().numpy()

    intersection = (output * target).sum()

    return (2. * intersection + smooth) / \
        (output.sum() + target.sum() + smooth)

IOU

def iou_score(output, target):
    smooth = 1e-5

    if torch.is_tensor(output):
        output = torch.sigmoid(output).data.cpu().numpy()
    if torch.is_tensor(target):
        target = target.data.cpu().numpy()
    output_ = output > 0.5
    target_ = target > 0.5
    intersection = (output_ & target_).sum()
    union = (output_ | target_).sum()

    return (intersection + smooth) / (union + smooth)

Sensitivity

\[\text { Sensitivity }=\frac{T P}{T P+F N} \]

def sensitivity(output, target):
    smooth = 1e-5

    if torch.is_tensor(output):
        output = torch.sigmoid(output).data.cpu().numpy()
    if torch.is_tensor(target):
        target = target.data.cpu().numpy()

    intersection = (output * target).sum()

    return (intersection + smooth) / (target.sum() + smooth)

PPV

\[P P V=\frac{T P}{T P+F P} \]

def ppv(output, target):
    smooth = 1e-5

    if torch.is_tensor(output):
        output = torch.sigmoid(output).data.cpu().numpy()
    if torch.is_tensor(target):
        target = target.data.cpu().numpy()

    intersection = (output * target).sum()

    return (intersection + smooth) / (output.sum() + smooth)

Hausdorff_95 (95% HD)

Dice对mask的内部填充比较敏感,而hausdorff distance 对分割出的边界比较敏感。

\[d_{H}(X, Y)=\max \left\{d_{X Y}, d_{Y X}\right]=\max \left\{\max _{x \in X} \min _{y \in Y} d(x, y), \max _{y \in Y} \min _{x \in X} d(x, y)\right\} \]

Hausdorff_95就是是最后的值乘以95%,目的是为了消除离群值的一个非常小的子集的影响。

  • 环境安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numba
pip install hausdorff
import numpy as np
from hausdorff import hausdorff_distance

# two random 2D arrays (second dimension must match)
np.random.seed(0)
X = np.random.random((1000,100))
Y = np.random.random((5000,100))

# Test computation of Hausdorff distance with different base distances
print("Hausdorff distance test: {0}".format( hausdorff_distance(X, Y, distance="manhattan") ))
print("Hausdorff distance test: {0}".format( hausdorff_distance(X, Y, distance="euclidean") ))
print("Hausdorff distance test: {0}".format( hausdorff_distance(X, Y, distance="chebyshev") ))
print("Hausdorff distance test: {0}".format( hausdorff_distance(X, Y, distance="cosine") ))

# For haversine, use 2D lat, lng coordinates
def rand_lat_lng(N):
    lats = np.random.uniform(-90, 90, N)
    lngs = np.random.uniform(-180, 180, N)
    return np.stack([lats, lngs], axis=-1)
        
X = rand_lat_lng(100)
Y = rand_lat_lng(250)
print("Hausdorff haversine test: {0}".format( hausdorff_distance(X, Y, distance="haversine") ))
posted @ 2022-03-03 15:04  梁君牧  阅读(676)  评论(0编辑  收藏  举报