# Why should I use data augmentation as Keras layer

I've seen in several Tensorflow/Keras tutorials that data augmentation functions are added as keras layers. When I converted my Keras Python model (for production purpose) to TensorflowJS I faced the issue that e.g. the RandomFlip layer is not available in TensorflowJS. So I have to use the ImageDataGenerator.

I don't understand why should I put the data augmentation layers at all in the model, I mean when I want to use my model in production I'll still have them in my model which doesn't make sense for me.

You don't need that at Inference time. It's for training purposes.
You can skip these Layers while exporting to JavsScript. These layers do not have weights.

On this example [Link], which has these Layers, I removed these layers and it worked fine.

# Added this code to remove the first 4 Aug layers

input = model.input
model_export = input

for layer in model.layers[4:]:
model_export = layer(model_export)

model_export = keras.Model(input, model_export)



Prediction output - This image most likely belongs to sunflowers with a 100.00 percent confidence.

### Model After

• thx, I don't know why almost all TF/Keras tutorials are using them as layers... they should promote more the ImageDataGenerator :) – Joba Nov 26 '20 at 14:56

Data Augmentation is basically a technique to increase the diversity of your training set by applying random (but realistic) transformations such as image rotation. Training deep learning neural network models on more data can result in more skillful models, and the augmentation techniques can create variations of the images that can improve the ability of the fit models to generalize what they have learned to new images. So you can decrease the rate of overfitting or take some precautions for it.

The Keras deep learning neural network library provides the capability to fit models using image data augmentation via the ImageDataGenerator class. By using this class you are able to modify the dataset by using its a lot of functionality.

• thx for your answere, I'm aware of the meaning and use of data augmentation. My problem was to understand why keras wants me to add this functionality as layer instead having it only for development time in trainings. :) – Joba Nov 26 '20 at 14:54