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.

584 lines
28 KiB

6 years ago
  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(
  50. self, detected_boxes, detected_scores, detected_class_labels,
  51. groundtruth_boxes, groundtruth_class_labels,
  52. groundtruth_is_difficult_list, groundtruth_is_group_of_list,
  53. detected_masks=None, groundtruth_masks=None):
  54. """Evaluates detections as being tp, fp or weighted from a single image.
  55. The evaluation is done in two stages:
  56. 1. All detections are matched to non group-of boxes; true positives are
  57. determined and detections matched to difficult boxes are ignored.
  58. 2. Detections that are determined as false positives are matched against
  59. group-of boxes and weighted if matched.
  60. Args:
  61. detected_boxes: A float numpy array of shape [N, 4], representing N
  62. regions of detected object regions.
  63. Each row is of the format [y_min, x_min, y_max, x_max]
  64. detected_scores: A float numpy array of shape [N, 1], representing
  65. the confidence scores of the detected N object instances.
  66. detected_class_labels: A integer numpy array of shape [N, 1], repreneting
  67. the class labels of the detected N object instances.
  68. groundtruth_boxes: A float numpy array of shape [M, 4], representing M
  69. regions of object instances in ground truth
  70. groundtruth_class_labels: An integer numpy array of shape [M, 1],
  71. representing M class labels of object instances in ground truth
  72. groundtruth_is_difficult_list: A boolean numpy array of length M denoting
  73. whether a ground truth box is a difficult instance or not
  74. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  75. whether a ground truth box has group-of tag
  76. detected_masks: (optional) A uint8 numpy array of shape
  77. [N, height, width]. If not None, the metrics will be computed based
  78. on masks.
  79. groundtruth_masks: (optional) A uint8 numpy array of shape
  80. [M, height, width].
  81. Returns:
  82. scores: A list of C float numpy arrays. Each numpy array is of
  83. shape [K, 1], representing K scores detected with object class
  84. label c
  85. tp_fp_labels: A list of C boolean numpy arrays. Each numpy array
  86. is of shape [K, 1], representing K True/False positive label of
  87. object instances detected with class label c
  88. is_class_correctly_detected_in_image: a numpy integer array of
  89. shape [C, 1], indicating whether the correponding class has a least
  90. one instance being correctly detected in the image
  91. """
  92. detected_boxes, detected_scores, detected_class_labels, detected_masks = (
  93. self._remove_invalid_boxes(detected_boxes, detected_scores,
  94. detected_class_labels, detected_masks))
  95. scores, tp_fp_labels = self._compute_tp_fp(
  96. detected_boxes=detected_boxes,
  97. detected_scores=detected_scores,
  98. detected_class_labels=detected_class_labels,
  99. groundtruth_boxes=groundtruth_boxes,
  100. groundtruth_class_labels=groundtruth_class_labels,
  101. groundtruth_is_difficult_list=groundtruth_is_difficult_list,
  102. groundtruth_is_group_of_list=groundtruth_is_group_of_list,
  103. detected_masks=detected_masks,
  104. groundtruth_masks=groundtruth_masks)
  105. is_class_correctly_detected_in_image = self._compute_cor_loc(
  106. detected_boxes=detected_boxes,
  107. detected_scores=detected_scores,
  108. detected_class_labels=detected_class_labels,
  109. groundtruth_boxes=groundtruth_boxes,
  110. groundtruth_class_labels=groundtruth_class_labels,
  111. detected_masks=detected_masks,
  112. groundtruth_masks=groundtruth_masks)
  113. return scores, tp_fp_labels, is_class_correctly_detected_in_image
  114. def _compute_cor_loc(self, detected_boxes, detected_scores,
  115. detected_class_labels, groundtruth_boxes,
  116. groundtruth_class_labels, detected_masks=None,
  117. groundtruth_masks=None):
  118. """Compute CorLoc score for object detection result.
  119. Args:
  120. detected_boxes: A float numpy array of shape [N, 4], representing N
  121. regions of detected object regions.
  122. Each row is of the format [y_min, x_min, y_max, x_max]
  123. detected_scores: A float numpy array of shape [N, 1], representing
  124. the confidence scores of the detected N object instances.
  125. detected_class_labels: A integer numpy array of shape [N, 1], repreneting
  126. the class labels of the detected N object instances.
  127. groundtruth_boxes: A float numpy array of shape [M, 4], representing M
  128. regions of object instances in ground truth
  129. groundtruth_class_labels: An integer numpy array of shape [M, 1],
  130. representing M class labels of object instances in ground truth
  131. detected_masks: (optional) A uint8 numpy array of shape
  132. [N, height, width]. If not None, the scores will be computed based
  133. on masks.
  134. groundtruth_masks: (optional) A uint8 numpy array of shape
  135. [M, height, width].
  136. Returns:
  137. is_class_correctly_detected_in_image: a numpy integer array of
  138. shape [C, 1], indicating whether the correponding class has a least
  139. one instance being correctly detected in the image
  140. Raises:
  141. ValueError: If detected masks is not None but groundtruth masks are None,
  142. or the other way around.
  143. """
  144. if (detected_masks is not None and
  145. groundtruth_masks is None) or (detected_masks is None and
  146. groundtruth_masks is not None):
  147. raise ValueError(
  148. 'If `detected_masks` is provided, then `groundtruth_masks` should '
  149. 'also be provided.'
  150. )
  151. is_class_correctly_detected_in_image = np.zeros(
  152. self.num_groundtruth_classes, dtype=int)
  153. for i in range(self.num_groundtruth_classes):
  154. (gt_boxes_at_ith_class, gt_masks_at_ith_class,
  155. detected_boxes_at_ith_class, detected_scores_at_ith_class,
  156. detected_masks_at_ith_class) = self._get_ith_class_arrays(
  157. detected_boxes, detected_scores, detected_masks,
  158. detected_class_labels, groundtruth_boxes, groundtruth_masks,
  159. groundtruth_class_labels, i)
  160. is_class_correctly_detected_in_image[i] = (
  161. self._compute_is_class_correctly_detected_in_image(
  162. detected_boxes=detected_boxes_at_ith_class,
  163. detected_scores=detected_scores_at_ith_class,
  164. groundtruth_boxes=gt_boxes_at_ith_class,
  165. detected_masks=detected_masks_at_ith_class,
  166. groundtruth_masks=gt_masks_at_ith_class))
  167. return is_class_correctly_detected_in_image
  168. def _compute_is_class_correctly_detected_in_image(
  169. self, detected_boxes, detected_scores, groundtruth_boxes,
  170. detected_masks=None, groundtruth_masks=None):
  171. """Compute CorLoc score for a single class.
  172. Args:
  173. detected_boxes: A numpy array of shape [N, 4] representing detected box
  174. coordinates
  175. detected_scores: A 1-d numpy array of length N representing classification
  176. score
  177. groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth
  178. box coordinates
  179. detected_masks: (optional) A np.uint8 numpy array of shape
  180. [N, height, width]. If not None, the scores will be computed based
  181. on masks.
  182. groundtruth_masks: (optional) A np.uint8 numpy array of shape
  183. [M, height, width].
  184. Returns:
  185. is_class_correctly_detected_in_image: An integer 1 or 0 denoting whether a
  186. class is correctly detected in the image or not
  187. """
  188. if detected_boxes.size > 0:
  189. if groundtruth_boxes.size > 0:
  190. max_score_id = np.argmax(detected_scores)
  191. mask_mode = False
  192. if detected_masks is not None and groundtruth_masks is not None:
  193. mask_mode = True
  194. if mask_mode:
  195. detected_boxlist = np_box_mask_list.BoxMaskList(
  196. box_data=np.expand_dims(detected_boxes[max_score_id], axis=0),
  197. mask_data=np.expand_dims(detected_masks[max_score_id], axis=0))
  198. gt_boxlist = np_box_mask_list.BoxMaskList(
  199. box_data=groundtruth_boxes, mask_data=groundtruth_masks)
  200. iou = np_box_mask_list_ops.iou(detected_boxlist, gt_boxlist)
  201. else:
  202. detected_boxlist = np_box_list.BoxList(
  203. np.expand_dims(detected_boxes[max_score_id, :], axis=0))
  204. gt_boxlist = np_box_list.BoxList(groundtruth_boxes)
  205. iou = np_box_list_ops.iou(detected_boxlist, gt_boxlist)
  206. if np.max(iou) >= self.matching_iou_threshold:
  207. return 1
  208. return 0
  209. def _compute_tp_fp(self, detected_boxes, detected_scores,
  210. detected_class_labels, groundtruth_boxes,
  211. groundtruth_class_labels, groundtruth_is_difficult_list,
  212. groundtruth_is_group_of_list,
  213. detected_masks=None, groundtruth_masks=None):
  214. """Labels true/false positives of detections of an image across all classes.
  215. Args:
  216. detected_boxes: A float numpy array of shape [N, 4], representing N
  217. regions of detected object regions.
  218. Each row is of the format [y_min, x_min, y_max, x_max]
  219. detected_scores: A float numpy array of shape [N, 1], representing
  220. the confidence scores of the detected N object instances.
  221. detected_class_labels: A integer numpy array of shape [N, 1], repreneting
  222. the class labels of the detected N object instances.
  223. groundtruth_boxes: A float numpy array of shape [M, 4], representing M
  224. regions of object instances in ground truth
  225. groundtruth_class_labels: An integer numpy array of shape [M, 1],
  226. representing M class labels of object instances in ground truth
  227. groundtruth_is_difficult_list: A boolean numpy array of length M denoting
  228. whether a ground truth box is a difficult instance or not
  229. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  230. whether a ground truth box has group-of tag
  231. detected_masks: (optional) A np.uint8 numpy array of shape
  232. [N, height, width]. If not None, the scores will be computed based
  233. on masks.
  234. groundtruth_masks: (optional) A np.uint8 numpy array of shape
  235. [M, height, width].
  236. Returns:
  237. result_scores: A list of float numpy arrays. Each numpy array is of
  238. shape [K, 1], representing K scores detected with object class
  239. label c
  240. result_tp_fp_labels: A list of boolean numpy array. Each numpy array is of
  241. shape [K, 1], representing K True/False positive label of object
  242. instances detected with class label c
  243. Raises:
  244. ValueError: If detected masks is not None but groundtruth masks are None,
  245. or the other way around.
  246. """
  247. if detected_masks is not None and groundtruth_masks is None:
  248. raise ValueError(
  249. 'Detected masks is available but groundtruth masks is not.')
  250. if detected_masks is None and groundtruth_masks is not None:
  251. raise ValueError(
  252. 'Groundtruth masks is available but detected masks is not.')
  253. result_scores = []
  254. result_tp_fp_labels = []
  255. for i in range(self.num_groundtruth_classes):
  256. groundtruth_is_difficult_list_at_ith_class = (
  257. groundtruth_is_difficult_list[groundtruth_class_labels == i])
  258. groundtruth_is_group_of_list_at_ith_class = (
  259. groundtruth_is_group_of_list[groundtruth_class_labels == i])
  260. (gt_boxes_at_ith_class, gt_masks_at_ith_class,
  261. detected_boxes_at_ith_class, detected_scores_at_ith_class,
  262. detected_masks_at_ith_class) = self._get_ith_class_arrays(
  263. detected_boxes, detected_scores, detected_masks,
  264. detected_class_labels, groundtruth_boxes, groundtruth_masks,
  265. groundtruth_class_labels, i)
  266. scores, tp_fp_labels = self._compute_tp_fp_for_single_class(
  267. detected_boxes=detected_boxes_at_ith_class,
  268. detected_scores=detected_scores_at_ith_class,
  269. groundtruth_boxes=gt_boxes_at_ith_class,
  270. groundtruth_is_difficult_list=
  271. groundtruth_is_difficult_list_at_ith_class,
  272. groundtruth_is_group_of_list=
  273. groundtruth_is_group_of_list_at_ith_class,
  274. detected_masks=detected_masks_at_ith_class,
  275. groundtruth_masks=gt_masks_at_ith_class)
  276. result_scores.append(scores)
  277. result_tp_fp_labels.append(tp_fp_labels)
  278. return result_scores, result_tp_fp_labels
  279. def _get_overlaps_and_scores_mask_mode(
  280. self, detected_boxes, detected_scores, detected_masks, groundtruth_boxes,
  281. groundtruth_masks, groundtruth_is_group_of_list):
  282. """Computes overlaps and scores between detected and groudntruth masks.
  283. Args:
  284. detected_boxes: A numpy array of shape [N, 4] representing detected box
  285. coordinates
  286. detected_scores: A 1-d numpy array of length N representing classification
  287. score
  288. detected_masks: A uint8 numpy array of shape [N, height, width]. If not
  289. None, the scores will be computed based on masks.
  290. groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth
  291. box coordinates
  292. groundtruth_masks: A uint8 numpy array of shape [M, height, width].
  293. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  294. whether a ground truth box has group-of tag. If a groundtruth box
  295. is group-of box, every detection matching this box is ignored.
  296. Returns:
  297. iou: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If
  298. gt_non_group_of_boxlist.num_boxes() == 0 it will be None.
  299. ioa: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If
  300. gt_group_of_boxlist.num_boxes() == 0 it will be None.
  301. scores: The score of the detected boxlist.
  302. num_boxes: Number of non-maximum suppressed detected boxes.
  303. """
  304. detected_boxlist = np_box_mask_list.BoxMaskList(
  305. box_data=detected_boxes, mask_data=detected_masks)
  306. detected_boxlist.add_field('scores', detected_scores)
  307. detected_boxlist = np_box_mask_list_ops.non_max_suppression(
  308. detected_boxlist, self.nms_max_output_boxes, self.nms_iou_threshold)
  309. gt_non_group_of_boxlist = np_box_mask_list.BoxMaskList(
  310. box_data=groundtruth_boxes[~groundtruth_is_group_of_list],
  311. mask_data=groundtruth_masks[~groundtruth_is_group_of_list])
  312. gt_group_of_boxlist = np_box_mask_list.BoxMaskList(
  313. box_data=groundtruth_boxes[groundtruth_is_group_of_list],
  314. mask_data=groundtruth_masks[groundtruth_is_group_of_list])
  315. iou = np_box_mask_list_ops.iou(detected_boxlist, gt_non_group_of_boxlist)
  316. ioa = np.transpose(
  317. np_box_mask_list_ops.ioa(gt_group_of_boxlist, detected_boxlist))
  318. scores = detected_boxlist.get_field('scores')
  319. num_boxes = detected_boxlist.num_boxes()
  320. return iou, ioa, scores, num_boxes
  321. def _get_overlaps_and_scores_box_mode(
  322. self,
  323. detected_boxes,
  324. detected_scores,
  325. groundtruth_boxes,
  326. groundtruth_is_group_of_list):
  327. """Computes overlaps and scores between detected and groudntruth boxes.
  328. Args:
  329. detected_boxes: A numpy array of shape [N, 4] representing detected box
  330. coordinates
  331. detected_scores: A 1-d numpy array of length N representing classification
  332. score
  333. groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth
  334. box coordinates
  335. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  336. whether a ground truth box has group-of tag. If a groundtruth box
  337. is group-of box, every detection matching this box is ignored.
  338. Returns:
  339. iou: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If
  340. gt_non_group_of_boxlist.num_boxes() == 0 it will be None.
  341. ioa: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If
  342. gt_group_of_boxlist.num_boxes() == 0 it will be None.
  343. scores: The score of the detected boxlist.
  344. num_boxes: Number of non-maximum suppressed detected boxes.
  345. """
  346. detected_boxlist = np_box_list.BoxList(detected_boxes)
  347. detected_boxlist.add_field('scores', detected_scores)
  348. detected_boxlist = np_box_list_ops.non_max_suppression(
  349. detected_boxlist, self.nms_max_output_boxes, self.nms_iou_threshold)
  350. gt_non_group_of_boxlist = np_box_list.BoxList(
  351. groundtruth_boxes[~groundtruth_is_group_of_list])
  352. gt_group_of_boxlist = np_box_list.BoxList(
  353. groundtruth_boxes[groundtruth_is_group_of_list])
  354. iou = np_box_list_ops.iou(detected_boxlist, gt_non_group_of_boxlist)
  355. ioa = np.transpose(
  356. np_box_list_ops.ioa(gt_group_of_boxlist, detected_boxlist))
  357. scores = detected_boxlist.get_field('scores')
  358. num_boxes = detected_boxlist.num_boxes()
  359. return iou, ioa, scores, num_boxes
  360. def _compute_tp_fp_for_single_class(
  361. self, detected_boxes, detected_scores, groundtruth_boxes,
  362. groundtruth_is_difficult_list, groundtruth_is_group_of_list,
  363. detected_masks=None, groundtruth_masks=None):
  364. """Labels boxes detected with the same class from the same image as tp/fp.
  365. Args:
  366. detected_boxes: A numpy array of shape [N, 4] representing detected box
  367. coordinates
  368. detected_scores: A 1-d numpy array of length N representing classification
  369. score
  370. groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth
  371. box coordinates
  372. groundtruth_is_difficult_list: A boolean numpy array of length M denoting
  373. whether a ground truth box is a difficult instance or not. If a
  374. groundtruth box is difficult, every detection matching this box
  375. is ignored.
  376. groundtruth_is_group_of_list: A boolean numpy array of length M denoting
  377. whether a ground truth box has group-of tag. If a groundtruth box
  378. is group-of box, every detection matching this box is ignored.
  379. detected_masks: (optional) A uint8 numpy array of shape
  380. [N, height, width]. If not None, the scores will be computed based
  381. on masks.
  382. groundtruth_masks: (optional) A uint8 numpy array of shape
  383. [M, height, width].
  384. Returns:
  385. Two arrays of the same size, containing all boxes that were evaluated as
  386. being true positives or false positives; if a box matched to a difficult
  387. box or to a group-of box, it is ignored.
  388. scores: A numpy array representing the detection scores.
  389. tp_fp_labels: a boolean numpy array indicating whether a detection is a
  390. true positive.
  391. """
  392. if detected_boxes.size == 0:
  393. return np.array([], dtype=float), np.array([], dtype=bool)
  394. mask_mode = False
  395. if detected_masks is not None and groundtruth_masks is not None:
  396. mask_mode = True
  397. if mask_mode:
  398. (iou, ioa, scores,
  399. num_detected_boxes) = self._get_overlaps_and_scores_mask_mode(
  400. detected_boxes=detected_boxes,
  401. detected_scores=detected_scores,
  402. detected_masks=detected_masks,
  403. groundtruth_boxes=groundtruth_boxes,
  404. groundtruth_masks=groundtruth_masks,
  405. groundtruth_is_group_of_list=groundtruth_is_group_of_list)
  406. else:
  407. (iou, ioa, scores,
  408. num_detected_boxes) = self._get_overlaps_and_scores_box_mode(
  409. detected_boxes=detected_boxes,
  410. detected_scores=detected_scores,
  411. groundtruth_boxes=groundtruth_boxes,
  412. groundtruth_is_group_of_list=groundtruth_is_group_of_list)
  413. if groundtruth_boxes.size == 0:
  414. return scores, np.zeros(num_detected_boxes, dtype=bool)
  415. tp_fp_labels = np.zeros(num_detected_boxes, dtype=bool)
  416. is_matched_to_difficult_box = np.zeros(num_detected_boxes, dtype=bool)
  417. is_matched_to_group_of_box = np.zeros(num_detected_boxes, dtype=bool)
  418. # The evaluation is done in two stages:
  419. # 1. All detections are matched to non group-of boxes; true positives are
  420. # determined and detections matched to difficult boxes are ignored.
  421. # 2. Detections that are determined as false positives are matched against
  422. # group-of boxes and scored with weight w per ground truth box is
  423. # matched.
  424. # Tp-fp evaluation for non-group of boxes (if any).
  425. if iou.shape[1] > 0:
  426. groundtruth_nongroup_of_is_difficult_list = groundtruth_is_difficult_list[
  427. ~groundtruth_is_group_of_list]
  428. max_overlap_gt_ids = np.argmax(iou, axis=1)
  429. is_gt_box_detected = np.zeros(iou.shape[1], dtype=bool)
  430. for i in range(num_detected_boxes):
  431. gt_id = max_overlap_gt_ids[i]
  432. if iou[i, gt_id] >= self.matching_iou_threshold:
  433. if not groundtruth_nongroup_of_is_difficult_list[gt_id]:
  434. if not is_gt_box_detected[gt_id]:
  435. tp_fp_labels[i] = True
  436. is_gt_box_detected[gt_id] = True
  437. else:
  438. is_matched_to_difficult_box[i] = True
  439. scores_group_of = np.zeros(ioa.shape[1], dtype=float)
  440. tp_fp_labels_group_of = self.group_of_weight * np.ones(
  441. ioa.shape[1], dtype=float)
  442. # Tp-fp evaluation for group of boxes.
  443. if ioa.shape[1] > 0:
  444. max_overlap_group_of_gt_ids = np.argmax(ioa, axis=1)
  445. for i in range(num_detected_boxes):
  446. gt_id = max_overlap_group_of_gt_ids[i]
  447. if (not tp_fp_labels[i] and not is_matched_to_difficult_box[i] and
  448. ioa[i, gt_id] >= self.matching_iou_threshold):
  449. is_matched_to_group_of_box[i] = True
  450. scores_group_of[gt_id] = max(scores_group_of[gt_id], scores[i])
  451. selector = np.where((scores_group_of > 0) & (tp_fp_labels_group_of > 0))
  452. scores_group_of = scores_group_of[selector]
  453. tp_fp_labels_group_of = tp_fp_labels_group_of[selector]
  454. return np.concatenate(
  455. (scores[~is_matched_to_difficult_box
  456. & ~is_matched_to_group_of_box],
  457. scores_group_of)), np.concatenate(
  458. (tp_fp_labels[~is_matched_to_difficult_box
  459. & ~is_matched_to_group_of_box].astype(float),
  460. tp_fp_labels_group_of))
  461. def _get_ith_class_arrays(self, detected_boxes, detected_scores,
  462. detected_masks, detected_class_labels,
  463. groundtruth_boxes, groundtruth_masks,
  464. groundtruth_class_labels, class_index):
  465. """Returns numpy arrays belonging to class with index `class_index`.
  466. Args:
  467. detected_boxes: A numpy array containing detected boxes.
  468. detected_scores: A numpy array containing detected scores.
  469. detected_masks: A numpy array containing detected masks.
  470. detected_class_labels: A numpy array containing detected class labels.
  471. groundtruth_boxes: A numpy array containing groundtruth boxes.
  472. groundtruth_masks: A numpy array containing groundtruth masks.
  473. groundtruth_class_labels: A numpy array containing groundtruth class
  474. labels.
  475. class_index: An integer index.
  476. Returns:
  477. gt_boxes_at_ith_class: A numpy array containing groundtruth boxes labeled
  478. as ith class.
  479. gt_masks_at_ith_class: A numpy array containing groundtruth masks labeled
  480. as ith class.
  481. detected_boxes_at_ith_class: A numpy array containing detected boxes
  482. corresponding to the ith class.
  483. detected_scores_at_ith_class: A numpy array containing detected scores
  484. corresponding to the ith class.
  485. detected_masks_at_ith_class: A numpy array containing detected masks
  486. corresponding to the ith class.
  487. """
  488. selected_groundtruth = (groundtruth_class_labels == class_index)
  489. gt_boxes_at_ith_class = groundtruth_boxes[selected_groundtruth]
  490. if groundtruth_masks is not None:
  491. gt_masks_at_ith_class = groundtruth_masks[selected_groundtruth]
  492. else:
  493. gt_masks_at_ith_class = None
  494. selected_detections = (detected_class_labels == class_index)
  495. detected_boxes_at_ith_class = detected_boxes[selected_detections]
  496. detected_scores_at_ith_class = detected_scores[selected_detections]
  497. if detected_masks is not None:
  498. detected_masks_at_ith_class = detected_masks[selected_detections]
  499. else:
  500. detected_masks_at_ith_class = None
  501. return (gt_boxes_at_ith_class, gt_masks_at_ith_class,
  502. detected_boxes_at_ith_class, detected_scores_at_ith_class,
  503. detected_masks_at_ith_class)
  504. def _remove_invalid_boxes(self, detected_boxes, detected_scores,
  505. detected_class_labels, detected_masks=None):
  506. """Removes entries with invalid boxes.
  507. A box is invalid if either its xmax is smaller than its xmin, or its ymax
  508. is smaller than its ymin.
  509. Args:
  510. detected_boxes: A float numpy array of size [num_boxes, 4] containing box
  511. coordinates in [ymin, xmin, ymax, xmax] format.
  512. detected_scores: A float numpy array of size [num_boxes].
  513. detected_class_labels: A int32 numpy array of size [num_boxes].
  514. detected_masks: A uint8 numpy array of size [num_boxes, height, width].
  515. Returns:
  516. valid_detected_boxes: A float numpy array of size [num_valid_boxes, 4]
  517. containing box coordinates in [ymin, xmin, ymax, xmax] format.
  518. valid_detected_scores: A float numpy array of size [num_valid_boxes].
  519. valid_detected_class_labels: A int32 numpy array of size
  520. [num_valid_boxes].
  521. valid_detected_masks: A uint8 numpy array of size
  522. [num_valid_boxes, height, width].
  523. """
  524. valid_indices = np.logical_and(detected_boxes[:, 0] < detected_boxes[:, 2],
  525. detected_boxes[:, 1] < detected_boxes[:, 3])
  526. detected_boxes = detected_boxes[valid_indices]
  527. detected_scores = detected_scores[valid_indices]
  528. detected_class_labels = detected_class_labels[valid_indices]
  529. if detected_masks is not None:
  530. detected_masks = detected_masks[valid_indices]
  531. return [
  532. detected_boxes, detected_scores, detected_class_labels, detected_masks
  533. ]