def test_prune_non_overlapping_boxes(self):
        rbox1 = tf.constant([[4.0, 3.0, 7.0, 5.0, 0.1],
                             [5.0, 6.0, 10.0, 7.0, 0.2]])
        rbox2 = tf.constant([[3.0, 4.0, 6.0, 8.0, 0.1],
                             [14.0, 14.0, 15.0, 15.0, 0.2],
                             [0.0, 0.0, 20.0, 20.0, 0.3]])
        boxes1 = rbox_list.RBoxList(rbox1)
        boxes2 = rbox_list.RBoxList(rbox2)
        minoverlap = 0.5

        exp_output_1 = boxes1
        exp_output_2 = rbox_list.RBoxList(rbox2[:1])
        output_1, keep_indices_1 = rbox_list_ops.prune_non_overlapping_boxes(
            boxes1, boxes2, min_overlap=minoverlap)
        output_2, keep_indices_2 = rbox_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_, [0])
 def test_concatenate_with_missing_fields(self):
     corners1 = tf.constant([[0, 0, 0, 0, 0], [1, 2, 3, 4, 5]], tf.float32)
     scores1 = tf.constant([1.0, 2.1])
     corners2 = tf.constant([[0, 3, 1, 6, 7], [2, 4, 3, 8, 9]], tf.float32)
     boxlist1 = rbox_list.RBoxList(corners1)
     boxlist1.add_field('scores', scores1)
     boxlist2 = rbox_list.RBoxList(corners2)
     with self.assertRaises(ValueError):
         rbox_list_ops.concatenate([boxlist1, boxlist2])
