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?