def _create_application_with_layer_outputs( self, layer_names, batchnorm_training, output_stride=16, align_feature_maps=False, batchnorm_scale=False, weight_decay=0.00004, default_batchnorm_momentum=0.9997, default_batchnorm_epsilon=0.001, ): """Constructs Keras inception_resnet_v2 that extracts layer outputs.""" # Have to clear the Keras backend to ensure isolation in layer naming tf.keras.backend.clear_session() if not layer_names: layer_names = _KERAS_LAYERS_TO_CHECK full_model = inception_resnet_v2.inception_resnet_v2( batchnorm_training=batchnorm_training, output_stride=output_stride, align_feature_maps=align_feature_maps, weights=None, batchnorm_scale=batchnorm_scale, weight_decay=weight_decay, default_batchnorm_momentum=default_batchnorm_momentum, default_batchnorm_epsilon=default_batchnorm_epsilon, include_top=False) layer_outputs = [ full_model.get_layer(name=layer).output for layer in layer_names ] return tf.keras.Model(inputs=full_model.inputs, outputs=layer_outputs)
def test_hyperparam_override(self): model = inception_resnet_v2.inception_resnet_v2( batchnorm_training=True, default_batchnorm_momentum=0.2, default_batchnorm_epsilon=0.1, weights=None, include_top=False) bn_layer = model.get_layer(name='freezable_batch_norm') self.assertAllClose(bn_layer.momentum, 0.2) self.assertAllClose(bn_layer.epsilon, 0.1)
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
def get_proposal_feature_extractor_model(self, name=None): """Returns a model that extracts first stage RPN features. Extracts features using the first half of the Inception Resnet v2 network. We construct the network in `align_feature_maps=True` mode, which means that all VALID paddings in the network are changed to SAME padding so that the feature maps are aligned. Args: name: A scope name to construct all variables within. Returns: A Keras model that takes preprocessed_inputs: A [batch, height, width, channels] float32 tensor representing a batch of images. And returns rpn_feature_map: A tensor with shape [batch, height, width, depth] """ 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_features = self.classification_backbone.get_layer( name='block17_20_ac').output keras_model = tf.keras.Model( inputs=self.classification_backbone.inputs, outputs=proposal_features) for variable in keras_model.variables: self._variable_dict[variable.name[:-2]] = variable return keras_model