Question

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.

Was it helpful?

Solution

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 before (Inital Layers)

enter image description here

Model After

enter image description here

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with datascience.stackexchange
scroll top