Beispiel #1
0
    def _variance(self):
        # We need to put the tf.where inside the outer tf.where to ensure we never
        # hit a NaN in the gradient.
        denom = tf.where(tf.greater(self.df, 2.), self.df - 2.,
                         tf.ones_like(self.df))
        # Abs(scale) superfluous.
        var = (tf.ones(self.batch_shape_tensor(), dtype=self.dtype) *
               tf.square(self.scale) * self.df / denom)
        # When 1 < df <= 2, variance is infinite.
        inf = dtype_util.as_numpy_dtype(self.dtype)(np.inf)
        result_where_defined = tf.where(
            self.df > tf.fill(self.batch_shape_tensor(), 2.), var,
            tf.fill(self.batch_shape_tensor(), inf, name="inf"))

        if self.allow_nan_stats:
            nan = dtype_util.as_numpy_dtype(self.dtype)(np.nan)
            return tf.where(
                tf.greater(
                    self.df,
                    tf.ones(self.batch_shape_tensor(), dtype=self.dtype)),
                result_where_defined,
                tf.fill(self.batch_shape_tensor(), nan, name="nan"))
        else:
            return distribution_util.with_dependencies([
                assert_util.assert_less(
                    tf.ones([], dtype=self.dtype),
                    self.df,
                    message="variance not defined for components of df <= 1"),
            ], result_where_defined)
def prune_outside_window(boxlist, window, scope=None):
    """Prunes bounding boxes that fall outside a given window.

  This function prunes bounding boxes that even partially fall outside the given
  window. See also clip_to_window which only prunes bounding boxes that fall
  completely outside the window, and clips any bounding boxes that partially
  overflow.

  Args:
    boxlist: a BoxList holding M_in boxes.
    window: a float tensor of shape [4] representing [ymin, xmin, ymax, xmax]
      of the window
    scope: name scope.

  Returns:
    pruned_corners: a tensor with shape [M_out, 4] where M_out <= M_in
    valid_indices: a tensor with shape [M_out] indexing the valid bounding boxes
     in the input tensor.
  """
    with tf.name_scope(scope, 'PruneOutsideWindow'):
        y_min, x_min, y_max, x_max = tf.split(value=boxlist.get(),
                                              num_or_size_splits=4,
                                              axis=1)
        win_y_min, win_x_min, win_y_max, win_x_max = tf.unstack(window)
        coordinate_violations = tf.concat([
            tf.less(y_min, win_y_min),
            tf.less(x_min, win_x_min),
            tf.greater(y_max, win_y_max),
            tf.greater(x_max, win_x_max)
        ], 1)
        valid_indices = tf.reshape(
            tf.where(tf.logical_not(tf.reduce_any(coordinate_violations, 1))),
            [-1])
        return gather(boxlist, valid_indices), valid_indices
