metrics.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright (c) 2020 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 calculate_area(pred, label, num_classes, ignore_index=255):
  18. """
  19. Calculate intersect, prediction and label area
  20. Args:
  21. pred (Tensor): The prediction by model.
  22. label (Tensor): The ground truth of image.
  23. num_classes (int): The unique number of target classes.
  24. ignore_index (int): Specifies a target value that is ignored. Default: 255.
  25. Returns:
  26. Tensor: The intersection area of prediction and the ground on all class.
  27. Tensor: The prediction area on all class.
  28. Tensor: The ground truth area on all class
  29. """
  30. if len(pred.shape) == 4:
  31. pred = paddle.squeeze(pred, axis=1)
  32. if len(label.shape) == 4:
  33. label = paddle.squeeze(label, axis=1)
  34. if not pred.shape == label.shape:
  35. raise ValueError('Shape of `pred` and `label should be equal, '
  36. 'but there are {} and {}.'.format(pred.shape,
  37. label.shape))
  38. # Delete ignore_index
  39. mask = label != ignore_index
  40. pred = pred + 1
  41. label = label + 1
  42. pred = pred * mask
  43. label = label * mask
  44. pred = F.one_hot(pred, num_classes + 1)
  45. label = F.one_hot(label, num_classes + 1)
  46. pred = pred[:, :, :, 1:]
  47. label = label[:, :, :, 1:]
  48. pred_area = []
  49. label_area = []
  50. intersect_area = []
  51. for i in range(num_classes):
  52. pred_i = pred[:, :, :, i]
  53. label_i = label[:, :, :, i]
  54. pred_area_i = paddle.sum(pred_i)
  55. label_area_i = paddle.sum(label_i)
  56. intersect_area_i = paddle.sum(pred_i * label_i)
  57. pred_area.append(pred_area_i)
  58. label_area.append(label_area_i)
  59. intersect_area.append(intersect_area_i)
  60. pred_area = paddle.concat(pred_area)
  61. label_area = paddle.concat(label_area)
  62. intersect_area = paddle.concat(intersect_area)
  63. return intersect_area, pred_area, label_area
  64. def mean_iou(intersect_area, pred_area, label_area):
  65. """
  66. Calculate iou.
  67. Args:
  68. intersect_area (Tensor): The intersection area of prediction and ground truth on all classes.
  69. pred_area (Tensor): The prediction area on all classes.
  70. label_area (Tensor): The ground truth area on all classes.
  71. Returns:
  72. np.ndarray: iou on all classes.
  73. float: mean iou of all classes.
  74. """
  75. intersect_area = intersect_area.numpy()
  76. pred_area = pred_area.numpy()
  77. label_area = label_area.numpy()
  78. union = pred_area + label_area - intersect_area
  79. class_iou = []
  80. for i in range(len(intersect_area)):
  81. if union[i] == 0:
  82. iou = 0
  83. else:
  84. iou = intersect_area[i] / union[i]
  85. class_iou.append(iou)
  86. miou = np.mean(class_iou)
  87. return np.array(class_iou), miou
  88. def accuracy(intersect_area, pred_area):
  89. """
  90. Calculate accuracy
  91. Args:
  92. intersect_area (Tensor): The intersection area of prediction and ground truth on all classes..
  93. pred_area (Tensor): The prediction area on all classes.
  94. Returns:
  95. np.ndarray: accuracy on all classes.
  96. float: mean accuracy.
  97. """
  98. intersect_area = intersect_area.numpy()
  99. pred_area = pred_area.numpy()
  100. class_acc = []
  101. for i in range(len(intersect_area)):
  102. if pred_area[i] == 0:
  103. acc = 0
  104. else:
  105. acc = intersect_area[i] / pred_area[i]
  106. class_acc.append(acc)
  107. macc = np.sum(intersect_area) / np.sum(pred_area)
  108. return np.array(class_acc), macc
  109. def kappa(intersect_area, pred_area, label_area):
  110. """
  111. Calculate kappa coefficient
  112. Args:
  113. intersect_area (Tensor): The intersection area of prediction and ground truth on all classes..
  114. pred_area (Tensor): The prediction area on all classes.
  115. label_area (Tensor): The ground truth area on all classes.
  116. Returns:
  117. float: kappa coefficient.
  118. """
  119. intersect_area = intersect_area.numpy()
  120. pred_area = pred_area.numpy()
  121. label_area = label_area.numpy()
  122. total_area = np.sum(label_area)
  123. po = np.sum(intersect_area) / total_area
  124. pe = np.sum(pred_area * label_area) / (total_area * total_area)
  125. kappa = (po - pe) / (1 - pe)
  126. return kappa