示例#1
0
def randomly_crop_points(mesh_inputs,
                         view_indices_2d_inputs,
                         x_random_crop_size,
                         y_random_crop_size,
                         epsilon=1e-5):
  """Randomly crops points.

  Args:
    mesh_inputs: A dictionary containing input mesh (point) tensors.
    view_indices_2d_inputs: A dictionary containing input point to view
      correspondence tensors.
    x_random_crop_size: Size of the random crop in x dimension. If None, random
      crop will not take place on x dimension.
    y_random_crop_size: Size of the random crop in y dimension. If None, random
      crop will not take place on y dimension.
    epsilon: Epsilon (a very small value) used to add as a small margin to
      thresholds.
  """
  if x_random_crop_size is None and y_random_crop_size is None:
    return

  points = mesh_inputs[standard_fields.InputDataFields.point_positions]
  num_points = tf.shape(points)[0]
  # Pick a random point
  if x_random_crop_size is not None or y_random_crop_size is not None:
    random_index = tf.random.uniform([],
                                     minval=0,
                                     maxval=num_points,
                                     dtype=tf.int32)
    center_x = points[random_index, 0]
    center_y = points[random_index, 1]

  points_x = points[:, 0]
  points_y = points[:, 1]
  min_x = tf.reduce_min(points_x) - epsilon
  max_x = tf.reduce_max(points_x) + epsilon
  min_y = tf.reduce_min(points_y) - epsilon
  max_y = tf.reduce_max(points_y) + epsilon

  if x_random_crop_size is not None:
    min_x = center_x - x_random_crop_size / 2.0 - epsilon
    max_x = center_x + x_random_crop_size / 2.0 + epsilon

  if y_random_crop_size is not None:
    min_y = center_y - y_random_crop_size / 2.0 - epsilon
    max_y = center_y + y_random_crop_size / 2.0 + epsilon

  x_mask = tf.logical_and(tf.greater(points_x, min_x), tf.less(points_x, max_x))
  y_mask = tf.logical_and(tf.greater(points_y, min_y), tf.less(points_y, max_y))
  points_mask = tf.logical_and(x_mask, y_mask)

  for key in sorted(mesh_inputs):
    mesh_inputs[key] = tf.boolean_mask(mesh_inputs[key], points_mask)

  for key in sorted(view_indices_2d_inputs):
    view_indices_2d_inputs[key] = tf.transpose(
        tf.boolean_mask(
            tf.transpose(view_indices_2d_inputs[key], [1, 0, 2]), points_mask),
        [1, 0, 2])
示例#2
0
def _points_to_voxel_indices(points, grid_cell_size):
  """Converts points into corresponding voxel indices.

  Maps each point into a voxel grid with cell size given by grid_cell_size.
  For each voxel, it computes a x, y, z index. Also converts the x, y, z index
  to a single number index where there is a one-on-one mapping between
  each x, y, z index value and its corresponding single number index value.

  Args:
    points: A tf.float32 tensor of size [N, 3].
    grid_cell_size: The size of the grid cells in x, y, z dimensions in the
      voxel grid. It should be either a tf.float32 tensor, a numpy array or a
      list of size [3].

  Returns:
    voxel_xyz_indices: A tf.int32 tensor of size [N, 3] containing the x, y, z
      index of the voxel corresponding to each given point.
    voxel_single_number_indices: A tf.int32 tensor of size [N] containing the
      single number index of the voxel corresponding to each given point.
    voxel_start_location: A tf.float32 tensor of size [3] containing the start
      location of the voxels.
  """
  voxel_start_location = tf.reduce_min(points, axis=0)
  voxel_xyz_indices = tf.cast(
      tf.math.floordiv(points - voxel_start_location, grid_cell_size),
      dtype=tf.int32)
  voxel_xyz_indices, voxel_single_number_indices = compute_pooled_voxel_indices(
      voxel_xyz_indices=voxel_xyz_indices, pooling_size=(1, 1, 1))
  return voxel_xyz_indices, voxel_single_number_indices, voxel_start_location
示例#3
0
def _variable_summaries(var):
    """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev', stddev)
        tf.summary.scalar('max', tf.reduce_max(var))
        tf.summary.scalar('min', tf.reduce_min(var))
        tf.summary.histogram('histogram', var)
def summarize_stats(stats):
  """Summarize a dictionary of variables.

  Args:
    stats: a dictionary of {name: tensor} to compute stats over.
  """
  for name, stat in stats.items():
    mean = tf.reduce_mean(stat)
    tf.summary.scalar('mean_%s' % name, mean)
    tf.summary.scalar('max_%s' % name, tf.reduce_max(stat))
    tf.summary.scalar('min_%s' % name, tf.reduce_min(stat))
    std = tf.sqrt(tf.reduce_mean(tf.square(stat)) - tf.square(mean) + 1e-10)
    tf.summary.scalar('std_%s' % name, std)
    tf.summary.histogram(name, stat)
示例#5
0
def _points_offset_in_voxels_unbatched(points, grid_cell_size):
  """Converts points into offsets in voxel grid for a single batch.

  The values range from -0.5 to 0.5

  Args:
    points: A tf.float32 tensor of size [N, 3].
    grid_cell_size: The size of the grid cells in x, y, z dimensions in the
      voxel grid. It should be either a tf.float32 tensor, a numpy array or a
      list of size [3].

  Returns:
    voxel_xyz_offsets: A tf.float32 tensor of size [N, 3].
  """
  min_points = tf.reduce_min(points, axis=0)
  points_index = tf.math.floordiv(points - min_points, grid_cell_size)
  points_offset = points - min_points - (points_index * grid_cell_size)
  return (points_offset / grid_cell_size) - 0.5
 def argmax(v, mask):
     return tf.argmax(input=(v - tf.reduce_min(input_tensor=v) + 1) * mask,
                      axis=0)
示例#7
0
 def argmax(v, mask):
     return tf.argmax((v - tf.reduce_min(v) + 1) * mask, axis=0)