You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

701 lines
33 KiB

  1. # Copyright 2017 The TensorFlow 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. # ==============================================================================
  15. """Evaluate Object Detection result on a single image.
  16. Annotate each detected result as true positives or false positive according to
  17. a predefined IOU ratio. Non Maximum Supression is used by default. Multi class
  18. detection is supported by default.
  19. Based on the settings, per image evaluation is either performed on boxes or
  20. on object masks.
  21. """
  22. import numpy as np
  23. from object_detection.utils import np_box_list
  24. from object_detection.utils import np_box_list_ops
  25. from object_detection.utils import np_box_mask_list
  26. from object_detection.utils import np_box_mask_list_ops
  27. class PerImageEvaluation(object):
  28. """Evaluate detection result of a single image."""
  29. def __init__(self,
  30. num_groundtruth_classes,
  31. matching_iou_threshold=0.5,
  32. nms_iou_threshold=0.3,
  33. nms_max_output_boxes=50,
  34. group_of_weight=0.0):
  35. """Initialized PerImageEvaluation by evaluation parameters.
  36. Args:
  37. num_groundtruth_classes: Number of ground truth object classes
  38. matching_iou_threshold: A ratio of area intersection to union, which is
  39. the threshold to consider whether a detection is true positive or not
  40. nms_iou_threshold: IOU threshold used in Non Maximum Suppression.
  41. nms_max_output_boxes: Number of maximum output boxes in NMS.
  42. group_of_weight: Weight of the group-of boxes.
  43. """
  44. self.matching_iou_threshold = matching_iou_threshold
  45. self.nms_iou_threshold = nms_iou_threshold
  46. self.nms_max_output_boxes = nms_max_output_boxes
  47. self.num_groundtruth_classes = num_groundtruth_classes
  48. self.group_of_weight = group_of_weight
  49. def compute_object_detection_metrics(self,
  50. detected_boxes,
  51. detected_scores,
  52. detected_class_labels,
  53. groundtruth_boxes,
  54. groundtruth_class_labels,
  55. groundtruth_is_difficult_list,
  56. groundtruth_is_group_of_list,
  57. detected_masks=None,
  58. groundtruth_masks=None):
  59. """Evaluates detections as being tp, fp or weighted from a single image.
  60. The evaluation is done in two stages:
  61. 1. All detections are matched to non group-of boxes; true positives are
  62. determined and detections matched to difficult boxes are ignored.
  63. 2. Detections that are determined as false positives are matched against
  64. group-of boxes and weighted if matched.
  65. Args:
  66. detected_boxes: A float numpy array of shape [N, 4], representing N
  67. regions of detected object regions. Each row is of the format [y_min,
  68. x_min, y_max, x_max]
  69. detected_scores: A float numpy array of shape [N, 1], representing the
  70. confidence scores of the detected N object instances.
  71. detected_class_labels: A integer numpy array of shape [N, 1], repreneting
  72. the class labels of the detected N object instances.
  73. groundtruth_boxes: A float numpy array of shape [M, 4], representing M
  74. regions of object instances in ground truth
  75. groundtruth_class_labels: An integer numpy array of shape [M, 1],
  76. representing M class labels of object instances in ground truth
  77. groundtruth_is_difficult_list: A boolean numpy array of length M denoting
  78. whether a ground truth box is a difficult instance or not
  79. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  80. whether a ground truth box has group-of tag
  81. detected_masks: (optional) A uint8 numpy array of shape [N, height,
  82. width]. If not None, the metrics will be computed based on masks.
  83. groundtruth_masks: (optional) A uint8 numpy array of shape [M, height,
  84. width]. Can have empty masks, i.e. where all values are 0.
  85. Returns:
  86. scores: A list of C float numpy arrays. Each numpy array is of
  87. shape [K, 1], representing K scores detected with object class
  88. label c
  89. tp_fp_labels: A list of C boolean numpy arrays. Each numpy array
  90. is of shape [K, 1], representing K True/False positive label of
  91. object instances detected with class label c
  92. is_class_correctly_detected_in_image: a numpy integer array of
  93. shape [C, 1], indicating whether the correponding class has a least
  94. one instance being correctly detected in the image
  95. """
  96. detected_boxes, detected_scores, detected_class_labels, detected_masks = (
  97. self._remove_invalid_boxes(detected_boxes, detected_scores,
  98. detected_class_labels, detected_masks))
  99. scores, tp_fp_labels = self._compute_tp_fp(
  100. detected_boxes=detected_boxes,
  101. detected_scores=detected_scores,
  102. detected_class_labels=detected_class_labels,
  103. groundtruth_boxes=groundtruth_boxes,
  104. groundtruth_class_labels=groundtruth_class_labels,
  105. groundtruth_is_difficult_list=groundtruth_is_difficult_list,
  106. groundtruth_is_group_of_list=groundtruth_is_group_of_list,
  107. detected_masks=detected_masks,
  108. groundtruth_masks=groundtruth_masks)
  109. is_class_correctly_detected_in_image = self._compute_cor_loc(
  110. detected_boxes=detected_boxes,
  111. detected_scores=detected_scores,
  112. detected_class_labels=detected_class_labels,
  113. groundtruth_boxes=groundtruth_boxes,
  114. groundtruth_class_labels=groundtruth_class_labels,
  115. detected_masks=detected_masks,
  116. groundtruth_masks=groundtruth_masks)
  117. return scores, tp_fp_labels, is_class_correctly_detected_in_image
  118. def _compute_cor_loc(self,
  119. detected_boxes,
  120. detected_scores,
  121. detected_class_labels,
  122. groundtruth_boxes,
  123. groundtruth_class_labels,
  124. detected_masks=None,
  125. groundtruth_masks=None):
  126. """Compute CorLoc score for object detection result.
  127. Args:
  128. detected_boxes: A float numpy array of shape [N, 4], representing N
  129. regions of detected object regions. Each row is of the format [y_min,
  130. x_min, y_max, x_max]
  131. detected_scores: A float numpy array of shape [N, 1], representing the
  132. confidence scores of the detected N object instances.
  133. detected_class_labels: A integer numpy array of shape [N, 1], repreneting
  134. the class labels of the detected N object instances.
  135. groundtruth_boxes: A float numpy array of shape [M, 4], representing M
  136. regions of object instances in ground truth
  137. groundtruth_class_labels: An integer numpy array of shape [M, 1],
  138. representing M class labels of object instances in ground truth
  139. detected_masks: (optional) A uint8 numpy array of shape [N, height,
  140. width]. If not None, the scores will be computed based on masks.
  141. groundtruth_masks: (optional) A uint8 numpy array of shape [M, height,
  142. width].
  143. Returns:
  144. is_class_correctly_detected_in_image: a numpy integer array of
  145. shape [C, 1], indicating whether the correponding class has a least
  146. one instance being correctly detected in the image
  147. Raises:
  148. ValueError: If detected masks is not None but groundtruth masks are None,
  149. or the other way around.
  150. """
  151. if (detected_masks is not None and
  152. groundtruth_masks is None) or (detected_masks is None and
  153. groundtruth_masks is not None):
  154. raise ValueError(
  155. 'If `detected_masks` is provided, then `groundtruth_masks` should '
  156. 'also be provided.')
  157. is_class_correctly_detected_in_image = np.zeros(
  158. self.num_groundtruth_classes, dtype=int)
  159. for i in range(self.num_groundtruth_classes):
  160. (gt_boxes_at_ith_class, gt_masks_at_ith_class,
  161. detected_boxes_at_ith_class, detected_scores_at_ith_class,
  162. detected_masks_at_ith_class) = self._get_ith_class_arrays(
  163. detected_boxes, detected_scores, detected_masks,
  164. detected_class_labels, groundtruth_boxes, groundtruth_masks,
  165. groundtruth_class_labels, i)
  166. is_class_correctly_detected_in_image[i] = (
  167. self._compute_is_class_correctly_detected_in_image(
  168. detected_boxes=detected_boxes_at_ith_class,
  169. detected_scores=detected_scores_at_ith_class,
  170. groundtruth_boxes=gt_boxes_at_ith_class,
  171. detected_masks=detected_masks_at_ith_class,
  172. groundtruth_masks=gt_masks_at_ith_class))
  173. return is_class_correctly_detected_in_image
  174. def _compute_is_class_correctly_detected_in_image(self,
  175. detected_boxes,
  176. detected_scores,
  177. groundtruth_boxes,
  178. detected_masks=None,
  179. groundtruth_masks=None):
  180. """Compute CorLoc score for a single class.
  181. Args:
  182. detected_boxes: A numpy array of shape [N, 4] representing detected box
  183. coordinates
  184. detected_scores: A 1-d numpy array of length N representing classification
  185. score
  186. groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth
  187. box coordinates
  188. detected_masks: (optional) A np.uint8 numpy array of shape [N, height,
  189. width]. If not None, the scores will be computed based on masks.
  190. groundtruth_masks: (optional) A np.uint8 numpy array of shape [M, height,
  191. width].
  192. Returns:
  193. is_class_correctly_detected_in_image: An integer 1 or 0 denoting whether a
  194. class is correctly detected in the image or not
  195. """
  196. if detected_boxes.size > 0:
  197. if groundtruth_boxes.size > 0:
  198. max_score_id = np.argmax(detected_scores)
  199. mask_mode = False
  200. if detected_masks is not None and groundtruth_masks is not None:
  201. mask_mode = True
  202. if mask_mode:
  203. detected_boxlist = np_box_mask_list.BoxMaskList(
  204. box_data=np.expand_dims(detected_boxes[max_score_id], axis=0),
  205. mask_data=np.expand_dims(detected_masks[max_score_id], axis=0))
  206. gt_boxlist = np_box_mask_list.BoxMaskList(
  207. box_data=groundtruth_boxes, mask_data=groundtruth_masks)
  208. iou = np_box_mask_list_ops.iou(detected_boxlist, gt_boxlist)
  209. else:
  210. detected_boxlist = np_box_list.BoxList(
  211. np.expand_dims(detected_boxes[max_score_id, :], axis=0))
  212. gt_boxlist = np_box_list.BoxList(groundtruth_boxes)
  213. iou = np_box_list_ops.iou(detected_boxlist, gt_boxlist)
  214. if np.max(iou) >= self.matching_iou_threshold:
  215. return 1
  216. return 0
  217. def _compute_tp_fp(self,
  218. detected_boxes,
  219. detected_scores,
  220. detected_class_labels,
  221. groundtruth_boxes,
  222. groundtruth_class_labels,
  223. groundtruth_is_difficult_list,
  224. groundtruth_is_group_of_list,
  225. detected_masks=None,
  226. groundtruth_masks=None):
  227. """Labels true/false positives of detections of an image across all classes.
  228. Args:
  229. detected_boxes: A float numpy array of shape [N, 4], representing N
  230. regions of detected object regions. Each row is of the format [y_min,
  231. x_min, y_max, x_max]
  232. detected_scores: A float numpy array of shape [N, 1], representing the
  233. confidence scores of the detected N object instances.
  234. detected_class_labels: A integer numpy array of shape [N, 1], repreneting
  235. the class labels of the detected N object instances.
  236. groundtruth_boxes: A float numpy array of shape [M, 4], representing M
  237. regions of object instances in ground truth
  238. groundtruth_class_labels: An integer numpy array of shape [M, 1],
  239. representing M class labels of object instances in ground truth
  240. groundtruth_is_difficult_list: A boolean numpy array of length M denoting
  241. whether a ground truth box is a difficult instance or not
  242. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  243. whether a ground truth box has group-of tag
  244. detected_masks: (optional) A np.uint8 numpy array of shape [N, height,
  245. width]. If not None, the scores will be computed based on masks.
  246. groundtruth_masks: (optional) A np.uint8 numpy array of shape [M, height,
  247. width].
  248. Returns:
  249. result_scores: A list of float numpy arrays. Each numpy array is of
  250. shape [K, 1], representing K scores detected with object class
  251. label c
  252. result_tp_fp_labels: A list of boolean numpy array. Each numpy array is of
  253. shape [K, 1], representing K True/False positive label of object
  254. instances detected with class label c
  255. Raises:
  256. ValueError: If detected masks is not None but groundtruth masks are None,
  257. or the other way around.
  258. """
  259. if detected_masks is not None and groundtruth_masks is None:
  260. raise ValueError(
  261. 'Detected masks is available but groundtruth masks is not.')
  262. if detected_masks is None and groundtruth_masks is not None:
  263. raise ValueError(
  264. 'Groundtruth masks is available but detected masks is not.')
  265. result_scores = []
  266. result_tp_fp_labels = []
  267. for i in range(self.num_groundtruth_classes):
  268. groundtruth_is_difficult_list_at_ith_class = (
  269. groundtruth_is_difficult_list[groundtruth_class_labels == i])
  270. groundtruth_is_group_of_list_at_ith_class = (
  271. groundtruth_is_group_of_list[groundtruth_class_labels == i])
  272. (gt_boxes_at_ith_class, gt_masks_at_ith_class,
  273. detected_boxes_at_ith_class, detected_scores_at_ith_class,
  274. detected_masks_at_ith_class) = self._get_ith_class_arrays(
  275. detected_boxes, detected_scores, detected_masks,
  276. detected_class_labels, groundtruth_boxes, groundtruth_masks,
  277. groundtruth_class_labels, i)
  278. scores, tp_fp_labels = self._compute_tp_fp_for_single_class(
  279. detected_boxes=detected_boxes_at_ith_class,
  280. detected_scores=detected_scores_at_ith_class,
  281. groundtruth_boxes=gt_boxes_at_ith_class,
  282. groundtruth_is_difficult_list=groundtruth_is_difficult_list_at_ith_class,
  283. groundtruth_is_group_of_list=groundtruth_is_group_of_list_at_ith_class,
  284. detected_masks=detected_masks_at_ith_class,
  285. groundtruth_masks=gt_masks_at_ith_class)
  286. result_scores.append(scores)
  287. result_tp_fp_labels.append(tp_fp_labels)
  288. return result_scores, result_tp_fp_labels
  289. def _get_overlaps_and_scores_mask_mode(self, detected_boxes, detected_scores,
  290. detected_masks, groundtruth_boxes,
  291. groundtruth_masks,
  292. groundtruth_is_group_of_list):
  293. """Computes overlaps and scores between detected and groudntruth masks.
  294. Args:
  295. detected_boxes: A numpy array of shape [N, 4] representing detected box
  296. coordinates
  297. detected_scores: A 1-d numpy array of length N representing classification
  298. score
  299. detected_masks: A uint8 numpy array of shape [N, height, width]. If not
  300. None, the scores will be computed based on masks.
  301. groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth
  302. box coordinates
  303. groundtruth_masks: A uint8 numpy array of shape [M, height, width].
  304. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  305. whether a ground truth box has group-of tag. If a groundtruth box is
  306. group-of box, every detection matching this box is ignored.
  307. Returns:
  308. iou: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If
  309. gt_non_group_of_boxlist.num_boxes() == 0 it will be None.
  310. ioa: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If
  311. gt_group_of_boxlist.num_boxes() == 0 it will be None.
  312. scores: The score of the detected boxlist.
  313. num_boxes: Number of non-maximum suppressed detected boxes.
  314. """
  315. detected_boxlist = np_box_mask_list.BoxMaskList(
  316. box_data=detected_boxes, mask_data=detected_masks)
  317. detected_boxlist.add_field('scores', detected_scores)
  318. detected_boxlist = np_box_mask_list_ops.non_max_suppression(
  319. detected_boxlist, self.nms_max_output_boxes, self.nms_iou_threshold)
  320. gt_non_group_of_boxlist = np_box_mask_list.BoxMaskList(
  321. box_data=groundtruth_boxes[~groundtruth_is_group_of_list],
  322. mask_data=groundtruth_masks[~groundtruth_is_group_of_list])
  323. gt_group_of_boxlist = np_box_mask_list.BoxMaskList(
  324. box_data=groundtruth_boxes[groundtruth_is_group_of_list],
  325. mask_data=groundtruth_masks[groundtruth_is_group_of_list])
  326. iou = np_box_mask_list_ops.iou(detected_boxlist, gt_non_group_of_boxlist)
  327. ioa = np.transpose(
  328. np_box_mask_list_ops.ioa(gt_group_of_boxlist, detected_boxlist))
  329. scores = detected_boxlist.get_field('scores')
  330. num_boxes = detected_boxlist.num_boxes()
  331. return iou, ioa, scores, num_boxes
  332. def _get_overlaps_and_scores_box_mode(self, detected_boxes, detected_scores,
  333. groundtruth_boxes,
  334. groundtruth_is_group_of_list):
  335. """Computes overlaps and scores between detected and groudntruth boxes.
  336. Args:
  337. detected_boxes: A numpy array of shape [N, 4] representing detected box
  338. coordinates
  339. detected_scores: A 1-d numpy array of length N representing classification
  340. score
  341. groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth
  342. box coordinates
  343. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  344. whether a ground truth box has group-of tag. If a groundtruth box is
  345. group-of box, every detection matching this box is ignored.
  346. Returns:
  347. iou: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If
  348. gt_non_group_of_boxlist.num_boxes() == 0 it will be None.
  349. ioa: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If
  350. gt_group_of_boxlist.num_boxes() == 0 it will be None.
  351. scores: The score of the detected boxlist.
  352. num_boxes: Number of non-maximum suppressed detected boxes.
  353. """
  354. detected_boxlist = np_box_list.BoxList(detected_boxes)
  355. detected_boxlist.add_field('scores', detected_scores)
  356. detected_boxlist = np_box_list_ops.non_max_suppression(
  357. detected_boxlist, self.nms_max_output_boxes, self.nms_iou_threshold)
  358. gt_non_group_of_boxlist = np_box_list.BoxList(
  359. groundtruth_boxes[~groundtruth_is_group_of_list])
  360. gt_group_of_boxlist = np_box_list.BoxList(
  361. groundtruth_boxes[groundtruth_is_group_of_list])
  362. iou = np_box_list_ops.iou(detected_boxlist, gt_non_group_of_boxlist)
  363. ioa = np.transpose(
  364. np_box_list_ops.ioa(gt_group_of_boxlist, detected_boxlist))
  365. scores = detected_boxlist.get_field('scores')
  366. num_boxes = detected_boxlist.num_boxes()
  367. return iou, ioa, scores, num_boxes
  368. def _compute_tp_fp_for_single_class(self,
  369. detected_boxes,
  370. detected_scores,
  371. groundtruth_boxes,
  372. groundtruth_is_difficult_list,
  373. groundtruth_is_group_of_list,
  374. detected_masks=None,
  375. groundtruth_masks=None):
  376. """Labels boxes detected with the same class from the same image as tp/fp.
  377. Args:
  378. detected_boxes: A numpy array of shape [N, 4] representing detected box
  379. coordinates
  380. detected_scores: A 1-d numpy array of length N representing classification
  381. score
  382. groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth
  383. box coordinates
  384. groundtruth_is_difficult_list: A boolean numpy array of length M denoting
  385. whether a ground truth box is a difficult instance or not. If a
  386. groundtruth box is difficult, every detection matching this box is
  387. ignored.
  388. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  389. whether a ground truth box has group-of tag. If a groundtruth box is
  390. group-of box, every detection matching this box is ignored.
  391. detected_masks: (optional) A uint8 numpy array of shape [N, height,
  392. width]. If not None, the scores will be computed based on masks.
  393. groundtruth_masks: (optional) A uint8 numpy array of shape [M, height,
  394. width].
  395. Returns:
  396. Two arrays of the same size, containing all boxes that were evaluated as
  397. being true positives or false positives; if a box matched to a difficult
  398. box or to a group-of box, it is ignored.
  399. scores: A numpy array representing the detection scores.
  400. tp_fp_labels: a boolean numpy array indicating whether a detection is a
  401. true positive.
  402. """
  403. if detected_boxes.size == 0:
  404. return np.array([], dtype=float), np.array([], dtype=bool)
  405. mask_mode = False
  406. if detected_masks is not None and groundtruth_masks is not None:
  407. mask_mode = True
  408. iou = np.ndarray([0, 0])
  409. ioa = np.ndarray([0, 0])
  410. iou_mask = np.ndarray([0, 0])
  411. ioa_mask = np.ndarray([0, 0])
  412. if mask_mode:
  413. # For Instance Segmentation Evaluation on Open Images V5, not all boxed
  414. # instances have corresponding segmentation annotations. Those boxes that
  415. # dont have segmentation annotations are represented as empty masks in
  416. # groundtruth_masks nd array.
  417. mask_presence_indicator = (np.sum(groundtruth_masks, axis=(1, 2)) > 0)
  418. (iou_mask, ioa_mask, scores,
  419. num_detected_boxes) = self._get_overlaps_and_scores_mask_mode(
  420. detected_boxes=detected_boxes,
  421. detected_scores=detected_scores,
  422. detected_masks=detected_masks,
  423. groundtruth_boxes=groundtruth_boxes[mask_presence_indicator, :],
  424. groundtruth_masks=groundtruth_masks[mask_presence_indicator, :],
  425. groundtruth_is_group_of_list=groundtruth_is_group_of_list[
  426. mask_presence_indicator])
  427. if sum(mask_presence_indicator) < len(mask_presence_indicator):
  428. # Not all masks are present - some masks are empty
  429. (iou, ioa, _,
  430. num_detected_boxes) = self._get_overlaps_and_scores_box_mode(
  431. detected_boxes=detected_boxes,
  432. detected_scores=detected_scores,
  433. groundtruth_boxes=groundtruth_boxes[~mask_presence_indicator, :],
  434. groundtruth_is_group_of_list=groundtruth_is_group_of_list[
  435. ~mask_presence_indicator])
  436. num_detected_boxes = detected_boxes.shape[0]
  437. else:
  438. mask_presence_indicator = np.zeros(
  439. groundtruth_is_group_of_list.shape, dtype=bool)
  440. (iou, ioa, scores,
  441. num_detected_boxes) = self._get_overlaps_and_scores_box_mode(
  442. detected_boxes=detected_boxes,
  443. detected_scores=detected_scores,
  444. groundtruth_boxes=groundtruth_boxes,
  445. groundtruth_is_group_of_list=groundtruth_is_group_of_list)
  446. if groundtruth_boxes.size == 0:
  447. return scores, np.zeros(num_detected_boxes, dtype=bool)
  448. tp_fp_labels = np.zeros(num_detected_boxes, dtype=bool)
  449. is_matched_to_box = np.zeros(num_detected_boxes, dtype=bool)
  450. is_matched_to_difficult = np.zeros(num_detected_boxes, dtype=bool)
  451. is_matched_to_group_of = np.zeros(num_detected_boxes, dtype=bool)
  452. def compute_match_iou(iou, groundtruth_nongroup_of_is_difficult_list,
  453. is_box):
  454. """Computes TP/FP for non group-of box matching.
  455. The function updates the following local variables:
  456. tp_fp_labels - if a box is matched to group-of
  457. is_matched_to_difficult - the detections that were processed at this are
  458. matched to difficult box.
  459. is_matched_to_box - the detections that were processed at this stage are
  460. marked as is_box.
  461. Args:
  462. iou: intersection-over-union matrix [num_gt_boxes]x[num_det_boxes].
  463. groundtruth_nongroup_of_is_difficult_list: boolean that specifies if gt
  464. box is difficult.
  465. is_box: boolean that specifies if currently boxes or masks are
  466. processed.
  467. """
  468. max_overlap_gt_ids = np.argmax(iou, axis=1)
  469. is_gt_detected = np.zeros(iou.shape[1], dtype=bool)
  470. for i in range(num_detected_boxes):
  471. gt_id = max_overlap_gt_ids[i]
  472. is_evaluatable = (not tp_fp_labels[i] and
  473. not is_matched_to_difficult[i] and
  474. iou[i, gt_id] >= self.matching_iou_threshold and
  475. not is_matched_to_group_of[i])
  476. if is_evaluatable:
  477. if not groundtruth_nongroup_of_is_difficult_list[gt_id]:
  478. if not is_gt_detected[gt_id]:
  479. tp_fp_labels[i] = True
  480. is_gt_detected[gt_id] = True
  481. is_matched_to_box[i] = is_box
  482. else:
  483. is_matched_to_difficult[i] = True
  484. def compute_match_ioa(ioa, is_box):
  485. """Computes TP/FP for group-of box matching.
  486. The function updates the following local variables:
  487. is_matched_to_group_of - if a box is matched to group-of
  488. is_matched_to_box - the detections that were processed at this stage are
  489. marked as is_box.
  490. Args:
  491. ioa: intersection-over-area matrix [num_gt_boxes]x[num_det_boxes].
  492. is_box: boolean that specifies if currently boxes or masks are
  493. processed.
  494. Returns:
  495. scores_group_of: of detections matched to group-of boxes
  496. [num_groupof_matched].
  497. tp_fp_labels_group_of: boolean array of size [num_groupof_matched], all
  498. values are True.
  499. """
  500. scores_group_of = np.zeros(ioa.shape[1], dtype=float)
  501. tp_fp_labels_group_of = self.group_of_weight * np.ones(
  502. ioa.shape[1], dtype=float)
  503. max_overlap_group_of_gt_ids = np.argmax(ioa, axis=1)
  504. for i in range(num_detected_boxes):
  505. gt_id = max_overlap_group_of_gt_ids[i]
  506. is_evaluatable = (not tp_fp_labels[i] and
  507. not is_matched_to_difficult[i] and
  508. ioa[i, gt_id] >= self.matching_iou_threshold and
  509. not is_matched_to_group_of[i])
  510. if is_evaluatable:
  511. is_matched_to_group_of[i] = True
  512. is_matched_to_box[i] = is_box
  513. scores_group_of[gt_id] = max(scores_group_of[gt_id], scores[i])
  514. selector = np.where((scores_group_of > 0) & (tp_fp_labels_group_of > 0))
  515. scores_group_of = scores_group_of[selector]
  516. tp_fp_labels_group_of = tp_fp_labels_group_of[selector]
  517. return scores_group_of, tp_fp_labels_group_of
  518. # The evaluation is done in two stages:
  519. # 1. Evaluate all objects that actually have instance level masks.
  520. # 2. Evaluate all objects that are not already evaluated as boxes.
  521. if iou_mask.shape[1] > 0:
  522. groundtruth_is_difficult_mask_list = groundtruth_is_difficult_list[
  523. mask_presence_indicator]
  524. groundtruth_is_group_of_mask_list = groundtruth_is_group_of_list[
  525. mask_presence_indicator]
  526. compute_match_iou(
  527. iou_mask,
  528. groundtruth_is_difficult_mask_list[
  529. ~groundtruth_is_group_of_mask_list],
  530. is_box=False)
  531. scores_mask_group_of = np.ndarray([0], dtype=float)
  532. tp_fp_labels_mask_group_of = np.ndarray([0], dtype=float)
  533. if ioa_mask.shape[1] > 0:
  534. scores_mask_group_of, tp_fp_labels_mask_group_of = compute_match_ioa(
  535. ioa_mask, is_box=False)
  536. # Tp-fp evaluation for non-group of boxes (if any).
  537. if iou.shape[1] > 0:
  538. groundtruth_is_difficult_box_list = groundtruth_is_difficult_list[
  539. ~mask_presence_indicator]
  540. groundtruth_is_group_of_box_list = groundtruth_is_group_of_list[
  541. ~mask_presence_indicator]
  542. compute_match_iou(
  543. iou,
  544. groundtruth_is_difficult_box_list[~groundtruth_is_group_of_box_list],
  545. is_box=True)
  546. scores_box_group_of = np.ndarray([0], dtype=float)
  547. tp_fp_labels_box_group_of = np.ndarray([0], dtype=float)
  548. if ioa.shape[1] > 0:
  549. scores_box_group_of, tp_fp_labels_box_group_of = compute_match_ioa(
  550. ioa, is_box=True)
  551. if mask_mode:
  552. # Note: here crowds are treated as ignore regions.
  553. valid_entries = (~is_matched_to_difficult & ~is_matched_to_group_of
  554. & ~is_matched_to_box)
  555. return np.concatenate(
  556. (scores[valid_entries], scores_mask_group_of)), np.concatenate(
  557. (tp_fp_labels[valid_entries].astype(float),
  558. tp_fp_labels_mask_group_of))
  559. else:
  560. valid_entries = (~is_matched_to_difficult & ~is_matched_to_group_of)
  561. return np.concatenate(
  562. (scores[valid_entries], scores_box_group_of)), np.concatenate(
  563. (tp_fp_labels[valid_entries].astype(float),
  564. tp_fp_labels_box_group_of))
  565. def _get_ith_class_arrays(self, detected_boxes, detected_scores,
  566. detected_masks, detected_class_labels,
  567. groundtruth_boxes, groundtruth_masks,
  568. groundtruth_class_labels, class_index):
  569. """Returns numpy arrays belonging to class with index `class_index`.
  570. Args:
  571. detected_boxes: A numpy array containing detected boxes.
  572. detected_scores: A numpy array containing detected scores.
  573. detected_masks: A numpy array containing detected masks.
  574. detected_class_labels: A numpy array containing detected class labels.
  575. groundtruth_boxes: A numpy array containing groundtruth boxes.
  576. groundtruth_masks: A numpy array containing groundtruth masks.
  577. groundtruth_class_labels: A numpy array containing groundtruth class
  578. labels.
  579. class_index: An integer index.
  580. Returns:
  581. gt_boxes_at_ith_class: A numpy array containing groundtruth boxes labeled
  582. as ith class.
  583. gt_masks_at_ith_class: A numpy array containing groundtruth masks labeled
  584. as ith class.
  585. detected_boxes_at_ith_class: A numpy array containing detected boxes
  586. corresponding to the ith class.
  587. detected_scores_at_ith_class: A numpy array containing detected scores
  588. corresponding to the ith class.
  589. detected_masks_at_ith_class: A numpy array containing detected masks
  590. corresponding to the ith class.
  591. """
  592. selected_groundtruth = (groundtruth_class_labels == class_index)
  593. gt_boxes_at_ith_class = groundtruth_boxes[selected_groundtruth]
  594. if groundtruth_masks is not None:
  595. gt_masks_at_ith_class = groundtruth_masks[selected_groundtruth]
  596. else:
  597. gt_masks_at_ith_class = None
  598. selected_detections = (detected_class_labels == class_index)
  599. detected_boxes_at_ith_class = detected_boxes[selected_detections]
  600. detected_scores_at_ith_class = detected_scores[selected_detections]
  601. if detected_masks is not None:
  602. detected_masks_at_ith_class = detected_masks[selected_detections]
  603. else:
  604. detected_masks_at_ith_class = None
  605. return (gt_boxes_at_ith_class, gt_masks_at_ith_class,
  606. detected_boxes_at_ith_class, detected_scores_at_ith_class,
  607. detected_masks_at_ith_class)
  608. def _remove_invalid_boxes(self,
  609. detected_boxes,
  610. detected_scores,
  611. detected_class_labels,
  612. detected_masks=None):
  613. """Removes entries with invalid boxes.
  614. A box is invalid if either its xmax is smaller than its xmin, or its ymax
  615. is smaller than its ymin.
  616. Args:
  617. detected_boxes: A float numpy array of size [num_boxes, 4] containing box
  618. coordinates in [ymin, xmin, ymax, xmax] format.
  619. detected_scores: A float numpy array of size [num_boxes].
  620. detected_class_labels: A int32 numpy array of size [num_boxes].
  621. detected_masks: A uint8 numpy array of size [num_boxes, height, width].
  622. Returns:
  623. valid_detected_boxes: A float numpy array of size [num_valid_boxes, 4]
  624. containing box coordinates in [ymin, xmin, ymax, xmax] format.
  625. valid_detected_scores: A float numpy array of size [num_valid_boxes].
  626. valid_detected_class_labels: A int32 numpy array of size
  627. [num_valid_boxes].
  628. valid_detected_masks: A uint8 numpy array of size
  629. [num_valid_boxes, height, width].
  630. """
  631. valid_indices = np.logical_and(detected_boxes[:, 0] < detected_boxes[:, 2],
  632. detected_boxes[:, 1] < detected_boxes[:, 3])
  633. detected_boxes = detected_boxes[valid_indices]
  634. detected_scores = detected_scores[valid_indices]
  635. detected_class_labels = detected_class_labels[valid_indices]
  636. if detected_masks is not None:
  637. detected_masks = detected_masks[valid_indices]
  638. return [
  639. detected_boxes, detected_scores, detected_class_labels, detected_masks
  640. ]