Beispiel #1
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)

        with self.assertRaises(ValueError):
            targetassigner.create_target_assigner('InvalidDetector',
                                                  stage='invalid_stage')
Beispiel #2
0
    def test_raises_error_on_invalid_groundtruth_labels(self):
        similarity_calc = region_similarity_calculator.NegSqDistSimilarity()
        matcher = bipartite_matcher.GreedyBipartiteMatcher()
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
        unmatched_cls_target = tf.constant([[0, 0], [0, 0], [0, 0]],
                                           tf.float32)
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc,
            matcher,
            box_coder,
            unmatched_cls_target=unmatched_cls_target)

        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5]])
        prior_stddevs = tf.constant([[1.0, 1.0, 1.0, 1.0]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        box_corners = [[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.9, 0.9],
                       [.75, 0, .95, .27]]
        boxes = box_list.BoxList(tf.constant(box_corners))
        groundtruth_labels = tf.constant([[[0, 1], [1, 0]]], tf.float32)

        with self.assertRaises(ValueError):
            target_assigner.assign(priors,
                                   boxes,
                                   groundtruth_labels,
                                   num_valid_rows=3)
Beispiel #3
0
 def testHardMiningNMS(self):
   location_losses = tf.constant([[100, 90, 80, 0],
                                  [0, 1, 2, 3]], tf.float32)
   cls_losses = tf.constant([[0, 10, 50, 110],
                             [9, 6, 3, 0]], tf.float32)
   box_corners = tf.constant([[0.1, 0.1, 0.9, 0.9],
                              [0.9, 0.9, 0.99, 0.99],
                              [0.1, 0.1, 0.9, 0.9],
                              [0.1, 0.1, 0.9, 0.9]], tf.float32)
   decoded_boxlist_list = []
   decoded_boxlist_list.append(box_list.BoxList(box_corners))
   decoded_boxlist_list.append(box_list.BoxList(box_corners))
   loss_op = losses.HardExampleMiner(num_hard_examples=2,
                                     iou_threshold=0.5,
                                     loss_type='cls',
                                     cls_loss_weight=1,
                                     loc_loss_weight=1)
   (loc_loss, cls_loss) = loss_op(location_losses, cls_losses,
                                  decoded_boxlist_list)
   exp_loc_loss = 0 + 90 + 0 + 1
   exp_cls_loss = 110 + 10 + 9 + 6
   with self.test_session() as sess:
     loc_loss_output = sess.run(loc_loss)
     self.assertAllClose(loc_loss_output, exp_loc_loss)
     cls_loss_output = sess.run(cls_loss)
     self.assertAllClose(cls_loss_output, exp_cls_loss)
def get_box_loss(pred, gt, num_classes):
    lamd_coord = 5.0
    xmin = pred[:, 0] - pred[:, 2] / 2
    xmax = pred[:, 0] + pred[:, 2] / 2
    ymin = pred[:, 1] - pred[:, 3] / 2
    ymax = pred[:, 1] + pred[:, 3] / 2
    boxes1 = tf.transpose(tf.stack([ymin, xmin, ymax, xmax]))
    xmin = gt[0] - gt[2] / 2
    xmax = gt[0] + gt[2] / 2
    ymin = gt[1] - gt[3] / 2
    ymax = gt[1] + gt[3] / 2
    boxes2 = tf.transpose(tf.stack([ymin, xmin, ymax, xmax]))
    boxes2 = tf.expand_dims(boxes2, 0)
    boxes_obj1 = box_list.BoxList(boxes1)
    boxes_obj2 = box_list.BoxList(boxes2)
    iou_score = box_list_ops.iou(boxes_obj2, boxes_obj1)
    boxes_obj1.add_field('scores', iou_score[0])
    boxes_obj1.add_field('classes', pred[:, 5:])
    boxes_obj1.add_field('objness', pred[:, 4])
    boxes_obj1 = box_list_ops.sort_by_field(boxes_obj1, 'scores')
    matched_info = boxes_obj1.get_center_coordinates_and_sizes()[0]
    pred_cen = matched_info[0:2]
    pred_size = tf.sqrt(matched_info[2:4])
    gt_center = tf.stack([gt[1], gt[0]])
    gt_size = tf.stack([gt[3], gt[2]])
    geo_loss = tf.losses.mean_squared_error(
        gt_center, pred_cen) + tf.losses.mean_squared_error(
            gt_size, pred_size)
    geo_loss = lamd_coord * geo_loss
    pred_confi = boxes_obj1.get_field('objness')[0] - 1
    confi_loss = pred_confi * pred_confi
    predi_class = boxes_obj1.get_field('classes')[0]
    box_class = gt[4:]
    class_loss = tf.losses.mean_squared_error(box_class, predi_class)
    return confi_loss + geo_loss + class_loss
Beispiel #5
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_, [])
Beispiel #6
0
 def testHardMiningWithSingleLossType(self):
     location_losses = tf.constant([[100, 90, 80, 0], [0, 1, 2, 3]],
                                   tf.float32)
     cls_losses = tf.constant([[0, 10, 50, 110], [9, 6, 3, 0]], tf.float32)
     box_corners = tf.constant([[0.1, 0.1, 0.9, 0.9], [0.1, 0.1, 0.9, 0.9],
                                [0.1, 0.1, 0.9, 0.9], [0.1, 0.1, 0.9, 0.9]],
                               tf.float32)
     decoded_boxlist_list = []
     decoded_boxlist_list.append(box_list.BoxList(box_corners))
     decoded_boxlist_list.append(box_list.BoxList(box_corners))
     # Uses only location loss to select hard examples
     loss_op = losses.HardExampleMiner(num_hard_examples=1,
                                       iou_threshold=0.0,
                                       loss_type='loc',
                                       cls_loss_weight=1,
                                       loc_loss_weight=1)
     (loc_loss, cls_loss) = loss_op(location_losses, cls_losses,
                                    decoded_boxlist_list)
     exp_loc_loss = 100 + 3
     exp_cls_loss = 0 + 0
     with self.test_session() as sess:
         loc_loss_output = sess.run(loc_loss)
         self.assertAllClose(loc_loss_output, exp_loc_loss)
         cls_loss_output = sess.run(cls_loss)
         self.assertAllClose(cls_loss_output, exp_cls_loss)
Beispiel #7
0
    def test_batch_assign_multidimensional_targets(self):
        box_list1 = box_list.BoxList(tf.constant([[0., 0., 0.2, 0.2]]))

        box_list2 = box_list.BoxList(
            tf.constant([[0, 0.25123152, 1, 1],
                         [0.015789, 0.0985, 0.55789, 0.3842]]))

        gt_box_batch = [box_list1, box_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]

        prior_means = tf.constant([[0, 0, .25, .25], [0, .25, 1, 1],
                                   [0, .1, .5, .5], [.75, .75, 1, 1]])
        prior_stddevs = tf.constant([[.1, .1, .1, .1], [.1, .1, .1, .1],
                                     [.1, .1, .1, .1], [.1, .1, .1, .1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        exp_reg_targets = [[[0, 0, -0.5, -0.5], [0, 0, 0, 0], [0, 0, 0, 0],
                            [0, 0, 0, 0]],
                           [[0, 0, 0, 0], [0, 0.01231521, 0, 0],
                            [0.15789001, -0.01500003, 0.57889998, -1.15799987],
                            [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(
            target_dimensions=(2, 3))

        (cls_targets, cls_weights, reg_targets, reg_weights,
         match_list) = targetassigner.batch_assign_targets(
             multiclass_target_assigner, priors, 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)
Beispiel #8
0
 def test_concatenate_with_missing_fields(self):
     corners1 = tf.constant([[0, 0, 0, 0], [1, 2, 3, 4]], tf.float32)
     scores1 = tf.constant([1.0, 2.1])
     corners2 = tf.constant([[0, 3, 1, 6], [2, 4, 3, 8]], tf.float32)
     boxlist1 = box_list.BoxList(corners1)
     boxlist1.add_field('scores', scores1)
     boxlist2 = box_list.BoxList(corners2)
     with self.assertRaises(ValueError):
         box_list_ops.concatenate([boxlist1, boxlist2])
    def match_single_image_info(self, image_info):
        """Match detections to groundtruth for a single image.

    Detections are matched to available groundtruth in the image based on the
    IOU threshold from the constructor.  The classes of the detections and
    groundtruth matches are then compared. Detections that do not have IOU above
    the required threshold or have different classes from their match are
    considered negative matches. All inputs in `image_info` originate or are
    inferred from the eval_dict passed to class method
    `get_estimator_eval_metric_ops`.

    Args:
      image_info: a tuple or list containing the following (in order):
        - gt_boxes: tf.float32 tensor of groundtruth boxes.
        - gt_classes: tf.int64 tensor of groundtruth classes associated with
            groundtruth boxes.
        - num_gt_box: scalar indicating the number of groundtruth boxes per
            image.
        - det_boxes: tf.float32 tensor of detection boxes.
        - det_classes: tf.int64 tensor of detection classes associated with
            detection boxes.
        - num_det_box: scalar indicating the number of detection boxes per
            image.
    Returns:
      is_class_matched: tf.int64 tensor identical in shape to det_boxes,
        indicating whether detection boxes matched with and had the same
        class as groundtruth annotations.
    """
        (gt_boxes, gt_classes, num_gt_box, det_boxes, det_classes,
         num_det_box) = image_info
        detection_boxes = det_boxes[:num_det_box]
        detection_classes = det_classes[:num_det_box]
        groundtruth_boxes = gt_boxes[:num_gt_box]
        groundtruth_classes = gt_classes[:num_gt_box]
        det_boxlist = box_list.BoxList(detection_boxes)
        gt_boxlist = box_list.BoxList(groundtruth_boxes)

        # Target assigner requires classes in one-hot format. An additional
        # dimension is required since gt_classes are 1-indexed; the zero index is
        # provided to all non-matches.
        one_hot_depth = tf.cast(tf.add(tf.reduce_max(groundtruth_classes), 1),
                                dtype=tf.int32)
        gt_classes_one_hot = tf.one_hot(groundtruth_classes,
                                        one_hot_depth,
                                        dtype=tf.float32)
        one_hot_cls_targets, _, _, _, _ = self._target_assigner.assign(
            det_boxlist,
            gt_boxlist,
            gt_classes_one_hot,
            unmatched_class_label=tf.zeros(shape=one_hot_depth,
                                           dtype=tf.float32))
        # Transform from one-hot back to indexes.
        cls_targets = tf.argmax(one_hot_cls_targets, axis=1)
        is_class_matched = tf.cast(tf.equal(tf.cast(cls_targets, tf.int64),
                                            detection_classes),
                                   dtype=tf.int64)
        return is_class_matched
Beispiel #10
0
    def test_assign_multidimensional_class_targets(self):
        similarity_calc = region_similarity_calculator.NegSqDistSimilarity()
        matcher = bipartite_matcher.GreedyBipartiteMatcher()
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
        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)

        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8],
                                   [0, 0.5, .5, 1.0], [.75, 0, 1.0, .25]])
        prior_stddevs = tf.constant(4 * [4 * [.1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        box_corners = [[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.9, 0.9],
                       [.75, 0, .95, .27]]
        boxes = box_list.BoxList(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, -1, 1], [0, 0, 0, 0],
                           [0, 0, -.5, .2]]
        exp_reg_weights = [1, 1, 0, 1]
        exp_matching_anchors = [0, 1, 3]

        result = target_assigner.assign(priors,
                                        boxes,
                                        groundtruth_labels,
                                        num_valid_rows=3)
        (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)
Beispiel #11
0
  def test_box_voting_fails_with_negative_scores(self):
    candidates = box_list.BoxList(
        tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32))
    pool = box_list.BoxList(tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32))
    pool.add_field('scores', tf.constant([-0.2]))
    averaged_boxes = box_list_ops.box_voting(candidates, pool)

    with self.test_session() as sess:
      with self.assertRaisesOpError('Scores must be non negative'):
        sess.run([averaged_boxes.get()])
Beispiel #12
0
 def test_matched_iou(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]])
   exp_output = [2.0 / 16.0, 0]
   boxes1 = box_list.BoxList(corners1)
   boxes2 = box_list.BoxList(corners2)
   iou = box_list_ops.matched_iou(boxes1, boxes2)
   with self.test_session() as sess:
     iou_output = sess.run(iou)
     self.assertAllClose(iou_output, exp_output)
Beispiel #13
0
  def test_box_voting_fails_when_unmatched(self):
    candidates = box_list.BoxList(
        tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32))
    pool = box_list.BoxList(tf.constant([[0.6, 0.6, 0.8, 0.8]], tf.float32))
    pool.add_field('scores', tf.constant([0.2]))
    averaged_boxes = box_list_ops.box_voting(candidates, pool)

    with self.test_session() as sess:
      with self.assertRaisesOpError('Each box in selected_boxes must match '
                                    'with at least one box in pool_boxes.'):
        sess.run([averaged_boxes.get()])
