Input 0 is incompatible with layer functional_3: expected shape=(None, 224, 224, 3), found shape=(None, 240, 240, 3)
Asked Answered
L

2

8

I am new to VGG19 and image processing in python. I am trying to test my trained VGG19 model for predicting an image. I am getting this error:-

ValueError: Input 0 is incompatible with layer functional_3: expected shape=(None, 224, 224, 3), found shape=(None, 240, 240, 3)

My tensorflow code for predicting is:-

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.keras.models import load_model
model = load_model('VGG19.h5')
CATEGORIES = ["Pneumonia", "Non-Pneumonia"]
img = cv2.imread('person1_bacteria_1.jpeg')
img = cv2.resize(img,(240,240))     # resize image to match model's expected sizing
img = np.reshape(img,[1,240,240,3]) # return the image with shaping that TF wants.
prediction = model.predict(img)
prediction

But in the case of .ipynb file I simply get a warning regarding this:-

This the image

Lucillalucille answered 21/12, 2020 at 6:23 Comment(0)
T
8

You are resizing to wrong shape. Instead of 240,240

img = cv2.resize(img,(240,240))     # resize image to match model's expected sizing
img = img.reshape(1,240,240,3) # return the image with shaping that TF wants.

Use 224,224

img = cv2.resize(img,(224,224))     # resize image to match model's expected sizing
img = img.reshape(1,224,224,3) # return the image with shaping that TF wants.
Tersina answered 21/12, 2020 at 6:26 Comment(0)
M
1

Your pretrained model is expecting an input of shape (224,224,3) and you are feeding it (240,240,3), hence the complaint.
So just do:

img = img.reshape(1,224,224,3)

And you are good to go!

Microspore answered 10/11, 2021 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.