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
示例#2
0
 def test_hyperparam_override(self):
     hyperparams = self._build_conv_hyperparams()
     model = mobilenet_v2.mobilenet_v2(batchnorm_training=True,
                                       conv_hyperparams=hyperparams,
                                       weights=None,
                                       use_explicit_padding=False,
                                       alpha=1.0,
                                       min_depth=32,
                                       include_top=False)
     hyperparams.params()
     bn_layer = model.get_layer(name='block_5_project_BN')
     self.assertAllClose(bn_layer.momentum, 0.2)
     self.assertAllClose(bn_layer.epsilon, 0.1)
示例#3
0
 def test_hyperparam_override(self):
   hyperparams = self._build_conv_hyperparams()
   model = mobilenet_v2.mobilenet_v2(
       batchnorm_training=True,
       conv_hyperparams=hyperparams,
       weights=None,
       use_explicit_padding=False,
       alpha=1.0,
       min_depth=32,
       include_top=False)
   hyperparams.params()
   bn_layer = model.get_layer(name='block_5_project_BN')
   self.assertAllClose(bn_layer.momentum, 0.2)
   self.assertAllClose(bn_layer.epsilon, 0.1)
示例#4
0
 def _create_application_with_layer_outputs(self,
                                            layer_names,
                                            batchnorm_training,
                                            conv_hyperparams=None,
                                            use_explicit_padding=False,
                                            alpha=1.0,
                                            min_depth=None):
     """Constructs Keras mobilenetv2 that extracts intermediate layer outputs."""
     if not layer_names:
         layer_names = _layers_to_check
     full_model = mobilenet_v2.mobilenet_v2(
         batchnorm_training=batchnorm_training,
         conv_hyperparams=conv_hyperparams,
         weights=None,
         use_explicit_padding=use_explicit_padding,
         alpha=alpha,
         min_depth=min_depth,
         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)
示例#5
0
 def _create_application_with_layer_outputs(
     self, layer_names, batchnorm_training,
     conv_hyperparams=None,
     use_explicit_padding=False,
     alpha=1.0,
     min_depth=None):
   """Constructs Keras mobilenetv2 that extracts intermediate layer outputs."""
   if not layer_names:
     layer_names = _layers_to_check
   full_model = mobilenet_v2.mobilenet_v2(
       batchnorm_training=batchnorm_training,
       conv_hyperparams=conv_hyperparams,
       weights=None,
       use_explicit_padding=use_explicit_padding,
       alpha=alpha,
       min_depth=min_depth,
       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 __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'))