• 实例分割计算指标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)还有标准中设定阈值.论文中得出的结论数据,就是从这些规则中得出的。

    源代码:

    1. from skimage import measure
    2. from scipy import ndimage
    3. import cv2 as cv
    4. import numpy as np
    5. def get_buildings(mask, pixel_threshold):
    6. gt_labeled_array, gt_num = ndimage.label(mask)
    7. unique, counts = np.unique(gt_labeled_array, return_counts=True)
    8. for (k, v) in dict(zip(unique, counts)).items():
    9. if v < pixel_threshold:
    10. mask[gt_labeled_array == k] = 0
    11. return measure.label(mask, return_num=True)
    12. def calculate_f1_buildings_score(y_pred_path, iou_threshold=0.40, component_size_threshold=0):
    13. #iou_threshold=0.40表示重合面积大于40%,判断为TP
    14. tp = 0
    15. fp = 0
    16. fn = 0
    17. # for m in tqdm(range(len(y_pred_list))):
    18. processed_gt = set()
    19. matched = set()
    20. #mask_img是预测图像
    21. # mask_img = cv.imread(r".\predictLabel\Halo-water.jpg", 0)
    22. # mask_img = cv.imread(r".\predictLabel\Halo_image.png", 0)
    23. mask_img = cv.imread(r".\predictLabel\RGB_image.png", 0)
    24. #gt_mask_img 是groundTruth图像
    25. gt_mask_img = cv.imread(r".\groundtruth\GT_image.png", 0)
    26. predicted_labels, predicted_count = get_buildings(mask_img, component_size_threshold)
    27. gt_labels, gt_count = get_buildings(gt_mask_img, component_size_threshold)
    28. gt_buildings = [rp.coords for rp in measure.regionprops(gt_labels)]
    29. pred_buildings = [rp.coords for rp in measure.regionprops(predicted_labels)]
    30. gt_buildings = [to_point_set(b) for b in gt_buildings]
    31. pred_buildings = [to_point_set(b) for b in pred_buildings]
    32. for j in range(predicted_count):
    33. match_found = False
    34. for i in range(gt_count):
    35. pred_ind = j + 1
    36. gt_ind = i + 1
    37. if match_found:
    38. break
    39. if gt_ind in processed_gt:
    40. continue
    41. pred_building = pred_buildings[j]
    42. gt_building = gt_buildings[i]
    43. intersection = len(pred_building.intersection(gt_building))
    44. union = len(pred_building) + len(gt_building) - intersection
    45. iou = intersection / union
    46. if iou > iou_threshold:
    47. processed_gt.add(gt_ind)
    48. matched.add(pred_ind)
    49. match_found = True
    50. tp += 1
    51. if not match_found:
    52. fp += 1
    53. fn += gt_count - len(processed_gt)
    54. precision = tp / (tp + fp)
    55. recall = tp / (tp + fn)
    56. if precision == 0 or recall == 0:
    57. return 0
    58. f_score = 2 * precision * recall / (precision + recall)
    59. return f_score , fp ,fn , tp ,precision , recall
    60. def to_point_set(building):
    61. return set([(row[0], row[1]) for row in building])
    62. #y_pred_path 没用到,随便填
    63. y_pred_path = 'predictLabel'
    64. f_score = calculate_f1_buildings_score(y_pred_path, iou_threshold=0.5, component_size_threshold=1)
    65. print(f_score)

    返回值

     我使用的groundTruth图像:

     预测图像

     

  • 相关阅读:
    math_三角升幂/降幂/微积分公式填空
    0基础学习VR全景平台篇第119篇:利用蒙版航拍补天 - PS教程
    基础架构之GitLab
    (接口封装)
    安装jdk1.6
    一维卷积英语电影评论情感分类项目
    Elasticsearch: Pipeline
    【LeetCode】349. 两个数组的交集
    一种让运行在CentOS下的.NET CORE的Web项目简单方便易部署的自动更新方案
    举个栗子!Tableau 技巧(259):文本表中省市县数据的灵活逐级下钻「方法一」
  • 原文地址:https://blog.csdn.net/weixin_42934729/article/details/126829104