Example #3
0
    def test_batch_assign_multidimensional_targets_rbox(self):
        rbox_list1 = rbox_list.RBoxList(tf.constant([[0., 0., 0.2, 0.2, 0.0]]))
        rbox_list2 = rbox_list.RBoxList(
            tf.constant([[0, 0.25123152, 1, 1, 0.0],
                         [0.015789, 0.0985, 0.55789, 0.3842, 0.0]]))

        gt_box_batch = [rbox_list1, rbox_list2]
        class_targets1 = tf.constant([[[0, 1, 1], [1, 1, 0]]], tf.float32)
        class_targets2 = tf.constant(
            [[[0, 1, 1], [1, 1, 0]], [[0, 0, 1], [0, 0, 1]]], tf.float32)

        gt_class_targets = [class_targets1, class_targets2]

        anchors = tf.constant([[0, 0, .25, .25, 0.0], [0, .25, 1, 1, 0.0],
                               [0, .1, .5, .5, 0.0], [.75, .75, 1, 1, 0.0]])
        anchors_rbox = rbox_list.RBoxList(anchors)

        exp_reg_targets = [[[0, 0, -0.223143, -0.223143, 0.0], [0, 0, 0, 0, 0],
                            [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
                           [[0, 0, 0, 0, 0], [0, 0.00123152, 0, 0, 0.0],
                            [0.031578, -0.00300001, 0.109554, -0.263445, 0.0],
                            [0, 0, 0, 0, 0]]]
        exp_cls_weights = [[1, 1, 1, 1], [1, 1, 1, 1]]

        exp_cls_targets = [[[[0., 1., 1.], [1., 1., 0.]],
                            [[0., 0., 0.], [0., 0., 0.]],
                            [[0., 0., 0.], [0., 0., 0.]],
                            [[0., 0., 0.], [0., 0., 0.]]],
                           [[[0., 0., 0.], [0., 0., 0.]],
                            [[0., 1., 1.], [1., 1., 0.]],
                            [[0., 0., 1.], [0., 0., 1.]],
                            [[0., 0., 0.], [0., 0., 0.]]]]
        exp_reg_weights = [[1, 0, 0, 0], [0, 1, 1, 0]]
        exp_match_0 = [0]
        exp_match_1 = [1, 2]

        multiclass_target_assigner = self._get_multi_dimensional_target_assigner_rbox(
            target_dimensions=(2, 3))

        (cls_targets, cls_weights, reg_targets, reg_weights, match_list) = \
            targetassigner.batch_assign_targets(multiclass_target_assigner,
                                                anchors_rbox,
                                                gt_box_batch,
                                                gt_class_targets)
        self.assertTrue(isinstance(match_list, list) and len(match_list) == 2)
        with self.test_session() as sess:
            (cls_targets_out, cls_weights_out, reg_targets_out,
             reg_weights_out, match_out_0, match_out_1) = sess.run(
                 [cls_targets, cls_weights, reg_targets, reg_weights] +
                 [match.matched_column_indices() for match in match_list])
            self.assertAllClose(cls_targets_out, exp_cls_targets)
            self.assertAllClose(cls_weights_out, exp_cls_weights)
            self.assertAllClose(reg_targets_out, exp_reg_targets)
            self.assertAllClose(reg_weights_out, exp_reg_weights)
            self.assertAllClose(match_out_0, exp_match_0)
            self.assertAllClose(match_out_1, exp_match_1)
 def test_gather_with_invalid_inputs(self):
     corners = tf.constant(
         [5 * [0.0], 5 * [1.0], 5 * [2.0], 5 * [3.0], 5 * [4.0]])
     indices_float32 = tf.constant([0, 2, 4], tf.float32)
     boxes = rbox_list.RBoxList(corners)
     with self.assertRaises(ValueError):
         _ = rbox_list_ops.gather(boxes, indices_float32)
     indices_2d = tf.constant([[0, 2, 4]], tf.int32)
     boxes = rbox_list.RBoxList(corners)
     with self.assertRaises(ValueError):
         _ = rbox_list_ops.gather(boxes, indices_2d)
 def test_concatenate_with_incompatible_field_shapes(self):
     rbox1 = tf.constant([[0, 0, 0, 0, 0], [1, 2, 3, 4, 5]], tf.float32)
     scores1 = tf.constant([1.0, 2.1])
     robx2 = tf.constant([[0, 3, 1, 6, 7], [2, 4, 3, 8, 9]], tf.float32)
     scores2 = tf.constant([[1.0, 1.0], [2.1, 3.2]])
     boxlist1 = rbox_list.RBoxList(rbox1)
     boxlist1.add_field('scores', scores1)
     boxlist2 = rbox_list.RBoxList(robx2)
     boxlist2.add_field('scores', scores2)
     with self.assertRaises(ValueError):
         rbox_list_ops.concatenate([boxlist1, boxlist2])
    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):
            _ = rbox_list.RBoxList(data0)
        with self.assertRaises(ValueError):
            _ = rbox_list.RBoxList(data1)
        with self.assertRaises(ValueError):
            _ = rbox_list.RBoxList(data2)
Example #7
0
 def test_get_correct_pairwise_similarity_based_on_iou_rbox(self):
     corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0, 0.0], [5.0, 6.0, 10.0, 7.0, 0.0]])
     corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0, 0.0], [14.0, 14.0, 15.0, 15.0, 0.0],
                             [0.0, 0.0, 20.0, 20.0, 0.0]])
     exp_output = [[0.495495, 0, 0.0875], [0.388235,  0.036907,  0.175]]
     rboxes1 = rbox_list.RBoxList(corners1)
     rboxes2 = rbox_list.RBoxList(corners2)
     iou_similarity_calculator = region_similarity_calculator.IouSimilarity()
     iou_similarity = iou_similarity_calculator.compare(rboxes1, rboxes2)
     with self.test_session() as sess:
         iou_output = sess.run(iou_similarity)
         self.assertAllClose(iou_output, exp_output)
 def test_matched_iou(self):
     rbox1 = tf.constant([[4.0, 3.0, 7.0, 5.0, 0.1],
                          [5.0, 6.0, 10.0, 7.0, 0.2]])
     rbox2 = tf.constant([[3.0, 4.0, 6.0, 8.0, 0.1],
                          [14.0, 14.0, 15.0, 15.0, 0.2]])
     exp_output = [27.025812 / 55.974188, 7.2258 / 287.7742]
     boxes1 = rbox_list.RBoxList(rbox1)
     boxes2 = rbox_list.RBoxList(rbox2)
     iou = rbox_list_ops.matched_iou(boxes1, boxes2)
     with self.test_session() as sess:
         iou_output = sess.run(iou)
         self.assertAllClose(iou_output, exp_output)
 def test_matched_intersection(self):
     rbox1 = tf.constant([[4.0, 3.0, 7.0, 5.0, 0.1],
                          [5.0, 6.0, 10.0, 7.0, 0.2]])
     rbox2 = tf.constant([[3.0, 4.0, 6.0, 8.0, 0.1],
                          [14.0, 14.0, 15.0, 15.0, 0.2]])
     exp_output = [27.025812, 7.2258]
     boxes1 = rbox_list.RBoxList(rbox1)
     boxes2 = rbox_list.RBoxList(rbox2)
     intersect = rbox_list_ops.matched_intersection(boxes1, boxes2)
     with self.test_session() as sess:
         intersect_output = sess.run(intersect)
         self.assertAllClose(intersect_output, exp_output)
