Is there possible to set a possibility when doing random flip operations by using tf.keras.layers.RandomFlip ?
for example:
def augmentation():
data_augmentation = keras.Sequential([
keras.layers.RandomFlip("horizontal", p=0.5),
keras.layers.RandomRotation(0.2, p=0.5)
])
return data_augmentation
CodePudding user response:
Try creating a simple Lambda
layer and defining your probability in a separate function:
import random
def random_flip_on_probability(image, probability= 0.5):
if random.random() < probability:
return tf.image.random_flip_left_right(image)
return image
def augmentation():
data_augmentation = keras.Sequential([
keras.layers.Lambda(random_flip_on_probability),
keras.layers.RandomRotation(0.2, p=0.5)
])
return data_augmentation
If you need to use data augmentation during training or inference, you will have to define your own custom layer. Try something like this:
class RandomFlipOnProbability(tf.keras.layers.Layer):
def __init__(self, probability):
super(MyDenseLayer, self).__init__()
self.probability = probability
def call(self, images):
if random.random() < self.probability:
return tf.image.flip_left_right(images)
return images