一、2D图像分割任务
1.像素精度 (Pixel Accuracy, PA)
2.平均像素精度(mean Pixel Accuracy, mPA)
每个类内被正确分类像素数的比例,之后求所有类的平均,k为类别数,+1为背景
3.平均交并比(mean Intersection over Union, mIoU)
计算两个集合的交集和并集之比,在每个类上求IoU,之后取平均
二、3D点云分割任务
1.平均交并比
代码示例(两种方法代码)
"""计算一个批次下的mIoU
"""
import numpy as np
NUM_CLASSES = 17
total_seen_class = [0 for _ in range(NUM_CLASSES)]
total_intersection_class = [0 for _ in range(NUM_CLASSES)]
total_union_class = [0 for _ in range(NUM_CLASSES)]
# label为标签真值数组,pred为标签预测数组
for index in range(NUM_CLASSES):
total_seen_class[index] += np.sum((label == index)) # 求该index类总元素个数
total_intersection_class[index] += np.sum((pred == index) & (label == index)) # 求真值为index且同样预测为index的元素个数,对应于IoU公式中的分子————交
total_union_class[index] += np.sum(((pred == index) | (label == index))) # 求真值为index或者预测为index的元素个数,对应于IoU公式中的分母————并
mIoU = np.mean(np.array(total_intersection_class) / (np.array(total_union_class, dtype=np.float) + 1e-6))
"""计算每一个批次batch的mIoU, acc
"""
import numpy as np
num_classes = 17
batchs = target.shape[0]
# 计算acc
correct = pred.eq(target.data).cpu().sum()
# 计算mIoU
shape_ious = []
for batch_idx in range(batchs): # target.shape是batch大小
parts = range(num_classes)
class_ious = []
for part in parts:
I = np.sum(np.logical_and(pred[batch_idx] == part, target[batch_idx] == part))
U = np.sum(np.logical_or(pred[batch_idx] == part, target[batch_idx] == part))
if U == 0:
iou = 1 #If the union of groundtruth and prediction points is empty, then count part IoU as 1
else:
iou = I / float(U)
class_ious.append(iou)
pass # 该对象,每一类的IoU计算完毕
shape_ious.append(np.mean(class_ious))
mIoU = np.mean(shape_ious)
acc = correct.item()/float(batchs * 50000)
pass
三、2D目标检测
1.平均精度(Average Precision, AP)
confidence:定一个阈值threshold,规定当检测框的置信度confidence > threshold时,才认定该预测框有效。
IoU:定一个阈值threshold,规定当检测框真值与预测的IoU > threshold时,才认定为预测成功。
TP:True Positive,真阳,真值为正,预测为正,预测对了
FP:False Positive,假阳,真值为负,预测为正,预测错了
FN:False Negative,假阴,真值为正,预测为负,漏检了
TN:True Negative,真阴,真值为负,预测为负
(1)Precision——精确度,查准率
表示预测为正的结果中有多少样本是正确的
(2)Recall——召回率,查全率
表示正样本结果中有多少正样本被正确检测出来
(3)P-R曲线
IoU取不同的值,则有不同的TP、FP、FN值,相应的,就会有不同的P、R,以R为横坐标,P为纵坐标,则可以画出一条曲线,成为P-R曲线。P-R曲线的下方面积则为最终的AP,不同类别的AP取平均值,则为mAP。
代码示例:(参考目标检测中的mAP+PyTorch实现_pytorch pred gt-CSDN博客,暂未测试)