seg_metrics.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Copyright (c) 2021 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. import paddle
  16. import paddle.nn.functional as F
  17. def loss_computation(logits_list, labels, losses):
  18. loss_list = []
  19. for i in range(len(logits_list)):
  20. logits = logits_list[i]
  21. loss_i = losses['types'][i]
  22. loss_list.append(losses['coef'][i] * loss_i(logits, labels))
  23. return loss_list
  24. def calculate_area(pred, label, num_classes, ignore_index=255):
  25. """
  26. Calculate intersect, prediction and label area
  27. Args:
  28. pred (Tensor): The prediction by model.
  29. label (Tensor): The ground truth of image.
  30. num_classes (int): The unique number of target classes.
  31. ignore_index (int): Specifies a target value that is ignored. Default: 255.
  32. Returns:
  33. Tensor: The intersection area of prediction and the ground on all class.
  34. Tensor: The prediction area on all class.
  35. Tensor: The ground truth area on all class
  36. """
  37. if len(pred.shape) == 4:
  38. pred = paddle.squeeze(pred, axis=1)
  39. if len(label.shape) == 4:
  40. label = paddle.squeeze(label, axis=1)
  41. if not pred.shape == label.shape:
  42. raise ValueError('Shape of `pred` and `label should be equal, '
  43. 'but there are {} and {}.'.format(pred.shape,
  44. label.shape))
  45. # Delete ignore_index
  46. mask = label != ignore_index
  47. pred = pred + 1
  48. label = label + 1
  49. pred = pred * mask
  50. label = label * mask
  51. pred = F.one_hot(pred, num_classes + 1)
  52. label = F.one_hot(label, num_classes + 1)
  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 = paddle.sum(pred_i)
  62. label_area_i = paddle.sum(label_i)
  63. intersect_area_i = paddle.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. pred_area = paddle.concat(pred_area)
  68. label_area = paddle.concat(label_area)
  69. intersect_area = paddle.concat(intersect_area)
  70. return intersect_area, pred_area, label_area
  71. def mean_iou(intersect_area, pred_area, label_area):
  72. """
  73. Calculate iou.
  74. Args:
  75. intersect_area (Tensor): The intersection area of prediction and ground truth on all classes.
  76. pred_area (Tensor): The prediction area on all classes.
  77. label_area (Tensor): The ground truth area on all classes.
  78. Returns:
  79. np.ndarray: iou on all classes.
  80. float: mean iou of all classes.
  81. """
  82. intersect_area = intersect_area.numpy()
  83. pred_area = pred_area.numpy()
  84. label_area = label_area.numpy()
  85. union = pred_area + label_area - intersect_area
  86. class_iou = []
  87. for i in range(len(intersect_area)):
  88. if union[i] == 0:
  89. iou = 0
  90. else:
  91. iou = intersect_area[i] / union[i]
  92. class_iou.append(iou)
  93. miou = np.mean(class_iou)
  94. return np.array(class_iou), miou
  95. def accuracy(intersect_area, pred_area):
  96. """
  97. Calculate accuracy
  98. Args:
  99. intersect_area (Tensor): The intersection area of prediction and ground truth on all classes..
  100. pred_area (Tensor): The prediction area on all classes.
  101. Returns:
  102. np.ndarray: accuracy on all classes.
  103. float: mean accuracy.
  104. """
  105. intersect_area = intersect_area.numpy()
  106. pred_area = pred_area.numpy()
  107. class_acc = []
  108. for i in range(len(intersect_area)):
  109. if pred_area[i] == 0:
  110. acc = 0
  111. else:
  112. acc = intersect_area[i] / pred_area[i]
  113. class_acc.append(acc)
  114. macc = np.sum(intersect_area) / np.sum(pred_area)
  115. return np.array(class_acc), macc
  116. def kappa(intersect_area, pred_area, label_area):
  117. """
  118. Calculate kappa coefficient
  119. Args:
  120. intersect_area (Tensor): The intersection area of prediction and ground truth on all classes..
  121. pred_area (Tensor): The prediction area on all classes.
  122. label_area (Tensor): The ground truth area on all classes.
  123. Returns:
  124. float: kappa coefficient.
  125. """
  126. intersect_area = intersect_area.numpy()
  127. pred_area = pred_area.numpy()
  128. label_area = label_area.numpy()
  129. total_area = np.sum(label_area)
  130. po = np.sum(intersect_area) / total_area
  131. pe = np.sum(pred_area * label_area) / (total_area * total_area)
  132. kappa = (po - pe) / (1 - pe)
  133. return kappa
  134. def f1_score(intersect_area, pred_area, label_area):
  135. intersect_area = intersect_area.numpy()
  136. pred_area = pred_area.numpy()
  137. label_area = label_area.numpy()
  138. class_f1_sco = []
  139. for i in range(len(intersect_area)):
  140. if pred_area[i] + label_area[i] == 0:
  141. f1_sco = 0
  142. elif pred_area[i] == 0:
  143. f1_sco = 0
  144. else:
  145. prec = intersect_area[i] / pred_area[i]
  146. rec = intersect_area[i] / label_area[i]
  147. f1_sco = 2 * prec * rec / (prec + rec)
  148. class_f1_sco.append(f1_sco)
  149. return np.array(class_f1_sco)