def graph_fn(image_features):
            box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead(
                box_code_size=box_code_size,
                conv_hyperparams=self._build_conv_hyperparams(),
                num_predictions_per_location=num_predictions_per_location)
            class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead(
                num_class_slots=num_classes_without_background + 1,
                conv_hyperparams=self._build_conv_hyperparams(),
                num_predictions_per_location=num_predictions_per_location)
            other_heads = {
                other_head_name:
                keras_mask_head.WeightSharedConvolutionalMaskHead(
                    num_classes=num_classes_without_background,
                    conv_hyperparams=self._build_conv_hyperparams(),
                    num_predictions_per_location=num_predictions_per_location,
                    mask_height=mask_height,
                    mask_width=mask_width)
            }

            conv_box_predictor = box_predictor.WeightSharedConvolutionalBoxPredictor(
                is_training=False,
                num_classes=num_classes_without_background,
                box_prediction_head=box_prediction_head,
                class_prediction_head=class_prediction_head,
                other_heads=other_heads,
                conv_hyperparams=self._build_conv_hyperparams(),
                freeze_batchnorm=False,
                inplace_batchnorm_update=False,
                depth=32,
                num_layers_before_predictor=2)
            box_predictions = conv_box_predictor([image_features])
            for key, value in box_predictions.items():
                box_predictions[key] = tf.concat(value, axis=1)
            assert len(box_predictions) == 3
            return (box_predictions[box_predictor.BOX_ENCODINGS],
                    box_predictions[
                        box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND],
                    box_predictions[other_head_name])
Пример #2
0
  def test_other_heads_predictions(self):
    box_code_size = 4
    num_classes_without_background = 3
    other_head_name = 'Mask'
    mask_height = 5
    mask_width = 5
    num_predictions_per_location = 5
    box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead(
        box_code_size=box_code_size,
        conv_hyperparams=self._build_conv_hyperparams(),
        num_predictions_per_location=num_predictions_per_location)
    class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead(
        num_class_slots=num_classes_without_background + 1,
        conv_hyperparams=self._build_conv_hyperparams(),
        num_predictions_per_location=num_predictions_per_location)
    other_heads = {
        other_head_name:
            keras_mask_head.WeightSharedConvolutionalMaskHead(
                num_classes=num_classes_without_background,
                conv_hyperparams=self._build_conv_hyperparams(),
                num_predictions_per_location=num_predictions_per_location,
                mask_height=mask_height,
                mask_width=mask_width)
    }

    conv_box_predictor = box_predictor.WeightSharedConvolutionalBoxPredictor(
        is_training=False,
        num_classes=num_classes_without_background,
        box_prediction_head=box_prediction_head,
        class_prediction_head=class_prediction_head,
        other_heads=other_heads,
        conv_hyperparams=self._build_conv_hyperparams(),
        freeze_batchnorm=False,
        inplace_batchnorm_update=False,
        depth=32,
        num_layers_before_predictor=2)
    def graph_fn(image_features):
      box_predictions = conv_box_predictor([image_features])
      for key, value in box_predictions.items():
        box_predictions[key] = tf.concat(value, axis=1)
      assert len(box_predictions) == 3
      return (box_predictions[box_predictor.BOX_ENCODINGS],
              box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND],
              box_predictions[other_head_name])

    batch_size = 4
    feature_ht = 8
    feature_wt = 8
    image_features = np.random.rand(batch_size, feature_ht, feature_wt,
                                    64).astype(np.float32)
    (box_encodings, class_predictions, other_head_predictions) = self.execute(
        graph_fn, [image_features])
    num_anchors = feature_ht * feature_wt * num_predictions_per_location
    self.assertAllEqual(box_encodings.shape,
                        [batch_size, num_anchors, box_code_size])
    self.assertAllEqual(
        class_predictions.shape,
        [batch_size, num_anchors, num_classes_without_background + 1])
    self.assertAllEqual(other_head_predictions.shape, [
        batch_size, num_anchors, num_classes_without_background, mask_height,
        mask_width
    ])
