常用分割评价指标Dice、Hausdorff_95、IOU、PPV等 + Python实现

v2-b673c4420f19e080eef70431cb9f38ac_b.jpg

 

v2-90ff0e56b01f771d251ef0bb4a3f26a4_b.jpg

TP(紫色部分)

TN:True Negative,被判定为负样本,事实上也是负样本,即红色与蓝色以外区域

v2-3ab5cc3d743399d3b302b0890b680bd0_b.jpg

TN(紫色部分)

FP:False Positive,被判定为正样本,但事实上是负样本,即红色中除了蓝色部分

v2-2f59348c83010896c1557128fe5519e9_b.jpg

FP(紫色部分)

FN:False Negative,被判定为负样本,但事实上是正样本,即蓝色中除了红色部分

v2-b125fb40406f70fdc6c1884914118b21_b.jpg

FN(紫色部分)

Dice

v2-5b6c0386a3fd6eb4e22a6360235a58d8_b.jpg

Pytorch 代码示例

        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

Pytorch 代码示例

        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

pytorch 代码示例

        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

Pytorch 代码示例

        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 对分割出的边界比较敏感。

v2-ae141681a17261b5eb73d4524277e8ab_b.png

v2-0f431a4c9e8cf45575d953e2b72fec49_b.jpg

下面8张图能够很清晰讲解它的原理:

v2-4a3b3c833cc314067c135b07811dadef_b.jpg

v2-a81618c7c49733d4e2c8862d1b51cf95_b.jpg

v2-59dbeaad773049f6043b6491559de041_b.jpg

v2-3d08bd2963b4de9f29ef6fd7e37e84b8_b.jpg

v2-b73d3c917275d2b669a272af8633b23d_b.jpg

v2-cd42fecdf43e7a5564ba95b0eb11b556_b.jpg

v2-a91f0f44e5401d4a7c6de24bb9b74a4a_b.jpg

v2-347f13310c0230598749c53ef5eab148_b.jpg

95% HD is similar to maximum HD. However, it is based on the calculation of the 95th percentile of the distances between boundary points in X and Y. The purpose for using this metric is to eliminate the impact of a very small subset of the outliers.

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

环境安装

        pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numba
pip install hausdorff
      

numpy 代码示例

        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") ))
      

v2-b97fafc668d7d17b219c051e84491989_b.jpg

猜你喜欢

转载自blog.csdn.net/weixin_40519315/article/details/105158547