pythontensorflowkerasdata-augmentation

Keras data augmentation layers in model or out of model


How exactly do the preprocessing layers in keras work, especially in the context of as a part of the model itself? This being compared to preprocessing being applied outside the model then inputting the results for training.

I'm trying to understand running data augmentation in keras models. Lets say I have 1000 images for training. Out of model I can apply augmentation 10x and get 10000 resultant images for training.

But I don't understand what's happening when you use a preprocess layer for augmentation. Does this (or these if you use many) layers take each image and apply the transformations before training? Does this mean the total number of images used for training (and validation I assume) to be the number of epochs*the original number of images?

Is one option better than the other? Does that depend on the number of images one originally has before augmentation?


Solution

  • The benefit of preprocessing layers is that the model is truly end-to-end, i.e. raw data comes in and a prediction comes out. It makes your model portable since the preprocessing procedure is included in the SavedModel.

    However, it will run everything on the GPU. Usually it makes sense to load the data using CPU worker(s) in the background while the GPU optimizes the model.

    Alternatively, you could use a preprocessing layer outside of the model and inside a Dataset. The benefit of that is that you can easily create an inference-only model including the layers, which then gives you the portability at inference time but still the speedup during training.

    For more information, see the Keras guide.