Exemple #1
0
    def test_create_ssd_anchors_returns_correct_shape(self):
        anchor_generator = ag.create_ssd_anchors(
            num_layers=6,
            min_scale=0.2,
            max_scale=0.95,
            aspect_ratios=(1.0, 2.0, 3.0, 1.0 / 2, 1.0 / 3),
            reduce_boxes_in_lowest_layer=True)

        feature_map_shape_list = [(38, 38), (19, 19), (10, 10), (5, 5), (3, 3),
                                  (1, 1)]
        anchors = anchor_generator.generate(
            feature_map_shape_list=feature_map_shape_list)
        anchor_corners = anchors.get()
        with self.test_session():
            anchor_corners_out = anchor_corners.eval()
            self.assertEquals(anchor_corners_out.shape, (7308, 4))

        anchor_generator = ag.create_ssd_anchors(
            num_layers=6,
            min_scale=0.2,
            max_scale=0.95,
            aspect_ratios=(1.0, 2.0, 3.0, 1.0 / 2, 1.0 / 3),
            reduce_boxes_in_lowest_layer=False)

        feature_map_shape_list = [(38, 38), (19, 19), (10, 10), (5, 5), (3, 3),
                                  (1, 1)]
        anchors = anchor_generator.generate(
            feature_map_shape_list=feature_map_shape_list)
        anchor_corners = anchors.get()
        with self.test_session():
            anchor_corners_out = anchor_corners.eval()
            self.assertEquals(anchor_corners_out.shape, (11640, 4))
Exemple #2
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))
  else:
    raise ValueError('Empty anchor generator.')
    def graph_fn2():
      anchor_generator = ag.create_ssd_anchors(
          num_layers=6, min_scale=0.2, max_scale=0.95,
          aspect_ratios=(1.0, 2.0, 3.0, 1.0/2, 1.0/3),
          reduce_boxes_in_lowest_layer=False)

      feature_map_shape_list = [(38, 38), (19, 19), (10, 10),
                                (5, 5), (3, 3), (1, 1)]
      anchors = anchor_generator.generate(
          feature_map_shape_list=feature_map_shape_list)
      return anchors.get()
        def graph_fn2():
            anchor_generator = ag.create_ssd_anchors(
                num_layers=6, min_scale=0.2, max_scale=0.95,
                aspect_ratios=(1.0, 2.0, 3.0, 1.0 / 2, 1.0 / 3),
                reduce_boxes_in_lowest_layer=False)

            feature_map_shape_list = [(38, 38), (19, 19), (10, 10),
                                      (5, 5), (3, 3), (1, 1)]
            anchors = anchor_generator.generate(
                feature_map_shape_list=feature_map_shape_list)
            return anchors.get()
def get_feature_map_anchor_boxes(feature_map_shape_list, **anchor_kwargs):
    """
    :param feature_map_shape_list: list of tuples containing feature map resolutions
    :returns: dict with feature map shape tuple as key and list of [ymin, xmin, ymax, xmax] box co-ordinates
    """
    anchor_generator = create_ssd_anchors(**anchor_kwargs)

    anchor_box_lists = anchor_generator.generate(feature_map_shape_list)
    
    feature_map_boxes = {}

    with tf.Session() as sess:
        for shape, box_list in zip(feature_map_shape_list, anchor_box_lists):
            feature_map_boxes[shape] = sess.run(box_list.data['boxes'])
            
    return feature_map_boxes
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(     #this is what we have in the faster_rcnn_resnet1-1_pet config file 
      'anchor_generator_oneof') == 'grid_anchor_generator':
    grid_anchor_generator_config = anchor_generator_config.grid_anchor_generator  #get the parameters relevent to grid_anchor_generator 
    return grid_anchor_generator.GridAnchorGenerator(
        scales=[float(scale) for scale in grid_anchor_generator_config.scales],  #scales passing to a list 
        aspect_ratios=[float(aspect_ratio)   #take the aspect ration in to a list 
                       for aspect_ratio
                       in grid_anchor_generator_config.aspect_ratios],
        base_anchor_size=[grid_anchor_generator_config.height,          #currently this is not given in the problem 
                          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,  #not given in the moment 
                       grid_anchor_generator_config.width_offset])   #returns an object where we can access and modify thngs 

#anchor_stride - difference in centers between base anchors for adjacent grid positions (length-2 float32 list, default=[16, 16])


  elif anchor_generator_config.WhichOneof(
      'anchor_generator_oneof') == 'ssd_anchor_generator':    #for the ssd generatot 
    ssd_anchor_generator_config = anchor_generator_config.ssd_anchor_generator #take the configurations 
    return multiple_grid_anchor_generator.create_ssd_anchors(         #Generates grid anchors on the fly corresponding to multiple CNN layers
        num_layers=ssd_anchor_generator_config.num_layers,
        min_scale=ssd_anchor_generator_config.min_scale,
        max_scale=ssd_anchor_generator_config.max_scale,                #generated grids hard corder and pretty much grid 
        aspect_ratios=ssd_anchor_generator_config.aspect_ratios,
        reduce_boxes_in_lowest_layer=(ssd_anchor_generator_config
                                      .reduce_boxes_in_lowest_layer))
  else:
    raise ValueError('Empty anchor generator.')
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.')