Beispiel #3
0
def get_non_empty_box_indices(boxes):
  """Get indices for non-empty boxes."""
  # Selects indices if box height or width is 0.
  height = boxes[:, 2] - boxes[:, 0]
  width = boxes[:, 3] - boxes[:, 1]
  indices = tf.where(tf.logical_and(tf.greater(height, 0),
                                    tf.greater(width, 0)))
  return indices[:, 0]
    def _resource_apply_dense(self, grad, param, apply_state=None):
        if grad is None or param is None:
            return tf.no_op()

        var_device, var_dtype = param.device, param.dtype.base_dtype
        coefficients = ((apply_state or {}).get((var_device, var_dtype)) or
                        self._fallback_apply_state(var_device, var_dtype))
        learning_rate = coefficients["lr_t"]

        param_name = param.name

        v = self.get_slot(param, "Momentum")

        if self._use_weight_decay(param_name):
            grad += self.weight_decay * param

        if self.classic_momentum:
            trust_ratio = 1.0
            if self._do_layer_adaptation(param_name):
                w_norm = tf.norm(param, ord=2)
                g_norm = tf.norm(grad, ord=2)
                trust_ratio = tf.where(
                    tf.greater(w_norm, 0),
                    tf.where(tf.greater(g_norm, 0), (self.eeta * w_norm / g_norm), 1.0),
                    1.0)
            scaled_lr = learning_rate * trust_ratio

            next_v = tf.multiply(self.momentum, v) + scaled_lr * grad
            if self.use_nesterov:
                update = tf.multiply(self.momentum, next_v) + scaled_lr * grad
            else:
                update = next_v
            next_param = param - update
        else:
            next_v = tf.multiply(self.momentum, v) + grad
            if self.use_nesterov:
                update = tf.multiply(self.momentum, next_v) + grad
            else:
                update = next_v

            trust_ratio = 1.0
            if self._do_layer_adaptation(param_name):
                w_norm = tf.norm(param, ord=2)
                v_norm = tf.norm(update, ord=2)
                trust_ratio = tf.where(
                    tf.greater(w_norm, 0),
                    tf.where(tf.greater(v_norm, 0), (self.eeta * w_norm / v_norm), 1.0),
                    1.0)
            scaled_lr = trust_ratio * learning_rate
            next_param = param - scaled_lr * update

        return tf.group(*[
            param.assign(next_param, use_locking=False),
            v.assign(next_v, use_locking=False)
        ])
        def _match_when_rows_are_non_empty():
            """Performs matching when the rows of similarity matrix are non empty.

      Returns:
        matches:  int32 tensor indicating the row each column matches to.
      """
            # Matches for each column
            matches = tf.argmax(input=similarity_matrix,
                                axis=0,
                                output_type=tf.int32)

            # Deal with matched and unmatched threshold
            if self._matched_threshold is not None:
                # Get logical indices of ignored and unmatched columns as tf.int64
                matched_vals = tf.reduce_max(input_tensor=similarity_matrix,
                                             axis=0)
                below_unmatched_threshold = tf.greater(
                    self._unmatched_threshold, matched_vals)
                between_thresholds = tf.logical_and(
                    tf.greater_equal(matched_vals, self._unmatched_threshold),
                    tf.greater(self._matched_threshold, matched_vals))

                if self._negatives_lower_than_unmatched:
                    matches = self._set_values_using_indicator(
                        matches, below_unmatched_threshold, -1)
                    matches = self._set_values_using_indicator(
                        matches, between_thresholds, -2)
                else:
                    matches = self._set_values_using_indicator(
                        matches, below_unmatched_threshold, -2)
                    matches = self._set_values_using_indicator(
                        matches, between_thresholds, -1)

            if self._force_match_for_each_row:
                similarity_matrix_shape = shape_utils.combined_static_and_dynamic_shape(
                    similarity_matrix)
                force_match_column_ids = tf.argmax(input=similarity_matrix,
                                                   axis=1,
                                                   output_type=tf.int32)
                force_match_column_indicators = tf.one_hot(
                    force_match_column_ids, depth=similarity_matrix_shape[1])
                force_match_row_ids = tf.argmax(
                    input=force_match_column_indicators,
                    axis=0,
                    output_type=tf.int32)
                force_match_column_mask = tf.cast(
                    tf.reduce_max(input_tensor=force_match_column_indicators,
                                  axis=0), tf.bool)
                final_matches = tf.where(force_match_column_mask,
                                         force_match_row_ids, matches)
                return final_matches
            else:
                return matches
def eval_step(model, x, fifth_embedding_1, fifth_embedding_2, label):
    """Evaluates a single example from the validation set."""
    assert x.shape[0] == 1, 'Only supports batch_size=1 for now'

    _, output_embedding = model(x, training=False)
    sim_1 = tf.matmul(output_embedding, fifth_embedding_1, transpose_b=True)
    sim_2 = tf.matmul(output_embedding, fifth_embedding_2, transpose_b=True)

    correct_1 = tf.squeeze(
        tf.logical_and(tf.greater(sim_1, sim_2), tf.equal(label, 0)))
    correct_2 = tf.squeeze(
        tf.logical_and(tf.greater(sim_2, sim_1), tf.equal(label, 1)))
    return tf.logical_or(correct_1, correct_2)
def filter_greater_than(boxlist, thresh, scope=None):
    """Filter to keep only boxes with score exceeding a given threshold.

  This op keeps the collection of boxes whose corresponding scores are
  greater than the input threshold.

  TODO(jonathanhuang): Change function name to filter_scores_greater_than

  Args:
    boxlist: BoxList holding N boxes.  Must contain a 'scores' field
      representing detection scores.
    thresh: scalar threshold
    scope: name scope.

  Returns:
    a BoxList holding M boxes where M <= N

  Raises:
    ValueError: if boxlist not a BoxList object or if it does not
      have a scores field
  """
    with tf.name_scope(scope, 'FilterGreaterThan'):
        if not isinstance(boxlist, box_list.BoxList):
            raise ValueError('boxlist must be a BoxList')
        if not boxlist.has_field('scores'):
            raise ValueError('input boxlist must have \'scores\' field')
        scores = boxlist.get_field('scores')
        if len(scores.shape.as_list()) > 2:
            raise ValueError('Scores should have rank 1 or 2')
        if len(scores.shape.as_list()) == 2 and scores.shape.as_list()[1] != 1:
            raise ValueError('Scores should have rank 1 or have shape '
                             'consistent with [None, 1]')
        high_score_indices = tf.cast(
            tf.reshape(tf.where(tf.greater(scores, thresh)), [-1]), tf.int32)
        return gather(boxlist, high_score_indices)
