def embedding_centers_to_logits(embedding,
                                centers,
                                similarity_strategy,
                                max_value=10.0):
    """logits from embedding and centers based on distance to centers.

  Args:
    embedding: A tf.float32 tensor of size [N, embedding_size].
    centers: A tf.float32 tensor of size [k, embedding_size].
    similarity_strategy: Strategy for computing the similarity between
                         embedding vectors.
    max_value: Maximum absolute value of any dimension of the logits.

  Returns:
    A tf.float32 tensor of [k, N] that contains k logit maps.

  Raises:
    ValueError: If similarity_strategy is unknown.
  """
    if similarity_strategy == 'dotproduct':
        logits = tf.matmul(centers, embedding, transpose_b=True)
    elif similarity_strategy == 'distance':
        logits = -instance_segmentation_utils.inputs_distances_to_centers(
            centers, embedding)
    else:
        raise ValueError('Similarity strategy is unknown')
    return tf.minimum(tf.maximum(logits, -max_value), max_value)
Exemple #2
0
  def body(i, init_samples, indices, min_distances):
    """while loop body. Pick the next center given previously picked centers.

    Args:
      i: For loop index.
      init_samples: Initial samples that is modified inside the body.
      indices: Indices of picked samples.
      min_distances: Minimum distance of the embedding to centers so far.

    Returns:
      i and init_centers.
    """
    best_new_sample_ind = tf.cast(
        tf.math.argmax(
            scores_coef * log_scores +
            tf.math.log(min_distances + sys.float_info.min),
            axis=0),
        dtype=tf.int32)
    indices += tf.pad(tf.stack([best_new_sample_ind]),
                      paddings=[[i, num_samples-i-1]])
    init_samples_i = tf.expand_dims(tf.gather(inputs, best_new_sample_ind), 0)
    init_samples += tf.pad(init_samples_i,
                           paddings=tf.stack([tf.stack([i, num_samples-i-1]),
                                              tf.stack([0, 0])]))
    min_distances = tf.minimum(
        tf.squeeze(
            instance_segmentation_utils.inputs_distances_to_centers(
                inputs, init_samples_i),
            axis=1), min_distances)
    i += 1
    return i, init_samples, indices, min_distances
Exemple #3
0
 def test_inputs_Distances_to_centers(self):
   inputs = tf.random.uniform(
       [100, 8], minval=-10, maxval=10.0, dtype=tf.float32)
   centers = tf.random.uniform(
       [5, 8], minval=-10, maxval=10.0, dtype=tf.float32)
   distances1 = isu.inputs_distances_to_centers(inputs, centers)
   num_centers = tf.shape(centers)[0]
   inputs_reshaped = tf.tile(tf.expand_dims(inputs, axis=1),
                             tf.stack([1, num_centers, 1]))
   distances2 = tf.reduce_sum(tf.square(inputs_reshaped - centers), axis=2)
   self.assertAllClose(distances1.numpy(), distances2.numpy(), atol=0.001)
def embedding_centers_to_soft_masks_by_distance(embedding, centers):
    """Masks from embedding and centers based on distance to centers.

  Args:
    embedding: A tf.float32 tensor of size [N, embedding_size].
    centers: A tf.float32 tensor of size [k, embedding_size].

  Returns:
    A tf.float32 tensor of [k, N] that contains k soft masks.
  """
    dists_to_centers = instance_segmentation_utils.inputs_distances_to_centers(
        centers, embedding)
    return tf.nn.sigmoid(-dists_to_centers) * 2.0
def embedding_centers_to_soft_masks_by_distance2(embedding, centers):
    """Masks from embedding and centers based on distance to centers.

  Args:
    embedding: A tf.float32 tensor of size [N, embedding_size].
    centers: A tf.float32 tensor of size [k, embedding_size].

  Returns:
    A tf.float32 tensor of [k, N] that contains k soft masks.
  """
    max_dist = 100.0
    dists_to_centers = instance_segmentation_utils.inputs_distances_to_centers(
        embedding, centers)
    return tf.transpose(
        tf.minimum(tf.maximum((max_dist - dists_to_centers) / max_dist, 0.0),
                   1.0))
Exemple #6
0
def sample_based_on_scores_and_distances(inputs,
                                         scores,
                                         num_samples,
                                         scores_coef):
  """Iteratively samples points based on distances and scores.

  Each point i's current total score is computed as
      total_score[i] = scores_coef * log(scores[i]) +
                       log(dist(i, closest previously picked point))

  Args:
    inputs: A tf.float32 tensor of size [N, dims] containing the input vectors.
    scores: A tf.float32 tensor of size [N] containing the scores. Scores
            should be all positive.
    num_samples: A tf.int32 scalar defining the number of samples.
    scores_coef: A tf.float32 coeffcieint that determines that weight assigned
                 to scores in comparison to distances.

  Returns:
    input_samples: The input vectors that are sampled. A tf.float32 tensor of
                   size [num_samples, dims].
    indices: A tf.int32 tensor of size [num_samples] containing the indices of
             the sampled points.
  """
  log_scores = tf.math.log(scores + sys.float_info.min)
  dims = tf.shape(inputs)[1]
  init_samples = tf.zeros(tf.stack([num_samples, dims]), dtype=tf.float32)
  indices = tf.zeros([num_samples], dtype=tf.int32)
  index_0 = tf.expand_dims(
      tf.cast(tf.math.argmax(scores, axis=0), dtype=tf.int32), axis=0)
  init_sample_0 = tf.gather(inputs, index_0)
  min_distances = tf.squeeze(
      instance_segmentation_utils.inputs_distances_to_centers(
          inputs, init_sample_0),
      axis=1)
  init_samples += tf.pad(
      init_sample_0,
      paddings=tf.stack([tf.stack([0, num_samples-1]), tf.stack([0, 0])]))
  indices += tf.pad(index_0, paddings=[[0, num_samples-1]])
  i = tf.constant(1, dtype=tf.int32)

  def body(i, init_samples, indices, min_distances):
    """while loop body. Pick the next center given previously picked centers.

    Args:
      i: For loop index.
      init_samples: Initial samples that is modified inside the body.
      indices: Indices of picked samples.
      min_distances: Minimum distance of the embedding to centers so far.

    Returns:
      i and init_centers.
    """
    best_new_sample_ind = tf.cast(
        tf.math.argmax(
            scores_coef * log_scores +
            tf.math.log(min_distances + sys.float_info.min),
            axis=0),
        dtype=tf.int32)
    indices += tf.pad(tf.stack([best_new_sample_ind]),
                      paddings=[[i, num_samples-i-1]])
    init_samples_i = tf.expand_dims(tf.gather(inputs, best_new_sample_ind), 0)
    init_samples += tf.pad(init_samples_i,
                           paddings=tf.stack([tf.stack([i, num_samples-i-1]),
                                              tf.stack([0, 0])]))
    min_distances = tf.minimum(
        tf.squeeze(
            instance_segmentation_utils.inputs_distances_to_centers(
                inputs, init_samples_i),
            axis=1), min_distances)
    i += 1
    return i, init_samples, indices, min_distances

  (i, init_samples, indices, min_distances) = tf.while_loop(
      lambda i, init_samples, indices, min_distances: i < num_samples,
      body,
      [i, init_samples, indices, min_distances])
  return init_samples, indices