def test_prune_non_overlapping_boxes(self):
        corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]])
        corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0],
                                [0.0, 0.0, 20.0, 20.0]])
        boxes1 = box_list.BoxList(corners1)
        boxes2 = box_list.BoxList(corners2)
        minoverlap = 0.5

        exp_output_1 = boxes1
        exp_output_2 = box_list.BoxList(tf.constant(0.0, shape=[0, 4]))
        output_1, keep_indices_1 = box_list_ops.prune_non_overlapping_boxes(
            boxes1, boxes2, min_overlap=minoverlap)
        output_2, keep_indices_2 = box_list_ops.prune_non_overlapping_boxes(
            boxes2, boxes1, min_overlap=minoverlap)
        with self.test_session() as sess:
            (output_1_, keep_indices_1_, output_2_, keep_indices_2_,
             exp_output_1_, exp_output_2_) = sess.run([
                 output_1.get(), keep_indices_1,
                 output_2.get(), keep_indices_2,
                 exp_output_1.get(),
                 exp_output_2.get()
             ])
            self.assertAllClose(output_1_, exp_output_1_)
            self.assertAllClose(output_2_, exp_output_2_)
            self.assertAllEqual(keep_indices_1_, [0, 1])
            self.assertAllEqual(keep_indices_2_, [])
Ejemplo n.º 2
0
    def graph_fn():
      corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]])
      corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0],
                              [0.0, 0.0, 20.0, 20.0]])
      boxes1 = box_list.BoxList(corners1)
      boxes2 = box_list.BoxList(corners2)
      minoverlap = 0.5

      exp_output_1 = boxes1
      exp_output_2 = box_list.BoxList(tf.constant(0.0, shape=[0, 4]))
      output_1, keep_indices_1 = box_list_ops.prune_non_overlapping_boxes(
          boxes1, boxes2, min_overlap=minoverlap)
      output_2, keep_indices_2 = box_list_ops.prune_non_overlapping_boxes(
          boxes2, boxes1, min_overlap=minoverlap)
      return (output_1.get(), keep_indices_1, output_2.get(), keep_indices_2,
              exp_output_1.get(), exp_output_2.get())
Ejemplo n.º 3
0
  def test_prune_non_overlapping_boxes(self):
    corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]])
    corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0],
                            [0.0, 0.0, 20.0, 20.0]])
    boxes1 = box_list.BoxList(corners1)
    boxes2 = box_list.BoxList(corners2)
    minoverlap = 0.5

    exp_output_1 = boxes1
    exp_output_2 = box_list.BoxList(tf.constant(0.0, shape=[0, 4]))
    output_1, keep_indices_1 = box_list_ops.prune_non_overlapping_boxes(
        boxes1, boxes2, min_overlap=minoverlap)
    output_2, keep_indices_2 = box_list_ops.prune_non_overlapping_boxes(
        boxes2, boxes1, min_overlap=minoverlap)
    with self.test_session() as sess:
      (output_1_, keep_indices_1_, output_2_, keep_indices_2_, exp_output_1_,
       exp_output_2_) = sess.run(
           [output_1.get(), keep_indices_1,
            output_2.get(), keep_indices_2,
            exp_output_1.get(), exp_output_2.get()])
      self.assertAllClose(output_1_, exp_output_1_)
      self.assertAllClose(output_2_, exp_output_2_)
      self.assertAllEqual(keep_indices_1_, [0, 1])
      self.assertAllEqual(keep_indices_2_, [])
Ejemplo n.º 4
0
def random_crop_to_aspect_ratio(image,
                                boxes,
                                labels,
                                difficult=None,
                                aspect_ratio=21. / 9.,
                                overlap_thresh=0.3):
    with tf.name_scope('RandomCropToAspectRatio', values=[image]):
        image_shape = tf.shape(image)
        orig_height = image_shape[0]
        orig_width = image_shape[1]
        orig_aspect_ratio = tf.to_float(orig_width) / tf.to_float(orig_height)
        target_aspect_ratio = tf.constant(aspect_ratio, dtype=tf.float32)

        def target_height_fn():
            return tf.to_int32(
                tf.round(tf.to_float(orig_width) / target_aspect_ratio))

        target_height = tf.cond(orig_aspect_ratio >= target_aspect_ratio,
                                lambda: orig_height, target_height_fn)

        def target_width_fn():
            return tf.to_int32(
                tf.round(tf.to_float(orig_height) * target_aspect_ratio))

        target_width = tf.cond(orig_aspect_ratio <= target_aspect_ratio,
                               lambda: orig_width, target_width_fn)

        offset_height = tf.random_uniform([],
                                          minval=0,
                                          maxval=orig_height - target_height +
                                          1,
                                          dtype=tf.int32)
        offset_width = tf.random_uniform([],
                                         minval=0,
                                         maxval=orig_width - target_width + 1,
                                         dtype=tf.int32)

        new_image = tf.image.crop_to_bounding_box(image, offset_height,
                                                  offset_width, target_height,
                                                  target_width)

        im_box = tf.stack([
            tf.to_float(offset_height) / tf.to_float(orig_height),
            tf.to_float(offset_width) / tf.to_float(orig_width),
            tf.to_float(offset_height + target_height) /
            tf.to_float(orig_height),
            tf.to_float(offset_width + target_width) / tf.to_float(orig_width)
        ])

        boxlist = box_list.BoxList(boxes)
        boxlist.add_field('labels', labels)

        if difficult is not None:
            boxlist.add_field('difficult', difficult)

        im_boxlist = box_list.BoxList(tf.expand_dims(im_box, axis=0))

        # remove boxes whose overlap with the image is less than overlap_thresh
        overlapping_boxlist, keep_ids = box_list_ops.prune_non_overlapping_boxes(
            boxlist, im_boxlist, overlap_thresh)

        # change the coordinate of the remaining boxes
        new_labels = overlapping_boxlist.get_field('labels')
        new_boxlist = box_list_ops.change_coordinate_frame(
            overlapping_boxlist, im_box)
        new_boxlist = box_list_ops.clip_to_window(
            new_boxlist, tf.constant([0.0, 0.0, 1.0, 1.0], tf.float32))
        new_boxes = new_boxlist.get()

        result = [new_image, new_boxes, new_labels]

        if difficult is not None:
            new_difficult = new_boxlist.get_field('difficult')
            result.append(new_difficult)

        return tuple(result)