def to_normalized_coordinates(boxlist, height, width,
                              check_range=True, scope=None):
  """Converts absolute box coordinates to normalized coordinates in [0, 1].

  Usually one uses the dynamic shape of the image or conv-layer tensor:
    boxlist = box_list_ops.to_normalized_coordinates(boxlist,
                                                     tf.shape(images)[1],
                                                     tf.shape(images)[2]),

  This function raises an assertion failed error at graph execution time when
  the maximum coordinate is smaller than 1.01 (which means that coordinates are
  already normalized). The value 1.01 is to deal with small rounding errors.

  Args:
    boxlist: BoxList with coordinates in terms of pixel-locations.
    height: Maximum value for height of absolute box coordinates.
    width: Maximum value for width of absolute box coordinates.
    check_range: If True, checks if the coordinates are normalized or not.
    scope: name scope.

  Returns:
    boxlist with normalized coordinates in [0, 1].
  """
  with tf.name_scope(scope, 'ToNormalizedCoordinates'):
    height = tf.cast(height, tf.float32)
    width = tf.cast(width, tf.float32)

    if check_range:
      max_val = tf.reduce_max(boxlist.get())
      max_assert = tf.Assert(tf.greater(max_val, 1.01),
                             ['max value is lower than 1.01: ', max_val])
      with tf.control_dependencies([max_assert]):
        width = tf.identity(width)

    return scale(boxlist, 1 / height, 1 / width)
  def call(self, inputs, count_weights=None):
    inputs = utils.ensure_tensor(inputs)

    if count_weights is not None:
      if self.output_mode != COUNT:
        raise ValueError(
            "`count_weights` is not used when `output_mode` is not `'count'`. "
            "Received `count_weights={}`.".format(count_weights))
      count_weights = utils.ensure_tensor(count_weights, self.compute_dtype)

    depth = self.num_tokens
    if isinstance(inputs, tf.SparseTensor):
      max_value = tf.reduce_max(inputs.values)
      min_value = tf.reduce_min(inputs.values)
    else:
      max_value = tf.reduce_max(inputs)
      min_value = tf.reduce_min(inputs)
    condition = tf.logical_and(
        tf.greater(tf.cast(depth, max_value.dtype), max_value),
        tf.greater_equal(min_value, tf.cast(0, min_value.dtype)))
    assertion = tf.Assert(condition, [
        "Input values must be in the range 0 <= values < num_tokens"
        " with num_tokens={}".format(depth)
    ])
    with tf.control_dependencies([assertion]):
      return utils.encode_categorical_inputs(
          inputs,
          output_mode=self.output_mode,
          depth=depth,
          dtype=self.compute_dtype,
          sparse=self.sparse,
          count_weights=count_weights)
