Beispiel #1
0
def assert_or_prune_invalid_boxes(boxes):
    """Makes sure boxes have valid sizes (ymax >= ymin, xmax >= xmin).

  When the hardware supports assertions, the function raises an error when
  boxes have an invalid size. If assertions are not supported (e.g. on TPU),
  boxes with invalid sizes are filtered out.

  Args:
    boxes: float tensor of shape [num_boxes, 4]

  Returns:
    boxes: float tensor of shape [num_valid_boxes, 4] with invalid boxes
      filtered out.

  Raises:
    tf.errors.InvalidArgumentError: When we detect boxes with invalid size.
      This is not supported on TPUs.
  """

    ymin, xmin, ymax, xmax = tf.split(boxes, num_or_size_splits=4, axis=1)

    height_check = tf.Assert(tf.reduce_all(ymax >= ymin), [ymin, ymax])
    width_check = tf.Assert(tf.reduce_all(xmax >= xmin), [xmin, xmax])

    with tf.control_dependencies([height_check, width_check]):
        boxes_tensor = tf.concat([ymin, xmin, ymax, xmax], axis=1)
        boxlist = box_list.BoxList(boxes_tensor)
        # TODO(b/149221748) Remove pruning when XLA supports assertions.
        boxlist = box_list_ops.prune_small_boxes(boxlist, 0)

    return boxlist.get()
Beispiel #2
0
 def graph_fn():
   boxes = tf.constant([[4.0, 3.0, 7.0, 5.0],
                        [5.0, 6.0, 10.0, 7.0],
                        [3.0, 4.0, 6.0, 8.0],
                        [14.0, 14.0, 15.0, 15.0],
                        [0.0, 0.0, 20.0, 20.0]])
   boxes = box_list.BoxList(boxes)
   pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3)
   return pruned_boxes.get()
 def test_prune_small_boxes(self):
     boxes = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0],
                          [3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0],
                          [0.0, 0.0, 20.0, 20.0]])
     exp_boxes = [[3.0, 4.0, 6.0, 8.0], [0.0, 0.0, 20.0, 20.0]]
     boxes = box_list.BoxList(boxes)
     pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3)
     with self.test_session() as sess:
         pruned_boxes = sess.run(pruned_boxes.get())
         self.assertAllEqual(pruned_boxes, exp_boxes)
Beispiel #4
0
 def graph_fn():
   boxes = tf.constant([[4.0, 3.0, 7.0, 5.0],
                        [5.0, 6.0, 10.0, 7.0],
                        [3.0, 4.0, 6.0, 8.0],
                        [14.0, 14.0, 15.0, 15.0],
                        [0.0, 0.0, 20.0, 20.0],
                        [2.0, 3.0, 1.5, 7.0],  # negative height
                        [2.0, 3.0, 5.0, 1.7]])  # negative width
   boxes = box_list.BoxList(boxes)
   pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3)
   return pruned_boxes.get()
 def test_prune_small_boxes(self):
   boxes = tf.constant([[4.0, 3.0, 7.0, 5.0],
                        [5.0, 6.0, 10.0, 7.0],
                        [3.0, 4.0, 6.0, 8.0],
                        [14.0, 14.0, 15.0, 15.0],
                        [0.0, 0.0, 20.0, 20.0]])
   exp_boxes = [[3.0, 4.0, 6.0, 8.0],
                [0.0, 0.0, 20.0, 20.0]]
   boxes = box_list.BoxList(boxes)
   pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3)
   with self.test_session() as sess:
     pruned_boxes = sess.run(pruned_boxes.get())
     self.assertAllEqual(pruned_boxes, exp_boxes)
 def test_prune_small_boxes_prunes_boxes_with_negative_side(self):
   boxes = tf.constant([[4.0, 3.0, 7.0, 5.0],
                        [5.0, 6.0, 10.0, 7.0],
                        [3.0, 4.0, 6.0, 8.0],
                        [14.0, 14.0, 15.0, 15.0],
                        [0.0, 0.0, 20.0, 20.0],
                        [2.0, 3.0, 1.5, 7.0],  # negative height
                        [2.0, 3.0, 5.0, 1.7]])  # negative width
   exp_boxes = [[3.0, 4.0, 6.0, 8.0],
                [0.0, 0.0, 20.0, 20.0]]
   boxes = box_list.BoxList(boxes)
   pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3)
   with self.test_session() as sess:
     pruned_boxes = sess.run(pruned_boxes.get())
     self.assertAllEqual(pruned_boxes, exp_boxes)
 def test_prune_small_boxes_prunes_boxes_with_negative_side(self):
   boxes = tf.constant([[4.0, 3.0, 7.0, 5.0],
                        [5.0, 6.0, 10.0, 7.0],
                        [3.0, 4.0, 6.0, 8.0],
                        [14.0, 14.0, 15.0, 15.0],
                        [0.0, 0.0, 20.0, 20.0],
                        [2.0, 3.0, 1.5, 7.0],  # negative height
                        [2.0, 3.0, 5.0, 1.7]])  # negative width
   exp_boxes = [[3.0, 4.0, 6.0, 8.0],
                [0.0, 0.0, 20.0, 20.0]]
   boxes = box_list.BoxList(boxes)
   pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3)
   with self.test_session() as sess:
     pruned_boxes = sess.run(pruned_boxes.get())
     self.assertAllEqual(pruned_boxes, exp_boxes)