Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
 def graph_fn(feature_map1_height, feature_map1_width,
              feature_map2_height, feature_map2_width):
     min_level = 5
     max_level = 6
     anchor_scale = 4.0
     aspect_ratios = [1.0]
     scales_per_octave = 1
     im_height = 64
     im_width = 64
     feature_map_shape_list = [
         (feature_map1_height, feature_map1_width),
         (feature_map2_height, feature_map2_width)
     ]
     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
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
 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])
Exemplo n.º 6
0
 def test_construct_single_anchor_fails_with_tensor_image_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)]
     anchor_generator = mg.MultiscaleGridAnchorGenerator(
         min_level,
         max_level,
         anchor_scale,
         aspect_ratios,
         scales_per_octave,
         normalize_coordinates=False)
     with self.assertRaisesRegexp(ValueError, 'statically defined'):
         anchor_generator.generate(feature_map_shape_list,
                                   im_height=im_height,
                                   im_width=im_width)
Exemplo n.º 7
0
 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,
Exemplo n.º 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 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.')