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

目录

源代码:

返回值

 我使用的groundTruth图像:

 预测图像


 

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

实例分割计算指标TP,FP,FN,F1(附代码)_第1张图片

其余计算指标:

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图像:

实例分割计算指标TP,FP,FN,F1(附代码)_第2张图片

 预测图像

 实例分割计算指标TP,FP,FN,F1(附代码)_第3张图片

你可能感兴趣的:(计算机视觉,opencv,python,实例分割)