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 test_construct_anchor_grid(self): base_anchor_size = [10, 10] anchor_stride = [19, 19] anchor_offset = [0, 0] scales = [0.5, 1.0, 2.0] aspect_ratios = [1.0] exp_anchor_corners = [[-2.5, -2.5, 2.5, 2.5], [-5., -5., 5., 5.], [-10., -10., 10., 10.], [-2.5, 16.5, 2.5, 21.5], [-5., 14., 5, 24], [-10., 9., 10, 29], [16.5, -2.5, 21.5, 2.5], [14., -5., 24, 5], [9., -10., 29, 10], [16.5, 16.5, 21.5, 21.5], [14., 14., 24, 24], [9., 9., 29, 29]] anchor_generator = grid_anchor_generator.GridAnchorGenerator( scales, aspect_ratios, base_anchor_size=base_anchor_size, anchor_stride=anchor_stride, anchor_offset=anchor_offset) anchors = anchor_generator.generate(feature_map_shape_list=[(2, 2)]) anchor_corners = anchors.get() with self.test_session(): anchor_corners_out = anchor_corners.eval() self.assertAllClose(anchor_corners_out, exp_anchor_corners)
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') == 'fpn_anchor_generator': fpn_anchor_generator_config = anchor_generator_config.fpn_anchor_generator return multiple_grid_anchor_generator.FpnAnchorGenerator( scales=[ float(scale) for scale in fpn_anchor_generator_config.scales ], aspect_ratios=[ float(aspect_ratio) for aspect_ratio in fpn_anchor_generator_config.aspect_ratios ], base_anchor_size=[ fpn_anchor_generator_config.height, fpn_anchor_generator_config.width ], ) else: raise ValueError('Empty anchor generator.')
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 = anchor_generator.generate(feature_map_shape_list=[(1, 1)]) anchor_corners = anchors.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
def get_real_model(self, training): """Get real model of AnchorGenerator.""" if self.model: return self.model else: self.model = grid_anchor_generator.GridAnchorGenerator( scales=[float(scale) for scale in self.scales], aspect_ratios=[float(aspect_ratio) for aspect_ratio in self.aspect_ratios], base_anchor_size=[self.height, self.width], anchor_stride=[self.height_stride, self.width_stride], anchor_offset=[self.height_offset, self.width_offset]) return self.model
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 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 = anchor_generator.generate( feature_map_shape_list=[(feature_map_height, feature_map_width)]) anchor_corners = anchors.get() return (anchor_corners,)
def test_construct_single_anchor(self): """Builds a 1x1 anchor grid to test the size of the output boxes.""" scales = [0.5, 1.0, 2.0] aspect_ratios = [0.25, 1.0, 4.0] anchor_offset = [7, -3] exp_anchor_corners = [[-121, -35, 135, 29], [-249, -67, 263, 61], [-505, -131, 519, 125], [-57, -67, 71, 61], [-121, -131, 135, 125], [-249, -259, 263, 253], [-25, -131, 39, 125], [-57, -259, 71, 253], [-121, -515, 135, 509]] anchor_generator = grid_anchor_generator.GridAnchorGenerator( scales, aspect_ratios, anchor_offset=anchor_offset) anchors = anchor_generator.generate(feature_map_shape_list=[(1, 1)]) anchor_corners = anchors.get() with self.test_session(): anchor_corners_out = anchor_corners.eval() self.assertAllClose(anchor_corners_out, exp_anchor_corners)
def _build_model(self, is_training, first_stage_only, second_stage_batch_size, first_stage_max_proposals=8, num_classes=2, hard_mining=False): def image_resizer_fn(image): return tf.identity(image) # anchors in this test are designed so that a subset of anchors are inside # the image and a subset of anchors are outside. first_stage_anchor_scales = (0.001, 0.005, 0.1) first_stage_anchor_aspect_ratios = (0.5, 1.0, 2.0) first_stage_anchor_strides = (1, 1) first_stage_anchor_generator = grid_anchor_generator.GridAnchorGenerator( first_stage_anchor_scales, first_stage_anchor_aspect_ratios, anchor_stride=first_stage_anchor_strides) fake_feature_extractor = FakeFasterRCNNFeatureExtractor() first_stage_box_predictor_hyperparams_text_proto = """ op: CONV activation: RELU regularizer { l2_regularizer { weight: 0.00004 } } initializer { truncated_normal_initializer { stddev: 0.03 } } """ first_stage_box_predictor_arg_scope = ( self._build_arg_scope_with_hyperparams( first_stage_box_predictor_hyperparams_text_proto, is_training)) first_stage_box_predictor_kernel_size = 3 first_stage_atrous_rate = 1 first_stage_box_predictor_depth = 512 first_stage_minibatch_size = 3 first_stage_positive_balance_fraction = .5 first_stage_nms_score_threshold = -1.0 first_stage_nms_iou_threshold = 1.0 first_stage_max_proposals = first_stage_max_proposals first_stage_localization_loss_weight = 1.0 first_stage_objectness_loss_weight = 1.0 post_processing_text_proto = """ batch_non_max_suppression { score_threshold: -20.0 iou_threshold: 1.0 max_detections_per_class: 5 max_total_detections: 5 } """ post_processing_config = post_processing_pb2.PostProcessing() text_format.Merge(post_processing_text_proto, post_processing_config) second_stage_non_max_suppression_fn, _ = post_processing_builder.build( post_processing_config) second_stage_balance_fraction = 1.0 second_stage_score_conversion_fn = tf.identity second_stage_localization_loss_weight = 1.0 second_stage_classification_loss_weight = 1.0 second_stage_mask_loss_weight = 1.0 second_stage_motion_loss_weight = 1.0 first_stage_camera_motion_arg_scope = None first_stage_camera_motion_loss_weight = 1.0 first_stage_predict_camera_motion = False hard_example_miner = None if hard_mining: hard_example_miner = losses.HardExampleMiner( num_hard_examples=1, iou_threshold=0.99, loss_type='both', cls_loss_weight=second_stage_classification_loss_weight, loc_loss_weight=second_stage_localization_loss_weight, max_negatives_per_positive=None) common_kwargs = { 'is_training': is_training, 'num_classes': num_classes, 'image_resizer_fn': image_resizer_fn, 'feature_extractor': fake_feature_extractor, 'first_stage_only': first_stage_only, 'first_stage_anchor_generator': first_stage_anchor_generator, 'first_stage_atrous_rate': first_stage_atrous_rate, 'first_stage_box_predictor_arg_scope': first_stage_box_predictor_arg_scope, 'first_stage_box_predictor_kernel_size': first_stage_box_predictor_kernel_size, 'first_stage_box_predictor_depth': first_stage_box_predictor_depth, 'first_stage_minibatch_size': first_stage_minibatch_size, 'first_stage_positive_balance_fraction': first_stage_positive_balance_fraction, 'first_stage_nms_score_threshold': first_stage_nms_score_threshold, 'first_stage_nms_iou_threshold': first_stage_nms_iou_threshold, 'first_stage_max_proposals': first_stage_max_proposals, 'first_stage_localization_loss_weight': first_stage_localization_loss_weight, 'first_stage_objectness_loss_weight': first_stage_objectness_loss_weight, 'second_stage_batch_size': second_stage_batch_size, 'second_stage_balance_fraction': second_stage_balance_fraction, 'second_stage_non_max_suppression_fn': second_stage_non_max_suppression_fn, 'second_stage_score_conversion_fn': second_stage_score_conversion_fn, 'second_stage_localization_loss_weight': second_stage_localization_loss_weight, 'second_stage_classification_loss_weight': second_stage_classification_loss_weight, 'second_stage_mask_loss_weight': second_stage_mask_loss_weight, 'second_stage_motion_loss_weight': second_stage_motion_loss_weight, 'first_stage_camera_motion_loss_weight': first_stage_camera_motion_loss_weight, 'first_stage_predict_camera_motion': first_stage_predict_camera_motion, 'first_stage_camera_motion_arg_scope': first_stage_camera_motion_arg_scope, 'hard_example_miner': hard_example_miner} return self._get_model(self._get_second_stage_box_predictor( num_classes=num_classes, is_training=is_training), **common_kwargs)
def _build_model(self, is_training, number_of_stages, second_stage_batch_size, first_stage_max_proposals=8, num_classes=2, hard_mining=False, softmax_second_stage_classification_loss=True, predict_masks=False, pad_to_max_dimension=None, masks_are_class_agnostic=False, use_matmul_crop_and_resize=False, clip_anchors_to_image=False, use_matmul_gather_in_matcher=False, use_static_shapes=False, calibration_mapping_value=None, share_box_across_classes=False, return_raw_detections_during_predict=False): use_keras = tf_version.is_tf2() def image_resizer_fn(image, masks=None): """Fake image resizer function.""" resized_inputs = [] resized_image = tf.identity(image) if pad_to_max_dimension is not None: resized_image = tf.image.pad_to_bounding_box( image, 0, 0, pad_to_max_dimension, pad_to_max_dimension) resized_inputs.append(resized_image) if masks is not None: resized_masks = tf.identity(masks) if pad_to_max_dimension is not None: resized_masks = tf.image.pad_to_bounding_box( tf.transpose(masks, [1, 2, 0]), 0, 0, pad_to_max_dimension, pad_to_max_dimension) resized_masks = tf.transpose(resized_masks, [2, 0, 1]) resized_inputs.append(resized_masks) resized_inputs.append(tf.shape(image)) return resized_inputs # anchors in this test are designed so that a subset of anchors are inside # the image and a subset of anchors are outside. first_stage_anchor_scales = (0.001, 0.005, 0.1) first_stage_anchor_aspect_ratios = (0.5, 1.0, 2.0) first_stage_anchor_strides = (1, 1) first_stage_anchor_generator = grid_anchor_generator.GridAnchorGenerator( first_stage_anchor_scales, first_stage_anchor_aspect_ratios, anchor_stride=first_stage_anchor_strides) first_stage_target_assigner = target_assigner.create_target_assigner( 'FasterRCNN', 'proposal', use_matmul_gather=use_matmul_gather_in_matcher) if use_keras: fake_feature_extractor = FakeFasterRCNNKerasFeatureExtractor() else: fake_feature_extractor = FakeFasterRCNNFeatureExtractor() first_stage_box_predictor_hyperparams_text_proto = """ op: CONV activation: RELU regularizer { l2_regularizer { weight: 0.00004 } } initializer { truncated_normal_initializer { stddev: 0.03 } } """ if use_keras: first_stage_box_predictor_arg_scope_fn = ( self._build_keras_layer_hyperparams( first_stage_box_predictor_hyperparams_text_proto)) else: first_stage_box_predictor_arg_scope_fn = ( self._build_arg_scope_with_hyperparams( first_stage_box_predictor_hyperparams_text_proto, is_training)) first_stage_box_predictor_kernel_size = 3 first_stage_atrous_rate = 1 first_stage_box_predictor_depth = 512 first_stage_minibatch_size = 3 first_stage_sampler = sampler.BalancedPositiveNegativeSampler( positive_fraction=0.5, is_static=use_static_shapes) first_stage_nms_score_threshold = -1.0 first_stage_nms_iou_threshold = 1.0 first_stage_max_proposals = first_stage_max_proposals first_stage_non_max_suppression_fn = functools.partial( post_processing.batch_multiclass_non_max_suppression, score_thresh=first_stage_nms_score_threshold, iou_thresh=first_stage_nms_iou_threshold, max_size_per_class=first_stage_max_proposals, max_total_size=first_stage_max_proposals, use_static_shapes=use_static_shapes) first_stage_localization_loss_weight = 1.0 first_stage_objectness_loss_weight = 1.0 post_processing_config = post_processing_pb2.PostProcessing() post_processing_text_proto = """ score_converter: IDENTITY batch_non_max_suppression { score_threshold: -20.0 iou_threshold: 1.0 max_detections_per_class: 5 max_total_detections: 5 use_static_shapes: """ + '{}'.format(use_static_shapes) + """ } """ if calibration_mapping_value: calibration_text_proto = """ calibration_config { function_approximation { x_y_pairs { x_y_pair { x: 0.0 y: %f } x_y_pair { x: 1.0 y: %f }}}}""" % (calibration_mapping_value, calibration_mapping_value) post_processing_text_proto = (post_processing_text_proto + ' ' + calibration_text_proto) text_format.Merge(post_processing_text_proto, post_processing_config) second_stage_non_max_suppression_fn, second_stage_score_conversion_fn = ( post_processing_builder.build(post_processing_config)) second_stage_target_assigner = target_assigner.create_target_assigner( 'FasterRCNN', 'detection', use_matmul_gather=use_matmul_gather_in_matcher) second_stage_sampler = sampler.BalancedPositiveNegativeSampler( positive_fraction=1.0, is_static=use_static_shapes) second_stage_localization_loss_weight = 1.0 second_stage_classification_loss_weight = 1.0 if softmax_second_stage_classification_loss: second_stage_classification_loss = ( losses.WeightedSoftmaxClassificationLoss()) else: second_stage_classification_loss = ( losses.WeightedSigmoidClassificationLoss()) hard_example_miner = None if hard_mining: hard_example_miner = losses.HardExampleMiner( num_hard_examples=1, iou_threshold=0.99, loss_type='both', cls_loss_weight=second_stage_classification_loss_weight, loc_loss_weight=second_stage_localization_loss_weight, max_negatives_per_positive=None) crop_and_resize_fn = (ops.matmul_crop_and_resize if use_matmul_crop_and_resize else ops.native_crop_and_resize) common_kwargs = { 'is_training': is_training, 'num_classes': num_classes, 'image_resizer_fn': image_resizer_fn, 'feature_extractor': fake_feature_extractor, 'number_of_stages': number_of_stages, 'first_stage_anchor_generator': first_stage_anchor_generator, 'first_stage_target_assigner': first_stage_target_assigner, 'first_stage_atrous_rate': first_stage_atrous_rate, 'first_stage_box_predictor_arg_scope_fn': first_stage_box_predictor_arg_scope_fn, 'first_stage_box_predictor_kernel_size': first_stage_box_predictor_kernel_size, 'first_stage_box_predictor_depth': first_stage_box_predictor_depth, 'first_stage_minibatch_size': first_stage_minibatch_size, 'first_stage_sampler': first_stage_sampler, 'first_stage_non_max_suppression_fn': first_stage_non_max_suppression_fn, 'first_stage_max_proposals': first_stage_max_proposals, 'first_stage_localization_loss_weight': first_stage_localization_loss_weight, 'first_stage_objectness_loss_weight': first_stage_objectness_loss_weight, 'second_stage_target_assigner': second_stage_target_assigner, 'second_stage_batch_size': second_stage_batch_size, 'second_stage_sampler': second_stage_sampler, 'second_stage_non_max_suppression_fn': second_stage_non_max_suppression_fn, 'second_stage_score_conversion_fn': second_stage_score_conversion_fn, 'second_stage_localization_loss_weight': second_stage_localization_loss_weight, 'second_stage_classification_loss_weight': second_stage_classification_loss_weight, 'second_stage_classification_loss': second_stage_classification_loss, 'hard_example_miner': hard_example_miner, 'crop_and_resize_fn': crop_and_resize_fn, 'clip_anchors_to_image': clip_anchors_to_image, 'use_static_shapes': use_static_shapes, 'resize_masks': True, 'return_raw_detections_during_predict': return_raw_detections_during_predict } return self._get_model( self._get_second_stage_box_predictor( num_classes=num_classes, is_training=is_training, use_keras=use_keras, predict_masks=predict_masks, masks_are_class_agnostic=masks_are_class_agnostic, share_box_across_classes=share_box_across_classes), **common_kwargs)
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.')
def _generate(self, feature_map_shape_list, im_height=1, im_width=1): """Generates a collection of bounding boxes to be used as anchors. For training, we require the input image shape to be statically defined. That is, im_height and im_width should be integers rather than tensors. For inference, im_height and im_width can be either integers (for fixed image size), or tensors (for arbitrary image size). 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. If both im_height and im_width are 1, anchors can only be generated in absolute coordinates. im_width: the width of the image to generate the grid for. If both im_height and im_width are 1, anchors can only be generated in absolute coordinates. 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. ValueError: if im_height and im_width are 1, but normalized coordinates were requested. """ 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 isinstance(im_height, int) and isinstance(im_width, int): if im_height % 2.0**level == 0 or im_height == 1: anchor_offset[0] = stride / 2.0 if im_width % 2.0**level == 0 or im_width == 1: anchor_offset[1] = stride / 2.0 if tf.is_tensor(im_height) and tf.is_tensor(im_width): anchor_offset[0] = stride / 2.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: if im_height == 1 or im_width == 1: raise ValueError( 'Normalized coordinates were requested upon construction of the ' 'MultiscaleGridAnchorGenerator, but a subsequent call to ' 'generate did not supply dimension information.') 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
import rfcn_meta_arch import faster_rcnn_inception_resnet_v2_feature_extractor from object_detection.anchor_generators import grid_anchor_generator from object_detection.core import box_predictor from object_detection.core import post_processing from object_detection.core import preprocessor from object_detection.core import losses from object_detection.utils import context_manager import numpy as np slim = tf.contrib.slim anchor_generator = grid_anchor_generator.GridAnchorGenerator(scales=(0.5, 1.0, 2.0), aspect_ratios=(0.5, 1.0, 2.0), base_anchor_size=[3, 3], anchor_stride=[3, 3]) rfcn_box_predictor = box_predictor.RfcnBoxPredictor(is_training=True, num_classes=12, conv_hyperparams_fn=None, num_spatial_bins=[3, 3], depth=3, crop_size=9, box_code_size=9) def logits_to_probabilities(logits): ar = np.array(logits) scores = tf.nn.softmax(ar) return scores