Пример #1
0
 def graph_fn():
     scales = [0.5, 1.0, 2.0]
     aspect_ratios = [0.25, 1.0, 4.0]
     anchor_offset = [7, -3]
     anchor_generator = grid_anchor_generator.GridAnchorGenerator(
         scales, aspect_ratios, anchor_offset=anchor_offset)
     anchors_list = anchor_generator.generate(
         feature_map_shape_list=[(1, 1)])
     anchor_corners = anchors_list[0].get()
     return anchor_corners,
    def _generate(self, feature_map_shape_list, im_height, im_width):
        """Generates a collection of bounding boxes to be used as anchors.

    Currently we require the input image shape to be statically defined.  That
    is, im_height and im_width should be integers rather than tensors.

    Args:
      feature_map_shape_list: list of pairs of convnet layer resolutions in the
        format [(height_0, width_0), (height_1, width_1), ...]. For example,
        setting feature_map_shape_list=[(8, 8), (7, 7)] asks for anchors that
        correspond to an 8x8 layer followed by a 7x7 layer.
      im_height: the height of the image to generate the grid for.
      im_width: the width of the image to generate the grid for.

    Returns:
      boxes_list: a list of BoxLists each holding anchor boxes corresponding to
        the input feature map shapes.
    Raises:
      ValueError: if im_height and im_width are not integers.
    """
        if not isinstance(im_height, int) or not isinstance(im_width, int):
            raise ValueError(
                'MultiscaleGridAnchorGenerator currently requires '
                'input image shape to be statically defined.')
        anchor_grid_list = []
        for feat_shape, grid_info in zip(feature_map_shape_list,
                                         self._anchor_grid_info):
            # TODO(rathodv) check the feature_map_shape_list is consistent with
            # self._anchor_grid_info
            level = grid_info['level']
            stride = 2**level
            scales, aspect_ratios, base_anchor_size, anchor_stride = grid_info[
                'info']
            feat_h = feat_shape[0]
            feat_w = feat_shape[1]
            anchor_offset = [0, 0]
            if im_height % 2.0**level == 0:
                anchor_offset[0] = stride / 2.0
            if im_width % 2.0**level == 0:
                anchor_offset[1] = stride / 2.0
            ag = grid_anchor_generator.GridAnchorGenerator(
                scales,
                aspect_ratios,
                base_anchor_size=base_anchor_size,
                anchor_stride=anchor_stride,
                anchor_offset=anchor_offset)
            (anchor_grid, ) = ag.generate(feature_map_shape_list=[(feat_h,
                                                                   feat_w)])

            if self._normalize_coordinates:
                anchor_grid = box_list_ops.to_normalized_coordinates(
                    anchor_grid, im_height, im_width, check_range=False)
            anchor_grid_list.append(anchor_grid)

        return anchor_grid_list
Пример #3
0
        def graph_fn(feature_map_height, feature_map_width):
            base_anchor_size = [10, 10]
            anchor_stride = [19, 19]
            anchor_offset = [0, 0]
            scales = [0.5, 1.0, 2.0]
            aspect_ratios = [1.0]
            anchor_generator = grid_anchor_generator.GridAnchorGenerator(
                scales,
                aspect_ratios,
                base_anchor_size=base_anchor_size,
                anchor_stride=anchor_stride,
                anchor_offset=anchor_offset)

            anchors_list = anchor_generator.generate(
                feature_map_shape_list=[(feature_map_height,
                                         feature_map_width)])
            anchor_corners = anchors_list[0].get()
            return anchor_corners,
Пример #4
0
def build(anchor_generator_config):
    """Builds an anchor generator based on the config.

  Args:
    anchor_generator_config: An anchor_generator.proto object containing the
      config for the desired anchor generator.

  Returns:
    Anchor generator based on the config.

  Raises:
    ValueError: On empty anchor generator proto.
  """
    if False and not isinstance(anchor_generator_config,
                                anchor_generator_pb2.AnchorGenerator):
        raise ValueError('anchor_generator_config not of type '
                         'anchor_generator_pb2.AnchorGenerator {0}'.format(
                             type(anchor_generator_config)))
    if anchor_generator_config.WhichOneof(
            'anchor_generator_oneof') == 'grid_anchor_generator':
        grid_anchor_generator_config = anchor_generator_config.grid_anchor_generator
        return grid_anchor_generator.GridAnchorGenerator(
            scales=[
                float(scale) for scale in grid_anchor_generator_config.scales
            ],
            aspect_ratios=[
                float(aspect_ratio)
                for aspect_ratio in grid_anchor_generator_config.aspect_ratios
            ],
            base_anchor_size=[
                grid_anchor_generator_config.height,
                grid_anchor_generator_config.width
            ],
            anchor_stride=[
                grid_anchor_generator_config.height_stride,
                grid_anchor_generator_config.width_stride
            ],
            anchor_offset=[
                grid_anchor_generator_config.height_offset,
                grid_anchor_generator_config.width_offset
            ])
    elif anchor_generator_config.WhichOneof(
            'anchor_generator_oneof') == 'ssd_anchor_generator':
        ssd_anchor_generator_config = anchor_generator_config.ssd_anchor_generator
        anchor_strides = None
        if ssd_anchor_generator_config.height_stride:
            anchor_strides = zip(ssd_anchor_generator_config.height_stride,
                                 ssd_anchor_generator_config.width_stride)
        anchor_offsets = None
        if ssd_anchor_generator_config.height_offset:
            anchor_offsets = zip(ssd_anchor_generator_config.height_offset,
                                 ssd_anchor_generator_config.width_offset)
        return multiple_grid_anchor_generator.create_ssd_anchors(
            num_layers=ssd_anchor_generator_config.num_layers,
            min_scale=ssd_anchor_generator_config.min_scale,
            max_scale=ssd_anchor_generator_config.max_scale,
            scales=[
                float(scale) for scale in ssd_anchor_generator_config.scales
            ],
            aspect_ratios=ssd_anchor_generator_config.aspect_ratios,
            interpolated_scale_aspect_ratio=(
                ssd_anchor_generator_config.interpolated_scale_aspect_ratio),
            base_anchor_size=[
                ssd_anchor_generator_config.base_anchor_height,
                ssd_anchor_generator_config.base_anchor_width
            ],
            anchor_strides=anchor_strides,
            anchor_offsets=anchor_offsets,
            reduce_boxes_in_lowest_layer=(
                ssd_anchor_generator_config.reduce_boxes_in_lowest_layer))
    elif anchor_generator_config.WhichOneof(
            'anchor_generator_oneof') == 'multiscale_anchor_generator':
        cfg = anchor_generator_config.multiscale_anchor_generator
        return multiscale_grid_anchor_generator.MultiscaleGridAnchorGenerator(
            cfg.min_level, cfg.max_level, cfg.anchor_scale,
            [float(aspect_ratio) for aspect_ratio in cfg.aspect_ratios],
            cfg.scales_per_octave, cfg.normalize_coordinates)
    else:
        raise ValueError('Empty anchor generator.')