In the previous blog, we discussed how to generate random parameters for a transformation. In this blog, we will discuss how to apply a random transformation to an image.
Keras API
1 |
random_transform(x, seed=None) |
This function returns a randomly transformed version of the input image x.
How this method works?
- First of all, this generates random parameters for a transformation using the “get_random_transform” method. For more details, refer to this blog.
- Then the image is transformed according to the parameters (generated above) using the “apply_transform” method. For more details, refer to this blog.
Below is the code for this (taken from Keras)
1 2 3 |
def random_transform(self, x, seed=None): params = self.get_random_transform(x.shape, seed) return self.apply_transform(x, params) |
How to use this?
To use this, you first need to provide the desired transformations in the ImageDataGenerator class. For instance, let’s say we just want to zoom the image. Firstly, we specify the parameters in the ImageDataGenerator class.
1 |
datagen = ImageDataGenerator(zoom_range=0.2) |
Then we apply the random_transform method.
1 |
random = datagen.random_transform(img) |
Similarly, you can apply any random transformation to the image. Just specify the transformations in the ImageDataGenerator class. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.