ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)

Question:

I made a model in another code and I would like to make prediction with it using an image.

import numpy as np
from keras.preprocessing.image import img_to_array, load_img
from tensorflow import keras

model = keras.models.load_model('Faces/model/alexnet')
# load the image
img = load_img('Faces/text.jpg', target_size=(32, 32))

# prepare the image
x = img_to_array(img)
# perform prediction
preds = model.predict(x)
print('Predicted:', preds)

I am getting an error where it is expecting shape=(None, 32, 32, 3) but found a shape that is similar but missing the None parameter, shape=(32, 32, 3). To me it looks like the load_img() is not loading the correct shape. What solutions can I apply?

Traceback (most recent call last):
File "d:ProjectsExperimentfindresult.py", line 12, in <module>
preds = model.predict(x)
File "C:UsersUSERAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packageskerasutilstraceback_utils.py", line 67, in error_handler   
raise e.with_traceback(filtered_tb) from None
File "C:UsersUSERAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagestensorflowpythonframeworkfunc_graph.py", line 1129, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

File "C:UsersUSERAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packageskerasenginetraining.py", line 1621, in predict_function  
*
return step_function(self, iterator)
File "C:UsersUSERAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packageskerasenginetraining.py", line 1611, in step_function  ** 
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:UsersUSERAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packageskerasenginetraining.py", line 1604, in run_step  **      
outputs = model.predict_step(data)
File "C:UsersUSERAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packageskerasenginetraining.py", line 1572, in predict_step      
return self(x, training=False)
File "C:UsersUSERAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packageskerasutilstraceback_utils.py", line 67, in error_handler 
raise e.with_traceback(filtered_tb) from None
File "C:UsersUSERAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packageskerasengineinput_spec.py", line 263, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)

I have tried changing the target_size=(32,32) to target_size=(224,244) and adjusted the model accordingly. But the error changed to expected shape=(None, 224, 224, 3), found shape=(32, 224, 3) instead.

Asked By: dsw

||

Answers:

After the code x = img_to_array(img) add code

x=np.expand_dims(x, axis=0)
Answered By: Gerry P