I have used keras to implement the cutmix augmentation.
Dataset used: https://www.kaggle.com/competitions/dogs-vs-cats/data.
In CutMix we mix two images and their labels respectively in order to generate new data point. To implement this, I have created a custom datagenerator. This custom datagenerator takes batches from two seperate keras datagenerator and returns one batch by applying cutmix on it.
train datagenerator (custom)
train_dataset = CutMixImageDataGenerator(
generator1= train_datagen1, #train_generator1,
generator2= train_datagen2, #train_generator2,
img_size=128,
batch_size=32,
)
valid datagenerator (default)
valid_dataset = valid_datagen.flow_from_dataframe(
dataframe=validate_df,
directory=image_path,
target_size=(128, 128),
x_col='filename',
y_col='category',
batch_size=32,
seed=42,
shuffle=True,
class_mode="categorical",
)
We have two notebooks 1. Without CutMix
and 2. With CutMix
. Both notebooks have everything exactly same(seed, batchsize, #epochs, rescaling etc
) only except the training datagenerator. And we notice that using CutMix augmentation has really improved the validation score
making the model more regularized and robust in comparision to model which is trained without using CutMix augmentation.
- original paper: https://arxiv.org/abs/1905.04899