Beispiel #10
0
 def update_state(self,
                  y_true: tf.Tensor,
                  y_pred: tf.Tensor,
                  sample_weight=None):
     # predicted_mask = y_pred[:, :, 1]
     # actual_mask = y_true[:, :, 1]
     # predicted_mask = tf.squeeze(tf.keras.activations.sigmoid(predicted_mask))
     # actual_mask = tf.squeeze(actual_mask)
     # tf.print("In accuracy metric, sum of predicted mask and actual mask",
     #          tf.reduce_sum(predicted_mask), tf.reduce_sum(actual_mask))
     actual_mask = tf.cast(y_true, dtype=tf.float32)
     predicted_mask = tf.cast(tf.keras.activations.sigmoid(y_pred),
                              dtype=tf.float32)
     actual_mask = tf.squeeze(actual_mask)
     predicted_mask = tf.squeeze(predicted_mask)
     hard_mask = tf.where(tf.greater(predicted_mask, 0.5),
                          tf.constant(1, dtype=tf.float32),
                          tf.constant(0, dtype=tf.float32))
     hard_mask = tf.squeeze(hard_mask)
     self.tn_metric.update_state(actual_mask, hard_mask)
     tn = self.tn_metric.result()
     self.tn_metric.reset_states()
     if sample_weight is not None:
         sample_weight = tf.cast(sample_weight, 'float32')
         tn = tf.multiply(tn, sample_weight)
     self.tn.assign_add(tn)
     self.step.assign_add(tf.constant(1., dtype=tf.float32))
    def _get_num_pos_neg_samples(self, sorted_indices_tensor, sample_size):
        """Counts the number of positives and negatives numbers to be sampled.

    Args:
      sorted_indices_tensor: A sorted int32 tensor of shape [N] which contains
        the signed indices of the examples where the sign is based on the label
        value. The examples that cannot be sampled are set to 0. It samples
        atmost sample_size*positive_fraction positive examples and remaining
        from negative examples.
      sample_size: Size of subsamples.

    Returns:
      A tuple containing the number of positive and negative labels in the
      subsample.
    """
        input_length = tf.shape(input=sorted_indices_tensor)[0]
        valid_positive_index = tf.greater(sorted_indices_tensor,
                                          tf.zeros(input_length, tf.int32))
        num_sampled_pos = tf.reduce_sum(
            input_tensor=tf.cast(valid_positive_index, tf.int32))
        max_num_positive_samples = tf.constant(
            int(sample_size * self._positive_fraction), tf.int32)
        num_positive_samples = tf.minimum(max_num_positive_samples,
                                          num_sampled_pos)
        num_negative_samples = tf.constant(sample_size,
                                           tf.int32) - num_positive_samples

        return num_positive_samples, num_negative_samples
Beispiel #12
0
  def call(self, inputs, count_weights=None):
    if isinstance(inputs, (list, np.ndarray)):
      inputs = tf.convert_to_tensor(inputs)
    if inputs.shape.rank == 1:
      inputs = tf.compat.v1.expand_dims(inputs, 1)

    if count_weights is not None and self.output_mode != COUNT:
      raise ValueError("count_weights is not used in "
                       "`output_mode='multi_hot'`. Please pass a single input.")

    out_depth = self.num_tokens
    multi_hot_output = (self.output_mode == MULTI_HOT)
    if isinstance(inputs, tf.SparseTensor):
      max_value = tf.reduce_max(inputs.values)
      min_value = tf.reduce_min(inputs.values)
    else:
      max_value = tf.reduce_max(inputs)
      min_value = tf.reduce_min(inputs)
    condition = tf.logical_and(
        tf.greater(
            tf.cast(out_depth, max_value.dtype), max_value),
        tf.greater_equal(
            min_value, tf.cast(0, min_value.dtype)))
    tf.Assert(condition, [
        "Input values must be in the range 0 <= values < num_tokens"
        " with num_tokens={}".format(out_depth)
    ])
    if self.sparse:
      return sparse_bincount(inputs, out_depth, multi_hot_output, count_weights)
    else:
      return dense_bincount(inputs, out_depth, multi_hot_output, count_weights)
Beispiel #13
0
    def write(self, elements):
        """Writes elements to the ringbuffer.

    Args:
      elements: Elements to write.

    Returns:
      Whether the write was successful (always True for now).
    """
        elements_size = tf.shape(elements)[0]
        start = tf.math.floormod(
            self._read_head.read_value() + self._data_size.read_value(),
            self._buffer_size)
        indices = tf.math.floormod(
            tf.range(start, limit=start + elements_size), self._buffer_size)

        tf.compat.v1.scatter_update(self._buffer, indices, elements)

        # special case when addition of new data, exceed _buffer_size:
        # we start overwriting existing data in circular manner
        # and need to update _read_head
        if tf.greater(self._data_size + elements_size, self._buffer_size):
            self._read_head.assign(
                tf.math.floormod(
                    self._read_head.read_value() + self._data_size +
                    tf.math.floormod(elements_size, self._buffer_size),
                    self._buffer_size))

        self._data_size.assign(
            tf.minimum(self._data_size + elements_size, self._buffer_size))
        return tf.convert_to_tensor(True)
 def exported_function(x):
     root.x = constant_op.constant([[37.0, -23.0], [1.0, 4.0]])
     root.y = tf.matmul(root.x, root.w)
     tf.compat.v1.Print(root.x, [root.x])
     tf.compat.v1.Assert(tf.greater(tf.reduce_max(root.x), 0), [root.x])
     tf.compat.v1.check_numerics(root.x, 'NaN found')
     return root.y * x
 def _fast_rcnn_box_loss(self,
                         box_outputs,
                         box_targets,
                         class_targets,
                         normalizer=1.0,
                         delta=1.):
     """Computes box regression loss."""
     # The delta is typically around the mean value of regression target.
     # for instances, the regression targets of 512x512 input with 6 anchors on
     # P2-P6 pyramid is about [0.1, 0.1, 0.2, 0.2].
     with tf.name_scope('fast_rcnn_box_loss'):
         mask = tf.tile(
             tf.expand_dims(tf.greater(class_targets, 0), axis=2),
             [1, 1, 4])
         # The loss is normalized by the sum of non-zero weights before additional
         # normalizer provided by the function caller.
         # Keep the compat.v1 loss because Keras does not have a
         # Reduction.SUM_BY_NONZERO_WEIGHTS substitution yet.
         # TODO(b/143720144): replace this loss.
         box_loss = tf.compat.v1.losses.huber_loss(
             box_targets,
             box_outputs,
             weights=mask,
             delta=delta,
             reduction=tf.compat.v1.losses.Reduction.SUM_BY_NONZERO_WEIGHTS)
         box_loss /= normalizer
         return box_loss
