In this blog, we will discuss ImageDataGenerator “standardize” method. This method performs in-place normalization to the batch of inputs. As already discussed, this is an important step in the flow method or data augmentation. So, let’s discuss it in detail.
Keras API
1 |
standardize(x) |
Here, x is the batch of inputs. This method returns the normalized inputs. Note that x is changed in-place. If you don’t want to change the inputs in-place, pass a copy of the input to this method.
How this works?
While performing data augmentation with ImageDataGenerator, we discussed different normalization techniques. These techniques include centering the entire distribution or a sample, rescaling the input, performing zca whitening, etc. Behind the scenes, these are implemented by the “standardize” method. Let’s see how.
For instance, we want to rescale the input by 1/255. So, first of all we will create an ImageDataGenerator instance as shown below
1 |
datagen = ImageDataGenerator(rescale=1/255.) |
Then for data augmentation, we will use the flow method as
1 |
training_generator = datagen.flow(x_train, y_train, batch_size=64,subset='training',seed=7) |
Thus, the training_generator will yield batches of augmented images. That’s all we usually do.
As already discussed in this blog, the flow method consists of three steps, of which the last step is the “standardize” method. All the normalization work in the ImageDataGenerator class is handled by this method.
Now, coming back to the above example, the “standardize” method will first check whether you want to rescale or not. If yes, then this will change the input in-place as shown below.
1 2 |
if self.rescale: x *= self.rescale |
Similarly, this method performs featurewise_center or samplewise_center or any other normalization. For more details, refer to Keras Github.
How to use this?
First of all, create an ImageDataGenerator instance with the desired transformations. Then apply the “standardize” method as shown below.
1 2 3 4 5 6 7 8 9 |
import matplotlib.pyplot as plt import numpy as np from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array img = load_img('D:/downloads/opencv_logo.PNG') img = img_to_array(img) datagen2 = ImageDataGenerator(samplewise_center=True) standardize = datagen2.standardize(np.copy(img)) plt.imshow(standardize/255.) |
Note: The standardize method only supports transformations that perform normalization such as featurewise_center, rescale, etc. Otherwise, this returns the same image or batch of inputs.
What does in-place means?
As already discussed, this method normalizes the inputs in-place. This is exactly what in-place operators in Python do. Let’s take an example to understand what does in-place means.
For instance, let’s rescale an image of all ones by 2. After the “standardize” method, see how the mean of the “images” change.
1 2 3 4 5 6 7 8 9 10 |
from keras.preprocessing.image import ImageDataGenerator datagen = ImageDataGenerator(rescale=2) images = np.ones((10, 128, 128, 3)) print(images.mean()) # 1.0 images_std = datagen.standardize(images) print(images.mean(), images_std.mean()) # 2.0 2.0 |
That’s all for “standardize” method. 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.