In the previous blog, we discussed how to generate batches of augmented data using the flow method. We also learned that the key ingredient in the flow method is the “get_random_transform” method. This generates random parameters for a transformation. So, in this blog, let’s discuss this method in detail.
Keras API
1 |
get_random_transform(img_shape, seed=None) |
Now, let’s see how this generates random parameters for transformations by just using the image shape information.
How this works?
This borrows the parameters from the ImageDataGenerator class. For instance, if we define rotation range in the ImageDataGenerator class
1 |
datagen = ImageDataGenerator(rotation_range=40) |
then the random parameters for this is obtained as
1 2 3 4 |
if self.rotation_range: theta = np.random.uniform(-self.rotation_range,self.rotation_range) else: theta = 0 |
Thus, whenever you generate examples, theta is obtained from the uniform distribution, specified according to the parameters provided in the ImageDataGenerator class.
Similarly, for every transformation provided in the ImageDataGenerator class, we can obtain the random parameters. For more details, refer to the Keras GitHub.
How to use this?
To use this, you first need to provide the transformations in the ImageDataGenerator class. For instance, if I just want to rotate the image, then first specify the parameters as
1 |
datagen = ImageDataGenerator(rotation_range=40) |
Now, to get the random parameters, call the “get_random_transform” method as
1 |
transform = datagen1.get_random_transform((257, 278, 3),seed = 2) |
This outputs the following parameters dictionary as
See only the transformations specified in the ImageDataGenerator class i.e. theta value is changed. Rest all values are the default.
So, this way one can generate random parameters for transformations. 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.