问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

语义分割评价指标——95% Hausdorff距离

创作时间:
作者:
@小白创作中心

语义分割评价指标——95% Hausdorff距离

引用
CSDN
1.
https://m.blog.csdn.net/weixin_72254790/article/details/142521930

语义分割是计算机视觉中的一个重要任务,用于将图像中的每个像素分类到预定义的类别中。在评估语义分割模型的性能时,95% Hausdorff距离是一个常用的评价指标。本文将详细介绍95% Hausdorff距离的概念及其计算方法。

一、最快理解

  • max(d_XY, d_YX):取X到Y的距离和Y到X的距离的最大值。
    其中
  • X到Y = max min x到y:X中所有点都和Y集合计算最小的距离,得到的距离集合再取最大值。
    同理
  • Y到X = max min y到x:Y中所有点都和X集合计算最小的距离,得到的距离集合再取最大值。

二、选取95%

  1. 针对二分类将预测出来的二值图像划分为布尔前景/背景
  2. 所有前景的坐标计算
  • numpy提供了argwhere方法筛选数组中非0项,然后返回这些项的坐标。
  1. 对坐标进行排序:np.sort()
  2. 列表切割(所有坐标数*95%)的数组

三、实现

import numpy as np
from scipy.spatial.distance import cdist

def hausdorff95(X, Y):
    # 将二值图像转换为布尔数组,True 表示前景
    X_foreground = X.astype(bool)
    Y_foreground = Y.astype(bool)
    # 计算前景点的坐标
    X_coords = np.argwhere(X_foreground)
    Y_coords = np.argwhere(Y_foreground)
    # 计算 X 中每个点到 Y 的所有点的距离
    X_to_Y_dist = cdist(X_coords, Y_coords)
    # 计算 Y 中每个点到 X 的所有点的距离
    Y_to_X_dist = cdist(Y_coords, X_coords)
    # 将 X 到 Y 的距离和 Y 到 X 的距离合并为一个数组
    all_distances = np.concatenate((X_to_Y_dist.flatten(), Y_to_X_dist.flatten()))
    # 排除掉最大的5%的距离
    distances_sorted = np.sort(all_distances)
    num_points = len(all_distances)
    threshold_index = int(num_points * 0.95)
    # 确保至少有一个距离被选中
    if threshold_index == 0:
        threshold_index = 1
    # 计算95% Hausdorff 距离
    distances_filtered = distances_sorted[:threshold_index]
    if len(distances_filtered) > 0:
        hd95_value = np.max(distances_filtered)  # 取最大值,而不是平均值
    else:
        hd95_value = 0  # 如果没有距离被选中,可以返回0或其他合适的值
    return hd95_value

注意: 在代码实现中,使用了scipy.spatial.distance.cdist来计算点之间的距离矩阵,这比使用distance_matrix更高效。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号