Example #10
0
    def test_assign_multidimensional_class_targets_rbox(self):
        similarity_calc = region_similarity_calculator.IouSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                               unmatched_threshold=0.3)
        box_coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder()
        unmatched_cls_target = tf.constant([[0, 0], [0, 0]], tf.float32)
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc,
            matcher,
            box_coder,
            unmatched_cls_target=unmatched_cls_target)

        anchors = tf.constant([[0.0, 0.0, 0.5, 0.5, 0.0],
                               [0.5, 0.5, 1.0, 0.8,
                                0.0], [0, 0.5, .5, 1.0, 0.0],
                               [.75, 0, 1.0, .25, 0.0]])
        anchors_rbox = rbox_list.RBoxList(anchors)

        box_corners = [[0.0, 0.0, 0.5, 0.5, 0.0], [0.5, 0.5, 0.9, 0.9, 0.0],
                       [.75, 0, .95, .27, 0.0]]
        rboxes = rbox_list.RBoxList(tf.constant(box_corners))

        groundtruth_labels = tf.constant(
            [[[0, 1], [1, 0]], [[1, 0], [0, 1]], [[0, 1], [1, .5]]],
            tf.float32)

        exp_cls_targets = [[[0, 1], [1, 0]], [[1, 0], [0, 1]], [[0, 0], [0,
                                                                         0]],
                           [[0, 1], [1, .5]]]
        exp_cls_weights = [1, 1, 1, 1]
        exp_reg_targets = [[0, 0, 0, 0, 0], [0, 0, -0.105361, 0.117783, 0],
                           [0, 0, 0, 0, 0], [0, 0, -0.0512933, .0769611, 0]]
        exp_reg_weights = [1, 1, 0, 1]
        exp_matching_anchors = [0, 1, 3]

        result = target_assigner.assign(anchors_rbox, rboxes,
                                        groundtruth_labels)
        (cls_targets, cls_weights, reg_targets, reg_weights, match) = result
        with self.test_session() as sess:
            (cls_targets_out, cls_weights_out, reg_targets_out, reg_weights_out, matching_anchors_out) = \
                sess.run([cls_targets, cls_weights, reg_targets, reg_weights, match.matched_column_indices()])

            self.assertAllClose(cls_targets_out, exp_cls_targets)
            self.assertAllClose(cls_weights_out, exp_cls_weights)
            self.assertAllClose(reg_targets_out, exp_reg_targets)
            self.assertAllClose(reg_weights_out, exp_reg_weights)
            self.assertAllClose(matching_anchors_out, exp_matching_anchors)
            self.assertEquals(cls_targets_out.dtype, np.float32)
            self.assertEquals(cls_weights_out.dtype, np.float32)
            self.assertEquals(reg_targets_out.dtype, np.float32)
            self.assertEquals(reg_weights_out.dtype, np.float32)
            self.assertEquals(matching_anchors_out.dtype, np.int32)
 def test_intersection_shapely(self):
     rbox1 = tf.constant([[4.0, 3.0, 7.0, 5.0, 0.1],
                          [5.0, 6.0, 10.0, 7.0, 0.2]])
     rbox2 = tf.constant([[3.0, 4.0, 6.0, 8.0, 0.1],
                          [14.0, 14.0, 15.0, 15.0, 0.2],
                          [0.0, 0.0, 20.0, 20.0, 0.3]])
     exp_output = [[27.025812, 0., 35.], [30.889588, 7.2258, 63.043903]]
     boxes1 = rbox_list.RBoxList(rbox1)
     boxes2 = rbox_list.RBoxList(rbox2)
     intersect = rbox_list_ops.intersection(boxes1, boxes2, ver='shapely')
     with self.test_session() as sess:
         intersect_output = sess.run(intersect)
         self.assertAllClose(intersect_output, exp_output)
    def test_get_correct_relative_codes_after_encoding(self):
        boxes = [[10.0, 10.0, 20.0, 15.0, 0.1], [0.2, 0.1, 0.5, 0.4, 0.2]]
        anchors = [[15.0, 12.0, 30.0, 18.0, 0.15], [0.1, 0.0, 0.7, 0.9, 0.1]]
        expected_rel_codes = [[
            -0.154833, -0.151374, -0.405465, -0.182322, -0.031831
        ], [0.127882, 0.121649, -0.336472, -0.810930, 0.063662]]

        boxes = rbox_list.RBoxList(tf.constant(boxes))
        anchors = rbox_list.RBoxList(tf.constant(anchors))
        coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder()
        rel_codes = coder.encode(boxes, anchors)
        with self.test_session() as sess:
            rel_codes_out, = sess.run([rel_codes])
            self.assertAllClose(rel_codes_out, expected_rel_codes)
    def test_change_coordinate_frame(self):
        corners = tf.constant([[0.25, 0.5, 0.75, 0.75, 0.1],
                               [0.5, 0.0, 1.0, 1.0, 0.2]])
        window = tf.constant([0.25, 0.25, 0.75, 0.75])
        boxes = rbox_list.RBoxList(corners)

        expected_corners = tf.constant([[0, 0.5, 1.5, 1.5, 0.1],
                                        [0.5, -0.5, 2.0, 2.0, 0.2]])
        expected_boxes = rbox_list.RBoxList(expected_corners)
        output = rbox_list_ops.change_coordinate_frame(boxes, window)

        with self.test_session() as sess:
            output_, expected_boxes_ = sess.run(
                [output.get(), expected_boxes.get()])
            self.assertAllClose(output_, expected_boxes_)
    def test_sort_by_field_descending_order(self):
        exp_corners = [[0, 0, 1, 1, 0.1], [0, 0.1, 1, 1.1, 0.2],
                       [0, -0.1, 1, 0.9, 0.3], [0, 10, 1, 11, 0.4],
                       [0, 10.1, 1, 11.1, 0.5], [0, 100, 1, 101, 0.6]]
        exp_scores = [.95, .9, .75, .6, .5, .3]
        exp_weights = [.2, .45, .6, .75, .8, .92]
        shuffle = [2, 4, 0, 5, 1, 3]

        corners = tf.constant([exp_corners[i] for i in shuffle], tf.float32)
        rboxes = rbox_list.RBoxList(corners)
        rboxes.add_field(
            'scores', tf.constant([exp_scores[i] for i in shuffle],
                                  tf.float32))
        rboxes.add_field(
            'weights',
            tf.constant([exp_weights[i] for i in shuffle], tf.float32))

        sort_by_score = rbox_list_ops.sort_by_field(rboxes, 'scores')
        with self.test_session() as sess:
            rboxes_out, corners_out, scores_out, weights_out = sess.run([
                rboxes.get_field('scores'),
                sort_by_score.get(),
                sort_by_score.get_field('scores'),
                sort_by_score.get_field('weights')
            ])
            self.assertAllClose(corners_out, exp_corners)
            self.assertAllClose(scores_out, exp_scores)
            self.assertAllClose(weights_out, exp_weights)
    def test_get_correct_relative_codes_after_encoding_with_scaling(self):
        boxes = [[10.0, 10.0, 20.0, 15.0, 0.1], [0.2, 0.1, 0.5, 0.4, 0.2]]
        anchors = [[15.0, 12.0, 30.0, 18.0, 0.15], [0.1, 0.0, 0.7, 0.9, 0.1]]
        scale_factors = [2, 3, 4, 5, 6]
        expected_rel_codes = [[
            -0.309665, -0.454122, -1.621860, -0.911607, -0.190986
        ], [0.2557630, 0.364945, -1.345888, -4.054651, 0.381972]]

        boxes = rbox_list.RBoxList(tf.constant(boxes))
        anchors = rbox_list.RBoxList(tf.constant(anchors))
        coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder(
            scale_factors=scale_factors)
        rel_codes = coder.encode(boxes, anchors)
        with self.test_session() as sess:
            rel_codes_out, = sess.run([rel_codes])
            self.assertAllClose(rel_codes_out, expected_rel_codes)
    def _decode(self, rel_codes, anchors):
        """Decode relative codes to rboxes.

        Args:
          rel_codes: a tensor representing N anchor-encoded rboxes.
          anchors: BoxList of anchors.

        Returns:
          boxes: BoxList holding N bounding rboxes.
        """
        ycenter_a, xcenter_a, ha, wa, ang_a = tf.unstack(
            tf.transpose(anchors.get()))
        ty, tx, th, tw, ta = tf.unstack(tf.transpose(rel_codes))

        if self._scale_factors:
            ty /= self._scale_factors[0]
            tx /= self._scale_factors[1]
            th /= self._scale_factors[2]
            tw /= self._scale_factors[3]
            ta /= self._scale_factors[4]

        ycenter = tx * wa * tf.sin(ang_a) + ty * ha * tf.cos(ang_a) + ycenter_a
        xcenter = tx * wa * tf.cos(
            ang_a) + ty * ha * -tf.sin(ang_a) + xcenter_a
        w = tf.exp(tw) * wa
        h = tf.exp(th) * ha
        ang = ta * (math.pi / 2) + ang_a

        return rbox_list.RBoxList(
            tf.transpose(tf.stack([ycenter, xcenter, h, w, ang])))
 def test_copy_extra_fields(self):
     corners = tf.constant([[0, 0, 1, 1, 0], [0, 0.1, 1, 1.1, 0]],
                           tf.float32)
     rboxes = rbox_list.RBoxList(corners)
     tensor1 = np.array([[1], [4]])
     tensor2 = np.array([[1, 1], [2, 2]])
     rboxes.add_field('tensor1', tf.constant(tensor1))
     rboxes.add_field('tensor2', tf.constant(tensor2))
     new_boxes = rbox_list.RBoxList(
         tf.constant([[0, 0, 10, 10, 0], [1, 3, 5, 5, 0]], tf.float32))
     new_boxes = rbox_list_ops._copy_extra_fields(new_boxes, rboxes)
     with self.test_session() as sess:
         self.assertAllClose(tensor1,
                             sess.run(new_boxes.get_field('tensor1')))
         self.assertAllClose(tensor2,
                             sess.run(new_boxes.get_field('tensor2')))
 def test_as_tensor_dict_missing_field(self):
     rboxlist = rbox_list.RBoxList(
         tf.constant([[0.1, 0.1, 0.4, 0.4, 0.1], [0.1, 0.1, 0.5, 0.5, 0.2]],
                     tf.float32))
     rboxlist.add_field('classes', tf.constant([0, 1]))
     rboxlist.add_field('scores', tf.constant([0.75, 0.2]))
     with self.assertRaises(ValueError):
         rboxlist.as_tensor_dict(['foo', 'bar'])
 def test_invalid_input_box_list_list(self):
     with self.assertRaises(ValueError):
         rbox_list_ops.concatenate(None)
     with self.assertRaises(ValueError):
         rbox_list_ops.concatenate([])
     with self.assertRaises(ValueError):
         corners = tf.constant([[0, 0, 0, 0, 0]], tf.float32)
         boxlist = rbox_list.RBoxList(corners)
         rbox_list_ops.concatenate([boxlist, 2])
 def test_iou(self):
     rbox1 = tf.constant([[4.0, 3.0, 7.0, 5.0, 0.1],
                          [5.0, 6.0, 10.0, 7.0, 0.2]])
     rbox2 = tf.constant([[3.0, 4.0, 6.0, 8.0, 0.1],
                          [14.0, 14.0, 15.0, 15.0, 0.2],
                          [0.0, 0.0, 20.0, 20.0, 0.3],
                          [0.0, 0.0, 20.0, 20.0, -1.57]])
     exp_output = [[27.025812 / 55.974188, 0. / 260, 35. / 400, -1],
                   [
                       30.889588 / 87.110412, 7.2258 / 287.7742,
                       63.043903 / 406.956097, -1
                   ]]
     boxes1 = rbox_list.RBoxList(rbox1)
     boxes2 = rbox_list.RBoxList(rbox2)
     iou = rbox_list_ops.iou(boxes1, boxes2)
     with self.test_session() as sess:
         iou_output = sess.run(iou)
         self.assertAllClose(iou_output, exp_output)
 def test_area(self):
     rbox = tf.constant([[0.0, 0.0, 10.0, 20.0, 0.1],
                         [1.0, 2.0, 3.0, 4.0, 0.2]])
     exp_output = [200.0, 12.0]
     boxes = rbox_list.RBoxList(rbox)
     areas = rbox_list_ops.area(boxes)
     with self.test_session() as sess:
         areas_output = sess.run(areas)
         self.assertAllClose(areas_output, exp_output)
 def test_ioaworks_on_empty_inputs(self):
     rbox1 = tf.constant([[4.0, 3.0, 7.0, 5.0, 0.1],
                          [5.0, 6.0, 10.0, 7.0, 0.2]])
     rbox2 = tf.constant([[3.0, 4.0, 6.0, 8.0, 0.1],
                          [14.0, 14.0, 15.0, 15.0, 0.2],
                          [0.0, 0.0, 20.0, 20.0, 0.3]])
     boxes1 = rbox_list.RBoxList(rbox1)
     boxes2 = rbox_list.RBoxList(rbox2)
     boxes_empty = rbox_list.RBoxList(tf.zeros((0, 5)))
     ioa_empty_1 = rbox_list_ops.ioa(boxes1, boxes_empty)
     ioa_empty_2 = rbox_list_ops.ioa(boxes_empty, boxes2)
     ioa_empty_3 = rbox_list_ops.ioa(boxes_empty, boxes_empty)
     with self.test_session() as sess:
         ioa_output_1, ioa_output_2, ioa_output_3 = sess.run(
             [ioa_empty_1, ioa_empty_2, ioa_empty_3])
         self.assertAllEqual(ioa_output_1.shape, (2, 0))
         self.assertAllEqual(ioa_output_2.shape, (0, 3))
         self.assertAllEqual(ioa_output_3.shape, (0, 0))
    def test_gather_with_invalid_field(self):
        corners = tf.constant([5 * [0.0], 5 * [1.0]])
        indices = tf.constant([0, 1], tf.int32)
        weights = tf.constant([[.1], [.3]], tf.float32)

        boxes = rbox_list.RBoxList(corners)
        boxes.add_field('weights', weights)
        with self.assertRaises(ValueError):
            rbox_list_ops.gather(boxes, indices, ['foo', 'bar'])