Beispiel #14
0
 def test_pairwise_distances(self):
     corners1 = tf.constant([[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 0.0, 2.0]])
     corners2 = tf.constant([[3.0, 4.0, 1.0, 0.0], [-4.0, 0.0, 0.0, 3.0],
                             [0.0, 0.0, 0.0, 0.0]])
     exp_output = [[26, 25, 0], [18, 27, 6]]
     boxes1 = box_list.BoxList(corners1)
     boxes2 = box_list.BoxList(corners2)
     dist_matrix = box_list_ops.sq_dist(boxes1, boxes2)
     with self.test_session() as sess:
         dist_output = sess.run(dist_matrix)
         self.assertAllClose(dist_output, exp_output)
Beispiel #15
0
 def test_intersection(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]])
     exp_output = [[2.0, 0.0, 6.0], [1.0, 0.0, 5.0]]
     boxes1 = box_list.BoxList(corners1)
     boxes2 = box_list.BoxList(corners2)
     intersect = box_list_ops.intersection(boxes1, boxes2)
     with self.test_session() as sess:
         intersect_output = sess.run(intersect)
         self.assertAllClose(intersect_output, exp_output)
    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)
Beispiel #17
0
 def test_gather_with_invalid_inputs(self):
     corners = tf.constant(
         [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]])
     indices_float32 = tf.constant([0, 2, 4], tf.float32)
     boxes = box_list.BoxList(corners)
     with self.assertRaises(ValueError):
         _ = box_list_ops.gather(boxes, indices_float32)
     indices_2d = tf.constant([[0, 2, 4]], tf.int32)
     boxes = box_list.BoxList(corners)
     with self.assertRaises(ValueError):
         _ = box_list_ops.gather(boxes, indices_2d)
Beispiel #18
0
 def test_very_small_Width_nan_after_encoding(self):
     boxes = [[10.0, 10.0, 10.0000001, 20.0]]
     anchors = [[15.0, 12.0, 30.0, 18.0]]
     expected_rel_codes = [[-0.833333, 0., -21.128731, 0.510826]]
     boxes = box_list.BoxList(tf.constant(boxes))
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = faster_rcnn_box_coder.FasterRcnnBoxCoder()
     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_correct_relative_codes_with_small_width(self):
   boxes = [[10.0, 10.0, 10.0000001, 20.0]]
   anchors = [[15.0, 12.0, 30.0, 18.0]]
   scale_factors = None
   expected_rel_codes = [[-1.317616, 0., -20.670586]]
   boxes = box_list.BoxList(tf.constant(boxes))
   anchors = box_list.BoxList(tf.constant(anchors))
   coder = square_box_coder.SquareBoxCoder(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)
Beispiel #20
0
 def test_get_correct_pairwise_similarity_based_on_iou(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]])
   exp_output = [[2.0 / 16.0, 0, 6.0 / 400.0], [1.0 / 16.0, 0.0, 5.0 / 400.0]]
   boxes1 = box_list.BoxList(corners1)
   boxes2 = box_list.BoxList(corners2)
   iou_similarity_calculator = region_similarity_calculator.IouSimilarity()
   iou_similarity = iou_similarity_calculator.compare(boxes1, boxes2)
   with self.test_session() as sess:
     iou_output = sess.run(iou_similarity)
     self.assertAllClose(iou_output, exp_output)