Beispiel #16
0
    def decode(self, serialized_example):
        """Decode the serialized example.

    Args:
      serialized_example: a single serialized tf.Example string.

    Returns:
      decoded_tensors: a dictionary of tensors with the following fields:
        - image: a uint8 tensor of shape [None, None, 3].
        - source_id: a string scalar tensor.
        - height: an integer scalar tensor.
        - width: an integer scalar tensor.
        - groundtruth_classes: a int64 tensor of shape [None].
        - groundtruth_is_crowd: a bool tensor of shape [None].
        - groundtruth_area: a float32 tensor of shape [None].
        - groundtruth_boxes: a float32 tensor of shape [None, 4].
        - groundtruth_instance_masks: a float32 tensor of shape
            [None, None, None].
        - groundtruth_instance_masks_png: a string tensor of shape [None].
    """
        parsed_tensors = tf.io.parse_single_example(
            serialized=serialized_example, features=self._keys_to_features)
        for k in parsed_tensors:
            if isinstance(parsed_tensors[k], tf.SparseTensor):
                if parsed_tensors[k].dtype == tf.string:
                    parsed_tensors[k] = tf.sparse.to_dense(parsed_tensors[k],
                                                           default_value='')
                else:
                    parsed_tensors[k] = tf.sparse.to_dense(parsed_tensors[k],
                                                           default_value=0)

        image = self._decode_image(parsed_tensors)
        boxes = self._decode_boxes(parsed_tensors)
        areas = self._decode_areas(parsed_tensors)
        is_crowds = tf.cond(
            tf.greater(tf.shape(parsed_tensors['image/object/is_crowd'])[0], 0),
            lambda: tf.cast(parsed_tensors['image/object/is_crowd'], dtype=tf.bool),
            lambda: tf.zeros_like(parsed_tensors['image/object/class/label'], dtype=tf.bool))  # pylint: disable=line-too-long
        if self._include_mask:
            masks = self._decode_masks(parsed_tensors)

        decoded_tensors = {
            'image': image,
            'source_id': parsed_tensors['image/source_id'],
            'height': parsed_tensors['image/height'],
            'width': parsed_tensors['image/width'],
            'groundtruth_classes': parsed_tensors['image/object/class/label'],
            'groundtruth_is_crowd': is_crowds,
            'groundtruth_area': areas,
            'groundtruth_boxes': boxes,
        }
        if self._include_mask:
            decoded_tensors.update({
                'groundtruth_instance_masks':
                masks,
                'groundtruth_instance_masks_png':
                parsed_tensors['image/object/mask'],
            })
        return decoded_tensors
 def my_fn(ex):
   inputs = ex["inputs"]
   res = ex.copy()
   res["inputs"] = tf.where(
       tf.greater(inputs, 15),
       tf.constant(50, tf.int64),
       inputs)
   return res
 def serve_fn(raw_features):
     raw_features = tf.compat.v1.expand_dims(raw_features, axis=0)
     transformed_features = model.feature_ps(raw_features)
     outputs = model(transformed_features)
     outputs = tf.compat.v1.squeeze(outputs, axis=0)
     outputs = tf.cast(tf.greater(outputs, 0.5), tf.int64)
     decoded_outputs = model.label_inverse_lookup_layer(outputs)
     return tf.compat.v1.squeeze(decoded_outputs, axis=0)
