コード例 #1
0
 def test_invalid_input_box_list_list(self):
     with self.assertRaises(ValueError):
         box_list_ops.concatenate(None)
     with self.assertRaises(ValueError):
         box_list_ops.concatenate([])
     with self.assertRaises(ValueError):
         corners = tf.constant([[0, 0, 0, 0]], tf.float32)
         boxlist = box_list.BoxList(corners)
         box_list_ops.concatenate([boxlist, 2])
コード例 #2
0
    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)
コード例 #3
0
    def test_gather_with_invalid_field(self):
        corners = tf.constant([4 * [0.0], 4 * [1.0]])
        indices = tf.constant([0, 1], tf.int32)
        weights = tf.constant([[.1], [.3]], tf.float32)

        boxes = box_list.BoxList(corners)
        boxes.add_field('weights', weights)
        with self.assertRaises(ValueError):
            box_list_ops.gather(boxes, indices, ['foo', 'bar'])
コード例 #4
0
 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)
コード例 #5
0
 def graph_fn(anchor_means, groundtruth_box_corners,
              groundtruth_keypoints):
     similarity_calc = region_similarity_calculator.IouSimilarity()
     matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                            unmatched_threshold=0.5)
     box_coder = keypoint_box_coder.KeypointBoxCoder(
         num_keypoints=6, scale_factors=[10.0, 10.0, 5.0, 5.0])
     target_assigner = targetassigner.TargetAssigner(
         similarity_calc, matcher, box_coder)
     anchors_boxlist = box_list.BoxList(anchor_means)
     groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
     groundtruth_boxlist.add_field(fields.BoxListFields.keypoints,
                                   groundtruth_keypoints)
     result = target_assigner.assign(anchors_boxlist,
                                     groundtruth_boxlist,
                                     unmatched_class_label=None)
     (cls_targets, cls_weights, reg_targets, reg_weights, _) = result
     return (cls_targets, cls_weights, reg_targets, reg_weights)
コード例 #6
0
def concatenate(boxlists, fields=None, scope=None):
    """Concatenate list of BoxLists.

  This op concatenates a list of input BoxLists into a larger BoxList.  It also
  handles concatenation of BoxList fields as long as the field tensor shapes
  are equal except for the first dimension.

  Args:
    boxlists: list of BoxList objects
    fields: optional list of fields to also concatenate.  By default, all
      fields from the first BoxList in the list are included in the
      concatenation.
    scope: name scope.

  Returns:
    a BoxList with number of boxes equal to
      sum([boxlist.num_boxes() for boxlist in BoxList])
  Raises:
    ValueError: if boxlists is invalid (i.e., is not a list, is empty, or
      contains non BoxList objects), or if requested fields are not contained in
      all boxlists
  """
    with tf.name_scope(scope, 'Concatenate'):
        if not isinstance(boxlists, list):
            raise ValueError('boxlists should be a list')
        if not boxlists:
            raise ValueError('boxlists should have nonzero length')
        for boxlist in boxlists:
            if not isinstance(boxlist, box_list.BoxList):
                raise ValueError(
                    'all elements of boxlists should be BoxList objects')
        concatenated = box_list.BoxList(
            tf.concat([boxlist.get() for boxlist in boxlists], 0))
        if fields is None:
            fields = boxlists[0].get_extra_fields()
        for field in fields:
            first_field_shape = boxlists[0].get_field(
                field).get_shape().as_list()
            first_field_shape[0] = -1
            if None in first_field_shape:
                raise ValueError(
                    'field %s must have fully defined shape except for the'
                    ' 0th dimension.' % field)
            for boxlist in boxlists:
                if not boxlist.has_field(field):
                    raise ValueError(
                        'boxlist must contain all requested fields')
                field_shape = boxlist.get_field(field).get_shape().as_list()
                field_shape[0] = -1
                if field_shape != first_field_shape:
                    raise ValueError(
                        'field %s must have same shape for all boxlists '
                        'except for the 0th dimension.' % field)
            concatenated_field = tf.concat(
                [boxlist.get_field(field) for boxlist in boxlists], 0)
            concatenated.add_field(field, concatenated_field)
        return concatenated
