def BatchNormalization(self, **kwargs): """Builds a normalization layer. Overrides the Keras application batch norm with the norm specified by the Object Detection configuration. Args: **kwargs: Only the name is used, all other params ignored. Required for matching `layers.BatchNormalization` calls in the Keras application. Returns: A normalization layer specified by the Object Detection hyperparameter configurations. """ name = kwargs.get('name') if self._conv_hyperparams: return self._conv_hyperparams.build_batch_norm( training=self._batchnorm_training, name=name) else: return freezable_batch_norm.FreezableBatchNorm( training=self._batchnorm_training, epsilon=1e-3, momentum=self._default_batchnorm_momentum, name=name)
def _build_model(self, training=None): model = tf.keras.models.Sequential() norm = freezable_batch_norm.FreezableBatchNorm(training=training, input_shape=(10,), momentum=0.8) model.add(norm) return model, norm
def build_batch_norm(self, training=None, **overrides): """Returns a Batch Normalization layer with the appropriate hyperparams. If the hyperparams are configured to not use batch normalization, this will return a Keras Lambda layer that only applies tf.Identity, without doing any normalization. Optionally overrides values in the batch_norm hyperparam dict. Overrides only apply to individual calls of this method, and do not affect future calls. Args: training: if True, the normalization layer will normalize using the batch statistics. If False, the normalization layer will be frozen and will act as if it is being used for inference. If None, the layer will look up the Keras learning phase at `call` time to decide what to do. **overrides: batch normalization construction args to override from the batch_norm hyperparams dictionary. Returns: Either a FreezableBatchNorm layer (if use_batch_norm() is True), or a Keras Lambda layer that applies the identity (if use_batch_norm() is False) """ if self.use_batch_norm(): return freezable_batch_norm.FreezableBatchNorm( training=training, **self.batch_norm_params(**overrides)) else: return tf.keras.layers.Lambda(tf.identity)
def build_batch_norm(self, training=None, **overrides): if self.use_batch_norm(): return freezable_batch_norm.FreezableBatchNorm( training=training, **self.batch_norm_params(**overrides)) else: return tf.keras.layers.Lambda(tf.identity)
def __init__(self, projection_dimension, **kwargs): self.batch_norm = freezable_batch_norm.FreezableBatchNorm( epsilon=0.001, center=True, scale=True, momentum=0.97, trainable=True) self.projection = tf.keras.layers.Dense(units=projection_dimension, use_bias=True) self.projection_dimension = projection_dimension super(ContextProjection, self).__init__(**kwargs)
def BatchNormalization(self, **kwargs): """Builds a normalization layer. Overrides the Keras application batch norm with the norm specified by the Object Detection configuration. Args: **kwargs: Keyword arguments from the `layers.BatchNormalization` calls in the Keras application. Returns: A normalization layer specified by the Object Detection hyperparameter configurations. """ kwargs['scale'] = self._batchnorm_scale return freezable_batch_norm.FreezableBatchNorm( training=self._batchnorm_training, epsilon=self._default_batchnorm_epsilon, momentum=self._default_batchnorm_momentum, **kwargs)