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.

89 lines
3.1 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.test_utils."""
  16. import numpy as np
  17. import tensorflow as tf
  18. from object_detection.utils import test_utils
  19. class TestUtilsTest(tf.test.TestCase):
  20. def test_diagonal_gradient_image(self):
  21. """Tests if a good pyramid image is created."""
  22. pyramid_image = test_utils.create_diagonal_gradient_image(3, 4, 2)
  23. # Test which is easy to understand.
  24. expected_first_channel = np.array([[3, 2, 1, 0],
  25. [4, 3, 2, 1],
  26. [5, 4, 3, 2]], dtype=np.float32)
  27. self.assertAllEqual(np.squeeze(pyramid_image[:, :, 0]),
  28. expected_first_channel)
  29. # Actual test.
  30. expected_image = np.array([[[3, 30],
  31. [2, 20],
  32. [1, 10],
  33. [0, 0]],
  34. [[4, 40],
  35. [3, 30],
  36. [2, 20],
  37. [1, 10]],
  38. [[5, 50],
  39. [4, 40],
  40. [3, 30],
  41. [2, 20]]], dtype=np.float32)
  42. self.assertAllEqual(pyramid_image, expected_image)
  43. def test_random_boxes(self):
  44. """Tests if valid random boxes are created."""
  45. num_boxes = 1000
  46. max_height = 3
  47. max_width = 5
  48. boxes = test_utils.create_random_boxes(num_boxes,
  49. max_height,
  50. max_width)
  51. true_column = np.ones(shape=(num_boxes)) == 1
  52. self.assertAllEqual(boxes[:, 0] < boxes[:, 2], true_column)
  53. self.assertAllEqual(boxes[:, 1] < boxes[:, 3], true_column)
  54. self.assertTrue(boxes[:, 0].min() >= 0)
  55. self.assertTrue(boxes[:, 1].min() >= 0)
  56. self.assertTrue(boxes[:, 2].max() <= max_height)
  57. self.assertTrue(boxes[:, 3].max() <= max_width)
  58. def test_first_rows_close_as_set(self):
  59. a = [1, 2, 3, 0, 0]
  60. b = [3, 2, 1, 0, 0]
  61. k = 3
  62. self.assertTrue(test_utils.first_rows_close_as_set(a, b, k))
  63. a = [[1, 2], [1, 4], [0, 0]]
  64. b = [[1, 4 + 1e-9], [1, 2], [0, 0]]
  65. k = 2
  66. self.assertTrue(test_utils.first_rows_close_as_set(a, b, k))
  67. a = [[1, 2], [1, 4], [0, 0]]
  68. b = [[1, 4 + 1e-9], [2, 2], [0, 0]]
  69. k = 2
  70. self.assertFalse(test_utils.first_rows_close_as_set(a, b, k))
  71. if __name__ == '__main__':
  72. tf.test.main()