コード例 #7
0
 def graph_fn(anchor_means, groundtruth_box_corners,
              groundtruth_labels):
     similarity_calc = region_similarity_calculator.IouSimilarity()
     matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                            unmatched_threshold=0.5)
     box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
     unmatched_class_label = tf.constant([0, 0, 0], tf.float32)
     anchors_boxlist = box_list.BoxList(anchor_means)
     groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
     target_assigner = targetassigner.TargetAssigner(
         similarity_calc, matcher, box_coder)
     result = target_assigner.assign(
         anchors_boxlist,
         groundtruth_boxlist,
         groundtruth_labels,
         unmatched_class_label=unmatched_class_label)
     (cls_targets, cls_weights, reg_targets, reg_weights, _) = result
     return (cls_targets, cls_weights, reg_targets, reg_weights)
コード例 #8
0
 def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2,
              class_targets1, class_targets2, groundtruth_weights1,
              groundtruth_weights2):
     box_list1 = box_list.BoxList(groundtruth_boxlist1)
     box_list2 = box_list.BoxList(groundtruth_boxlist2)
     gt_box_batch = [box_list1, box_list2]
     gt_class_targets = [class_targets1, class_targets2]
     gt_weights = [groundtruth_weights1, groundtruth_weights2]
     anchors_boxlist = box_list.BoxList(anchor_means)
     multiclass_target_assigner = self._get_target_assigner()
     num_classes = 3
     unmatched_class_label = tf.constant([1] + num_classes * [0],
                                         tf.float32)
     (cls_targets, cls_weights, reg_targets, reg_weights,
      _) = targetassigner.batch_assign_targets(
          multiclass_target_assigner, anchors_boxlist, gt_box_batch,
          gt_class_targets, unmatched_class_label, gt_weights)
     return (cls_targets, cls_weights, reg_targets, reg_weights)
コード例 #9
0
    def test_get_correct_pairwise_similarity_based_on_thresholded_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]])
        scores = tf.constant([.3, .6])
        iou_threshold = .013

        exp_output = tf.constant([[0.3, 0., 0.3], [0.6, 0., 0.]])
        boxes1 = box_list.BoxList(corners1)
        boxes1.add_field(fields.BoxListFields.scores, scores)
        boxes2 = box_list.BoxList(corners2)
        iou_similarity_calculator = (
            region_similarity_calculator.ThresholdedIouSimilarity(
                iou_threshold=iou_threshold))
        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)
コード例 #10
0
 def test_get_correct_pairwise_similarity_based_on_ioa(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_1 = [[2.0 / 12.0, 0, 6.0 / 400.0],
                     [1.0 / 12.0, 0.0, 5.0 / 400.0]]
     exp_output_2 = [[2.0 / 6.0, 1.0 / 5.0], [0, 0], [6.0 / 6.0, 5.0 / 5.0]]
     boxes1 = box_list.BoxList(corners1)
     boxes2 = box_list.BoxList(corners2)
     ioa_similarity_calculator = region_similarity_calculator.IoaSimilarity(
     )
     ioa_similarity_1 = ioa_similarity_calculator.compare(boxes1, boxes2)
     ioa_similarity_2 = ioa_similarity_calculator.compare(boxes2, boxes1)
     with self.test_session() as sess:
         iou_output_1, iou_output_2 = sess.run(
             [ioa_similarity_1, ioa_similarity_2])
         self.assertAllClose(iou_output_1, exp_output_1)
         self.assertAllClose(iou_output_2, exp_output_2)
コード例 #11
0
    def test_select_random_box_with_empty_boxlist(self):
        corners = tf.constant([], shape=[0, 4], dtype=tf.float32)
        boxlist = box_list.BoxList(corners)
        random_bbox, valid = box_list_ops.select_random_box(boxlist)
        with self.test_session() as sess:
            random_bbox_out, valid_out = sess.run([random_bbox, valid])

        expected_bbox_out = np.array([[-1., -1., -1., -1.]], dtype=np.float32)
        self.assertAllEqual(expected_bbox_out, random_bbox_out)
        self.assertFalse(valid_out)
コード例 #12
0
 def test_gather(self):
     corners = tf.constant(
         [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]])
     indices = tf.constant([0, 2, 4], tf.int32)
     expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]]
     boxes = box_list.BoxList(corners)
     subset = box_list_ops.gather(boxes, indices)
     with self.test_session() as sess:
         subset_output = sess.run(subset.get())
         self.assertAllClose(subset_output, expected_subset)
