Image normalization in Keras

BoiKo report abuse

I'm trying to do image classification with Keras. I have read that it is usually a good idea to normalize input numbers in machine learning. It is easier for the model to train, that's why the training process with normalized data is faster and more effective. But how I can perform image normalization in Keras? Is there a built-in method to do this, or should I devise my own function for normalizing pixel values of the images in the training set?

Answers

Ameli1323 report abuse

You can do this using ImageDataGenerator class. Its primary goal is to perform data augmentation. However, it can be used for image preprocessing also. By the way, the class is located in keras.preprocessing.image module. You can read more about ImageDataGenerator class here - https://keras.io/preprocessing/image/ . All that you need from this class is to use its rescale parameter. You can provide a value for this parameter which will be used in multiplication with pixels data.

BoiKo report abuse

Could you provide an example, please?

Ameli1323 report abuse

Sure. Imagine that you have set the rescale parameter to the value 1/255. This means that each pixel value from the original image will be divided by 255 (or multiplied by 1/255 in other words). The code can look like this:

from tensorflow.keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator(rescale=1/255)

BoiKo report abuse

Thank you. It seems that it works as I expected!

Add Answer

Need support?

Just drop us an email to ... Show more