语义分割常用metric

  1. Pixel acc:比较预测label和实际label像素间误差。对于位置的偏移十分敏感,小的偏移都会产生大量的像素误差。
  2. IOU: 交并比,DetectionResult与Ground Truth的交集比上它们的并集。只是从单个像素点本身去考虑该点的预测是否与label重合,而没有从全局去考虑预测的整体形状是否与label结果形状吻合。

混淆矩阵(Confusion Matrix)
在这里插入图片描述

  • 分别统计分类模型中归错,归对类的观测值的个数,然后把结果放在一个表里展示出来。
  • 矩阵对角线上的数字,为当前类别预测正确的类别数目;非对角线数字,预测都是错误的!
  • 矩阵每一行数字求和的值,其含义:真实值中,真实情况下属于该行对应类别的数目!
  • 矩阵每一列数字求和的值,其含义:预测值中,预测为该列对应类别的数目!
  • PA:Accuracy = (TP + TN) / (TP + TN + FP + FN)
  • CPA:Precision = TP / (TP + FP) 或 TN / (TN + FN)
  • Recall = TP / (TP + FN) 或 TN / (TN + FP)

语义分割评价指标

  • PA:像素准确率,预测类别正确的像素数占总像素数的比例
  • CPA:类别像素准确率,模型对类别 i 的预测值有很多,其中有对有错,预测对的值占预测总值的比例
  • MPA:类别平均像素准确率,分别计算每个类被正确分类像素数的比例CPA,然后累加求平均
  • IoU:交并比,模型对某一类别预测结果和真实值的交集与并集的比值
  • MIoU:平均交并比,模型对每一类预测的结果和真实值的交集与并集的比值,求和再平均的结果

A代表真实值(ground truth),B代表预测样本(prediction),预测值和真实值的关系如下:
在这里插入图片描述

  • TP(True Positive):
    橙色,TP = A ∩ B
    预测正确,真正例,模型预测为正例,实际是正例(模型预测为类别1,实际是类别1)
  • FP(False Positive):
    黄色,FP = B - (A ∩ B)
    预测错误,假正例,模型预测为正例,实际是反例 (模型预测为类别1,实际是类别2)
  • FN(False Negative):
    红色,FN = A - (A ∩ B)
    预测错误,假反例,模型预测为反例,实际是正例 (模型预测为类别2,实际是类别1)
  • TN(True Negative):
    白色,TN = ~(A ∪ B)
    预测正确,真反例,模型预测为反例,实际是反例 (模型预测为类别2,实际是类别2)

IoU正例pIoU_{正例p}IoU

### Tensorflow Keras 中实现 MIoU 的计算 MIoU 是语义分割中最常用的评价指标之一,它衡量的是预测结果与真实标注之间的交并比均值。以下是基于 TensorFlow 和 NumPy 的 MIoU 计算代码示例: #### 使用 TensorFlow 实现 MIoU 在 TensorFlow 1.x 版本中,`tf.metrics.mean_iou` 可以直接用于计算 MIoU[^1]。然而,在 TensorFlow 2.x 中,推荐使用 `tf.keras.metrics.MeanIoU` 来替代。 下面是完整的代码实现: ```python import tensorflow as tf import numpy as np def compute_miou(labels, predictions, num_classes): """ Compute Mean IoU using TensorFlow's MeanIoU metric. Args: labels: Ground truth label matrix with shape (height, width). predictions: Predicted label matrix with the same shape as labels. num_classes: Number of classes in segmentation task. Returns: miou_value: Scalar value representing mean intersection over union. """ # Flatten input tensors to match required format for MeanIoU labels_flat = tf.reshape(labels, [-1]) preds_flat = tf.reshape(predictions, [-1]) # Initialize MeanIoU object miou_metric = tf.keras.metrics.MeanIoU(num_classes=num_classes) # Update state and calculate result miou_metric.update_state(labels_flat, preds_flat) miou_value = miou_metric.result().numpy() return miou_value # Example usage num_classes = 6 # Assuming there are 6 classes including background labels = np.array([ [0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 255] # '255' represents ignored region ], dtype=np.int32) predictions = np.array([ [0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 255] ], dtype=np.int32) # Ignore regions marked by '255' valid_mask = labels != 255 labels_valid = labels[valid_mask] predictions_valid = predictions[valid_mask] miou_result = compute_miou(labels_valid, predictions_valid, num_classes) print(f"Mean IoU: {miou_result}") ``` 此代码片段展示了如何通过过滤掉被标记为忽略区域(如值为 `255` 的部分)来正确计算 MIoU[^2]。 --- #### 手动实现 MIoU 如果不想依赖于框架中的内置函数,也可以手动实现 MIoU 的计算逻辑。具体方法如下: ```python def manual_compute_miou(labels, predictions, num_classes): """ Manually compute Mean IoU without relying on external libraries like TF or PyTorch. Args: labels: Ground truth label matrix with shape (height, width). predictions: Predicted label matrix with the same shape as labels. num_classes: Number of classes in segmentation task. Returns: miou_value: Scalar value representing mean intersection over union. """ ious = [] for class_id in range(1, num_classes): # Exclude background if necessary true_class = labels == class_id pred_class = predictions == class_id intersect = np.sum(true_class & pred_class) union = np.sum(true_class | pred_class) if union > 0: # Avoid division by zero iou = intersect / union ious.append(iou) miou_value = np.mean(ious) if len(ious) > 0 else 0.0 return miou_value # Example usage manual_miou_result = manual_compute_miou(labels_valid, predictions_valid, num_classes) print(f"Manual Mean IoU: {manual_miou_result}") ``` 该方法逐类别地计算交集和并集,并最终取平均值得到 MIoU[^4]。 --- ### 总结 无论是采用 TensorFlow 提供的工具还是自行编写代码,都需要特别注意处理那些应被忽略的区域(通常由特殊值如 `255` 表示)。这一步骤对于获得准确的结果至关重要。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值