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.

561 lines
27 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. """Tests for object_detection.utils.per_image_evaluation."""
  16. import numpy as np
  17. import tensorflow as tf
  18. from object_detection.utils import per_image_evaluation
  19. class SingleClassTpFpWithDifficultBoxesTest(tf.test.TestCase):
  20. def setUp(self):
  21. num_groundtruth_classes = 1
  22. matching_iou_threshold = 0.5
  23. nms_iou_threshold = 1.0
  24. nms_max_output_boxes = 10000
  25. self.eval = per_image_evaluation.PerImageEvaluation(
  26. num_groundtruth_classes, matching_iou_threshold, nms_iou_threshold,
  27. nms_max_output_boxes)
  28. self.detected_boxes = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3]],
  29. dtype=float)
  30. self.detected_scores = np.array([0.6, 0.8, 0.5], dtype=float)
  31. detected_masks_0 = np.array([[0, 1, 1, 0],
  32. [0, 0, 1, 0],
  33. [0, 0, 0, 0]], dtype=np.uint8)
  34. detected_masks_1 = np.array([[1, 0, 0, 0],
  35. [1, 1, 0, 0],
  36. [0, 0, 0, 0]], dtype=np.uint8)
  37. detected_masks_2 = np.array([[0, 0, 0, 0],
  38. [0, 1, 1, 0],
  39. [0, 1, 0, 0]], dtype=np.uint8)
  40. self.detected_masks = np.stack(
  41. [detected_masks_0, detected_masks_1, detected_masks_2], axis=0)
  42. self.groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 10, 10]],
  43. dtype=float)
  44. groundtruth_masks_0 = np.array([[1, 1, 0, 0],
  45. [1, 1, 0, 0],
  46. [0, 0, 0, 0]], dtype=np.uint8)
  47. groundtruth_masks_1 = np.array([[0, 0, 0, 1],
  48. [0, 0, 0, 1],
  49. [0, 0, 0, 1]], dtype=np.uint8)
  50. self.groundtruth_masks = np.stack(
  51. [groundtruth_masks_0, groundtruth_masks_1], axis=0)
  52. def test_match_to_gt_box_0(self):
  53. groundtruth_groundtruth_is_difficult_list = np.array([False, True],
  54. dtype=bool)
  55. groundtruth_groundtruth_is_group_of_list = np.array(
  56. [False, False], dtype=bool)
  57. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  58. self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
  59. groundtruth_groundtruth_is_difficult_list,
  60. groundtruth_groundtruth_is_group_of_list)
  61. expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  62. expected_tp_fp_labels = np.array([False, True, False], dtype=bool)
  63. self.assertTrue(np.allclose(expected_scores, scores))
  64. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  65. def test_mask_match_to_gt_mask_0(self):
  66. groundtruth_groundtruth_is_difficult_list = np.array([False, True],
  67. dtype=bool)
  68. groundtruth_groundtruth_is_group_of_list = np.array(
  69. [False, False], dtype=bool)
  70. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  71. self.detected_boxes,
  72. self.detected_scores,
  73. self.groundtruth_boxes,
  74. groundtruth_groundtruth_is_difficult_list,
  75. groundtruth_groundtruth_is_group_of_list,
  76. detected_masks=self.detected_masks,
  77. groundtruth_masks=self.groundtruth_masks)
  78. expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  79. expected_tp_fp_labels = np.array([True, False, False], dtype=bool)
  80. self.assertTrue(np.allclose(expected_scores, scores))
  81. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  82. def test_match_to_gt_box_1(self):
  83. groundtruth_groundtruth_is_difficult_list = np.array([True, False],
  84. dtype=bool)
  85. groundtruth_groundtruth_is_group_of_list = np.array(
  86. [False, False], dtype=bool)
  87. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  88. self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
  89. groundtruth_groundtruth_is_difficult_list,
  90. groundtruth_groundtruth_is_group_of_list)
  91. expected_scores = np.array([0.8, 0.5], dtype=float)
  92. expected_tp_fp_labels = np.array([False, False], dtype=bool)
  93. self.assertTrue(np.allclose(expected_scores, scores))
  94. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  95. def test_mask_match_to_gt_mask_1(self):
  96. groundtruth_groundtruth_is_difficult_list = np.array([True, False],
  97. dtype=bool)
  98. groundtruth_groundtruth_is_group_of_list = np.array(
  99. [False, False], dtype=bool)
  100. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  101. self.detected_boxes,
  102. self.detected_scores,
  103. self.groundtruth_boxes,
  104. groundtruth_groundtruth_is_difficult_list,
  105. groundtruth_groundtruth_is_group_of_list,
  106. detected_masks=self.detected_masks,
  107. groundtruth_masks=self.groundtruth_masks)
  108. expected_scores = np.array([0.6, 0.5], dtype=float)
  109. expected_tp_fp_labels = np.array([False, False], dtype=bool)
  110. self.assertTrue(np.allclose(expected_scores, scores))
  111. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  112. class SingleClassTpFpWithGroupOfBoxesTest(tf.test.TestCase):
  113. def setUp(self):
  114. num_groundtruth_classes = 1
  115. matching_iou_threshold = 0.5
  116. nms_iou_threshold = 1.0
  117. nms_max_output_boxes = 10000
  118. self.eval = per_image_evaluation.PerImageEvaluation(
  119. num_groundtruth_classes, matching_iou_threshold, nms_iou_threshold,
  120. nms_max_output_boxes)
  121. self.detected_boxes = np.array(
  122. [[0, 0, 1, 1], [0, 0, 2, 1], [0, 0, 3, 1]], dtype=float)
  123. self.detected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  124. detected_masks_0 = np.array([[0, 1, 1, 0],
  125. [0, 0, 1, 0],
  126. [0, 0, 0, 0]], dtype=np.uint8)
  127. detected_masks_1 = np.array([[1, 0, 0, 0],
  128. [1, 1, 0, 0],
  129. [0, 0, 0, 0]], dtype=np.uint8)
  130. detected_masks_2 = np.array([[0, 0, 0, 0],
  131. [0, 1, 1, 0],
  132. [0, 1, 0, 0]], dtype=np.uint8)
  133. self.detected_masks = np.stack(
  134. [detected_masks_0, detected_masks_1, detected_masks_2], axis=0)
  135. self.groundtruth_boxes = np.array(
  136. [[0, 0, 1, 1], [0, 0, 5, 5], [10, 10, 20, 20]], dtype=float)
  137. groundtruth_masks_0 = np.array([[1, 0, 0, 0],
  138. [1, 0, 0, 0],
  139. [1, 0, 0, 0]], dtype=np.uint8)
  140. groundtruth_masks_1 = np.array([[0, 0, 1, 0],
  141. [0, 0, 1, 0],
  142. [0, 0, 1, 0]], dtype=np.uint8)
  143. groundtruth_masks_2 = np.array([[0, 1, 0, 0],
  144. [0, 1, 0, 0],
  145. [0, 1, 0, 0]], dtype=np.uint8)
  146. self.groundtruth_masks = np.stack(
  147. [groundtruth_masks_0, groundtruth_masks_1, groundtruth_masks_2], axis=0)
  148. def test_match_to_non_group_of_and_group_of_box(self):
  149. groundtruth_groundtruth_is_difficult_list = np.array(
  150. [False, False, False], dtype=bool)
  151. groundtruth_groundtruth_is_group_of_list = np.array(
  152. [False, True, True], dtype=bool)
  153. expected_scores = np.array([0.8], dtype=float)
  154. expected_tp_fp_labels = np.array([True], dtype=bool)
  155. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  156. self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
  157. groundtruth_groundtruth_is_difficult_list,
  158. groundtruth_groundtruth_is_group_of_list)
  159. self.assertTrue(np.allclose(expected_scores, scores))
  160. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  161. def test_mask_match_to_non_group_of_and_group_of_box(self):
  162. groundtruth_groundtruth_is_difficult_list = np.array(
  163. [False, False, False], dtype=bool)
  164. groundtruth_groundtruth_is_group_of_list = np.array(
  165. [False, True, True], dtype=bool)
  166. expected_scores = np.array([0.6], dtype=float)
  167. expected_tp_fp_labels = np.array([True], dtype=bool)
  168. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  169. self.detected_boxes,
  170. self.detected_scores,
  171. self.groundtruth_boxes,
  172. groundtruth_groundtruth_is_difficult_list,
  173. groundtruth_groundtruth_is_group_of_list,
  174. detected_masks=self.detected_masks,
  175. groundtruth_masks=self.groundtruth_masks)
  176. self.assertTrue(np.allclose(expected_scores, scores))
  177. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  178. def test_match_two_to_group_of_box(self):
  179. groundtruth_groundtruth_is_difficult_list = np.array(
  180. [False, False, False], dtype=bool)
  181. groundtruth_groundtruth_is_group_of_list = np.array(
  182. [True, False, True], dtype=bool)
  183. expected_scores = np.array([0.5], dtype=float)
  184. expected_tp_fp_labels = np.array([False], dtype=bool)
  185. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  186. self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
  187. groundtruth_groundtruth_is_difficult_list,
  188. groundtruth_groundtruth_is_group_of_list)
  189. self.assertTrue(np.allclose(expected_scores, scores))
  190. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  191. def test_mask_match_two_to_group_of_box(self):
  192. groundtruth_groundtruth_is_difficult_list = np.array(
  193. [False, False, False], dtype=bool)
  194. groundtruth_groundtruth_is_group_of_list = np.array(
  195. [True, False, True], dtype=bool)
  196. expected_scores = np.array([0.8], dtype=float)
  197. expected_tp_fp_labels = np.array([True], dtype=bool)
  198. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  199. self.detected_boxes,
  200. self.detected_scores,
  201. self.groundtruth_boxes,
  202. groundtruth_groundtruth_is_difficult_list,
  203. groundtruth_groundtruth_is_group_of_list,
  204. detected_masks=self.detected_masks,
  205. groundtruth_masks=self.groundtruth_masks)
  206. self.assertTrue(np.allclose(expected_scores, scores))
  207. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  208. class SingleClassTpFpWithGroupOfBoxesTestWeighted(tf.test.TestCase):
  209. def setUp(self):
  210. num_groundtruth_classes = 1
  211. matching_iou_threshold = 0.5
  212. nms_iou_threshold = 1.0
  213. nms_max_output_boxes = 10000
  214. self.group_of_weight = 0.5
  215. self.eval = per_image_evaluation.PerImageEvaluation(
  216. num_groundtruth_classes, matching_iou_threshold, nms_iou_threshold,
  217. nms_max_output_boxes, self.group_of_weight)
  218. self.detected_boxes = np.array(
  219. [[0, 0, 1, 1], [0, 0, 2, 1], [0, 0, 3, 1]], dtype=float)
  220. self.detected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  221. detected_masks_0 = np.array(
  222. [[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]], dtype=np.uint8)
  223. detected_masks_1 = np.array(
  224. [[1, 0, 0, 0], [1, 1, 0, 0], [0, 0, 0, 0]], dtype=np.uint8)
  225. detected_masks_2 = np.array(
  226. [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 0, 0]], dtype=np.uint8)
  227. self.detected_masks = np.stack(
  228. [detected_masks_0, detected_masks_1, detected_masks_2], axis=0)
  229. self.groundtruth_boxes = np.array(
  230. [[0, 0, 1, 1], [0, 0, 5, 5], [10, 10, 20, 20]], dtype=float)
  231. groundtruth_masks_0 = np.array(
  232. [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]], dtype=np.uint8)
  233. groundtruth_masks_1 = np.array(
  234. [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]], dtype=np.uint8)
  235. groundtruth_masks_2 = np.array(
  236. [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]], dtype=np.uint8)
  237. self.groundtruth_masks = np.stack(
  238. [groundtruth_masks_0, groundtruth_masks_1, groundtruth_masks_2], axis=0)
  239. def test_match_to_non_group_of_and_group_of_box(self):
  240. groundtruth_groundtruth_is_difficult_list = np.array(
  241. [False, False, False], dtype=bool)
  242. groundtruth_groundtruth_is_group_of_list = np.array(
  243. [False, True, True], dtype=bool)
  244. expected_scores = np.array([0.8, 0.6], dtype=float)
  245. expected_tp_fp_labels = np.array([1.0, self.group_of_weight], dtype=float)
  246. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  247. self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
  248. groundtruth_groundtruth_is_difficult_list,
  249. groundtruth_groundtruth_is_group_of_list)
  250. self.assertTrue(np.allclose(expected_scores, scores))
  251. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  252. def test_mask_match_to_non_group_of_and_group_of_box(self):
  253. groundtruth_groundtruth_is_difficult_list = np.array(
  254. [False, False, False], dtype=bool)
  255. groundtruth_groundtruth_is_group_of_list = np.array(
  256. [False, True, True], dtype=bool)
  257. expected_scores = np.array([0.6, 0.8, 0.5], dtype=float)
  258. expected_tp_fp_labels = np.array(
  259. [1.0, self.group_of_weight, self.group_of_weight], dtype=float)
  260. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  261. self.detected_boxes,
  262. self.detected_scores,
  263. self.groundtruth_boxes,
  264. groundtruth_groundtruth_is_difficult_list,
  265. groundtruth_groundtruth_is_group_of_list,
  266. detected_masks=self.detected_masks,
  267. groundtruth_masks=self.groundtruth_masks)
  268. tf.logging.info(
  269. "test_mask_match_to_non_group_of_and_group_of_box {} {}".format(
  270. tp_fp_labels, expected_tp_fp_labels))
  271. self.assertTrue(np.allclose(expected_scores, scores))
  272. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  273. def test_match_two_to_group_of_box(self):
  274. groundtruth_groundtruth_is_difficult_list = np.array(
  275. [False, False, False], dtype=bool)
  276. groundtruth_groundtruth_is_group_of_list = np.array(
  277. [True, False, True], dtype=bool)
  278. expected_scores = np.array([0.5, 0.8], dtype=float)
  279. expected_tp_fp_labels = np.array([0.0, self.group_of_weight], dtype=float)
  280. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  281. self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
  282. groundtruth_groundtruth_is_difficult_list,
  283. groundtruth_groundtruth_is_group_of_list)
  284. tf.logging.info("test_match_two_to_group_of_box {} {}".format(
  285. tp_fp_labels, expected_tp_fp_labels))
  286. self.assertTrue(np.allclose(expected_scores, scores))
  287. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  288. def test_mask_match_two_to_group_of_box(self):
  289. groundtruth_groundtruth_is_difficult_list = np.array(
  290. [False, False, False], dtype=bool)
  291. groundtruth_groundtruth_is_group_of_list = np.array(
  292. [True, False, True], dtype=bool)
  293. expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  294. expected_tp_fp_labels = np.array(
  295. [1.0, self.group_of_weight, self.group_of_weight], dtype=float)
  296. scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  297. self.detected_boxes,
  298. self.detected_scores,
  299. self.groundtruth_boxes,
  300. groundtruth_groundtruth_is_difficult_list,
  301. groundtruth_groundtruth_is_group_of_list,
  302. detected_masks=self.detected_masks,
  303. groundtruth_masks=self.groundtruth_masks)
  304. tf.logging.info("test_mask_match_two_to_group_of_box {} {}".format(
  305. tp_fp_labels, expected_tp_fp_labels))
  306. self.assertTrue(np.allclose(expected_scores, scores))
  307. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  308. class SingleClassTpFpNoDifficultBoxesTest(tf.test.TestCase):
  309. def setUp(self):
  310. num_groundtruth_classes = 1
  311. matching_iou_threshold_high_iou = 0.5
  312. matching_iou_threshold_low_iou = 0.1
  313. nms_iou_threshold = 1.0
  314. nms_max_output_boxes = 10000
  315. self.eval_high_iou = per_image_evaluation.PerImageEvaluation(
  316. num_groundtruth_classes, matching_iou_threshold_high_iou,
  317. nms_iou_threshold, nms_max_output_boxes)
  318. self.eval_low_iou = per_image_evaluation.PerImageEvaluation(
  319. num_groundtruth_classes, matching_iou_threshold_low_iou,
  320. nms_iou_threshold, nms_max_output_boxes)
  321. self.detected_boxes = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3]],
  322. dtype=float)
  323. self.detected_scores = np.array([0.6, 0.8, 0.5], dtype=float)
  324. detected_masks_0 = np.array([[0, 1, 1, 0],
  325. [0, 0, 1, 0],
  326. [0, 0, 0, 0]], dtype=np.uint8)
  327. detected_masks_1 = np.array([[1, 0, 0, 0],
  328. [1, 1, 0, 0],
  329. [0, 0, 0, 0]], dtype=np.uint8)
  330. detected_masks_2 = np.array([[0, 0, 0, 0],
  331. [0, 1, 1, 0],
  332. [0, 1, 0, 0]], dtype=np.uint8)
  333. self.detected_masks = np.stack(
  334. [detected_masks_0, detected_masks_1, detected_masks_2], axis=0)
  335. def test_no_true_positives(self):
  336. groundtruth_boxes = np.array([[100, 100, 105, 105]], dtype=float)
  337. groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
  338. groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
  339. scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
  340. self.detected_boxes, self.detected_scores, groundtruth_boxes,
  341. groundtruth_groundtruth_is_difficult_list,
  342. groundtruth_groundtruth_is_group_of_list)
  343. expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  344. expected_tp_fp_labels = np.array([False, False, False], dtype=bool)
  345. self.assertTrue(np.allclose(expected_scores, scores))
  346. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  347. def test_mask_no_true_positives(self):
  348. groundtruth_boxes = np.array([[100, 100, 105, 105]], dtype=float)
  349. groundtruth_masks_0 = np.array([[1, 1, 1, 1],
  350. [1, 1, 1, 1],
  351. [1, 1, 1, 1]], dtype=np.uint8)
  352. groundtruth_masks = np.stack([groundtruth_masks_0], axis=0)
  353. groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
  354. groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
  355. scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
  356. self.detected_boxes,
  357. self.detected_scores,
  358. groundtruth_boxes,
  359. groundtruth_groundtruth_is_difficult_list,
  360. groundtruth_groundtruth_is_group_of_list,
  361. detected_masks=self.detected_masks,
  362. groundtruth_masks=groundtruth_masks)
  363. expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  364. expected_tp_fp_labels = np.array([False, False, False], dtype=bool)
  365. self.assertTrue(np.allclose(expected_scores, scores))
  366. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  367. def test_one_true_positives_with_large_iou_threshold(self):
  368. groundtruth_boxes = np.array([[0, 0, 1, 1]], dtype=float)
  369. groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
  370. groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
  371. scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
  372. self.detected_boxes, self.detected_scores, groundtruth_boxes,
  373. groundtruth_groundtruth_is_difficult_list,
  374. groundtruth_groundtruth_is_group_of_list)
  375. expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  376. expected_tp_fp_labels = np.array([False, True, False], dtype=bool)
  377. self.assertTrue(np.allclose(expected_scores, scores))
  378. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  379. def test_mask_one_true_positives_with_large_iou_threshold(self):
  380. groundtruth_boxes = np.array([[0, 0, 1, 1]], dtype=float)
  381. groundtruth_masks_0 = np.array([[1, 0, 0, 0],
  382. [1, 1, 0, 0],
  383. [0, 0, 0, 0]], dtype=np.uint8)
  384. groundtruth_masks = np.stack([groundtruth_masks_0], axis=0)
  385. groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
  386. groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
  387. scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
  388. self.detected_boxes,
  389. self.detected_scores,
  390. groundtruth_boxes,
  391. groundtruth_groundtruth_is_difficult_list,
  392. groundtruth_groundtruth_is_group_of_list,
  393. detected_masks=self.detected_masks,
  394. groundtruth_masks=groundtruth_masks)
  395. expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  396. expected_tp_fp_labels = np.array([True, False, False], dtype=bool)
  397. self.assertTrue(np.allclose(expected_scores, scores))
  398. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  399. def test_one_true_positives_with_very_small_iou_threshold(self):
  400. groundtruth_boxes = np.array([[0, 0, 1, 1]], dtype=float)
  401. groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
  402. groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
  403. scores, tp_fp_labels = self.eval_low_iou._compute_tp_fp_for_single_class(
  404. self.detected_boxes, self.detected_scores, groundtruth_boxes,
  405. groundtruth_groundtruth_is_difficult_list,
  406. groundtruth_groundtruth_is_group_of_list)
  407. expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  408. expected_tp_fp_labels = np.array([True, False, False], dtype=bool)
  409. self.assertTrue(np.allclose(expected_scores, scores))
  410. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  411. def test_two_true_positives_with_large_iou_threshold(self):
  412. groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 3.5, 3.5]], dtype=float)
  413. groundtruth_groundtruth_is_difficult_list = np.zeros(2, dtype=bool)
  414. groundtruth_groundtruth_is_group_of_list = np.array(
  415. [False, False], dtype=bool)
  416. scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
  417. self.detected_boxes, self.detected_scores, groundtruth_boxes,
  418. groundtruth_groundtruth_is_difficult_list,
  419. groundtruth_groundtruth_is_group_of_list)
  420. expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
  421. expected_tp_fp_labels = np.array([False, True, True], dtype=bool)
  422. self.assertTrue(np.allclose(expected_scores, scores))
  423. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  424. class MultiClassesTpFpTest(tf.test.TestCase):
  425. def test_tp_fp(self):
  426. num_groundtruth_classes = 3
  427. matching_iou_threshold = 0.5
  428. nms_iou_threshold = 1.0
  429. nms_max_output_boxes = 10000
  430. eval1 = per_image_evaluation.PerImageEvaluation(num_groundtruth_classes,
  431. matching_iou_threshold,
  432. nms_iou_threshold,
  433. nms_max_output_boxes)
  434. detected_boxes = np.array([[0, 0, 1, 1], [10, 10, 5, 5], [0, 0, 2, 2],
  435. [5, 10, 10, 5], [10, 5, 5, 10], [0, 0, 3, 3]],
  436. dtype=float)
  437. detected_scores = np.array([0.8, 0.1, 0.8, 0.9, 0.7, 0.8], dtype=float)
  438. detected_class_labels = np.array([0, 1, 1, 2, 0, 2], dtype=int)
  439. groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 3.5, 3.5]], dtype=float)
  440. groundtruth_class_labels = np.array([0, 2], dtype=int)
  441. groundtruth_groundtruth_is_difficult_list = np.zeros(2, dtype=float)
  442. groundtruth_groundtruth_is_group_of_list = np.array(
  443. [False, False], dtype=bool)
  444. scores, tp_fp_labels, _ = eval1.compute_object_detection_metrics(
  445. detected_boxes, detected_scores, detected_class_labels,
  446. groundtruth_boxes, groundtruth_class_labels,
  447. groundtruth_groundtruth_is_difficult_list,
  448. groundtruth_groundtruth_is_group_of_list)
  449. expected_scores = [np.array([0.8], dtype=float)] * 3
  450. expected_tp_fp_labels = [np.array([True]), np.array([False]), np.array([True
  451. ])]
  452. for i in range(len(expected_scores)):
  453. self.assertTrue(np.allclose(expected_scores[i], scores[i]))
  454. self.assertTrue(np.array_equal(expected_tp_fp_labels[i], tp_fp_labels[i]))
  455. class CorLocTest(tf.test.TestCase):
  456. def test_compute_corloc_with_normal_iou_threshold(self):
  457. num_groundtruth_classes = 3
  458. matching_iou_threshold = 0.5
  459. nms_iou_threshold = 1.0
  460. nms_max_output_boxes = 10000
  461. eval1 = per_image_evaluation.PerImageEvaluation(num_groundtruth_classes,
  462. matching_iou_threshold,
  463. nms_iou_threshold,
  464. nms_max_output_boxes)
  465. detected_boxes = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3],
  466. [0, 0, 5, 5]], dtype=float)
  467. detected_scores = np.array([0.9, 0.9, 0.1, 0.9], dtype=float)
  468. detected_class_labels = np.array([0, 1, 0, 2], dtype=int)
  469. groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 3, 3], [0, 0, 6, 6]],
  470. dtype=float)
  471. groundtruth_class_labels = np.array([0, 0, 2], dtype=int)
  472. is_class_correctly_detected_in_image = eval1._compute_cor_loc(
  473. detected_boxes, detected_scores, detected_class_labels,
  474. groundtruth_boxes, groundtruth_class_labels)
  475. expected_result = np.array([1, 0, 1], dtype=int)
  476. self.assertTrue(np.array_equal(expected_result,
  477. is_class_correctly_detected_in_image))
  478. def test_compute_corloc_with_very_large_iou_threshold(self):
  479. num_groundtruth_classes = 3
  480. matching_iou_threshold = 0.9
  481. nms_iou_threshold = 1.0
  482. nms_max_output_boxes = 10000
  483. eval1 = per_image_evaluation.PerImageEvaluation(num_groundtruth_classes,
  484. matching_iou_threshold,
  485. nms_iou_threshold,
  486. nms_max_output_boxes)
  487. detected_boxes = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3],
  488. [0, 0, 5, 5]], dtype=float)
  489. detected_scores = np.array([0.9, 0.9, 0.1, 0.9], dtype=float)
  490. detected_class_labels = np.array([0, 1, 0, 2], dtype=int)
  491. groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 3, 3], [0, 0, 6, 6]],
  492. dtype=float)
  493. groundtruth_class_labels = np.array([0, 0, 2], dtype=int)
  494. is_class_correctly_detected_in_image = eval1._compute_cor_loc(
  495. detected_boxes, detected_scores, detected_class_labels,
  496. groundtruth_boxes, groundtruth_class_labels)
  497. expected_result = np.array([1, 0, 0], dtype=int)
  498. self.assertTrue(np.array_equal(expected_result,
  499. is_class_correctly_detected_in_image))
  500. if __name__ == "__main__":
  501. tf.test.main()