Skip to content

Instantly share code, notes, and snippets.

@AdityaKane2001
Created January 15, 2022 16:13
Show Gist options
  • Save AdityaKane2001/afcfc05ecac2bfaacfebd41e686c0c0f to your computer and use it in GitHub Desktop.
Save AdityaKane2001/afcfc05ecac2bfaacfebd41e686c0c0f to your computer and use it in GitHub Desktop.
def _mixup(self, image, label, alpha=0.2) -> Tuple:
"""
Function to apply mixup augmentation. To be applied after
one hot encoding and before batching.
Args:
entry1: Entry from first dataset. Should be one hot encoded and batched.
entry2: Entry from second dataset. Must be one hot encoded and batched.
Returns:
Tuple with same structure as the entries.
"""
image1, label1 = image, label
image2, label2 = tf.reverse(
image, axis=[0]), tf.reverse(label, axis=[0])
image1 = tf.cast(image1, tf.float32)
image2 = tf.cast(image2, tf.float32)
alpha = [alpha]
dist = tfd.Beta(alpha, alpha)
l = dist.sample(1)[0][0]
img = l * image1 + (1 - l) * image2
lab = l * label1 + (1 - l) * label2
img = tf.cast(tf.math.round(tf.image.resize(
img, (self.crop_size, self.crop_size))), tf.uint8)
return img, lab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment