0
votes

I know that ImageDataGenerator generates for each input image one image randomly augmented . Now, I would like to generate for each input image two augmented images :

datagen = tf.keras.preprocessing.image.ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest')
train_ds = datagen.flow_from_directory('/home/train/')

To explain more, I would like to apply 2 distinct augmentation functions on the same image, i.e, if we sample 5 images, we end up with 2 × 5 = 10 augmented observations in the batch

So how I can proceed please ?

1

1 Answers

0
votes

I would recommend creating a custom data generator that inherits from tf.keras.utils.Sequence. There are a number of ways to go about this, but this should be along the lines of what you are looking for:

class double_aug_generator(tf.keras.utils.Sequence):
    def __init__(self, x, y, batch_size, aug_params1, aug_params2):
        self.x, self.y = x, y
        self.batch_size = batch_size
        self.datagen = tf.keras.preprocessing.image.ImageDataGenerator(**aug_params1)
        
        // dictionary of parameters for the second augmentation
        self.aug_params2 = aug_params2

    def __len__(self):
        return math.ceil(len(self.x) / self.batch_size)
    
    def load(self, file_names):
        // load and return raw images however you like

    def __getitem__(self, idx):
        batch_x = self.x[idx * self.batch_size:(idx + 1) *
        self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) *
        self.batch_size]
        
        // load images
        batch_x = self.load(batch_x)
        
        // apply first augmentation
        batch_x = self.datagen.flow(batch_x)
        
        // apply second
        batch_x = self.datagen.apply_transform(batch_x, self.aug_params2)
        
        return batch_x, np.array(batch_y)