I am kind of confused regarding the topic. I have built a CNN architecture for the cat-dog image classification around 6000 images of cat and 6000 images of dog and I am predicting on test images. I have used Rescaling() in my layers.

inputs = layers.Input(shape=(256, 256) + (3,))
x = layers.Rescaling(1.0/255)(inputs)

My question should we use rescaling on the test images and then feed it for predict() or should we not ?

Say a image of cat I feed into the model. When I am predicting the test images without rescaling it gives me 100% cat and 0% dog probabilities. But when I am rescaling the image

img_array = keras.preprocessing.image.img_to_array(img)
img_array = img_array/255 # Rescaling
img_array = tf.expand_dims(img_array, 0)  # Create batch axis

before feeding the image, it gives me around 78% cat and 21% dog.

Suppose if I have a image of dog, without rescaling gives 0.03% cat and 99.97% dog and with rescaling gives 78% cat and 21% dog.

  • 1
    $\begingroup$ When testing your model on new images, you should always apply the same transformations you applied to your training images, except for any augmentation steps that were applied. So in this case, yes, you should be rescaling the images in your test dataset. $\endgroup$
    – Oxbowerce
    Oct 23, 2021 at 11:57
  • $\begingroup$ @Oxbowerce Ok. But can you explain the scores I am getting with and without scaling. Why is this happening or anything ? $\endgroup$ Oct 23, 2021 at 12:12
  • $\begingroup$ Probably use a generator function like in this post stackoverflow.com/a/55991598/9524424 $\endgroup$
    – Peter
    Oct 23, 2021 at 18:13
  • $\begingroup$ @Rafael, I claim the scores you are getting are simply circumstantial and they can also be otherwise. As already mentioned test dataset is transformed the same way as train dataset $\endgroup$
    – Nikos M.
    Oct 24, 2021 at 15:39
  • $\begingroup$ @NikosM. I was following keras example. Why here even though they have used rescaling in training, they didn't use rescaling in test images ? $\endgroup$ Oct 24, 2021 at 16:43

1 Answer 1


According to the original Keras example:

def make_model(input_shape, num_classes):
    inputs = keras.Input(shape=input_shape)
    # Image augmentation block
    x = data_augmentation(inputs)

    # Entry block
    x = layers.Rescaling(1.0 / 255)(x) ### first layer is a rescaling layer ###
    x = layers.Conv2D(32, 3, strides=2, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    x = layers.Conv2D(64, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    previous_block_activation = x  # Set aside residual

    for size in [128, 256, 512, 728]:
        x = layers.Activation("relu")(x)
        x = layers.SeparableConv2D(size, 3, padding="same")(x)
        x = layers.BatchNormalization()(x)

        x = layers.Activation("relu")(x)
        x = layers.SeparableConv2D(size, 3, padding="same")(x)
        x = layers.BatchNormalization()(x)

        x = layers.MaxPooling2D(3, strides=2, padding="same")(x)

        # Project residual
        residual = layers.Conv2D(size, 1, strides=2, padding="same")(
        x = layers.add([x, residual])  # Add back residual
        previous_block_activation = x  # Set aside next residual

    x = layers.SeparableConv2D(1024, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    x = layers.GlobalAveragePooling2D()(x)
    if num_classes == 2:
        activation = "sigmoid"
        units = 1
        activation = "softmax"
        units = num_classes

    x = layers.Dropout(0.5)(x)
    outputs = layers.Dense(units, activation=activation)(x)
    return keras.Model(inputs, outputs)

The model contains a rescaling layer among the first layers and rescales every input (whether in train set or in test set) by itself. So everything gets rescaled the same as it should be. So model.predict() indeed rescales the input.

Note that technically rescaling is not a rescaling of the image but instead acts as a normalization process so that pixel values in the range $[0, 255]$ get translated to float values in the range $[0, 1.0]$ which are more "natural" inputs for neural networks.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.