def test_construct_normalized_anchors_fails_with_unit_dimensions(self):
   anchor_generator = mg.MultiscaleGridAnchorGenerator(
       min_level=5, max_level=5, anchor_scale=1.0, aspect_ratios=[1.0],
       scales_per_octave=1, normalize_coordinates=True)
   with self.assertRaisesRegexp(ValueError, 'Normalized coordinates'):
     anchor_generator.generate(
         feature_map_shape_list=[(2, 2)], im_height=1, im_width=1)
  def test_construct_single_anchor_unit_dimensions(self):
    min_level = 5
    max_level = 5
    anchor_scale = 1.0
    aspect_ratios = [1.0]
    scales_per_octave = 1
    im_height = 1
    im_width = 1
    feature_map_shape_list = [(2, 2)]
    # Positive offsets are produced.
    exp_anchor_corners = [[0, 0, 32, 32],
                          [0, 32, 32, 64],
                          [32, 0, 64, 32],
                          [32, 32, 64, 64]]

    anchor_generator = mg.MultiscaleGridAnchorGenerator(
        min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave,
        normalize_coordinates=False)
    anchors_list = anchor_generator.generate(
        feature_map_shape_list, im_height=im_height, im_width=im_width)
    anchor_corners = anchors_list[0].get()

    with self.test_session():
      anchor_corners_out = anchor_corners.eval()
      self.assertAllClose(anchor_corners_out, exp_anchor_corners)
  def test_construct_single_anchor_dynamic_size(self):
    min_level = 5
    max_level = 5
    anchor_scale = 4.0
    aspect_ratios = [1.0]
    scales_per_octave = 1
    im_height = tf.constant(64)
    im_width = tf.constant(64)
    feature_map_shape_list = [(2, 2)]
    # Zero offsets are used.
    exp_anchor_corners = [[-64, -64, 64, 64],
                          [-64, -32, 64, 96],
                          [-32, -64, 96, 64],
                          [-32, -32, 96, 96]]

    anchor_generator = mg.MultiscaleGridAnchorGenerator(
        min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave,
        normalize_coordinates=False)
    anchors_list = anchor_generator.generate(
        feature_map_shape_list, im_height=im_height, im_width=im_width)
    anchor_corners = anchors_list[0].get()

    with self.test_session():
      anchor_corners_out = anchor_corners.eval()
      self.assertAllClose(anchor_corners_out, exp_anchor_corners)
 def test_num_anchors_per_location(self):
   min_level = 5
   max_level = 6
   anchor_scale = 4.0
   aspect_ratios = [1.0, 2.0]
   scales_per_octave = 3
   anchor_generator = mg.MultiscaleGridAnchorGenerator(
       min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave,
       normalize_coordinates=False)
   self.assertEqual(anchor_generator.num_anchors_per_location(), [6, 6])
 def graph_fn():
   min_level = 5
   max_level = 5
   anchor_scale = 4.0
   aspect_ratios = [1.0]
   scales_per_octave = 1
   im_height = 65
   im_width = 65
   feature_map_shape_list = [(3, 3)]
   anchor_generator = mg.MultiscaleGridAnchorGenerator(
       min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave,
       normalize_coordinates=False)
   anchors_list = anchor_generator.generate(
       feature_map_shape_list, im_height=im_height, im_width=im_width)
   anchor_corners = anchors_list[0].get()
   return (anchor_corners,)
 def graph_fn():
   min_level = 6
   max_level = 6
   anchor_scale = 4.0
   aspect_ratios = [1.0, 2.0]
   scales_per_octave = 2
   im_height = 64
   im_width = 64
   feature_map_shape_list = [(1, 1)]
   anchor_generator = mg.MultiscaleGridAnchorGenerator(
       min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave,
       normalize_coordinates=False)
   anchors_list = anchor_generator.generate(feature_map_shape_list,
                                            im_height=im_height,
                                            im_width=im_width)
   anchor_corners = [anchors.get() for anchors in anchors_list]
   return anchor_corners
  def test_construct_single_anchor_in_normalized_coordinates(self):
    min_level = 5
    max_level = 5
    anchor_scale = 4.0
    aspect_ratios = [1.0]
    scales_per_octave = 1
    im_height = 64
    im_width = 128
    feature_map_shape_list = [(2, 2)]
    exp_anchor_corners = [[-48./64, -48./128, 80./64, 80./128],
                          [-48./64, -16./128, 80./64, 112./128],
                          [-16./64, -48./128, 112./64, 80./128],
                          [-16./64, -16./128, 112./64, 112./128]]
    anchor_generator = mg.MultiscaleGridAnchorGenerator(
        min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave,
        normalize_coordinates=True)
    anchors_list = anchor_generator.generate(
        feature_map_shape_list, im_height=im_height, im_width=im_width)
    anchor_corners = anchors_list[0].get()

    with self.test_session():
      anchor_corners_out = anchor_corners.eval()
      self.assertAllClose(anchor_corners_out, exp_anchor_corners)
Beispiel #8
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 not isinstance(anchor_generator_config,
                      anchor_generator_pb2.AnchorGenerator):
        raise ValueError('anchor_generator_config not of type '
                         'anchor_generator_pb2.AnchorGenerator')
    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)
    elif anchor_generator_config.WhichOneof(
            'anchor_generator_oneof') == 'flexible_grid_anchor_generator':
        cfg = anchor_generator_config.flexible_grid_anchor_generator
        base_sizes = []
        aspect_ratios = []
        strides = []
        offsets = []
        for anchor_grid in cfg.anchor_grid:
            base_sizes.append(tuple(anchor_grid.base_sizes))
            aspect_ratios.append(tuple(anchor_grid.aspect_ratios))
            strides.append(
                (anchor_grid.height_stride, anchor_grid.width_stride))
            offsets.append(
                (anchor_grid.height_offset, anchor_grid.width_offset))
        return flexible_grid_anchor_generator.FlexibleGridAnchorGenerator(
            base_sizes, aspect_ratios, strides, offsets,
            cfg.normalize_coordinates)
    else:
        raise ValueError('Empty anchor generator.')