Skip to content

Instantly share code, notes, and snippets.

@AdityaKane2001
Created January 15, 2022 16:14
Show Gist options
  • Save AdityaKane2001/95448dbeb74326fa11afd2b3d84c34de to your computer and use it in GitHub Desktop.
Save AdityaKane2001/95448dbeb74326fa11afd2b3d84c34de to your computer and use it in GitHub Desktop.
def _pca_jitter(self, image, target):
"""
Applies PCA jitter to images.
Args:
image: Batch of images to perform random rotation on.
target: Target tensor.
Returns:
Augmented example with batch of images and targets with same dimensions.
"""
aug_images = tf.cast(image, tf.float32) / 255.
alpha = tf.random.normal((self.batch_size, 3), stddev=0.1)
alpha = tf.stack([alpha, alpha, alpha], axis=1)
rgb = tf.math.reduce_sum(
alpha * self.eigen_vals * self.eigen_vecs, axis=2)
rgb = tf.expand_dims(rgb, axis=1)
rgb = tf.expand_dims(rgb, axis=1)
aug_images = aug_images + rgb
aug_images = aug_images * 255.
aug_images = tf.cast(tf.clip_by_value(aug_images, 0, 255), tf.uint8)
return aug_images, target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment