Ejemplo n.º 1
0
    def __init__(self, net, labels_one_hot, model_params, method_params):
        """
        Stores arguments in member variable for further use
        :param net: shape [batch_size, num_features, feature_size] which contains some extracted image features
        :param labels_one_hot: [batch_size, seq_length, num_char_classes]- ground truth labels for the input features
        :param model_params: a namedtuple with model parameters
        :param method_params: A SequenceLayerParams
        """
        self._params = model_params
        self._mparams = method_params
        self._net = net
        self._labels_one_hot = labels_one_hot
        self._batch_size = net.get_shape().dims[0]

        # Initialize parameters for char logits which will be computed on the fly
        # inside an LSTM decoder.
        self._char_logits = {}
        regularizer = tf_slim.l2_regularizer(self._mparams.weight_decay)

        self._softmax_w = tf_slim.model_variable(
            'softmax_w',
            [self._mparams.num_lstm_units, self._params.num_char_classes],
            initializer=orthogonal_initializer,
            regularizer=regularizer)

        self._softmax_b = tf_slim.model_variable(
            'softmax_b', [self._params.num_char_classes],
            initializer=tf.zeros_initializer(),
            regularizer=regularizer)
Ejemplo n.º 2
0
 def _quant_var(self,
                name,
                initializer_val,
                vars_collection=tf.GraphKeys.MOVING_AVERAGE_VARIABLES):
     """Create an var for storing the min/max quantization range."""
     return slim.model_variable(
         name,
         shape=[],
         initializer=tf.constant_initializer(initializer_val),
         collections=[vars_collection],
         trainable=False)
 def _label_conditioned_variable(name, initializer, labels, num_categories):
   """Label conditioning."""
   shape = tf.TensorShape([num_categories]).concatenate(params_shape)
   var_collections = slim.utils.get_variable_collections(
       variables_collections, name)
   var = slim.model_variable(name,
                             shape=shape,
                             dtype=dtype,
                             initializer=initializer,
                             collections=var_collections,
                             trainable=trainable)
   conditioned_var = tf.gather(var, labels)
   conditioned_var = tf.expand_dims(tf.expand_dims(conditioned_var, 1), 1)
   return conditioned_var
 def _weighted_variable(name, initializer, weights, num_categories):
   """Weighting."""
   shape = tf.TensorShape([num_categories]).concatenate(params_shape)
   var_collections = slim.utils.get_variable_collections(
       variables_collections, name)
   var = slim.model_variable(name,
                             shape=shape,
                             dtype=dtype,
                             initializer=initializer,
                             collections=var_collections,
                             trainable=trainable)
   weights = tf.reshape(
       weights,
       weights.get_shape().concatenate([1] * params_shape.ndims))
   conditioned_var = weights * var
   conditioned_var = tf.reduce_sum(conditioned_var, 0, keep_dims=True)
   conditioned_var = tf.expand_dims(tf.expand_dims(conditioned_var, 1), 1)
   return conditioned_var
def normalize_to_target(inputs,
                        target_norm_value,
                        dim,
                        epsilon=1e-7,
                        trainable=True,
                        scope='NormalizeToTarget',
                        summarize=True):
  """L2 normalizes the inputs across the specified dimension to a target norm.

  This op implements the L2 Normalization layer introduced in
  Liu, Wei, et al. "SSD: Single Shot MultiBox Detector."
  and Liu, Wei, Andrew Rabinovich, and Alexander C. Berg.
  "Parsenet: Looking wider to see better." and is useful for bringing
  activations from multiple layers in a convnet to a standard scale.

  Note that the rank of `inputs` must be known and the dimension to which
  normalization is to be applied should be statically defined.

  TODO(jonathanhuang): Add option to scale by L2 norm of the entire input.

  Args:
    inputs: A `Tensor` of arbitrary size.
    target_norm_value: A float value that specifies an initial target norm or
      a list of floats (whose length must be equal to the depth along the
      dimension to be normalized) specifying a per-dimension multiplier
      after normalization.
    dim: The dimension along which the input is normalized.
    epsilon: A small value to add to the inputs to avoid dividing by zero.
    trainable: Whether the norm is trainable or not
    scope: Optional scope for variable_scope.
    summarize: Whether or not to add a tensorflow summary for the op.

  Returns:
    The input tensor normalized to the specified target norm.

  Raises:
    ValueError: If dim is smaller than the number of dimensions in 'inputs'.
    ValueError: If target_norm_value is not a float or a list of floats with
      length equal to the depth along the dimension to be normalized.
  """
  with tf.variable_scope(scope, 'NormalizeToTarget', [inputs]):
    if not inputs.get_shape():
      raise ValueError('The input rank must be known.')
    input_shape = inputs.get_shape().as_list()
    input_rank = len(input_shape)
    if dim < 0 or dim >= input_rank:
      raise ValueError(
          'dim must be non-negative but smaller than the input rank.')
    if not input_shape[dim]:
      raise ValueError('input shape should be statically defined along '
                       'the specified dimension.')
    depth = input_shape[dim]
    if not (isinstance(target_norm_value, float) or
            (isinstance(target_norm_value, list) and
             len(target_norm_value) == depth) and
            all([isinstance(val, float) for val in target_norm_value])):
      raise ValueError('target_norm_value must be a float or a list of floats '
                       'with length equal to the depth along the dimension to '
                       'be normalized.')
    if isinstance(target_norm_value, float):
      initial_norm = depth * [target_norm_value]
    else:
      initial_norm = target_norm_value
    target_norm = slim.model_variable(
        name='weights',
        dtype=tf.float32,
        initializer=tf.constant(initial_norm, dtype=tf.float32),
        trainable=trainable)
    if summarize:
      mean = tf.reduce_mean(target_norm)
      tf.summary.scalar(tf.get_variable_scope().name, mean)
    lengths = epsilon + tf.sqrt(tf.reduce_sum(tf.square(inputs), dim, True))
    mult_shape = input_rank*[1]
    mult_shape[dim] = depth
    return tf.reshape(target_norm, mult_shape) * tf.truediv(inputs, lengths)