Example #24
0
    def test_create_target_assigner(self):
        """Tests that named constructor gives working target assigners.

        TODO: Make this test more general.
        """
        corners = [[0.0, 0.0, 1.0, 1.0]]
        groundtruth = box_list.BoxList(tf.constant(corners))

        priors = box_list.BoxList(tf.constant(corners))
        prior_stddevs = tf.constant([[1.0, 1.0, 1.0, 1.0]])
        priors.add_field('stddev', prior_stddevs)
        multibox_ta = (targetassigner.create_target_assigner('Multibox',
                                                             stage='proposal'))
        multibox_ta.assign(priors, groundtruth)
        # No tests on output, as that may vary arbitrarily as new target assigners
        # are added. As long as it is constructed correctly and runs without errors,
        # tests on the individual assigners cover correctness of the assignments.

        anchors = box_list.BoxList(tf.constant(corners))
        faster_rcnn_proposals_ta = (targetassigner.create_target_assigner(
            'FasterRCNN', stage='proposal'))
        faster_rcnn_proposals_ta.assign(anchors, groundtruth)

        fast_rcnn_ta = (targetassigner.create_target_assigner('FastRCNN'))
        fast_rcnn_ta.assign(anchors, groundtruth)

        faster_rcnn_detection_ta = (targetassigner.create_target_assigner(
            'FasterRCNN', stage='detection'))
        faster_rcnn_detection_ta.assign(anchors, groundtruth)

        corners = [[0.0, 0.0, 1.0, 1.0, 0.0]]
        groundtruth = rbox_list.RBoxList(tf.constant(corners))
        anchors = rbox_list.RBoxList(tf.constant(corners))
        rfaster_rcnn_proposals_ta = (targetassigner.create_target_assigner(
            'RFasterRCNN', stage='proposal'))
        rfaster_rcnn_proposals_ta.assign(anchors, groundtruth)

        rfaster_rcnn_detection_ta = (targetassigner.create_target_assigner(
            'RFasterRCNN', stage='detection'))
        rfaster_rcnn_detection_ta.assign(anchors, groundtruth)

        with self.assertRaises(ValueError):
            targetassigner.create_target_assigner('InvalidDetector',
                                                  stage='invalid_stage')
    def test_ioa(self):
        rbox1 = tf.constant([[4.0, 3.0, 7.0, 5.0, 0.1],
                             [5.0, 6.0, 10.0, 7.0, 0.2]])
        rbox2 = tf.constant([[3.0, 4.0, 6.0, 8.0, 0.1],
                             [14.0, 14.0, 15.0, 15.0, 0.2],
                             [0.0, 0.0, 20.0, 20.0, 0.3]])
        exp_output_1 = [[27.025812 / 48, 0. / 225, 35. / 400],
                        [30.889588 / 48, 7.2258 / 225, 63.043903 / 400]]
        exp_output_2 = [[27.025812 / 35, 30.889588 / 70],
                        [0. / 35, 7.2258 / 70], [35. / 35, 63.043903 / 70]]

        boxes1 = rbox_list.RBoxList(rbox1)
        boxes2 = rbox_list.RBoxList(rbox2)
        ioa_1 = rbox_list_ops.ioa(boxes1, boxes2)
        ioa_2 = rbox_list_ops.ioa(boxes2, boxes1)
        with self.test_session() as sess:
            ioa_output_1, ioa_output_2 = sess.run([ioa_1, ioa_2])
            self.assertAllClose(ioa_output_1, exp_output_1)
            self.assertAllClose(ioa_output_2, exp_output_2)
    def test_num_boxes(self):
        data = tf.constant(
            [[0, 0, 1, 1, 0.1], [1, 1, 2, 3, 0.2], [3, 4, 5, 5, 0.3]],
            tf.float32)
        expected_num_boxes = 3

        rboxes = rbox_list.RBoxList(data)
        with self.test_session() as sess:
            num_boxes_output = sess.run(rboxes.num_boxes())
            self.assertEquals(num_boxes_output, expected_num_boxes)
 def test_boolean_mask(self):
     corners = tf.constant(
         [5 * [0.0], 5 * [1.0], 5 * [2.0], 5 * [3.0], 5 * [4.0]])
     indicator = tf.constant([True, False, True, False, True], tf.bool)
     expected_subset = [5 * [0.0], 5 * [2.0], 5 * [4.0]]
     boxes = rbox_list.RBoxList(corners)
     subset = rbox_list_ops.boolean_mask(boxes, indicator)
     with self.test_session() as sess:
         subset_output = sess.run(subset.get())
         self.assertAllClose(subset_output, expected_subset)
 def test_gather(self):
     corners = tf.constant(
         [5 * [0.0], 5 * [1.0], 5 * [2.0], 5 * [3.0], 5 * [4.0]])
     indices = tf.constant([0, 2, 4], tf.int32)
     expected_subset = [5 * [0.0], 5 * [2.0], 5 * [4.0]]
     boxes = rbox_list.RBoxList(corners)
     subset = rbox_list_ops.gather(boxes, indices)
     with self.test_session() as sess:
         subset_output = sess.run(subset.get())
         self.assertAllClose(subset_output, expected_subset)
    def test_to_absolute_coordinates_already_abolute(self):
        coordinates = tf.constant(
            [[0, 0, 100, 100, 0.1], [25, 25, 75, 75, 0.1]], tf.float32)
        img = tf.ones((128, 100, 100, 3))
        rboxlist = rbox_list.RBoxList(coordinates)
        absolute_rboxlist = rbox_list_ops.to_absolute_coordinates(
            rboxlist, tf.shape(img)[1], tf.shape(img)[2], check_range=True)

        with self.test_session() as sess:
            with self.assertRaisesOpError('assertion failed'):
                sess.run(absolute_rboxlist.get())
    def test_to_normalized_coordinates_already_normalized(self):
        coordinates = tf.constant(
            [[0, 0, 1, 1, 0.1], [0.25, 0.25, 0.75, 0.75, 0.1]], tf.float32)
        img = tf.ones((128, 100, 100, 3))
        boxlist = rbox_list.RBoxList(coordinates)
        normalized_boxlist = rbox_list_ops.to_normalized_coordinates(
            boxlist, tf.shape(img)[1], tf.shape(img)[2], check_range=True)

        with self.test_session() as sess:
            with self.assertRaisesOpError('assertion failed'):
                sess.run(normalized_boxlist.get())