Input 0 与 layerfunctional_3 不兼容:expected shape=(None, 224, 224, 3), found shape=(None, 240, 240, 3)

问题描述

我是 VGG19 和 Python 图像处理的新手。我正在尝试测试我训练的 VGG19 模型来预测图像。我收到此错误:-

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

我用于预测的 tensorflow 代码是:-

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,3]) # return the image with shaping that TF wants.
prediction = model.predict(img)
prediction

但在 .ipynb 文件的情况下,我只是收到有关此的警告:-

This the image

解决方法

你是resizing to wrong shape。而不是240,240

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

使用224,224

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