def build(self, input_shape): full_mobilenet_v2 = mobilenet_v2.mobilenet_v2( batchnorm_training=(self._is_training and not self._freeze_batchnorm), conv_hyperparams=(self._conv_hyperparams if self._override_base_feature_extractor_hyperparams else None), weights=None, use_explicit_padding=self._use_explicit_padding, alpha=self._depth_multiplier, min_depth=self._min_depth, include_top=False) conv2d_11_pointwise = full_mobilenet_v2.get_layer( name='block_13_expand_relu').output conv2d_13_pointwise = full_mobilenet_v2.get_layer( name='out_relu').output self.mobilenet_v2 = tf.keras.Model( inputs=full_mobilenet_v2.inputs, outputs=[conv2d_11_pointwise, conv2d_13_pointwise]) self.feature_map_generator = ( feature_map_generators.KerasMultiResolutionFeatureMaps( feature_map_layout=self._feature_map_layout, depth_multiplier=self._depth_multiplier, min_depth=self._min_depth, insert_1x1_conv=True, is_training=self._is_training, conv_hyperparams=self._conv_hyperparams, freeze_batchnorm=self._freeze_batchnorm, name='FeatureMaps')) self.built = True
def _build_feature_map_generator(self, feature_map_layout, use_keras, pool_residual=False): if use_keras: return feature_map_generators.KerasMultiResolutionFeatureMaps( feature_map_layout=feature_map_layout, depth_multiplier=1, min_depth=32, insert_1x1_conv=True, freeze_batchnorm=False, is_training=True, conv_hyperparams=self._build_conv_hyperparams(), name='FeatureMaps') else: def feature_map_generator(image_features): return feature_map_generators.multi_resolution_feature_maps( feature_map_layout=feature_map_layout, depth_multiplier=1, min_depth=32, insert_1x1_conv=True, image_features=image_features, pool_residual=pool_residual) return feature_map_generator
def build(self, input_shape): model = build_model_base_keras_model(input_shape[1:], self._network_name, self._is_training) # inputs = tf.keras.layers.Input(input_shape[1:]) inputs = model.inputs # _, endpoints = build_model_base(inputs, self._network_name, self._is_training) # outputs = [endpoints[x] for x in self._used_nodes if x] outputs = [model.get_layer(x).output for x in self._used_nodes if x] self.net = tf.keras.Model(inputs=inputs, outputs=outputs) # feature map generator self._feature_map_generator = feature_map_generators.KerasMultiResolutionFeatureMaps( feature_map_layout=self._feature_map_layout, depth_multiplier=self._depth_multiplier, min_depth=self._min_depth, insert_1x1_conv=True, is_training=self._is_training, conv_hyperparams=self._conv_hyperparams, freeze_batchnorm=self._freeze_batchnorm, name=None ) self.built = True
def __init__(self, is_training, depth_multiplier, min_depth, pad_to_multiple, conv_hyperparams, freeze_batchnorm, inplace_batchnorm_update, use_explicit_padding=False, use_depthwise=False, override_base_feature_extractor_hyperparams=False, name=None): """MobileNetV2 Feature Extractor for SSD Models. Mobilenet v2 (experimental), designed by sandler@. More details can be found in //knowledge/cerebra/brain/compression/mobilenet/mobilenet_experimental.py Args: is_training: whether the network is in training mode. depth_multiplier: float depth multiplier for feature extractor (Functions as a width multiplier for the mobilenet_v2 network itself). min_depth: minimum feature extractor depth. pad_to_multiple: the nearest multiple to zero pad the input height and width dimensions to. conv_hyperparams: `hyperparams_builder.KerasLayerHyperparams` object containing convolution hyperparameters for the layers added on top of the base feature extractor. 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. use_explicit_padding: Whether to use explicit padding when extracting features. Default is False. use_depthwise: Whether to use depthwise convolutions. Default is False. override_base_feature_extractor_hyperparams: Whether to override hyperparameters of the base feature extractor with the one from `conv_hyperparams_fn`. name: A string name scope to assign to the model. If 'None', Keras will auto-generate one from the class name. """ super(SSDMobileNetV2KerasFeatureExtractor, self).__init__( is_training=is_training, depth_multiplier=depth_multiplier, min_depth=min_depth, pad_to_multiple=pad_to_multiple, conv_hyperparams=conv_hyperparams, freeze_batchnorm=freeze_batchnorm, inplace_batchnorm_update=inplace_batchnorm_update, use_explicit_padding=use_explicit_padding, use_depthwise=use_depthwise, override_base_feature_extractor_hyperparams= override_base_feature_extractor_hyperparams, name=name) feature_map_layout = { 'from_layer': ['layer_15/expansion_output', 'layer_19', '', '', '', ''], 'layer_depth': [-1, -1, 512, 256, 256, 128], 'use_depthwise': self._use_depthwise, 'use_explicit_padding': self._use_explicit_padding, } with tf.name_scope('MobilenetV2'): full_mobilenet_v2 = mobilenet_v2.mobilenet_v2( batchnorm_training=(is_training and not freeze_batchnorm), conv_hyperparams=(conv_hyperparams if self._override_base_feature_extractor_hyperparams else None), weights=None, use_explicit_padding=use_explicit_padding, alpha=self._depth_multiplier, min_depth=self._min_depth, include_top=False) conv2d_11_pointwise = full_mobilenet_v2.get_layer( name='block_13_expand_relu').output conv2d_13_pointwise = full_mobilenet_v2.get_layer(name='out_relu').output self.mobilenet_v2 = tf.keras.Model( inputs=full_mobilenet_v2.inputs, outputs=[conv2d_11_pointwise, conv2d_13_pointwise]) self.feature_map_generator = ( feature_map_generators.KerasMultiResolutionFeatureMaps( feature_map_layout=feature_map_layout, depth_multiplier=self._depth_multiplier, min_depth=self._min_depth, insert_1x1_conv=True, is_training=is_training, conv_hyperparams=conv_hyperparams, freeze_batchnorm=freeze_batchnorm, name='FeatureMaps'))