示例#1
0
def _build_ssd_model(ssd_config, is_training):
    """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.

    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(ssd_config.feature_extractor,
                                                     is_training)

    box_coder = box_coder_builder.build(ssd_config.box_coder)
    # matcher contains a method named "match" to return a "Match" Object.
    matcher = matcher_builder.build(ssd_config.matcher)
    # region_similarity_calculator.compare: return a tensor with shape [N, M] representing the IOA/IOU score, etc.
    region_similarity_calculator = sim_calc.build(
        ssd_config.similarity_calculator)
    # ssd_box_predictor.predict: returns a prediction dictionary
    ssd_box_predictor = box_predictor_builder.build(hyperparams_builder.build,
                                                    ssd_config.box_predictor,
                                                    is_training, num_classes)

    # anchor_generator: is MultipleGridAnchorGenerator object are always in normalized coordinate
    # Usage: anchor_generator.generate: Generates a collection of bounding boxes to be used as anchors.
    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) = losses_builder.build(ssd_config.loss)
    normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches

    return ssd_meta_arch.SSDMetaArch(
        is_training,
        anchor_generator,
        ssd_box_predictor,
        box_coder,
        feature_extractor,
        matcher,
        region_similarity_calculator,
        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)
示例#2
0
    def _create_model(self,
                      apply_hard_mining=True,
                      normalize_loc_loss_by_codesize=False):
        is_training = False
        num_classes = 1
        mock_anchor_generator = MockAnchorGenerator2x2()
        mock_box_predictor = test_utils.MockBoxPredictor(
            is_training, num_classes)
        mock_box_coder = test_utils.MockBoxCoder()
        fake_feature_extractor = FakeSSDFeatureExtractor()
        mock_matcher = test_utils.MockMatcher()
        region_similarity_calculator = sim_calc.IouSimilarity()
        encode_background_as_zeros = False

        def image_resizer_fn(image):
            return [tf.identity(image), tf.shape(image)]

        classification_loss = losses.WeightedSigmoidClassificationLoss()
        localization_loss = losses.WeightedSmoothL1LocalizationLoss()
        non_max_suppression_fn = functools.partial(
            post_processing.batch_multiclass_non_max_suppression,
            score_thresh=-20.0,
            iou_thresh=1.0,
            max_size_per_class=5,
            max_total_size=5)
        classification_loss_weight = 1.0
        localization_loss_weight = 1.0
        negative_class_weight = 1.0
        normalize_loss_by_num_matches = False

        hard_example_miner = None
        if apply_hard_mining:
            # This hard example miner is expected to be a no-op.
            hard_example_miner = losses.HardExampleMiner(
                num_hard_examples=None, iou_threshold=1.0)

        code_size = 4
        model = ssd_meta_arch.SSDMetaArch(
            is_training,
            mock_anchor_generator,
            mock_box_predictor,
            mock_box_coder,
            fake_feature_extractor,
            mock_matcher,
            region_similarity_calculator,
            encode_background_as_zeros,
            negative_class_weight,
            image_resizer_fn,
            non_max_suppression_fn,
            tf.identity,
            classification_loss,
            localization_loss,
            classification_loss_weight,
            localization_loss_weight,
            normalize_loss_by_num_matches,
            hard_example_miner,
            add_summaries=False,
            normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize)
        return model, num_classes, mock_anchor_generator.num_anchors(
        ), code_size
示例#3
0
    def setUp(self):
        """Set up mock SSD model.

    Here we set up a simple mock SSD model that will always predict 4
    detections that happen to always be exactly the anchors that are set up
    in the above MockAnchorGenerator.  Because we let max_detections=5,
    we will also always end up with an extra padded row in the detection
    results.
    """
        is_training = False
        self._num_classes = 1
        conv_hyperparams = {}
        self._conv_hyperparams = conv_hyperparams
        mock_anchor_generator = MockAnchorGenerator2x2()
        mock_box_predictor = test_utils.MockBoxPredictor(
            is_training, self._num_classes)
        mock_class_predictor = test_utils.MockClassPredictor(
            is_training, 7, self._conv_hyperparams, True, 0.5, 5, 0.0, False)
        mock_box_coder = test_utils.MockBoxCoder()
        fake_feature_extractor = FakeSSDFeatureExtractor()
        mock_matcher = test_utils.MockMatcher()
        region_similarity_calculator = sim_calc.IouSimilarity()

        def image_resizer_fn(image):
            return tf.identity(image)

        classification_loss = losses.WeightedSigmoidClassificationLoss(
            anchorwise_output=True)
        localization_loss = losses.WeightedSmoothL1LocalizationLoss(
            anchorwise_output=True)

        classification_loss_in_image_level = losses.WeightedSigmoidClassificationLossInImageLevel(
        )

        non_max_suppression_fn = functools.partial(
            post_processing.batch_multiclass_non_max_suppression,
            score_thresh=-20.0,
            iou_thresh=1.0,
            max_size_per_class=5,
            max_total_size=5)
        classification_loss_weight = 1.0
        localization_loss_weight = 1.0
        classification_loss_in_image_level_weight = 1.0
        normalize_loss_by_num_matches = False

        # This hard example miner is expected to be a no-op.
        hard_example_miner = losses.HardExampleMiner(num_hard_examples=None,
                                                     iou_threshold=1.0)

        self._num_anchors = 4
        self._code_size = 4
        self._model = ssd_meta_arch.SSDMetaArch(
            is_training, mock_anchor_generator, mock_box_predictor,
            mock_class_predictor, mock_box_coder, fake_feature_extractor,
            mock_matcher, region_similarity_calculator, image_resizer_fn,
            non_max_suppression_fn, tf.identity, classification_loss,
            localization_loss, classification_loss_in_image_level,
            classification_loss_weight, localization_loss_weight,
            classification_loss_in_image_level_weight,
            normalize_loss_by_num_matches, hard_example_miner)
示例#4
0
def _build_ssd_model(ssd_config, is_training, add_summaries):
  """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.

  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(ssd_config.feature_extractor,
                                                   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
  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) = losses_builder.build(ssd_config.loss)
  normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches

  return ssd_meta_arch.SSDMetaArch(
      is_training,
      anchor_generator,
      ssd_box_predictor,
      box_coder,
      feature_extractor,
      matcher,
      region_similarity_calculator,
      encode_background_as_zeros,
      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)
def _build_sssfd_model(sssfd_config,
                       is_training,
                       add_summaries,
                       add_background_class=True):
    num_classes = sssfd_config.num_classes

    # Feature extractor
    feature_extractor = _build_sssfd_feature_extractor(
        feature_extractor_config=sssfd_config.feature_extractor,
        is_training=is_training)

    box_coder = box_coder_builder.build(sssfd_config.box_coder)
    matcher = matcher_builder.build(sssfd_config.matcher)
    region_similarity_calculator = sim_calc.build(
        sssfd_config.similarity_calculator)
    encode_background_as_zeros = sssfd_config.encode_background_as_zeros
    negative_class_weight = sssfd_config.negative_class_weight
    sssfd_box_predictor = box_predictor_builder.build(
        hyperparams_builder.build, sssfd_config.box_predictor, is_training,
        num_classes)
    anchor_generator = anchor_generator_builder.build(
        sssfd_config.anchor_generator)
    image_resizer_fn = image_resizer_builder.build(sssfd_config.image_resizer)
    non_max_suppression_fn, score_conversion_fn = post_processing_builder.build(
        sssfd_config.post_processing)
    (classification_loss, localization_loss, classification_weight,
     localization_weight, hard_example_miner,
     random_example_sampler) = losses_builder.build(sssfd_config.loss)
    normalize_loss_by_num_matches = sssfd_config.normalize_loss_by_num_matches
    normalize_loc_loss_by_codesize = sssfd_config.normalize_loc_loss_by_codesize

    return ssd_meta_arch.SSDMetaArch(
        is_training,
        anchor_generator,
        sssfd_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=sssfd_config.freeze_batchnorm,
        inplace_batchnorm_update=sssfd_config.inplace_batchnorm_update,
        add_background_class=add_background_class,
        random_example_sampler=random_example_sampler)
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)
def _build_ssd_model(ssd_config, is_training):
    """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.

  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(
        ssd_config.feature_extractor, 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)
    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) = losses_builder.build(ssd_config.loss)
    normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches

    common_kwargs = {
        'is_training': is_training,
        'anchor_generator': anchor_generator,
        'box_predictor': ssd_box_predictor,
        'box_coder': box_coder,
        'feature_extractor': feature_extractor,
        'matcher': matcher,
        'region_similarity_calculator': region_similarity_calculator,
        'image_resizer_fn': image_resizer_fn,
        'non_max_suppression_fn': non_max_suppression_fn,
        'score_conversion_fn': score_conversion_fn,
        'classification_loss': classification_loss,
        'localization_loss': localization_loss,
        'classification_loss_weight': classification_weight,
        'localization_loss_weight': localization_weight,
        'normalize_loss_by_num_matches': normalize_loss_by_num_matches,
        'hard_example_miner': hard_example_miner
    }

    if isinstance(anchor_generator,
                  yolo_grid_anchor_generator.YoloGridAnchorGenerator):
        return yolo_meta_arch.YOLOMetaArch(**common_kwargs)
    else:
        return ssd_meta_arch.SSDMetaArch(**common_kwargs)
示例#8
0
    def _create_model(self,
                      apply_hard_mining=True,
                      normalize_loc_loss_by_codesize=False,
                      add_background_class=True,
                      random_example_sampling=False,
                      weight_regression_loss_by_score=False,
                      use_expected_classification_loss_under_sampling=False,
                      minimum_negative_sampling=1,
                      desired_negative_sampling_ratio=3,
                      use_keras=False):
        is_training = False
        num_classes = 1
        mock_anchor_generator = MockAnchorGenerator2x2()
        if use_keras:
            mock_box_predictor = test_utils.MockKerasBoxPredictor(
                is_training, num_classes)
        else:
            mock_box_predictor = test_utils.MockBoxPredictor(
                is_training, num_classes)
        mock_box_coder = test_utils.MockBoxCoder()
        if use_keras:
            fake_feature_extractor = FakeSSDKerasFeatureExtractor()
        else:
            fake_feature_extractor = FakeSSDFeatureExtractor()
        mock_matcher = test_utils.MockMatcher()
        region_similarity_calculator = sim_calc.IouSimilarity()
        encode_background_as_zeros = False

        def image_resizer_fn(image):
            return [tf.identity(image), tf.shape(image)]

        classification_loss = losses.WeightedSigmoidClassificationLoss()
        localization_loss = losses.WeightedSmoothL1LocalizationLoss()
        non_max_suppression_fn = functools.partial(
            post_processing.batch_multiclass_non_max_suppression,
            score_thresh=-20.0,
            iou_thresh=1.0,
            max_size_per_class=5,
            max_total_size=5)
        classification_loss_weight = 1.0
        localization_loss_weight = 1.0
        negative_class_weight = 1.0
        normalize_loss_by_num_matches = False

        hard_example_miner = None
        if apply_hard_mining:
            # This hard example miner is expected to be a no-op.
            hard_example_miner = losses.HardExampleMiner(
                num_hard_examples=None, iou_threshold=1.0)

        random_example_sampler = None
        if random_example_sampling:
            random_example_sampler = sampler.BalancedPositiveNegativeSampler(
                positive_fraction=0.5)

        target_assigner_instance = target_assigner.TargetAssigner(
            region_similarity_calculator,
            mock_matcher,
            mock_box_coder,
            negative_class_weight=negative_class_weight,
            weight_regression_loss_by_score=weight_regression_loss_by_score)

        expected_classification_loss_under_sampling = None
        if use_expected_classification_loss_under_sampling:
            expected_classification_loss_under_sampling = functools.partial(
                ops.expected_classification_loss_under_sampling,
                minimum_negative_sampling=minimum_negative_sampling,
                desired_negative_sampling_ratio=desired_negative_sampling_ratio
            )

        code_size = 4
        model = ssd_meta_arch.SSDMetaArch(
            is_training,
            mock_anchor_generator,
            mock_box_predictor,
            mock_box_coder,
            fake_feature_extractor,
            mock_matcher,
            region_similarity_calculator,
            encode_background_as_zeros,
            negative_class_weight,
            image_resizer_fn,
            non_max_suppression_fn,
            tf.identity,
            classification_loss,
            localization_loss,
            classification_loss_weight,
            localization_loss_weight,
            normalize_loss_by_num_matches,
            hard_example_miner,
            target_assigner_instance=target_assigner_instance,
            add_summaries=False,
            normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize,
            freeze_batchnorm=False,
            inplace_batchnorm_update=False,
            add_background_class=add_background_class,
            random_example_sampler=random_example_sampler,
            expected_classification_loss_under_sampling=
            expected_classification_loss_under_sampling)
        return model, num_classes, mock_anchor_generator.num_anchors(
        ), code_size
示例#9
0
def _build_ssd_model(ssd_config, is_training):
  """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.

  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  #number of clases 

  # Feature extractor
  feature_extractor = _build_ssd_feature_extractor(ssd_config.feature_extractor,      #we use ssd_mobilenet_v1 as the feature extractor 
                                                   is_training)    #set the class in ssd_mobilenr_v1_feature_extractor amd ssd_meta+arch.py 

