예제 #1
0
  def test_simple_model(self):
    inputs = tf.keras.Input(shape=(256,))  # Returns a placeholder tensor

    # A layer instance is callable on a tensor, and returns a tensor.
    x = tf.keras.layers.Dense(128, activation='relu', name='a')(inputs)
    x = tf.keras.layers.Dense(64, activation='relu', name='b')(x)
    x = tf.keras.layers.Dense(32, activation='relu', name='c')(x)
    x = tf.keras.layers.Dense(16, activation='relu', name='d')(x)
    x = tf.keras.layers.Dense(8, activation='relu', name='e')(x)
    predictions = tf.keras.layers.Dense(10, activation='softmax')(x)

    model = tf.keras.Model(inputs=inputs, outputs=predictions)

    new_in = model.get_layer(
        name='b').input
    new_out = model.get_layer(
        name='d').output

    new_model = model_util.extract_submodel(
        model=model,
        inputs=new_in,
        outputs=new_out)

    batch_size = 3
    ones = tf.ones((batch_size, 128))
    final_out = new_model(ones)
    self.assertAllEqual(final_out.shape, (batch_size, 16))
    def get_box_classifier_feature_extractor_model(self, name=None):
        """Returns a model that extracts second stage box classifier features.

    This function reconstructs the "second half" of the ResNet v1
    network after the part defined in `get_proposal_feature_extractor_model`.

    Args:
      name: A scope name to construct all variables within.

    Returns:
      A Keras model that takes proposal_feature_maps:
        A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, crop_height, crop_width, depth]
        representing the feature map cropped to each proposal.
      And returns proposal_classifier_features:
        A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, height, width, depth]
        representing box classifier features for each proposal.
    """
        if not self.classification_backbone:
            self.classification_backbone = self._resnet_v1_base_model(
                batchnorm_training=self._train_batch_norm,
                conv_hyperparams=None,
                weight_decay=self._weight_decay,
                classes=None,
                weights=None,
                include_top=False)
        with tf.name_scope(name):
            with tf.name_scope('ResnetV1'):
                conv4_last_layer = _RESNET_MODEL_CONV4_LAST_LAYERS[
                    self._resnet_v1_base_model_name]
                proposal_feature_maps = self.classification_backbone.get_layer(
                    name=conv4_last_layer).output
                proposal_classifier_features = self.classification_backbone.get_layer(
                    name='conv5_block3_out').output

                keras_model = model_util.extract_submodel(
                    model=self.classification_backbone,
                    inputs=proposal_feature_maps,
                    outputs=proposal_classifier_features)
                for variable in keras_model.variables:
                    self._variable_dict[variable.name[:-2]] = variable
                return keras_model
    def get_box_classifier_feature_extractor_model(self, name=None):
        """Returns a model that extracts second stage box classifier features.

    This function reconstructs the "second half" of the Inception ResNet v2
    network after the part defined in `get_proposal_feature_extractor_model`.

    Args:
      name: A scope name to construct all variables within.

    Returns:
      A Keras model that takes proposal_feature_maps:
        A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, crop_height, crop_width, depth]
        representing the feature map cropped to each proposal.
      And returns proposal_classifier_features:
        A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, height, width, depth]
        representing box classifier features for each proposal.
    """
        if not self.classification_backbone:
            self.classification_backbone = inception_resnet_v2.inception_resnet_v2(
                self._train_batch_norm,
                output_stride=self._first_stage_features_stride,
                align_feature_maps=True,
                weight_decay=self._weight_decay,
                weights=None,
                include_top=False)
        with tf.name_scope(name):
            with tf.name_scope('InceptionResnetV2'):
                proposal_feature_maps = self.classification_backbone.get_layer(
                    name='block17_20_ac').output
                proposal_classifier_features = self.classification_backbone.get_layer(
                    name='conv_7b_ac').output

                keras_model = model_util.extract_submodel(
                    model=self.classification_backbone,
                    inputs=proposal_feature_maps,
                    outputs=proposal_classifier_features)
                for variable in keras_model.variables:
                    self._variable_dict[variable.name[:-2]] = variable
                return keras_model