Пример #3
0
def build_weight_shared_convolutional_keras_box_predictor(
    is_training,
    num_classes,
    conv_hyperparams,
    freeze_batchnorm,
    inplace_batchnorm_update,
    num_predictions_per_location_list,
    depth,
    num_layers_before_predictor,
    box_code_size,
    kernel_size=3,
    add_background_class=True,
    class_prediction_bias_init=0.0,
    use_dropout=False,
    dropout_keep_prob=0.8,
    share_prediction_tower=False,
    apply_batch_norm=True,
    use_depthwise=False,
    score_converter_fn=tf.identity,
    box_encodings_clip_range=None,
    name='WeightSharedConvolutionalBoxPredictor'):
  """Builds the Keras WeightSharedConvolutionalBoxPredictor from the arguments.

  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}).
    conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object
      containing hyperparameters for convolution 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.
    inplace_batchnorm_update: Whether to update batch norm moving average
      values inplace. When this is false train op must add a control
      dependency on tf.graphkeys.UPDATE_OPS collection in order to update
      batch norm statistics.
    num_predictions_per_location_list: A list of integers representing the
      number of box predictions to be made per spatial location for each
      feature map.
    depth: depth of conv layers.
    num_layers_before_predictor: Number of the additional conv layers before
      the predictor.
    box_code_size: Size of encoding for each box.
    kernel_size: Size of final convolution kernel.
    add_background_class: Whether to add an implicit background class.
    class_prediction_bias_init: constant value to initialize bias of the last
      conv2d layer before class prediction.
    use_dropout: Whether to apply dropout to class prediction head.
        dropout_keep_prob: Probability of keeping activiations.
    share_prediction_tower: Whether to share the multi-layer tower between box
      prediction and class prediction heads.
    apply_batch_norm: Whether to apply batch normalization to conv layers in
      this predictor.
    use_depthwise: Whether to use depthwise separable conv2d instead of conv2d.
    score_converter_fn: Callable score converter to perform elementwise op on
      class scores.
    box_encodings_clip_range: Min and max values for clipping the box_encodings.
    name: A string name scope to assign to the box predictor. If `None`, Keras
      will auto-generate one from the class name.

  Returns:
    A Keras WeightSharedConvolutionalBoxPredictor class.
  """
  if len(set(num_predictions_per_location_list)) > 1:
    raise ValueError('num predictions per location must be same for all'
                     'feature maps, found: {}'.format(
                         num_predictions_per_location_list))
  num_predictions_per_location = num_predictions_per_location_list[0]

  box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead(
      box_code_size=box_code_size,
      kernel_size=kernel_size,
      conv_hyperparams=conv_hyperparams,
      num_predictions_per_location=num_predictions_per_location,
      use_depthwise=use_depthwise,
      box_encodings_clip_range=box_encodings_clip_range,
      name='WeightSharedConvolutionalBoxHead')
  class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead(
      num_class_slots=(
          num_classes + 1 if add_background_class else num_classes),
      use_dropout=use_dropout,
      dropout_keep_prob=dropout_keep_prob,
      kernel_size=kernel_size,
      conv_hyperparams=conv_hyperparams,
      num_predictions_per_location=num_predictions_per_location,
      class_prediction_bias_init=class_prediction_bias_init,
      use_depthwise=use_depthwise,
      score_converter_fn=score_converter_fn,
      name='WeightSharedConvolutionalClassHead')
  other_heads = {}

  return (
      convolutional_keras_box_predictor.WeightSharedConvolutionalBoxPredictor(
          is_training=is_training,
          num_classes=num_classes,
          box_prediction_head=box_prediction_head,
          class_prediction_head=class_prediction_head,
          other_heads=other_heads,
          conv_hyperparams=conv_hyperparams,
          depth=depth,
          num_layers_before_predictor=num_layers_before_predictor,
          freeze_batchnorm=freeze_batchnorm,
          inplace_batchnorm_update=inplace_batchnorm_update,
          kernel_size=kernel_size,
          apply_batch_norm=apply_batch_norm,
          share_prediction_tower=share_prediction_tower,
          use_depthwise=use_depthwise,
          name=name))
def build_weight_shared_convolutional_keras_box_predictor(
    is_training,
    num_classes,
    conv_hyperparams,
    freeze_batchnorm,
    inplace_batchnorm_update,
    num_predictions_per_location_list,
    depth,
    num_layers_before_predictor,
    box_code_size,
    kernel_size=3,
    add_background_class=True,
    class_prediction_bias_init=0.0,
    use_dropout=False,
    dropout_keep_prob=0.8,
    share_prediction_tower=False,
    apply_batch_norm=True,
    use_depthwise=False,
    score_converter_fn=tf.identity,
    box_encodings_clip_range=None,
    name='WeightSharedConvolutionalBoxPredictor'):
    if len(set(num_predictions_per_location_list)) > 1:
    raise ValueError('num predictions per location must be same for all'
                     'feature maps, found: {}'.format(
                         num_predictions_per_location_list))
  num_predictions_per_location = num_predictions_per_location_list[0]

  box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead(
      box_code_size=box_code_size,
      kernel_size=kernel_size,
      conv_hyperparams=conv_hyperparams,
      num_predictions_per_location=num_predictions_per_location,
      use_depthwise=use_depthwise,
      box_encodings_clip_range=box_encodings_clip_range,
      name='WeightSharedConvolutionalBoxHead')
  class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead(
      num_class_slots=(
          num_classes + 1 if add_background_class else num_classes),
      use_dropout=use_dropout,
      dropout_keep_prob=dropout_keep_prob,
      kernel_size=kernel_size,
      conv_hyperparams=conv_hyperparams,
      num_predictions_per_location=num_predictions_per_location,
      class_prediction_bias_init=class_prediction_bias_init,
      use_depthwise=use_depthwise,
      score_converter_fn=score_converter_fn,
      name='WeightSharedConvolutionalClassHead')
  other_heads = {}

  return (
      convolutional_keras_box_predictor.WeightSharedConvolutionalBoxPredictor(
          is_training=is_training,
          num_classes=num_classes,
          box_prediction_head=box_prediction_head,
          class_prediction_head=class_prediction_head,
          other_heads=other_heads,
          conv_hyperparams=conv_hyperparams,
          depth=depth,
          num_layers_before_predictor=num_layers_before_predictor,
          freeze_batchnorm=freeze_batchnorm,
          inplace_batchnorm_update=inplace_batchnorm_update,
          kernel_size=kernel_size,
          apply_batch_norm=apply_batch_norm,
          share_prediction_tower=share_prediction_tower,
          use_depthwise=use_depthwise,
          name=name))