コード例 #13
0
 def test_height_width(self):
     corners = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]])
     exp_output_heights = [10., 2.]
     exp_output_widths = [20., 2.]
     boxes = box_list.BoxList(corners)
     heights, widths = box_list_ops.height_width(boxes)
     with self.test_session() as sess:
         output_heights, output_widths = sess.run([heights, widths])
         self.assertAllClose(output_heights, exp_output_heights)
         self.assertAllClose(output_widths, exp_output_widths)
コード例 #14
0
 def test_boolean_mask(self):
     corners = tf.constant(
         [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]])
     indicator = tf.constant([True, False, True, False, True], tf.bool)
     expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]]
     boxes = box_list.BoxList(corners)
     subset = box_list_ops.boolean_mask(boxes, indicator)
     with self.test_session() as sess:
         subset_output = sess.run(subset.get())
         self.assertAllClose(subset_output, expected_subset)
コード例 #15
0
 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)
コード例 #16
0
def tile_anchors(grid_height, grid_width, scales, aspect_ratios,
                 base_anchor_size, anchor_stride, anchor_offset):
    """Create a tiled set of anchors strided along a grid in image space.

  This op creates a set of anchor boxes by placing a "basis" collection of
  boxes with user-specified scales and aspect ratios centered at evenly
  distributed points along a grid.  The basis collection is specified via the
  scale and aspect_ratios arguments.  For example, setting scales=[.1, .2, .2]
  and aspect ratios = [2,2,1/2] means that we create three boxes: one with scale
  .1, aspect ratio 2, one with scale .2, aspect ratio 2, and one with scale .2
  and aspect ratio 1/2.  Each box is multiplied by "base_anchor_size" before
  placing it over its respective center.

  Grid points are specified via grid_height, grid_width parameters as well as
  the anchor_stride and anchor_offset parameters.

  Args:
    grid_height: size of the grid in the y direction (int or int scalar tensor)
    grid_width: size of the grid in the x direction (int or int scalar tensor)
    scales: a 1-d  (float) tensor representing the scale of each box in the
      basis set.
    aspect_ratios: a 1-d (float) tensor representing the aspect ratio of each
      box in the basis set.  The length of the scales and aspect_ratios tensors
      must be equal.
    base_anchor_size: base anchor size as [height, width]
      (float tensor of shape [2])
    anchor_stride: difference in centers between base anchors for adjacent grid
                   positions (float tensor of shape [2])
    anchor_offset: center of the anchor with scale and aspect ratio 1 for the
                   upper left element of the grid, this should be zero for
                   feature networks with only VALID padding and even receptive
                   field size, but may need some additional calculation if other
                   padding is used (float tensor of shape [2])
  Returns:
    a BoxList holding a collection of N anchor boxes
  """
    ratio_sqrts = tf.sqrt(aspect_ratios)
    heights = scales / ratio_sqrts * base_anchor_size[0]
    widths = scales * ratio_sqrts * base_anchor_size[1]

    # Get a grid of box centers
    y_centers = tf.to_float(tf.range(grid_height))
    y_centers = y_centers * anchor_stride[0] + anchor_offset[0]
    x_centers = tf.to_float(tf.range(grid_width))
    x_centers = x_centers * anchor_stride[1] + anchor_offset[1]
    x_centers, y_centers = ops.meshgrid(x_centers, y_centers)

    widths_grid, x_centers_grid = ops.meshgrid(widths, x_centers)
    heights_grid, y_centers_grid = ops.meshgrid(heights, y_centers)
    bbox_centers = tf.stack([y_centers_grid, x_centers_grid], axis=3)
    bbox_sizes = tf.stack([heights_grid, widths_grid], axis=3)
    bbox_centers = tf.reshape(bbox_centers, [-1, 2])
    bbox_sizes = tf.reshape(bbox_sizes, [-1, 2])
    bbox_corners = _center_size_bbox_to_corners_bbox(bbox_centers, bbox_sizes)
    return box_list.BoxList(bbox_corners)
コード例 #17
0
 def test_get_correct_relative_codes_after_encoding(self):
     boxes = [[10., 10., 20., 15.], [0.2, 0.1, 0.5, 0.4]]
     keypoints = [[[15., 12.], [10., 15.]], [[0.5, 0.3], [0.2, 0.4]]]
     num_keypoints = len(keypoints[0])
     anchors = [[15., 12., 30., 18.], [0.1, 0.0, 0.7, 0.9]]
     expected_rel_codes = [[
         -0.5, -0.416666, -0.405465, -0.182321, -0.5, -0.5, -0.833333, 0.
     ],
                           [
                               -0.083333, -0.222222, -0.693147, -1.098612,
                               0.166667, -0.166667, -0.333333, -0.055556
                           ]]
     boxes = box_list.BoxList(tf.constant(boxes))
     boxes.add_field(fields.BoxListFields.keypoints, tf.constant(keypoints))
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = keypoint_box_coder.KeypointBoxCoder(num_keypoints)
     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)
コード例 #18
0
 def test_get_correct_boxes_after_decoding(self):
     anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]]
     rel_codes = [[-0.5, -0.416666, -0.405465, -0.182321],
                  [-0.083333, -0.222222, -0.693147, -1.098612]]
     expected_boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]]
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = faster_rcnn_box_coder.FasterRcnnBoxCoder()
     boxes = coder.decode(rel_codes, anchors)
     with self.test_session() as sess:
         boxes_out, = sess.run([boxes.get()])
         self.assertAllClose(boxes_out, expected_boxes)
コード例 #19
0
    def test_batch_decode(self):
        mock_anchor_corners = tf.constant(
            [[0, 0.1, 0.2, 0.3], [0.2, 0.4, 0.4, 0.6]], tf.float32)
        mock_anchors = box_list.BoxList(mock_anchor_corners)
        mock_box_coder = MockBoxCoder()

        expected_boxes = [[[0.0, 0.1, 0.5, 0.6], [0.5, 0.6, 0.7, 0.8]],
                          [[0.1, 0.2, 0.3, 0.4], [0.7, 0.8, 0.9, 1.0]]]

        encoded_boxes_list = [
            mock_box_coder.encode(box_list.BoxList(tf.constant(boxes)),
                                  mock_anchors) for boxes in expected_boxes
        ]
        encoded_boxes = tf.stack(encoded_boxes_list)
        decoded_boxes = box_coder.batch_decode(encoded_boxes, mock_box_coder,
                                               mock_anchors)

        with self.test_session() as sess:
            decoded_boxes_result = sess.run(decoded_boxes)
            self.assertAllClose(expected_boxes, decoded_boxes_result)
コード例 #20
0
    def _compute_loss(self, prediction_tensor, target_tensor, weights):
        """Compute loss function.

    Args:
      prediction_tensor: A float tensor of shape [batch_size, num_anchors, 4]
        representing the decoded predicted boxes
      target_tensor: A float tensor of shape [batch_size, num_anchors, 4]
        representing the decoded target boxes
      weights: a float tensor of shape [batch_size, num_anchors]

    Returns:
      loss: a float tensor of shape [batch_size, num_anchors] tensor
        representing the value of the loss function.
    """
        predicted_boxes = box_list.BoxList(
            tf.reshape(prediction_tensor, [-1, 4]))
        target_boxes = box_list.BoxList(tf.reshape(target_tensor, [-1, 4]))
        per_anchor_iou_loss = 1.0 - box_list_ops.matched_iou(
            predicted_boxes, target_boxes)
        return tf.reshape(weights, [-1]) * per_anchor_iou_loss