#when taking the regression loss we are working with some transorfmation. That means our predictors will predict 4 cordinates and those codinates should be regressed with some kind embedding which was made with ground truth boxes and default boxes , then after getting those we docode them for real images 


  box_coder = box_coder_builder.build(ssd_config.box_coder) #set en encoding w.r.t ground truth boxes and achor boxes . The output creating with this object will then regressed with the predicted onece. chenck equation 2 in the ssd paper 
  matcher = matcher_builder.build(ssd_config.matcher) #matching the predicted to ground trunth- Builds a matcher object based on the matcher config
#in obove object matching is done with default boxes and ground truth boxes , that's how xij value in the paper obtained . 

  region_similarity_calculator = sim_calc.build(         #how to calculate the similarity parameter is iou .
      ssd_config.similarity_calculator)

  ssd_box_predictor = box_predictor_builder.build(hyperparams_builder.build,    #This will take care of the convolutional kernal 
                                                  ssd_config.box_predictor,    
                                                  is_training, num_classes)  #this returns a box_predictor object 


  anchor_generator = anchor_generator_builder.build(         #pass an instance or object where we can create ancho boxes for differen featuremaps
      ssd_config.anchor_generator)

  image_resizer_fn = image_resizer_builder.build(ssd_config.image_resizer)    #this is imortatnt  we use   fixed_shape_resizer

  non_max_suppression_fn, score_conversion_fn = post_processing_builder.build(   #this is to work with NMS supression  output
      ssd_config.post_processing)     #score conversion function will convert logits to probabilities 

  (classification_loss, localization_loss, classification_weight,
   localization_weight,
   hard_example_miner) = losses_builder.build(ssd_config.loss)           #now the loss for hard examples  these outputs are objects 

  normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches # we devide by the matching acnhorboxes 

  return ssd_meta_arch.SSDMetaArch(        #here we initialized a object of ssd_meta_arch which will be used in trainign 
      is_training,
      anchor_generator,
      ssd_box_predictor,
      box_coder,
      feature_extractor,
      matcher,
      region_similarity_calculator,
      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)