def test_raise_value_error_on_empty_anchor_genertor(self): anchor_generator_text_proto = """ """ anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) with self.assertRaises(ValueError): anchor_generator_builder.build(anchor_generator_proto)
def test_build_ssd_anchor_generator_with_defaults(self): anchor_generator_text_proto = """ ssd_anchor_generator { aspect_ratios: [1.0] } """ anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) anchor_generator_object = anchor_generator_builder.build( anchor_generator_proto) self.assertTrue( isinstance( anchor_generator_object, multiple_grid_anchor_generator.MultipleGridAnchorGenerator)) for actual_scales, expected_scales in zip( list(anchor_generator_object._scales), [(0.1, 0.2, 0.2), (0.35, 0.418), (0.499, 0.570), (0.649, 0.721), (0.799, 0.871), (0.949, 0.974)]): self.assert_almost_list_equal(expected_scales, actual_scales, delta=1e-2) for actual_aspect_ratio, expected_aspect_ratio in zip( list(anchor_generator_object._aspect_ratios), [(1.0, 2.0, 0.5)] + 5 * [(1.0, 1.0)]): self.assert_almost_list_equal(expected_aspect_ratio, actual_aspect_ratio) with self.test_session() as sess: base_anchor_size = sess.run( anchor_generator_object._base_anchor_size) self.assertAllClose(base_anchor_size, [1.0, 1.0])
def test_build_grid_anchor_generator_with_non_default_parameters(self): anchor_generator_text_proto = """ grid_anchor_generator { height: 128 width: 512 height_stride: 10 width_stride: 20 height_offset: 30 width_offset: 40 scales: [0.4, 2.2] aspect_ratios: [0.3, 4.5] } """ anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) anchor_generator_object = anchor_generator_builder.build( anchor_generator_proto) self.assertTrue( isinstance(anchor_generator_object, grid_anchor_generator.GridAnchorGenerator)) self.assert_almost_list_equal(anchor_generator_object._scales, [0.4, 2.2]) self.assert_almost_list_equal(anchor_generator_object._aspect_ratios, [0.3, 4.5]) with self.test_session() as sess: base_anchor_size, anchor_offset, anchor_stride = sess.run([ anchor_generator_object._base_anchor_size, anchor_generator_object._anchor_offset, anchor_generator_object._anchor_stride ]) self.assertAllEqual(anchor_offset, [30, 40]) self.assertAllEqual(anchor_stride, [10, 20]) self.assertAllEqual(base_anchor_size, [128, 512])
def test_build_multiscale_anchor_generator_custom_aspect_ratios(self): anchor_generator_text_proto = """ multiscale_anchor_generator { aspect_ratios: [1.0] } """ anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) anchor_generator_object = anchor_generator_builder.build( anchor_generator_proto) self.assertTrue( isinstance( anchor_generator_object, multiscale_grid_anchor_generator. MultiscaleGridAnchorGenerator)) for level, anchor_grid_info in zip( range(3, 8), anchor_generator_object._anchor_grid_info): self.assertEqual(set(anchor_grid_info.keys()), set(['level', 'info'])) self.assertTrue(level, anchor_grid_info['level']) self.assertEqual(len(anchor_grid_info['info']), 4) self.assertAllClose(anchor_grid_info['info'][0], [2**0, 2**0.5]) self.assertTrue(anchor_grid_info['info'][1], 1.0) self.assertAllClose(anchor_grid_info['info'][2], [4.0 * 2**level, 4.0 * 2**level]) self.assertAllClose(anchor_grid_info['info'][3], [2**level, 2**level]) self.assertTrue(anchor_generator_object._normalize_coordinates)
def test_build_ssd_anchor_generator_with_non_default_parameters(self): anchor_generator_text_proto = """ ssd_anchor_generator { num_layers: 2 min_scale: 0.3 max_scale: 0.8 aspect_ratios: [2.0] height_stride: 16 height_stride: 32 width_stride: 20 width_stride: 30 height_offset: 8 height_offset: 16 width_offset: 0 width_offset: 10 } """ anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) anchor_generator_object = anchor_generator_builder.build( anchor_generator_proto) self.assertTrue( isinstance( anchor_generator_object, multiple_grid_anchor_generator.MultipleGridAnchorGenerator)) for actual_scales, expected_scales in zip( list(anchor_generator_object._scales), [(0.1, 0.3, 0.3), (0.8, 0.894)]): self.assert_almost_list_equal(expected_scales, actual_scales, delta=1e-2) for actual_aspect_ratio, expected_aspect_ratio in zip( list(anchor_generator_object._aspect_ratios), [(1.0, 2.0, 0.5), (2.0, 1.0)]): self.assert_almost_list_equal(expected_aspect_ratio, actual_aspect_ratio) for actual_strides, expected_strides in zip( list(anchor_generator_object._anchor_strides), [(16, 20), (32, 30)]): self.assert_almost_list_equal(expected_strides, actual_strides) for actual_offsets, expected_offsets in zip( list(anchor_generator_object._anchor_offsets), [(8, 0), (16, 10)]): self.assert_almost_list_equal(expected_offsets, actual_offsets) with self.test_session() as sess: base_anchor_size = sess.run( anchor_generator_object._base_anchor_size) self.assertAllClose(base_anchor_size, [1.0, 1.0])
def test_build_multiscale_anchor_generator_with_anchors_in_pixel_coordinates( self): anchor_generator_text_proto = """ multiscale_anchor_generator { aspect_ratios: [1.0] normalize_coordinates: false } """ anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) anchor_generator_object = anchor_generator_builder.build( anchor_generator_proto) self.assertTrue( isinstance( anchor_generator_object, multiscale_grid_anchor_generator. MultiscaleGridAnchorGenerator)) self.assertFalse(anchor_generator_object._normalize_coordinates)
def test_build_ssd_anchor_generator_with_custom_interpolated_scale(self): anchor_generator_text_proto = """ ssd_anchor_generator { aspect_ratios: [0.5] interpolated_scale_aspect_ratio: 0.5 reduce_boxes_in_lowest_layer: false } """ anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) anchor_generator_object = anchor_generator_builder.build( anchor_generator_proto) self.assertTrue( isinstance( anchor_generator_object, multiple_grid_anchor_generator.MultipleGridAnchorGenerator)) for actual_aspect_ratio, expected_aspect_ratio in zip( list(anchor_generator_object._aspect_ratios), 6 * [(0.5, 0.5)]): self.assert_almost_list_equal(expected_aspect_ratio, actual_aspect_ratio)
def test_build_grid_anchor_generator_with_defaults(self): anchor_generator_text_proto = """ grid_anchor_generator { } """ anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) anchor_generator_object = anchor_generator_builder.build( anchor_generator_proto) self.assertTrue( isinstance(anchor_generator_object, grid_anchor_generator.GridAnchorGenerator)) self.assertListEqual(anchor_generator_object._scales, []) self.assertListEqual(anchor_generator_object._aspect_ratios, []) with self.test_session() as sess: base_anchor_size, anchor_offset, anchor_stride = sess.run([ anchor_generator_object._base_anchor_size, anchor_generator_object._anchor_offset, anchor_generator_object._anchor_stride ]) self.assertAllEqual(anchor_offset, [0, 0]) self.assertAllEqual(anchor_stride, [16, 16]) self.assertAllEqual(base_anchor_size, [256, 256])
def test_build_ssd_anchor_generator_with_custom_scales(self): anchor_generator_text_proto = """ ssd_anchor_generator { aspect_ratios: [1.0] scales: [0.1, 0.15, 0.2, 0.4, 0.6, 0.8] reduce_boxes_in_lowest_layer: false } """ anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) anchor_generator_object = anchor_generator_builder.build( anchor_generator_proto) self.assertTrue( isinstance( anchor_generator_object, multiple_grid_anchor_generator.MultipleGridAnchorGenerator)) for actual_scales, expected_scales in zip( list(anchor_generator_object._scales), [(0.1, math.sqrt(0.1 * 0.15)), (0.15, math.sqrt(0.15 * 0.2)), (0.2, math.sqrt(0.2 * 0.4)), (0.4, math.sqrt(0.4 * 0.6)), (0.6, math.sqrt(0.6 * 0.8)), (0.8, math.sqrt(0.8 * 1.0))]): self.assert_almost_list_equal(expected_scales, actual_scales, delta=1e-2)
def _build_faster_rcnn_model(frcnn_config, is_training, add_summaries): """Builds a Faster R-CNN or R-FCN detection model based on the model config. Builds R-FCN model if the second_stage_box_predictor in the config is of type `rfcn_box_predictor` else builds a Faster R-CNN model. Args: frcnn_config: A faster_rcnn.proto object containing the config for the desired FasterRCNNMetaArch or RFCNMetaArch. is_training: True if this model is being built for training purposes. add_summaries: Whether to add tf summaries in the model. Returns: FasterRCNNMetaArch based on the config. Raises: ValueError: If frcnn_config.type is not recognized (i.e. not registered in model_class_map). """ num_classes = frcnn_config.num_classes image_resizer_fn = image_resizer_builder.build(frcnn_config.image_resizer) feature_extractor = _build_faster_rcnn_feature_extractor( frcnn_config.feature_extractor, is_training, frcnn_config.inplace_batchnorm_update) number_of_stages = frcnn_config.number_of_stages first_stage_anchor_generator = anchor_generator_builder.build( frcnn_config.first_stage_anchor_generator) first_stage_target_assigner = target_assigner.create_target_assigner( 'FasterRCNN', 'proposal', use_matmul_gather=frcnn_config.use_matmul_gather_in_matcher) first_stage_atrous_rate = frcnn_config.first_stage_atrous_rate first_stage_box_predictor_arg_scope_fn = hyperparams_builder.build( frcnn_config.first_stage_box_predictor_conv_hyperparams, is_training) first_stage_box_predictor_kernel_size = ( frcnn_config.first_stage_box_predictor_kernel_size) first_stage_box_predictor_depth = frcnn_config.first_stage_box_predictor_depth first_stage_minibatch_size = frcnn_config.first_stage_minibatch_size first_stage_sampler = sampler.BalancedPositiveNegativeSampler( positive_fraction=frcnn_config.first_stage_positive_balance_fraction, is_static=frcnn_config.use_static_balanced_label_sampler) first_stage_nms_score_threshold = frcnn_config.first_stage_nms_score_threshold first_stage_nms_iou_threshold = frcnn_config.first_stage_nms_iou_threshold first_stage_max_proposals = frcnn_config.first_stage_max_proposals first_stage_loc_loss_weight = ( frcnn_config.first_stage_localization_loss_weight) first_stage_obj_loss_weight = frcnn_config.first_stage_objectness_loss_weight initial_crop_size = frcnn_config.initial_crop_size maxpool_kernel_size = frcnn_config.maxpool_kernel_size maxpool_stride = frcnn_config.maxpool_stride second_stage_target_assigner = target_assigner.create_target_assigner( 'FasterRCNN', 'detection', use_matmul_gather=frcnn_config.use_matmul_gather_in_matcher) second_stage_box_predictor = box_predictor_builder.build( hyperparams_builder.build, frcnn_config.second_stage_box_predictor, is_training=is_training, num_classes=num_classes) second_stage_batch_size = frcnn_config.second_stage_batch_size second_stage_sampler = sampler.BalancedPositiveNegativeSampler( positive_fraction=frcnn_config.second_stage_balance_fraction, is_static=frcnn_config.use_static_balanced_label_sampler) (second_stage_non_max_suppression_fn, second_stage_score_conversion_fn) = post_processing_builder.build( frcnn_config.second_stage_post_processing) second_stage_localization_loss_weight = ( frcnn_config.second_stage_localization_loss_weight) second_stage_classification_loss = ( losses_builder.build_faster_rcnn_classification_loss( frcnn_config.second_stage_classification_loss)) second_stage_classification_loss_weight = ( frcnn_config.second_stage_classification_loss_weight) second_stage_mask_prediction_loss_weight = ( frcnn_config.second_stage_mask_prediction_loss_weight) hard_example_miner = None if frcnn_config.HasField('hard_example_miner'): hard_example_miner = losses_builder.build_hard_example_miner( frcnn_config.hard_example_miner, second_stage_classification_loss_weight, second_stage_localization_loss_weight) use_matmul_crop_and_resize = (frcnn_config.use_matmul_crop_and_resize) clip_anchors_to_image = (frcnn_config.clip_anchors_to_image) common_kwargs = { 'is_training': is_training, 'num_classes': num_classes, 'image_resizer_fn': image_resizer_fn, 'feature_extractor': 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_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_loc_loss_weight, 'first_stage_objectness_loss_weight': first_stage_obj_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': second_stage_classification_loss, 'second_stage_classification_loss_weight': second_stage_classification_loss_weight, 'hard_example_miner': hard_example_miner, 'add_summaries': add_summaries, 'use_matmul_crop_and_resize': use_matmul_crop_and_resize, 'clip_anchors_to_image': clip_anchors_to_image } if isinstance(second_stage_box_predictor, rfcn_box_predictor.RfcnBoxPredictor): return rfcn_meta_arch.RFCNMetaArch( second_stage_rfcn_box_predictor=second_stage_box_predictor, **common_kwargs) else: return faster_rcnn_meta_arch.FasterRCNNMetaArch( initial_crop_size=initial_crop_size, maxpool_kernel_size=maxpool_kernel_size, maxpool_stride=maxpool_stride, second_stage_mask_rcnn_box_predictor=second_stage_box_predictor, second_stage_mask_prediction_loss_weight=( second_stage_mask_prediction_loss_weight), **common_kwargs)
def _build_ssd_model(ssd_config, is_training, add_summaries, add_background_class=True): """Builds an SSD detection model based on the model config. Args: ssd_config: A ssd.proto object containing the config for the desired SSDMetaArch. is_training: True if this model is being built for training purposes. add_summaries: Whether to add tf summaries in the model. add_background_class: Whether to add an implicit background class to one-hot encodings of groundtruth labels. Set to false if using groundtruth labels with an explicit background class or using multiclass scores instead of truth in the case of distillation. Returns: SSDMetaArch based on the config. Raises: ValueError: If ssd_config.type is not recognized (i.e. not registered in model_class_map). """ num_classes = ssd_config.num_classes # Feature extractor feature_extractor = _build_ssd_feature_extractor( feature_extractor_config=ssd_config.feature_extractor, is_training=is_training) box_coder = box_coder_builder.build(ssd_config.box_coder) matcher = matcher_builder.build(ssd_config.matcher) region_similarity_calculator = sim_calc.build( ssd_config.similarity_calculator) encode_background_as_zeros = ssd_config.encode_background_as_zeros negative_class_weight = ssd_config.negative_class_weight ssd_box_predictor = box_predictor_builder.build(hyperparams_builder.build, ssd_config.box_predictor, is_training, num_classes) anchor_generator = anchor_generator_builder.build( ssd_config.anchor_generator) image_resizer_fn = image_resizer_builder.build(ssd_config.image_resizer) non_max_suppression_fn, score_conversion_fn = post_processing_builder.build( ssd_config.post_processing) (classification_loss, localization_loss, classification_weight, localization_weight, hard_example_miner, random_example_sampler) = losses_builder.build(ssd_config.loss) normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches normalize_loc_loss_by_codesize = ssd_config.normalize_loc_loss_by_codesize return ssd_meta_arch.SSDMetaArch( is_training, anchor_generator, ssd_box_predictor, box_coder, feature_extractor, matcher, region_similarity_calculator, encode_background_as_zeros, negative_class_weight, image_resizer_fn, non_max_suppression_fn, score_conversion_fn, classification_loss, localization_loss, classification_weight, localization_weight, normalize_loss_by_num_matches, hard_example_miner, add_summaries=add_summaries, normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize, freeze_batchnorm=ssd_config.freeze_batchnorm, inplace_batchnorm_update=ssd_config.inplace_batchnorm_update, add_background_class=add_background_class, random_example_sampler=random_example_sampler)