コード例 #21
0
  def _assign_targets(self, groundtruth_boxes_list, groundtruth_classes_list,
                      groundtruth_keypoints_list=None,
                      groundtruth_weights_list=None):
    """Assign groundtruth targets.

    Adds a background class to each one-hot encoding of groundtruth classes
    and uses target assigner to obtain regression and classification targets.

    Args:
      groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4]
        containing coordinates of the groundtruth boxes.
          Groundtruth boxes are provided in [y_min, x_min, y_max, x_max]
          format and assumed to be normalized and clipped
          relative to the image window with y_min <= y_max and x_min <= x_max.
      groundtruth_classes_list: a list of 2-D one-hot (or k-hot) tensors of
        shape [num_boxes, num_classes] containing the class targets with the 0th
        index assumed to map to the first non-background class.
      groundtruth_keypoints_list: (optional) a list of 3-D tensors of shape
        [num_boxes, num_keypoints, 2]
      groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape
        [num_boxes] containing weights for groundtruth boxes.

    Returns:
      batch_cls_targets: a tensor with shape [batch_size, num_anchors,
        num_classes],
      batch_cls_weights: a tensor with shape [batch_size, num_anchors],
      batch_reg_targets: a tensor with shape [batch_size, num_anchors,
        box_code_dimension]
      batch_reg_weights: a tensor with shape [batch_size, num_anchors],
      match_list: a list of matcher.Match objects encoding the match between
        anchors and groundtruth boxes for each image of the batch,
        with rows of the Match objects corresponding to groundtruth boxes
        and columns corresponding to anchors.
    """
    groundtruth_boxlists = [
        box_list.BoxList(boxes) for boxes in groundtruth_boxes_list
    ]
    if self._add_background_class:
      groundtruth_classes_with_background_list = [
          tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')
          for one_hot_encoding in groundtruth_classes_list
      ]
    else:
      groundtruth_classes_with_background_list = groundtruth_classes_list

    if groundtruth_keypoints_list is not None:
      for boxlist, keypoints in zip(
          groundtruth_boxlists, groundtruth_keypoints_list):
        boxlist.add_field(fields.BoxListFields.keypoints, keypoints)
    return target_assigner.batch_assign_targets(
        self._target_assigner, self.anchors, groundtruth_boxlists,
        groundtruth_classes_with_background_list, self._unmatched_class_label,
        groundtruth_weights_list)
コード例 #22
0
 def test_correct_boxes_with_non_default_scale(self):
     anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]]
     rel_codes = [[-1., -1.25, -1.62186], [-0.166667, -0.666667, -2.772588]]
     scale_factors = [2, 3, 4]
     expected_boxes = [[14.594306, 7.884875, 20.918861, 14.209432],
                       [0.155051, 0.102989, 0.522474, 0.470412]]
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = square_box_coder.SquareBoxCoder(scale_factors=scale_factors)
     boxes = coder.decode(rel_codes, anchors)
     with self.test_session() as sess:
         (boxes_out, ) = sess.run([boxes.get()])
         self.assertAllClose(boxes_out, expected_boxes)
コード例 #23
0
 def test_visualize_boxes_in_image(self):
     image = tf.zeros((6, 4, 3))
     corners = tf.constant([[0, 0, 5, 3], [0, 0, 3, 2]], tf.float32)
     boxes = box_list.BoxList(corners)
     image_and_boxes = box_list_ops.visualize_boxes_in_image(image, boxes)
     image_and_boxes_bw = tf.to_float(
         tf.greater(tf.reduce_sum(image_and_boxes, 2), 0.0))
     exp_result = [[1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0], [1, 0, 1, 0],
                   [1, 1, 1, 0], [0, 0, 0, 0]]
     with self.test_session() as sess:
         output = sess.run(image_and_boxes_bw)
         self.assertAllEqual(output.astype(int), exp_result)
コード例 #24
0
    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)
コード例 #25
0
    def test_get_minimal_coverage_box(self):
        boxes = [[0., 0., 1., 1.], [-1., 1., 2., 3.], [0., 2., 3., 4.]]

        expected_coverage_box = [[-1., 0., 3., 4.]]

        corners = tf.constant(boxes, dtype=tf.float32)
        boxlist = box_list.BoxList(corners)
        coverage_box = box_list_ops.get_minimal_coverage_box(boxlist)
        with self.test_session() as sess:
            coverage_box_out = sess.run(coverage_box)

        self.assertAllClose(expected_coverage_box, coverage_box_out)
