eval_det_iou.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from collections import namedtuple
  4. import numpy as np
  5. from shapely.geometry import Polygon
  6. """
  7. reference from :
  8. https://github.com/MhLiao/DB/blob/3c32b808d4412680310d3d28eeb6a2d5bf1566c5/concern/icdar2015_eval/detection/iou.py#L8
  9. """
  10. class DetectionIoUEvaluator(object):
  11. def __init__(self, iou_constraint=0.5, area_precision_constraint=0.5):
  12. self.iou_constraint = iou_constraint
  13. self.area_precision_constraint = area_precision_constraint
  14. def evaluate_image(self, gt, pred):
  15. def get_union(pD, pG):
  16. return Polygon(pD).union(Polygon(pG)).area
  17. def get_intersection_over_union(pD, pG):
  18. return get_intersection(pD, pG) / get_union(pD, pG)
  19. def get_intersection(pD, pG):
  20. return Polygon(pD).intersection(Polygon(pG)).area
  21. def compute_ap(confList, matchList, numGtCare):
  22. correct = 0
  23. AP = 0
  24. if len(confList) > 0:
  25. confList = np.array(confList)
  26. matchList = np.array(matchList)
  27. sorted_ind = np.argsort(-confList)
  28. confList = confList[sorted_ind]
  29. matchList = matchList[sorted_ind]
  30. for n in range(len(confList)):
  31. match = matchList[n]
  32. if match:
  33. correct += 1
  34. AP += float(correct) / (n + 1)
  35. if numGtCare > 0:
  36. AP /= numGtCare
  37. return AP
  38. perSampleMetrics = {}
  39. matchedSum = 0
  40. Rectangle = namedtuple('Rectangle', 'xmin ymin xmax ymax')
  41. numGlobalCareGt = 0
  42. numGlobalCareDet = 0
  43. arrGlobalConfidences = []
  44. arrGlobalMatches = []
  45. recall = 0
  46. precision = 0
  47. hmean = 0
  48. detMatched = 0
  49. iouMat = np.empty([1, 1])
  50. gtPols = []
  51. detPols = []
  52. gtPolPoints = []
  53. detPolPoints = []
  54. # Array of Ground Truth Polygons' keys marked as don't Care
  55. gtDontCarePolsNum = []
  56. # Array of Detected Polygons' matched with a don't Care GT
  57. detDontCarePolsNum = []
  58. pairs = []
  59. detMatchedNums = []
  60. arrSampleConfidences = []
  61. arrSampleMatch = []
  62. evaluationLog = ""
  63. for n in range(len(gt)):
  64. points = gt[n]['points']
  65. dontCare = gt[n]['ignore']
  66. if not Polygon(points).is_valid:
  67. continue
  68. gtPol = points
  69. gtPols.append(gtPol)
  70. gtPolPoints.append(points)
  71. if dontCare:
  72. gtDontCarePolsNum.append(len(gtPols) - 1)
  73. evaluationLog += "GT polygons: " + str(len(gtPols)) + (
  74. " (" + str(len(gtDontCarePolsNum)) + " don't care)\n"
  75. if len(gtDontCarePolsNum) > 0 else "\n")
  76. for n in range(len(pred)):
  77. points = pred[n]['points']
  78. if not Polygon(points).is_valid:
  79. continue
  80. detPol = points
  81. detPols.append(detPol)
  82. detPolPoints.append(points)
  83. if len(gtDontCarePolsNum) > 0:
  84. for dontCarePol in gtDontCarePolsNum:
  85. dontCarePol = gtPols[dontCarePol]
  86. intersected_area = get_intersection(dontCarePol, detPol)
  87. pdDimensions = Polygon(detPol).area
  88. precision = 0 if pdDimensions == 0 else intersected_area / pdDimensions
  89. if (precision > self.area_precision_constraint):
  90. detDontCarePolsNum.append(len(detPols) - 1)
  91. break
  92. evaluationLog += "DET polygons: " + str(len(detPols)) + (
  93. " (" + str(len(detDontCarePolsNum)) + " don't care)\n"
  94. if len(detDontCarePolsNum) > 0 else "\n")
  95. if len(gtPols) > 0 and len(detPols) > 0:
  96. # Calculate IoU and precision matrixs
  97. outputShape = [len(gtPols), len(detPols)]
  98. iouMat = np.empty(outputShape)
  99. gtRectMat = np.zeros(len(gtPols), np.int8)
  100. detRectMat = np.zeros(len(detPols), np.int8)
  101. for gtNum in range(len(gtPols)):
  102. for detNum in range(len(detPols)):
  103. pG = gtPols[gtNum]
  104. pD = detPols[detNum]
  105. iouMat[gtNum, detNum] = get_intersection_over_union(pD, pG)
  106. for gtNum in range(len(gtPols)):
  107. for detNum in range(len(detPols)):
  108. if gtRectMat[gtNum] == 0 and detRectMat[
  109. detNum] == 0 and gtNum not in gtDontCarePolsNum and detNum not in detDontCarePolsNum:
  110. if iouMat[gtNum, detNum] > self.iou_constraint:
  111. gtRectMat[gtNum] = 1
  112. detRectMat[detNum] = 1
  113. detMatched += 1
  114. pairs.append({'gt': gtNum, 'det': detNum})
  115. detMatchedNums.append(detNum)
  116. evaluationLog += "Match GT #" + \
  117. str(gtNum) + " with Det #" + str(detNum) + "\n"
  118. numGtCare = (len(gtPols) - len(gtDontCarePolsNum))
  119. numDetCare = (len(detPols) - len(detDontCarePolsNum))
  120. if numGtCare == 0:
  121. recall = float(1)
  122. precision = float(0) if numDetCare > 0 else float(1)
  123. else:
  124. recall = float(detMatched) / numGtCare
  125. precision = 0 if numDetCare == 0 else float(detMatched) / numDetCare
  126. hmean = 0 if (precision + recall) == 0 else 2.0 * \
  127. precision * recall / (precision + recall)
  128. matchedSum += detMatched
  129. numGlobalCareGt += numGtCare
  130. numGlobalCareDet += numDetCare
  131. perSampleMetrics = {
  132. 'gtCare': numGtCare,
  133. 'detCare': numDetCare,
  134. 'detMatched': detMatched,
  135. }
  136. return perSampleMetrics
  137. def combine_results(self, results):
  138. numGlobalCareGt = 0
  139. numGlobalCareDet = 0
  140. matchedSum = 0
  141. for result in results:
  142. numGlobalCareGt += result['gtCare']
  143. numGlobalCareDet += result['detCare']
  144. matchedSum += result['detMatched']
  145. methodRecall = 0 if numGlobalCareGt == 0 else float(
  146. matchedSum) / numGlobalCareGt
  147. methodPrecision = 0 if numGlobalCareDet == 0 else float(
  148. matchedSum) / numGlobalCareDet
  149. methodHmean = 0 if methodRecall + methodPrecision == 0 else 2 * \
  150. methodRecall * methodPrecision / (
  151. methodRecall + methodPrecision)
  152. methodMetrics = {
  153. 'precision': methodPrecision,
  154. 'recall': methodRecall,
  155. 'hmean': methodHmean
  156. }
  157. return methodMetrics
  158. if __name__ == '__main__':
  159. evaluator = DetectionIoUEvaluator()
  160. gts = [[{
  161. 'points': [(0, 0), (1, 0), (1, 1), (0, 1)],
  162. 'text': 1234,
  163. 'ignore': False,
  164. }, {
  165. 'points': [(2, 2), (3, 2), (3, 3), (2, 3)],
  166. 'text': 5678,
  167. 'ignore': False,
  168. }]]
  169. preds = [[{
  170. 'points': [(0.1, 0.1), (1, 0), (1, 1), (0, 1)],
  171. 'text': 123,
  172. 'ignore': False,
  173. }]]
  174. results = []
  175. for gt, pred in zip(gts, preds):
  176. results.append(evaluator.evaluate_image(gt, pred))
  177. metrics = evaluator.combine_results(results)
  178. print(metrics)