def build_mask_rcnn_keras_box_predictor(is_training,
                                        num_classes,
                                        fc_hyperparams,
                                        freeze_batchnorm,
                                        use_dropout,
                                        dropout_keep_prob,
                                        box_code_size,
                                        add_background_class=True,
                                        share_box_across_classes=False,
                                        predict_instance_masks=False,
                                        conv_hyperparams=None,
                                        mask_height=14,
                                        mask_width=14,
                                        mask_prediction_num_conv_layers=2,
                                        mask_prediction_conv_depth=256,
                                        masks_are_class_agnostic=False,
                                        convolve_then_upsample_masks=False):
  
  box_prediction_head = keras_box_head.MaskRCNNBoxHead(
      is_training=is_training,
      num_classes=num_classes,
      fc_hyperparams=fc_hyperparams,
      freeze_batchnorm=freeze_batchnorm,
      use_dropout=use_dropout,
      dropout_keep_prob=dropout_keep_prob,
      box_code_size=box_code_size,
      share_box_across_classes=share_box_across_classes)
  class_prediction_head = keras_class_head.MaskRCNNClassHead(
      is_training=is_training,
      num_class_slots=num_classes + 1 if add_background_class else num_classes,
      fc_hyperparams=fc_hyperparams,
      freeze_batchnorm=freeze_batchnorm,
      use_dropout=use_dropout,
      dropout_keep_prob=dropout_keep_prob)
  third_stage_heads = {}
  if predict_instance_masks:
    third_stage_heads[
        mask_rcnn_box_predictor.
        MASK_PREDICTIONS] = keras_mask_head.MaskRCNNMaskHead(
            is_training=is_training,
            num_classes=num_classes,
            conv_hyperparams=conv_hyperparams,
            freeze_batchnorm=freeze_batchnorm,
            mask_height=mask_height,
            mask_width=mask_width,
            mask_prediction_num_conv_layers=mask_prediction_num_conv_layers,
            mask_prediction_conv_depth=mask_prediction_conv_depth,
            masks_are_class_agnostic=masks_are_class_agnostic,
            convolve_then_upsample=convolve_then_upsample_masks)
  return mask_rcnn_keras_box_predictor.MaskRCNNKerasBoxPredictor(
      is_training=is_training,
      num_classes=num_classes,
      freeze_batchnorm=freeze_batchnorm,
      box_prediction_head=box_prediction_head,
      class_prediction_head=class_prediction_head,
      third_stage_heads=third_stage_heads)
 def test_prediction_size(self):
     class_prediction_head = keras_class_head.MaskRCNNClassHead(
         is_training=False,
         num_class_slots=20,
         fc_hyperparams=self._build_fc_hyperparams(),
         freeze_batchnorm=False,
         use_dropout=True,
         dropout_keep_prob=0.5)
     roi_pooled_features = tf.random_uniform([64, 7, 7, 1024],
                                             minval=-10.0,
                                             maxval=10.0,
                                             dtype=tf.float32)
     prediction = class_prediction_head(roi_pooled_features)
     self.assertAllEqual([64, 1, 20], prediction.get_shape().as_list())
Example #3
0
def build_mask_rcnn_keras_box_predictor(is_training,
                                        num_classes,
                                        fc_hyperparams,
                                        freeze_batchnorm,
                                        use_dropout,
                                        dropout_keep_prob,
                                        box_code_size,
                                        add_background_class=True,
                                        share_box_across_classes=False,
                                        predict_instance_masks=False,
                                        conv_hyperparams=None,
                                        mask_height=14,
                                        mask_width=14,
                                        mask_prediction_num_conv_layers=2,
                                        mask_prediction_conv_depth=256,
                                        masks_are_class_agnostic=False,
                                        convolve_then_upsample_masks=False):
    """Builds and returns a MaskRCNNKerasBoxPredictor class.

  Args:
    is_training: Indicates whether the BoxPredictor is in training mode.
    num_classes: number of classes.  Note that num_classes *does not*
      include the background category, so if groundtruth labels take values
      in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the
      assigned classification targets can range from {0,... K}).
    fc_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object
      containing hyperparameters for fully connected dense ops.
    freeze_batchnorm: Whether to freeze batch norm parameters during
      training or not. When training with a small batch size (e.g. 1), it is
      desirable to freeze batch norm update and use pretrained batch norm
      params.
    use_dropout: Option to use dropout or not.  Note that a single dropout
      op is applied here prior to both box and class predictions, which stands
      in contrast to the ConvolutionalBoxPredictor below.
    dropout_keep_prob: Keep probability for dropout.
      This is only used if use_dropout is True.
    box_code_size: Size of encoding for each box.
    add_background_class: Whether to add an implicit background class.
    share_box_across_classes: Whether to share boxes across classes rather
      than use a different box for each class.
    predict_instance_masks: If True, will add a third stage mask prediction
      to the returned class.
    conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object
      containing hyperparameters for convolution ops.
    mask_height: Desired output mask height. The default value is 14.
    mask_width: Desired output mask width. The default value is 14.
    mask_prediction_num_conv_layers: Number of convolution layers applied to
      the image_features in mask prediction branch.
    mask_prediction_conv_depth: The depth for the first conv2d_transpose op
      applied to the image_features in the mask prediction branch. If set
      to 0, the depth of the convolution layers will be automatically chosen
      based on the number of object classes and the number of channels in the
      image features.
    masks_are_class_agnostic: Boolean determining if the mask-head is
      class-agnostic or not.
    convolve_then_upsample_masks: Whether to apply convolutions on mask
      features before upsampling using nearest neighbor resizing. Otherwise,
      mask features are resized to [`mask_height`, `mask_width`] using
      bilinear resizing before applying convolutions.

  Returns:
    A MaskRCNNKerasBoxPredictor class.
  """
    box_prediction_head = keras_box_head.MaskRCNNBoxHead(
        is_training=is_training,
        num_classes=num_classes,
        fc_hyperparams=fc_hyperparams,
        freeze_batchnorm=freeze_batchnorm,
        use_dropout=use_dropout,
        dropout_keep_prob=dropout_keep_prob,
        box_code_size=box_code_size,
        share_box_across_classes=share_box_across_classes)
    class_prediction_head = keras_class_head.MaskRCNNClassHead(
        is_training=is_training,
        num_class_slots=num_classes +
        1 if add_background_class else num_classes,
        fc_hyperparams=fc_hyperparams,
        freeze_batchnorm=freeze_batchnorm,
        use_dropout=use_dropout,
        dropout_keep_prob=dropout_keep_prob)
    third_stage_heads = {}
    if predict_instance_masks:
        third_stage_heads[
            mask_rcnn_box_predictor.
            MASK_PREDICTIONS] = keras_mask_head.MaskRCNNMaskHead(
                is_training=is_training,
                num_classes=num_classes,
                conv_hyperparams=conv_hyperparams,
                freeze_batchnorm=freeze_batchnorm,
                mask_height=mask_height,
                mask_width=mask_width,
                mask_prediction_num_conv_layers=mask_prediction_num_conv_layers,
                mask_prediction_conv_depth=mask_prediction_conv_depth,
                masks_are_class_agnostic=masks_are_class_agnostic,
                convolve_then_upsample=convolve_then_upsample_masks)
    return mask_rcnn_keras_box_predictor.MaskRCNNKerasBoxPredictor(
        is_training=is_training,
        num_classes=num_classes,
        freeze_batchnorm=freeze_batchnorm,
        box_prediction_head=box_prediction_head,
        class_prediction_head=class_prediction_head,
        third_stage_heads=third_stage_heads)