'Input 0 of layer "sequential_3" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 324, 500, 3)

I'm having an issue if anyone can help please comment

input_shape=(BATCH_SIZE,256,256,3)
model=models.Sequential([
        resize_and_rescale,
        data_augmentation,

        layers.Conv2D(32,(3,3), activation="relu", input_shape=input_shape),
        layers.MaxPooling2D((2,2)),

        layers.Conv2D(64,kernel_size=(3,3), activation="relu"),
        layers.MaxPooling2D((2,2)),
        layers.Conv2D(64,kernel_size=(3,3), activation="relu"),
        layers.MaxPooling2D((2,2)),
        
        layers.Conv2D(64,(3,3), activation="relu"),
        layers.MaxPooling2D((2,2)),
        layers.Conv2D(64,(3,3), activation="relu"),
        layers.MaxPooling2D((2,2)),
        layers.Conv2D(64,(3,3), activation="relu"),
        layers.MaxPooling2D((2,2)),

        layers.Flatten(),
        layers.Dense(64,activation="relu"),

        layers.Dense(n_classes, activation="softmax")

  ])
  model.build(input_shape=input_shape)

this is my model which is working perfectly but when i post image of any size different from 256,256 from postman

@app.post("/predict")
async def predict(
file: UploadFile = File(...)
):
image = read_file_as_image(await file.read())
img_batch = np.expand_dims(image, 0)

predictions = MODEL.predict(img_batch)

predicted_class = CLASS_NAMES[np.argmax(predictions[0])]
confidence = np.max(predictions[0])
return {
    'class': predicted_class,
    'confidence': float(confidence)
}

if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8000)

this is what my fast api returns->

Input 0 of layer "sequential_3" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 324, 500, 3)

i tried resizing image from Pillow but it didn't work , i dont know much about fastapi so if anyone knows how to resolve this error please comment.



Solution 1:[1]

Small correction, the below code is incorrect img=cv2.resize(img, (256,256,3)

Instead, You have to only mention the pixel size img = cv2.resize(img, dsize = (256, 256))

Solution 2:[2]

what is read_file_as_image(await file.read()), is it a function you wrote? try this

import matplotlib.pyplot as plt
import cv2
img_path= define the full path to the image here
img=plt.imread(img_path)
img=cv2.resize(img, (256,256,3)
img=np.expand_dims(img, axis=0)
print (img.shape)

Solution 3:[3]

You need resize your image before use this:

import cv2
...
image = read_file_as_image(await file.read())
image = cv2.resize(image, (256,256,3))
img_batch = np.expand_dims(image, 0) 
# OR
img_batch = image[None,...]
...

More Explanation:

>>> import numpy as np
>>> a = np.array([[1,2],[2,3]])

>>> a.shape
(2, 2)

>>> a[None, ...].shape
(1, 2, 2)

>>> np.expand_dims(a, 0).shape
(1, 2, 2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Agasthya Nu
Solution 2 Gerry P
Solution 3 I'mahdi