title: Agumenting data in keras using albumentations
body: <p>I am trying to train a keras <code>ResNet50</code> model for image classification model using a <a href="https://www.kaggle.com/suniliitb96/tutorial-keras-transfer-learning-with-resnet50" rel="nofollow noreferrer">tutorial</a>. Instead of the inbuilt data generator, I want to use <a href="https://albumentations.ai" rel="nofollow noreferrer">albumentations</a> library for augmentation.</p>
  <pre><code>from albumentations import Compose
  transforms = Compose([HorizontalFlip()])
  </code></pre>
  <p>I have read a few articles, but I could not figure out how to implement albumentations.</p>
  <p>Which line of code should I modify to implement albumentations.<br />
  I am reproducing the code below after removing non necessary lines.</p>
  <pre><code>NUM_CLASSES  = 2
  CHANNELS     = 3
  IMAGE_RESIZE = 224
  
  RESNET50_POOLING_AVERAGE = 'avg'
  DENSE_LAYER_ACTIVATION   = 'softmax'
  OBJECTIVE_FUNCTION       = 'categorical_crossentropy'
  LOSS_METRICS             = ['accuracy']
  
  NUM_EPOCHS = 300
  EARLY_STOP_PATIENCE = 20
  
  STEPS_PER_EPOCH_TRAINING = 20
  STEPS_PER_EPOCH_VALIDATION = 20
  
  BATCH_SIZE_TRAINING = 10
  BATCH_SIZE_VALIDATION = 10
  
  # %% ---------------------------------------------------------------------
  TrainingData_directory   = 'C:/datafolder/Train'
  ValidationData_directory = 'C:/datafolder/Validation'
  ModelCheckpointPath      = 'C:/datafolder/ResNet50_Weights.hdf5'
  # %% ---------------------------------------------------------------------
  from albumentations import Compose
  import tensorflow as tf
  from tensorflow.keras.applications import ResNet50
  from tensorflow.keras.models import Sequential
  from tensorflow.keras.layers import Dense
  
  # %%  ---------------------------------------------------------------------
  model = Sequential()
  model.add(ResNet50(include_top = False, pooling = RESNET50_POOLING_AVERAGE, weights = 'imagenet'))
  model.add(Dense(NUM_CLASSES, activation = DENSE_LAYER_ACTIVATION))
  model.layers[0].trainable = False
  
  
  from tensorflow.keras import optimizers
  sgd = optimizers.SGD(lr = 0.001, decay = 1e-6, momentum = 0.9, nesterov = True)
  model.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)
  
  from keras.applications.resnet50 import preprocess_input
  from keras.preprocessing.image import ImageDataGenerator
  
  image_size = IMAGE_RESIZE
  
  data_generator = ImageDataGenerator(preprocessing_function = preprocess_input)
  
  train_generator = data_generator.flow_from_directory(TrainingData_directory,
          target_size = (image_size, image_size),
          batch_size = BATCH_SIZE_TRAINING,
          class_mode = 'categorical')
  
  validation_generator = data_generator.flow_from_directory(ValidationData_directory,
          target_size = (image_size, image_size),
          batch_size = BATCH_SIZE_VALIDATION,
          class_mode = 'categorical')
  
  from tensorflow.python.keras.callbacks import EarlyStopping, ModelCheckpoint
  
  cb_early_stopper = EarlyStopping(monitor = 'val_loss', patience = EARLY_STOP_PATIENCE)
  cb_checkpointer = ModelCheckpoint(filepath = ModelCheckpointPath,
                                    monitor = 'val_loss', save_best_only = True, mode = 'auto')
  
  fit_history = model.fit_generator(
          train_generator,
          steps_per_epoch=STEPS_PER_EPOCH_TRAINING,
          epochs = NUM_EPOCHS,
          validation_data=validation_generator,
          validation_steps=STEPS_PER_EPOCH_VALIDATION,
          callbacks=[cb_checkpointer, cb_early_stopper]
  )
  </code></pre>