Beispiel #19
0
  def _get_reset_state(self, observation, done, default_state):
    """Resets the state wherever marked in `done` tensor.

    Consider the following example with num_timesteps=2, batch_size=3,
    state_size=1:
      default_state (batch_size, state_size) = [[5.], [5.], [5.]]
      done (num_timesteps, batch_size) = [[True, True, False],
                                          [False, True, False]]
      observation (num_timesteps, batch_size, 1) = [[[1.], [2.], [3.]],
                                                    [[4.], [5.], [6.]]]
      self.get_initial_state implements `observation + 10`.
    then returned tensor will be of shape (num_timesteps, batch_size,
    state_size) and its value will be:
      [[[11.], [12.], [0.]],
       [[0.],  [15.], [0.]]]
    where state values are replaced by call to `self.get_initial_state` wherever
    done=True. Note that the state values where done=False are set to zeros and
    are expected not to be used by the caller.

    Args:
      observation: A nested structure with individual tensors that have first
        two dimensions equal to [num_timesteps, batch_size].
      done: A boolean tensor of shape  [num_timesteps, batch_size].
      default_state: A tensor or nested structure with individual tensors that
        have first dimension equal to batch_size and no time dimension.

    Returns:
      A structure similar to `default_state` except that all tensors in the
      returned structure have an additional leading dimension equal to
      num_timesteps.
    """
    reset_indices = tf.compat.v1.where(tf.equal(done, True))

    def _get_reset_state_indices():
      reset_indices_obs = tf.nest.map_structure(
          lambda t: tf.gather_nd(t, reset_indices), observation)
      # shape: [num_indices_to_reset, ...]
      reset_indices_state = self.get_initial_state(
          reset_indices_obs, batch_size=tf.shape(reset_indices)[0])
      # Scatter tensors in `reset_indices_state` to shape: [num_timesteps,
      # batch_size, ...]
      return tf.nest.map_structure(
          lambda reset_tensor: tf.scatter_nd(  
              indices=reset_indices,
              updates=reset_tensor,
              shape=done.shape.as_list() + reset_tensor.shape.as_list()[1:]),
          reset_indices_state)

    # A minor optimization wherein if all elements in `done` are False, we
    # simply return a structure with zeros tensors of correct shape.
    return tf.cond(
        tf.greater(tf.size(reset_indices), 0),
        _get_reset_state_indices,
        lambda: tf.nest.map_structure(  
            lambda t: tf.zeros(         
                shape=done.shape.as_list() + t.shape.as_list()[1:],
                dtype=t.dtype),
            default_state))
def _decode_areas(parsed_tensors):
    xmin = parsed_tensors['image/object/bbox/xmin']
    xmax = parsed_tensors['image/object/bbox/xmax']
    ymin = parsed_tensors['image/object/bbox/ymin']
    ymax = parsed_tensors['image/object/bbox/ymax']
    return tf.cond(
        tf.greater(tf.shape(parsed_tensors['image/object/area'])[0],
                   0), lambda: parsed_tensors['image/object/area'], lambda:
        (xmax - xmin) * (ymax - ymin))
Beispiel #21
0
    def __call__(self,
                 logits,
                 scaled_labels,
                 classes,
                 category_loss=True,
                 mse_loss=False):
        """Compute instance segmentation loss.

    Args:
      logits: A Tensor of shape [batch_size * num_points, height, width,
        num_classes]. The logits are not necessarily between 0 and 1.
      scaled_labels: A float16 Tensor of shape [batch_size, num_instances,
          mask_size, mask_size], where mask_size =
          mask_crop_size * gt_upsample_scale for fine mask, or mask_crop_size
          for coarse masks and shape priors.
      classes: A int tensor of shape [batch_size, num_instances].
      category_loss: use class specific mask prediction or not.
      mse_loss: use mean square error for mask loss or not

    Returns:
      mask_loss: an float tensor representing total mask classification loss.
      iou: a float tensor representing the IoU between target and prediction.
    """
        classes = tf.reshape(classes, [-1])
        _, _, height, width = scaled_labels.get_shape().as_list()
        scaled_labels = tf.reshape(scaled_labels, [-1, height, width])

        if not category_loss:
            logits = logits[:, :, :, 0]
        else:
            logits = tf.transpose(a=logits, perm=(0, 3, 1, 2))
            gather_idx = tf.stack(
                [tf.range(tf.size(input=classes)), classes - 1], axis=1)
            logits = tf.gather_nd(logits, gather_idx)

        # Ignore loss on empty mask targets.
        valid_labels = tf.reduce_any(input_tensor=tf.greater(scaled_labels, 0),
                                     axis=[1, 2])
        if mse_loss:
            # Logits are probabilities in the case of shape prior prediction.
            logits *= tf.reshape(tf.cast(valid_labels, logits.dtype),
                                 [-1, 1, 1])
            weighted_loss = tf.nn.l2_loss(scaled_labels - logits)
            probs = logits
        else:
            weighted_loss = tf.nn.sigmoid_cross_entropy_with_logits(
                labels=scaled_labels, logits=logits)
            probs = tf.sigmoid(logits)
            weighted_loss *= tf.reshape(
                tf.cast(valid_labels, weighted_loss.dtype), [-1, 1, 1])

        iou = tf.reduce_sum(
            input_tensor=tf.minimum(scaled_labels, probs)) / tf.reduce_sum(
                input_tensor=tf.maximum(scaled_labels, probs))
        mask_loss = tf.reduce_sum(input_tensor=weighted_loss) / tf.reduce_sum(
            input_tensor=scaled_labels)
        return tf.cast(mask_loss, tf.float32), tf.cast(iou, tf.float32)
Beispiel #22
0
def _do_scale(image, size):
  """Rescale the image by scaling the smaller spatial dimension to `size`."""
  shape = tf.cast(tf.shape(image), tf.float32)
  w_greater = tf.greater(shape[0], shape[1])
  shape = tf.cond(w_greater,
                  lambda: tf.cast([shape[0] / shape[1] * size, size], tf.int32),
                  lambda: tf.cast([size, shape[1] / shape[0] * size], tf.int32))

  return tf.image.resize([image], shape, method='bicubic')[0]
Beispiel #23
0
    def matched_column_indices(self):
        """Returns column indices that match to some row.

    The indices returned by this op are always sorted in increasing order.

    Returns:
      column_indices: int32 tensor of shape [K] with column indices.
    """
        return self._reshape_and_cast(
            tf.where(tf.greater(self._match_results, -1)))
Beispiel #24
0
    def unmatched_or_ignored_column_indices(self):
        """Returns column indices that are unmatched or ignored.

    The indices returned by this op are always sorted in increasing order.

    Returns:
      column_indices: int32 tensor of shape [K] with column indices.
    """
        return self._reshape_and_cast(
            tf.where(tf.greater(0, self._match_results)))
  def _get_rpn_samples(self, match_results):
    """Computes anchor labels.

    This function performs subsampling for foreground (fg) and background (bg)
    anchors.
    Args:
      match_results: A integer tensor with shape [N] representing the
        matching results of anchors. (1) match_results[i]>=0,
        meaning that column i is matched with row match_results[i].
        (2) match_results[i]=-1, meaning that column i is not matched.
        (3) match_results[i]=-2, meaning that column i is ignored.
    Returns:
      score_targets: a integer tensor with the a shape of [N].
        (1) score_targets[i]=1, the anchor is a positive sample.
        (2) score_targets[i]=0, negative. (3) score_targets[i]=-1, the anchor is
        don't care (ignore).
    """
    sampler = (
        balanced_positive_negative_sampler.BalancedPositiveNegativeSampler(
            positive_fraction=self._rpn_fg_fraction, is_static=False))
    # indicator includes both positive and negative labels.
    # labels includes only positives labels.
    # positives = indicator & labels.
    # negatives = indicator & !labels.
    # ignore = !indicator.
    indicator = tf.greater(match_results, -2)
    labels = tf.greater(match_results, -1)

    samples = sampler.subsample(
        indicator, self._rpn_batch_size_per_im, labels)
    positive_labels = tf.where(
        tf.logical_and(samples, labels),
        tf.constant(2, dtype=tf.int32, shape=match_results.shape),
        tf.constant(0, dtype=tf.int32, shape=match_results.shape))
    negative_labels = tf.where(
        tf.logical_and(samples, tf.logical_not(labels)),
        tf.constant(1, dtype=tf.int32, shape=match_results.shape),
        tf.constant(0, dtype=tf.int32, shape=match_results.shape))
    ignore_labels = tf.fill(match_results.shape, -1)

    return (ignore_labels + positive_labels + negative_labels,
            positive_labels, negative_labels)
