def test_build_softmax_score_converter(self):
     post_processing_text_proto = """
       score_converter: SOFTMAX
     """
     post_processing_config = post_processing_pb2.PostProcessing()
     text_format.Merge(post_processing_text_proto, post_processing_config)
     _, score_converter = post_processing_builder.build(
         post_processing_config)
     self.assertEqual(score_converter, tf.nn.softmax)
 def test_build_softmax_score_converter(self):
     post_processing_text_proto = """
   score_converter: SOFTMAX
 """
     post_processing_config = post_processing_pb2.PostProcessing()
     text_format.Merge(post_processing_text_proto, post_processing_config)
     _, score_converter = post_processing_builder.build(
         post_processing_config)
     self.assertEqual(score_converter.__name__, 'softmax_with_logit_scale')
 def test_build_sigmoid_score_converter(self):
     post_processing_text_proto = """
       score_converter: SIGMOID
     """
     post_processing_config = post_processing_pb2.PostProcessing()
     text_format.Merge(post_processing_text_proto, post_processing_config)
     _, score_converter = post_processing_builder.build(
         post_processing_config)
     self.assertEqual(score_converter, tf.sigmoid)
 def test_build_identity_score_converter(self):
     post_processing_text_proto = """
       score_converter: IDENTITY
     """
     post_processing_config = post_processing_pb2.PostProcessing()
     text_format.Merge(post_processing_text_proto, post_processing_config)
     _, score_converter = post_processing_builder.build(
         post_processing_config)
     self.assertEqual(score_converter, tf.identity)
    def test_build_identity_score_converter(self):
        post_processing_text_proto = """
      score_converter: IDENTITY
    """
        post_processing_config = post_processing_pb2.PostProcessing()
        text_format.Merge(post_processing_text_proto, post_processing_config)
        _, score_converter = post_processing_builder.build(
            post_processing_config)
        self.assertEqual(score_converter.__name__, 'identity_with_logit_scale')

        inputs = tf.constant([1, 1], tf.float32)
        outputs = score_converter(inputs)
        with self.test_session() as sess:
            converted_scores = sess.run(outputs)
            expected_converted_scores = sess.run(inputs)
            self.assertAllClose(converted_scores, expected_converted_scores)
 def test_build_non_max_suppressor_with_correct_parameters(self):
     post_processing_text_proto = """
   batch_non_max_suppression {
     score_threshold: 0.7
     iou_threshold: 0.6
     max_detections_per_class: 100
     max_total_detections: 300
   }
 """
     post_processing_config = post_processing_pb2.PostProcessing()
     text_format.Merge(post_processing_text_proto, post_processing_config)
     non_max_suppressor, _ = post_processing_builder.build(
         post_processing_config)
     self.assertEqual(non_max_suppressor.keywords['max_size_per_class'],
                      100)
     self.assertEqual(non_max_suppressor.keywords['max_total_size'], 300)
     self.assertAlmostEqual(non_max_suppressor.keywords['score_thresh'],
                            0.7)
     self.assertAlmostEqual(non_max_suppressor.keywords['iou_thresh'], 0.6)
    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_angles = (0.0, 0.1)
        first_stage_anchor_strides = (1, 1)
        first_stage_anchor_generator = grid_rbbox_anchor_generator.GridRbboxAnchorGenerator(
            first_stage_anchor_scales,
            first_stage_anchor_aspect_ratios,
            first_stage_anchor_angles,
            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 {
        box_type: RBBOX
        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

        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,
            '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)