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.

94 lines
3.9 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_vrd_evaluation."""
  16. import numpy as np
  17. import tensorflow as tf
  18. from object_detection.utils import per_image_vrd_evaluation
  19. class SingleClassPerImageVrdEvaluationTest(tf.test.TestCase):
  20. def setUp(self):
  21. matching_iou_threshold = 0.5
  22. self.eval = per_image_vrd_evaluation.PerImageVRDEvaluation(
  23. matching_iou_threshold)
  24. box_data_type = np.dtype([('subject', 'f4', (4,)), ('object', 'f4', (4,))])
  25. self.detected_box_tuples = np.array(
  26. [([0, 0, 1.1, 1], [1, 1, 2, 2]), ([0, 0, 1, 1], [1, 1, 2, 2]),
  27. ([1, 1, 2, 2], [0, 0, 1.1, 1])],
  28. dtype=box_data_type)
  29. self.detected_scores = np.array([0.8, 0.2, 0.1], dtype=float)
  30. self.groundtruth_box_tuples = np.array(
  31. [([0, 0, 1, 1], [1, 1, 2, 2])], dtype=box_data_type)
  32. def test_tp_fp_eval(self):
  33. tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  34. self.detected_box_tuples, self.groundtruth_box_tuples)
  35. expected_tp_fp_labels = np.array([True, False, False], dtype=bool)
  36. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  37. def test_tp_fp_eval_empty_gt(self):
  38. box_data_type = np.dtype([('subject', 'f4', (4,)), ('object', 'f4', (4,))])
  39. tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
  40. self.detected_box_tuples, np.array([], dtype=box_data_type))
  41. expected_tp_fp_labels = np.array([False, False, False], dtype=bool)
  42. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  43. class MultiClassPerImageVrdEvaluationTest(tf.test.TestCase):
  44. def setUp(self):
  45. matching_iou_threshold = 0.5
  46. self.eval = per_image_vrd_evaluation.PerImageVRDEvaluation(
  47. matching_iou_threshold)
  48. box_data_type = np.dtype([('subject', 'f4', (4,)), ('object', 'f4', (4,))])
  49. label_data_type = np.dtype([('subject', 'i4'), ('object', 'i4'),
  50. ('relation', 'i4')])
  51. self.detected_box_tuples = np.array(
  52. [([0, 0, 1, 1], [1, 1, 2, 2]), ([0, 0, 1.1, 1], [1, 1, 2, 2]),
  53. ([1, 1, 2, 2], [0, 0, 1.1, 1]), ([0, 0, 1, 1], [3, 4, 5, 6])],
  54. dtype=box_data_type)
  55. self.detected_class_tuples = np.array(
  56. [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 4, 5)], dtype=label_data_type)
  57. self.detected_scores = np.array([0.2, 0.8, 0.1, 0.5], dtype=float)
  58. self.groundtruth_box_tuples = np.array(
  59. [([0, 0, 1, 1], [1, 1, 2, 2]), ([1, 1, 2, 2], [0, 0, 1.1, 1]),
  60. ([0, 0, 1, 1], [3, 4, 5, 5.5])],
  61. dtype=box_data_type)
  62. self.groundtruth_class_tuples = np.array(
  63. [(1, 2, 3), (1, 7, 3), (1, 4, 5)], dtype=label_data_type)
  64. def test_tp_fp_eval(self):
  65. scores, tp_fp_labels, mapping = self.eval.compute_detection_tp_fp(
  66. self.detected_box_tuples, self.detected_scores,
  67. self.detected_class_tuples, self.groundtruth_box_tuples,
  68. self.groundtruth_class_tuples)
  69. expected_scores = np.array([0.8, 0.5, 0.2, 0.1], dtype=float)
  70. expected_tp_fp_labels = np.array([True, True, False, False], dtype=bool)
  71. expected_mapping = np.array([1, 3, 0, 2])
  72. self.assertTrue(np.allclose(expected_scores, scores))
  73. self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))
  74. self.assertTrue(np.allclose(expected_mapping, mapping))
  75. if __name__ == '__main__':
  76. tf.test.main()