Search code examples
pythonkerastensorflow2.0

Single Prediction Image doesn't need to be rescaled?


I followed a tutorial to make my first Convolutional Neural Network using Keras and I have a small question regarding the rescaling step.

So when we are importing the training set and test set, we create an instance of the tf.keras.preprocessing.image.ImageDataGenerator class and use it as:

train_datagen = ImageDataGenerator(rescale=1/255)

Along with some other augmentation parameters. My understanding is that we use the rescale parameter to normalize the pixel values of the images imported.

But when we load up a single image to run through the CNN, we write something like (code from keras docs):

image = tf.keras.preprocessing.image.load_img(image_path)
input_arr = keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr])  # Convert single image to a batch.
predictions = model.predict(input_arr)

My question is, I cannot see the single input image being rescaled anywhere. Is it being done implicitly, or is there no need to actually perform rescaling? If the latter, then why is it so?

Thanks!


Solution

  • The image should be normalized that it should be divided by 255, if it's done during the training. Network will not be able to interpret that.

    Also, when we use test_datagen, we apply Rescaling by 1/255 for the predict generator.

    Normalization, mean subtraction and std deviation needs to be done at the testing time, if that has been applied during the training stage.