常用分割评价指标Dice、Hausdorff_95、IOU、PPV等 + Python实现
What is脑肿瘤为例蓝色部分表示真实脑肿瘤区域(GroundTruth),蓝色的其它部分为正常脑区域红色部分表示预测的脑肿瘤区域,红色的其它部分为预测的正常脑区域What is假设正样本为脑肿瘤,负样本为正常脑组织,则有如下:T...
·
TN:True Negative,被判定为负样本,事实上也是负样本,即红色与蓝色以外区域
FP:False Positive,被判定为正样本,但事实上是负样本,即红色中除了蓝色部分
FN:False Negative,被判定为负样本,但事实上是正样本,即蓝色中除了红色部分
Dice
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 对分割出的边界比较敏感。
下面8张图能够很清晰讲解它的原理:
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") ))
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献2条内容
所有评论(0)