Prediction after loading a saved model

I have a model defined as follows:

input_shape = (Batch_Size, Image_Size, Image_Size, Channels)
num_classes = 3
model = models.Sequential([
    resize_and_rescale,
    data_augmentation,
    layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=input_shape),
    layers.MaxPool2D((2,2)),
    layers.Conv2D(64, (3,3), activation='relu'),
    layers.MaxPool2D((2,2)),
    layers.Conv2D(128, (3,3), activation='relu'),
    layers.MaxPool2D((2,2)),
    layers.Conv2D(64, (3,3), activation='relu'),
    layers.MaxPool2D((2,2)),
    layers.Conv2D(128, (3,3), activation='relu'),
    layers.MaxPool2D((2,2)),
    layers.Conv2D(64, (3,3), activation='relu'),
    layers.MaxPool2D((2,2)),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(num_classes, activation='softmax'),
])

model.build(input_shape=input_shape)

After saving this model, I am trying to use it with an image pulled off the web in the following code:

import tensorflow as tf
import numpy as np
from tensorflow.keras import models, layers
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt

Image_size = 256
class_name = ['Potato_Early_blight', 'Potato_Late_blight', 'Potato_healthy']


def potatoLeafClassifier():
    image_path = './potato_late-blight.jpeg'

    # Load the model
    model = tf.keras.models.load_model('../Models/Potato-Blight-Model-v1.keras')
    predict_and_display(model, image_path)


def predict_and_display(model, img_path):
    input_image = image.load_img(img_path)
    image_array = image.img_to_array(image.load_img(img_path,
                                                    target_size=(Image_size, Image_size))) / 255
    # Expand dimensions to match model input shape
    expanded_image_array = np.expand_dims(image_array, axis=0)

    predictions = model.predict(expanded_image_array)
    predicted_class = class_name[np.argmax(predictions)]
    confidence = round(100 * (np.max(predictions)), 2)

    plt.imshow(input_image)
    plt.title(f"Predicted: {predicted_class}\n Confidence: {confidence}%")
    plt.axis("off")
    plt.show()


potatoLeafClassifier()

The model was created by resizing all images to [256,256] & rescaling them to 0-1. These are the first two steps of the model above.

If I follow the same steps, resize and rescale, the predictions are wrong. If I omit the resizing, I do get an error asking the image to be resized, which is what is expected.

If I keep the rescaling (division by 255) in place, the predictions are wrong, however I skip this step, the predications are correct.

Can someone help me understand why this should be the case?

Hi @Nader_Afshar, Once the model is trained with resize and rescale how are you able to skip it. Also please share the complete code to reproduce the issue. Thank You.

1 Like

The model, once trained is simply saved into a file. When needed, I simply read the model and call predict() on it with a given image.

Since the model was trained on scaled( Image_Size) and normalized (0-1), my assumption is that any image fed to the model for prediction, needs to have the same preprocessing. The code provided here, appears to have an issue with the normalization part, which to me does not make sense, unless I am doing something wrong in the process of normalization, hence my post.

The entire code is provided above, however if you wish to see the model development code, you can see it here:

Model: (Potato_Blight | Kaggle)

Thank you

Hi @Nader_Afshar, I have trained a model with resize and rescale function inside the model and then saved and loaded the model. while making predictions with the loaded model i have passed an image array without normalizing and got the correct predictions. Please refer to gist for working code example.
Thank You!

@Kiran_Sai_Ramineni , I see the same thing happening in the “gist” code you have referred to. In this code the selected image for inference, is neither scaled nor normalized.

Perhaps the scaling issue has been avoided since the image is less than the Image_Size (256), however I cannot see how this “predict()” function is working, as is mine, without normalization, even though normalization is clearly done for training. This question is really confounding me !!

Problem solved. A little note in tf.keras.layers.Rescaling clarifies that rescaling is applied at the “Inference” automatically as well :

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Rescaling#:~:text=The%20rescaling%20is%20applied%20both%20during%20training%20and%20inference.