Skip to content
Advertisement

Rotate image for data augmentation using tf keras only in specific angles

In tf keras, it is possible to have a data augmentation layer that performs rotation on each given image during training, in the following way as the docs say:

JavaScript

The factor argument indicates the value of maximum rotation if a float is given and indicates lower and upper limits if a tuple is given.

For my specific application only specific rotations are allowed, say 0°, 90°, 180° and 270°.

Is there any way I can achieve this using the RandomRotation class or a good alternative to this or should I just augment the whole dataset before training?

Advertisement

Answer

You can do this by creating a custom PreprocessingLayer.

JavaScript

One thing to consider, if the inputs’ height and width are not the same, in other words they are not square you need to define input_shape as (None, None, channels) while creating the model.

Examples:

JavaScript

enter image description here

With training = False, they remain the same so this layer is not active during inference.

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement