输入0与层functional_3不兼容:expectedshape=(None,224,224,3),foundshape=(None,240,240,3)

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

ValueError: Input 0 is incompatible with layer functional_3: expected shape=(None, 224, 224, 3), found shape=(None, 240, 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,240,240,3]) # return the image with shaping that TF wants.
prediction = model.predict(img)
prediction

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

这是图像

回答

你是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,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,224,3) # return the image with shaping that TF wants.


以上是输入0与层functional_3不兼容:expectedshape=(None,224,224,3),foundshape=(None,240,240,3)的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>