Mamo
Mamo

Reputation: 1

ValueError in shape - prediction with keras.models.Sequential

When executing the following code to predict the category of an image using a model trained using the fashion MNIST dataset, the following error occurs. How can I fix this error? Any advice would be appreciated.

prediction = model.predict(image)

ValueError: in user code:

File "c:\Users\mkcor\anaconda3\Lib\site-packages\keras\src\engine\training.py", line 2440, in predict_function  *

... Call arguments received by layer 'sequential' (type Sequential): • inputs=tf.Tensor(shape=, dtype=float32) • training=False • mask=None

The model code is as follows:

model = keras.models.Sequential([
    keras.layers.Flatten(), 
    keras.layers.Dense(512, activation='relu'), 
    keras.layers.Dropout(0.2),
    keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)

Tried: preprocessed the test image as follows. Expect: error to be fixed. resulted: no change from above error.

from keras.preprocessing import image
import numpy as np

# Load the image
img_path = 'image.jpg'

# Load and resize image to match Fashion MNIST
img = image.load_img(img_path, target_size=(28, 28), color_mode='grayscale')  

# Convert image to array and normalize
img_array = image.img_to_array(img)
img_array /= 255.0  # Normalize pixel values

# Reshape the image for model prediction
img_input = np.expand_dims(img_array, axis=0)

Upvotes: 0

Views: 22

Answers (0)

Related Questions