seg_metrics.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import numpy as np
  15. def f1_score(intersect_area, pred_area, label_area):
  16. class_f1_sco = []
  17. for i in range(len(intersect_area)):
  18. if pred_area[i] + label_area[i] == 0:
  19. f1_sco = 0
  20. elif pred_area[i] == 0:
  21. f1_sco = 0
  22. else:
  23. prec = intersect_area[i] / pred_area[i]
  24. rec = intersect_area[i] / label_area[i]
  25. f1_sco = 2 * prec * rec / (prec + rec)
  26. class_f1_sco.append(f1_sco)
  27. return np.array(class_f1_sco)
  28. def calculate_area(pred, label, num_classes, ignore_index=255):
  29. """
  30. Calculate intersect, prediction and label area
  31. Args:
  32. pred (np.ndarray): The prediction by model.
  33. label (np.ndarray): The ground truth of image.
  34. num_classes (int): The unique number of target classes.
  35. ignore_index (int): Specifies a target value that is ignored. Default: 255.
  36. Returns:
  37. Numpy Array: The intersection area of prediction and the ground on all class.
  38. Numpy Array: The prediction area on all class.
  39. Numpy Array: The ground truth area on all class
  40. """
  41. if not pred.shape == label.shape:
  42. raise ValueError(
  43. "Shape of `pred` and `label should be equal, "
  44. "but there are {} and {}.".format(pred.shape, label.shape)
  45. )
  46. mask = label != ignore_index
  47. pred = pred + 1
  48. label = label + 1
  49. pred = pred * mask
  50. label = label * mask
  51. pred = np.eye(num_classes + 1)[pred]
  52. label = np.eye(num_classes + 1)[label]
  53. pred = pred[:, 1:]
  54. label = label[:, 1:]
  55. pred_area = []
  56. label_area = []
  57. intersect_area = []
  58. for i in range(num_classes):
  59. pred_i = pred[:, :, i]
  60. label_i = label[:, :, i]
  61. pred_area_i = np.sum(pred_i)
  62. label_area_i = np.sum(label_i)
  63. intersect_area_i = np.sum(pred_i * label_i)
  64. pred_area.append(pred_area_i)
  65. label_area.append(label_area_i)
  66. intersect_area.append(intersect_area_i)
  67. return np.array(intersect_area), np.array(pred_area), np.array(label_area)
  68. def mean_iou(intersect_area, pred_area, label_area):
  69. """
  70. Calculate iou.
  71. Args:
  72. intersect_area (np.ndarray): The intersection area of prediction and ground truth on all classes.
  73. pred_area (np.ndarray): The prediction area on all classes.
  74. label_area (np.ndarray): The ground truth area on all classes.
  75. Returns:
  76. np.ndarray: iou on all classes.
  77. float: mean iou of all classes.
  78. """
  79. union = pred_area + label_area - intersect_area
  80. class_iou = []
  81. for i in range(len(intersect_area)):
  82. if union[i] == 0:
  83. iou = 0
  84. else:
  85. iou = intersect_area[i] / union[i]
  86. class_iou.append(iou)
  87. miou = np.mean(class_iou)
  88. return np.array(class_iou), miou
  89. def accuracy(intersect_area, pred_area):
  90. """
  91. Calculate accuracy
  92. Args:
  93. intersect_area (np.ndarray): The intersection area of prediction and ground truth on all classes..
  94. pred_area (np.ndarray): The prediction area on all classes.
  95. Returns:
  96. np.ndarray: accuracy on all classes.
  97. float: mean accuracy.
  98. """
  99. class_acc = []
  100. for i in range(len(intersect_area)):
  101. if pred_area[i] == 0:
  102. acc = 0
  103. else:
  104. acc = intersect_area[i] / pred_area[i]
  105. class_acc.append(acc)
  106. macc = np.sum(intersect_area) / np.sum(pred_area)
  107. return np.array(class_acc), macc
  108. def kappa(intersect_area, pred_area, label_area):
  109. """
  110. Calculate kappa coefficient
  111. Args:
  112. intersect_area (np.ndarray): The intersection area of prediction and ground truth on all classes..
  113. pred_area (np.ndarray): The prediction area on all classes.
  114. label_area (np.ndarray): The ground truth area on all classes.
  115. Returns:
  116. float: kappa coefficient.
  117. """
  118. total_area = np.sum(label_area)
  119. po = np.sum(intersect_area) / total_area
  120. pe = np.sum(pred_area * label_area) / (total_area * total_area)
  121. kappa = (po - pe) / (1 - pe)
  122. return kappa