Beispiel #21
0
 def test_get_correct_relative_codes_after_encoding(self):
     boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]]
     anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]]
     expected_rel_codes = [[-0.5, -0.416666, -0.405465, -0.182321],
                           [-0.083333, -0.222222, -0.693147, -1.098612]]
     boxes = box_list.BoxList(tf.constant(boxes))
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = faster_rcnn_box_coder.FasterRcnnBoxCoder()
     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)
Beispiel #22
0
  def test_change_coordinate_frame(self):
    corners = tf.constant([[0.25, 0.5, 0.75, 0.75], [0.5, 0.0, 1.0, 1.0]])
    window = tf.constant([0.25, 0.25, 0.75, 0.75])
    boxes = box_list.BoxList(corners)

    expected_corners = tf.constant([[0, 0.5, 1.0, 1.0], [0.5, -0.5, 1.5, 1.5]])
    expected_boxes = box_list.BoxList(expected_corners)
    output = box_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 graph_fn(anchor_means, anchor_stddevs, groundtruth_box_corners):
   similarity_calc = region_similarity_calculator.IouSimilarity()
   matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                          unmatched_threshold=0.3)
   box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
   target_assigner = targetassigner.TargetAssigner(
       similarity_calc, matcher, box_coder, unmatched_cls_target=None)
   anchors_boxlist = box_list.BoxList(anchor_means)
   anchors_boxlist.add_field('stddev', anchor_stddevs)
   groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
   result = target_assigner.assign(anchors_boxlist, groundtruth_boxlist)
   (cls_targets, cls_weights, reg_targets, reg_weights, _) = result
   return (cls_targets, cls_weights, reg_targets, reg_weights)
 def test_correct_relative_codes_with_non_default_scale(self):
   boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]]
   anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]]
   scale_factors = [2, 3, 4]
   expected_rel_codes = [[-1.581139, -0.790569, -1.175573],
                         [-0.136083, -0.816497, -3.583519]]
   boxes = box_list.BoxList(tf.constant(boxes))
   anchors = box_list.BoxList(tf.constant(anchors))
   coder = square_box_coder.SquareBoxCoder(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 test_get_correct_pairwise_similarity_based_on_squared_distances(self):
     corners1 = tf.constant([[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 0.0, 2.0]])
     corners2 = tf.constant([[3.0, 4.0, 1.0, 0.0], [-4.0, 0.0, 0.0, 3.0],
                             [0.0, 0.0, 0.0, 0.0]])
     exp_output = [[-26, -25, 0], [-18, -27, -6]]
     boxes1 = box_list.BoxList(corners1)
     boxes2 = box_list.BoxList(corners2)
     dist_similarity_calc = region_similarity_calculator.NegSqDistSimilarity(
     )
     dist_similarity = dist_similarity_calc.compare(boxes1, boxes2)
     with self.test_session() as sess:
         dist_output = sess.run(dist_similarity)
         self.assertAllClose(dist_output, exp_output)
  def test_correct_relative_codes_with_default_scale(self):
    boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]]
    anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]]
    scale_factors = None
    expected_rel_codes = [[-0.790569, -0.263523, -0.293893],
                          [-0.068041, -0.272166, -0.89588]]

    boxes = box_list.BoxList(tf.constant(boxes))
    anchors = box_list.BoxList(tf.constant(anchors))
    coder = square_box_coder.SquareBoxCoder(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 graph_fn(anchor_means, anchor_stddevs, groundtruth_boxlist1,
              groundtruth_boxlist2):
   box_list1 = box_list.BoxList(groundtruth_boxlist1)
   box_list2 = box_list.BoxList(groundtruth_boxlist2)
   gt_box_batch = [box_list1, box_list2]
   gt_class_targets = [None, None]
   anchors_boxlist = box_list.BoxList(anchor_means)
   anchors_boxlist.add_field('stddev', anchor_stddevs)
   agnostic_target_assigner = self._get_agnostic_target_assigner()
   (cls_targets, cls_weights, reg_targets, reg_weights,
    _) = targetassigner.batch_assign_targets(
        agnostic_target_assigner, anchors_boxlist, gt_box_batch,
        gt_class_targets)
   return (cls_targets, cls_weights, reg_targets, reg_weights)
Beispiel #28
0
 def test_copy_extra_fields(self):
   corners = tf.constant([[0, 0, 1, 1],
                          [0, 0.1, 1, 1.1]], tf.float32)
   boxes = box_list.BoxList(corners)
   tensor1 = np.array([[1], [4]])
   tensor2 = np.array([[1, 1], [2, 2]])
   boxes.add_field('tensor1', tf.constant(tensor1))
   boxes.add_field('tensor2', tf.constant(tensor2))
   new_boxes = box_list.BoxList(tf.constant([[0, 0, 10, 10],
                                             [1, 3, 5, 5]], tf.float32))
   new_boxes = box_list_ops._copy_extra_fields(new_boxes, boxes)
   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')))
Beispiel #29
0
  def testGetCorrectRelativeCodesAfterEncoding(self):
    box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.5]]
    boxes = box_list.BoxList(tf.constant(box_corners))
    expected_rel_codes = [[0.0, 0.0, 0.0, 0.0], [-5.0, -5.0, -5.0, -3.0]]
    prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8]])
    prior_stddevs = tf.constant(2 * [4 * [.1]])
    priors = box_list.BoxList(prior_means)
    priors.add_field('stddev', prior_stddevs)

    coder = mean_stddev_box_coder.MeanStddevBoxCoder()
    rel_codes = coder.encode(boxes, priors)
    with self.test_session() as sess:
      rel_codes_out = sess.run(rel_codes)
      self.assertAllClose(rel_codes_out, expected_rel_codes)
Beispiel #30
0
 def test_get_correct_relative_codes_after_encoding_with_scaling(self):
     boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]]
     anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]]
     scale_factors = [2, 3, 4, 5]
     expected_rel_codes = [[-1., -1.25, -1.62186, -0.911608],
                           [-0.166667, -0.666667, -2.772588, -5.493062]]
     boxes = box_list.BoxList(tf.constant(boxes))
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = faster_rcnn_box_coder.FasterRcnnBoxCoder(
         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)