File size: 5,524 Bytes
9a393e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Tests for object_detection.core.box_list."""

import tensorflow as tf

from object_detection.core import box_list


class BoxListTest(tf.test.TestCase):
  """Tests for BoxList class."""

  def test_num_boxes(self):
    data = tf.constant([[0, 0, 1, 1], [1, 1, 2, 3], [3, 4, 5, 5]], tf.float32)
    expected_num_boxes = 3

    boxes = box_list.BoxList(data)
    with self.test_session() as sess:
      num_boxes_output = sess.run(boxes.num_boxes())
      self.assertEquals(num_boxes_output, expected_num_boxes)

  def test_get_correct_center_coordinates_and_sizes(self):
    boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]]
    boxes = box_list.BoxList(tf.constant(boxes))
    centers_sizes = boxes.get_center_coordinates_and_sizes()
    expected_centers_sizes = [[15, 0.35], [12.5, 0.25], [10, 0.3], [5, 0.3]]
    with self.test_session() as sess:
      centers_sizes_out = sess.run(centers_sizes)
      self.assertAllClose(centers_sizes_out, expected_centers_sizes)

  def test_create_box_list_with_dynamic_shape(self):
    data = tf.constant([[0, 0, 1, 1], [1, 1, 2, 3], [3, 4, 5, 5]], tf.float32)
    indices = tf.reshape(tf.where(tf.greater([1, 0, 1], 0)), [-1])
    data = tf.gather(data, indices)
    assert data.get_shape().as_list() == [None, 4]
    expected_num_boxes = 2

    boxes = box_list.BoxList(data)
    with self.test_session() as sess:
      num_boxes_output = sess.run(boxes.num_boxes())
      self.assertEquals(num_boxes_output, expected_num_boxes)

  def test_transpose_coordinates(self):
    boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]]
    boxes = box_list.BoxList(tf.constant(boxes))
    boxes.transpose_coordinates()
    expected_corners = [[10.0, 10.0, 15.0, 20.0], [0.1, 0.2, 0.4, 0.5]]
    with self.test_session() as sess:
      corners_out = sess.run(boxes.get())
      self.assertAllClose(corners_out, expected_corners)

  def test_box_list_invalid_inputs(self):
    data0 = tf.constant([[[0, 0, 1, 1], [3, 4, 5, 5]]], tf.float32)
    data1 = tf.constant([[0, 0, 1], [1, 1, 2], [3, 4, 5]], tf.float32)
    data2 = tf.constant([[0, 0, 1], [1, 1, 2], [3, 4, 5]], tf.int32)

    with self.assertRaises(ValueError):
      _ = box_list.BoxList(data0)
    with self.assertRaises(ValueError):
      _ = box_list.BoxList(data1)
    with self.assertRaises(ValueError):
      _ = box_list.BoxList(data2)

  def test_num_boxes_static(self):
    box_corners = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]]
    boxes = box_list.BoxList(tf.constant(box_corners))
    self.assertEquals(boxes.num_boxes_static(), 2)
    self.assertEquals(type(boxes.num_boxes_static()), int)

  def test_num_boxes_static_for_uninferrable_shape(self):
    placeholder = tf.placeholder(tf.float32, shape=[None, 4])
    boxes = box_list.BoxList(placeholder)
    self.assertEquals(boxes.num_boxes_static(), None)

  def test_as_tensor_dict(self):
    boxlist = box_list.BoxList(
        tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]], tf.float32))
    boxlist.add_field('classes', tf.constant([0, 1]))
    boxlist.add_field('scores', tf.constant([0.75, 0.2]))
    tensor_dict = boxlist.as_tensor_dict()

    expected_boxes = [[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]]
    expected_classes = [0, 1]
    expected_scores = [0.75, 0.2]

    with self.test_session() as sess:
      tensor_dict_out = sess.run(tensor_dict)
      self.assertAllEqual(3, len(tensor_dict_out))
      self.assertAllClose(expected_boxes, tensor_dict_out['boxes'])
      self.assertAllEqual(expected_classes, tensor_dict_out['classes'])
      self.assertAllClose(expected_scores, tensor_dict_out['scores'])

  def test_as_tensor_dict_with_features(self):
    boxlist = box_list.BoxList(
        tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]], tf.float32))
    boxlist.add_field('classes', tf.constant([0, 1]))
    boxlist.add_field('scores', tf.constant([0.75, 0.2]))
    tensor_dict = boxlist.as_tensor_dict(['boxes', 'classes', 'scores'])

    expected_boxes = [[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]]
    expected_classes = [0, 1]
    expected_scores = [0.75, 0.2]

    with self.test_session() as sess:
      tensor_dict_out = sess.run(tensor_dict)
      self.assertAllEqual(3, len(tensor_dict_out))
      self.assertAllClose(expected_boxes, tensor_dict_out['boxes'])
      self.assertAllEqual(expected_classes, tensor_dict_out['classes'])
      self.assertAllClose(expected_scores, tensor_dict_out['scores'])

  def test_as_tensor_dict_missing_field(self):
    boxlist = box_list.BoxList(
        tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]], tf.float32))
    boxlist.add_field('classes', tf.constant([0, 1]))
    boxlist.add_field('scores', tf.constant([0.75, 0.2]))
    with self.assertRaises(ValueError):
      boxlist.as_tensor_dict(['foo', 'bar'])


if __name__ == '__main__':
  tf.test.main()