コード例 #26
0
    def test_concatenate_is_correct(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], [1, 0, 5, 10]],
                               tf.float32)
        scores2 = tf.constant([1.0, 2.1, 5.6])

        exp_corners = [[0, 0, 0, 0], [1, 2, 3, 4], [0, 3, 1, 6], [2, 4, 3, 8],
                       [1, 0, 5, 10]]
        exp_scores = [1.0, 2.1, 1.0, 2.1, 5.6]

        boxlist1 = box_list.BoxList(corners1)
        boxlist1.add_field('scores', scores1)
        boxlist2 = box_list.BoxList(corners2)
        boxlist2.add_field('scores', scores2)
        result = box_list_ops.concatenate([boxlist1, boxlist2])
        with self.test_session() as sess:
            corners_output, scores_output = sess.run(
                [result.get(), result.get_field('scores')])
            self.assertAllClose(corners_output, exp_corners)
            self.assertAllClose(scores_output, exp_scores)
コード例 #27
0
    def test_to_normalized_coordinates_already_normalized(self):
        coordinates = tf.constant([[0, 0, 1, 1], [0.25, 0.25, 0.75, 0.75]],
                                  tf.float32)
        img = tf.ones((128, 100, 100, 3))
        boxlist = box_list.BoxList(coordinates)
        normalized_boxlist = box_list_ops.to_normalized_coordinates(
            boxlist,
            tf.shape(img)[1],
            tf.shape(img)[2])

        with self.test_session() as sess:
            with self.assertRaisesOpError('assertion failed'):
                sess.run(normalized_boxlist.get())
コード例 #28
0
 def test_get_correct_boxes_after_decoding_with_scaling(self):
     anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]]
     rel_codes = [[-1., -1.25, -1.62186, -0.911608],
                  [-0.166667, -0.666667, -2.772588, -5.493062]]
     scale_factors = [2, 3, 4, 5]
     expected_boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]]
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = faster_rcnn_box_coder.FasterRcnnBoxCoder(
         scale_factors=scale_factors)
     boxes = coder.decode(rel_codes, anchors)
     with self.test_session() as sess:
         boxes_out, = sess.run([boxes.get()])
         self.assertAllClose(boxes_out, expected_boxes)
コード例 #29
0
    def test_box_voting(self):
        candidates = box_list.BoxList(
            tf.constant([[0.1, 0.1, 0.4, 0.4], [0.6, 0.6, 0.8, 0.8]],
                        tf.float32))
        candidates.add_field('ExtraField', tf.constant([1, 2]))
        pool = box_list.BoxList(
            tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5],
                         [0.6, 0.6, 0.8, 0.8]], tf.float32))
        pool.add_field('scores', tf.constant([0.75, 0.25, 0.3]))
        averaged_boxes = box_list_ops.box_voting(candidates, pool)
        expected_boxes = [[0.1, 0.1, 0.425, 0.425], [0.6, 0.6, 0.8, 0.8]]
        expected_scores = [0.5, 0.3]
        with self.test_session() as sess:
            boxes_out, scores_out, extra_field_out = sess.run([
                averaged_boxes.get(),
                averaged_boxes.get_field('scores'),
                averaged_boxes.get_field('ExtraField')
            ])

            self.assertAllClose(expected_boxes, boxes_out)
            self.assertAllClose(expected_scores, scores_out)
            self.assertAllEqual(extra_field_out, [1, 2])
コード例 #30
0
    def test_to_absolute_coordinates_already_abolute(self):
        coordinates = tf.constant([[0, 0, 100, 100], [25, 25, 75, 75]],
                                  tf.float32)
        img = tf.ones((128, 100, 100, 3))
        boxlist = box_list.BoxList(coordinates)
        absolute_boxlist = box_list_ops.to_absolute_coordinates(
            boxlist,
            tf.shape(img)[1],
            tf.shape(img)[2])

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