Beispiel #26
0
 def length_norm_fn(log_probs_BxM, length_int):
   """Normalize sum log probabilities given a sequence length."""
   dtype = log_probs_BxM.dtype
   norm_flt = tf.pow(((start + tf.cast(length_int, dtype)) / (1. + start)),
                     alpha)
   log_probs_BxM /= norm_flt
   too_short_bool = tf.less(length_int, min_len)
   too_long_bool = tf.logical_and(tf.greater(length_int, max_len), max_len > 0)
   out_of_range_bool = tf.logical_or(too_long_bool, too_short_bool)
   log_probs_BxM += out_of_range_penalty * tf.cast(out_of_range_bool, dtype)
   return log_probs_BxM
Beispiel #27
0
def _ndtr(x):
    """Implements ndtr core logic."""
    half_sqrt_2 = tf.constant(0.5 * np.sqrt(2.),
                              dtype=x.dtype,
                              name="half_sqrt_2")
    w = x * half_sqrt_2
    z = tf.abs(w)
    y = tf1.where(
        tf.less(z, half_sqrt_2), 1. + tf.math.erf(w),
        tf1.where(tf.greater(w, 0.), 2. - tf.math.erfc(z), tf.math.erfc(z)))
    return 0.5 * y
Beispiel #28
0
    def call(self, inputs, count_weights=None):
        if isinstance(inputs, (list, np.ndarray)):
            inputs = tf.convert_to_tensor(inputs)

        def expand_dims(inputs, axis):
            if tf_utils.is_sparse(inputs):
                return tf.sparse.expand_dims(inputs, axis)
            else:
                return tf.compat.v1.expand_dims(inputs, axis)

        original_shape = inputs.shape
        # In all cases, we should uprank scalar input to a single sample.
        if inputs.shape.rank == 0:
            inputs = expand_dims(inputs, -1)
        # One hot will unprank only if the final output dimension is not already 1.
        if self.output_mode == ONE_HOT:
            if inputs.shape[-1] != 1:
                inputs = expand_dims(inputs, -1)

        # TODO(b/190445202): remove output rank restriction.
        if inputs.shape.rank > 2:
            raise ValueError(
                "Received input shape {}, which would result in output rank {}. "
                "Currently only outputs up to rank 2 are supported.".format(
                    original_shape, inputs.shape.rank))

        if count_weights is not None and self.output_mode != COUNT:
            raise ValueError(
                "`count_weights` is not used when `output_mode` is not `'count'`. "
                "Received `count_weights={}`.".format(count_weights))

        out_depth = self.num_tokens
        binary_output = self.output_mode in (MULTI_HOT, ONE_HOT)
        if isinstance(inputs, tf.SparseTensor):
            max_value = tf.reduce_max(inputs.values)
            min_value = tf.reduce_min(inputs.values)
        else:
            max_value = tf.reduce_max(inputs)
            min_value = tf.reduce_min(inputs)
        condition = tf.logical_and(
            tf.greater(tf.cast(out_depth, max_value.dtype), max_value),
            tf.greater_equal(min_value, tf.cast(0, min_value.dtype)))
        assertion = tf.Assert(condition, [
            "Input values must be in the range 0 <= values < num_tokens"
            " with num_tokens={}".format(out_depth)
        ])
        with tf.control_dependencies([assertion]):
            if self.sparse:
                return sparse_bincount(inputs, out_depth, binary_output,
                                       count_weights)
            else:
                return dense_bincount(inputs, out_depth, binary_output,
                                      count_weights)
Beispiel #29
0
        def step_fn(inputs):
          """The computation to run on each worker."""
          features, labels = inputs
          with tf.GradientTape() as tape:
            pred = model(features, training=True)
            loss = keras.losses.binary_crossentropy(labels, pred)
            loss = tf.nn.compute_average_loss(loss)
          grads = tape.gradient(loss, model.trainable_variables)
          optimizer.apply_gradients(list(zip(grads, model.trainable_variables)))

          actual_pred = tf.cast(tf.greater(pred, 0.5), tf.int64)
          accuracy.update_state(labels, actual_pred)
      def replica_fn(iterator):
        batch_data, labels = next(iterator)
        with tf.GradientTape() as tape:
          pred = model(batch_data, training=True)
          loss = tf.nn.compute_average_loss(
              keras.losses.BinaryCrossentropy(
                  reduction=losses_utils.ReductionV2.NONE)(labels, pred))
          gradients = tape.gradient(loss, model.trainable_variables)

        optimizer.apply_gradients(zip(gradients, model.trainable_variables))

        actual_pred = tf.cast(tf.greater(pred, 0.5), tf.int64)
        accuracy.update_state(labels, actual_pred)