实例分割计算指标TP,FP,FN,F1(附代码)

目录

源代码:

返回值

 我使用的groundTruth图像:

 预测图像


 

 基于IOU的F1是评价模型实例分割能力的一种评价指标,该指标在2018年的Urban 3D Challenge和2020年的阿里天池建筑智能普查竞赛中作为评价标准。
计算公式如下:

在这里插入图片描述

其余计算指标:

1、IoU:  交并比,两个区域重叠的部分除以两个区域的集合部分, IOU算出的值score > 0.5 就可以被认为一个不错的结果了

2、mIoU(mean IoU):均交并比,识别或者分割图像一般都有好几个类别,把每个分类得出的分数进行平均一下就可以得到mean IoU,也就是mIoU。

3、Precision:精确率,混淆矩阵计算得出,P = TP/(TP+FP)

4、Recall:召回率,R = TP/(TP+FN)

5、Accuracy:准确率,accuracy = (TP+TN)/(TP+TN+FP+FN)

     即PA(Pixel Accuracy,像素精度?标记正确的像素占总像素的比例):表示检测物体的准确度,重点判断标准为是否检测到了物体
IoU只是用于评价一幅图的标准,如果我们要评价一套算法,并不能只从一张图片的标准中得出结论。一般对于一个数据集、或者一个模型来说。评价的标准通常来说遍历所有图像中各种类型、各种大小(size)还有标准中设定阈值.论文中得出的结论数据,就是从这些规则中得出的。

源代码:


from skimage import measure
from scipy import ndimage
import cv2 as cv
import numpy as np

def get_buildings(mask, pixel_threshold):
    gt_labeled_array, gt_num = ndimage.label(mask)
    unique, counts = np.unique(gt_labeled_array, return_counts=True)
    for (k, v) in dict(zip(unique, counts)).items():
        if v < pixel_threshold:
            mask[gt_labeled_array == k] = 0
    return measure.label(mask, return_num=True)


def calculate_f1_buildings_score(y_pred_path, iou_threshold=0.40, component_size_threshold=0):
    #iou_threshold=0.40表示重合面积大于40%,判断为TP
    tp = 0
    fp = 0
    fn = 0


    # for m in tqdm(range(len(y_pred_list))):
    processed_gt = set()
    matched = set()

    #mask_img是预测图像
    # mask_img = cv.imread(r".\predictLabel\Halo-water.jpg", 0)
    # mask_img = cv.imread(r".\predictLabel\Halo_image.png", 0)
    mask_img = cv.imread(r".\predictLabel\RGB_image.png", 0)
    #gt_mask_img 是groundTruth图像
    gt_mask_img = cv.imread(r".\groundtruth\GT_image.png", 0)

    predicted_labels, predicted_count = get_buildings(mask_img, component_size_threshold)
    gt_labels, gt_count = get_buildings(gt_mask_img, component_size_threshold)

    gt_buildings = [rp.coords for rp in measure.regionprops(gt_labels)]
    pred_buildings = [rp.coords for rp in measure.regionprops(predicted_labels)]
    gt_buildings = [to_point_set(b) for b in gt_buildings]
    pred_buildings = [to_point_set(b) for b in pred_buildings]
    for j in range(predicted_count):
        match_found = False
        for i in range(gt_count):
            pred_ind = j + 1
            gt_ind = i + 1
            if match_found:
                break
            if gt_ind in processed_gt:
                continue
            pred_building = pred_buildings[j]
            gt_building = gt_buildings[i]
            intersection = len(pred_building.intersection(gt_building))
            union = len(pred_building) + len(gt_building) - intersection
            iou = intersection / union
            if iou > iou_threshold:
                processed_gt.add(gt_ind)
                matched.add(pred_ind)
                match_found = True
                tp += 1
        if not match_found:
            fp += 1
    fn += gt_count - len(processed_gt)
    precision = tp / (tp + fp)
    recall = tp / (tp + fn)
    if precision == 0 or recall == 0:
        return 0
    f_score = 2 * precision * recall / (precision + recall)
    return f_score , fp ,fn , tp ,precision , recall


def to_point_set(building):
    return set([(row[0], row[1]) for row in building])

#y_pred_path 没用到,随便填
y_pred_path = 'predictLabel'

f_score = calculate_f1_buildings_score(y_pred_path, iou_threshold=0.5, component_size_threshold=1)

print(f_score)

返回值

 我使用的groundTruth图像:

 预测图像

 

PyTorch本身不提供计算TPFPFN、TN的函数,但可以使用numpy或其他库进行计算。下面给出一个使用numpy计算TPFPFN、TN的示例代码: ```python import numpy as np # y_pred为预测结果,y_true为真实结果,threshold为分类阈值 def calculate_confusion_matrix(y_pred, y_true, threshold=0.5): y_pred = np.where(y_pred > threshold, 1, 0) # 根据阈值将预测结果转为0或1 tp = ((y_pred == 1) & (y_true == 1)).sum() # 计算TP fp = ((y_pred == 1) & (y_true == 0)).sum() # 计算FP fn = ((y_pred == 0) & (y_true == 1)).sum() # 计算FN tn = ((y_pred == 0) & (y_true == 0)).sum() # 计算TN return tp, fp, fn, tn ``` 其中,y_pred和y_true都是numpy数组,分别表示模型的预测结果和真实结果。threshold是分类阈值,可以根据需要调整。函数返回TPFPFN、TN四个值。使用时,可以将预测结果和真实结果转为numpy数组后传入该函数,如下所示: ```python import torch # 创建一个大小为(2, 3)的模型输出结果Tensor y_pred = torch.tensor([[0.2, 0.5, 0.8], [0.1, 0.4, 0.9]]) # 创建一个大小为(2, 3)的真实结果Tensor y_true = torch.tensor([[0, 1, 1], [1, 0, 1]]) # 将Tensor转为numpy数组,并调用calculate_confusion_matrix函数计算混淆矩阵 tp, fp, fn, tn = calculate_confusion_matrix(y_pred.numpy(), y_true.numpy()) print("TP: {}, FP: {}, FN: {}, TN: {}".format(tp, fp, fn, tn)) ``` 输出结果为: ``` TP: 3, FP: 1, FN: 1, TN: 5 ``` 说明模型预测了3个正例,但其中有1个是错误的;有1个正例没有被预测出来;有5个负例被正确预测。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值