Search code examples
pythontensorflowkerassequential

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


I am trying to use an external image to test my deep learning model however even after using image = cv2.resize(image,(196,196)) I get the error that the expected shape is not matching the found shape; expected shape=(None, 196, 196, 3), found shape=(None, 196, 3). Here is the surrounding code for more context:

image=cv2.imread(image)
image = cv2.resize(image,(196,196))  
predicted_label = model.predict(image).argmax()

Solution

  • You need to add batch_size to your image:

    image = cv2.imread('1.jpg')
    image = cv2.resize(image,(224,224))  
    model = tf.keras.applications.densenet.DenseNet201(include_top=True, weights="imagenet")
    prd_img = model.predict(image[None, ...])
    pred = tf.keras.applications.densenet.decode_predictions(prd_img)
    print(pred)
    

    Output:

    [[('n07920052', 'espresso', 0.8365456), 
      ('n07930864', 'cup', 0.14428616), 
      ('n04263257', 'soup_bowl', 0.007808877), 
      ('n03063599', 'coffee_mug', 0.0030550184), 
      ('n04476259', 'tray', 0.0025293417)]]
    

    Input image:

    enter image description here