Exemplo n.º 1
0
def ApplyDepthImageDistortions(depth_images,
                               random_noise_level=0.05,
                               random_noise_apply_probability=0.5,
                               scaling_noise=True,
                               gamma_shape=1000.0,
                               gamma_scale_inverse=1000.0,
                               min_depth_allowed=0.25,
                               max_depth_allowed=2.5):
    """Apply photometric distortions to the input depth images.

  Args:
    depth_images: Tensor of shape [batch_size, h, w, 1] containing a batch of
      depth images to apply the random photometric distortions to.
    random_noise_level: The standard deviation of the Gaussian distribution for
      the noise that is applied to the depth image. When 0.0, then no noise is
      applied.
    random_noise_apply_probability: Probability of applying additive random
      noise to the images.
    scaling_noise: If True; sample a random variable from a Gamma distribution
      to scale the depth image.
    gamma_shape: Float; shape parameter of a Gamma distribution.
    gamma_scale_inverse: Float; inverse of scale parameter of a Gamma
      distribution.
    min_depth_allowed: Float; minimum clip value for depth.
    max_depth_allowed: Float; max clip value for depth.

  Returns:
    depth_images: Tensor of shape [batch_size, h, w, 1] containing a
      batch of images resulting from applying random photometric distortions to
      the inputs.
  """
    assert depth_images[0].get_shape().as_list()[-1] == 1
    with tf.variable_scope('distortions_depth_images'):
        # Add random Gaussian noise.
        if random_noise_level:
            for i, image in enumerate(depth_images):
                img_shape = tf.shape(image)
                rnd_noise = tf.random_normal(img_shape,
                                             stddev=random_noise_level)

                def ReturnImageTensor(value):
                    return lambda: value

                if scaling_noise:
                    alpha = tf.random_gamma([], gamma_shape,
                                            gamma_scale_inverse)
                image = tf.cond(
                    tf.reduce_all(
                        tf.greater(tf.random.uniform([1]),
                                   random_noise_apply_probability)),
                    ReturnImageTensor(image),
                    ReturnImageTensor(alpha * image + rnd_noise))
                depth_images[i] = tf.reshape(image, img_shape)

        # Clip to valid range.
        for i, image in enumerate(depth_images):
            depth_images[i] = tf.clip_by_value(image, min_depth_allowed,
                                               max_depth_allowed)
    return depth_images
Exemplo n.º 2
0
def random_exponential(shape, rate=1.0, dtype=tf.float32, seed=None):
    """
  Helper function to sample from the exponential distribution, which is not
  included in core TensorFlow.
  """
    return tf.random_gamma(shape,
                           alpha=1,
                           beta=1. / rate,
                           dtype=dtype,
                           seed=seed)