使用预训练模型进行评估会导致类型错误

Evaluation with pre-trained model results in Type error

我有一个经过训练的 inceptionV3 模型,我想在新数据集上进行测试。但是,我收到有关图像数据形状的类型错误。 InceptionV3 模型是在 1500 个图像分类数据集上训练的。

from tensorflow import keras
import cv2
from tensorflow.keras.preprocessing import image
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model


# dimensions of our images    -----   are these then grayscale (black and white)?
img_width, img_height = 139, 139

# load the model we saved
model = load_model('/home/DEV/model_inception.h5', compile=False)

# Get test image ready
test_image = cv2.imread('/home/images/0b53daf814304dd0d74efb2fa052ef23_0.png')
test_image = np.array(test_image)
test_image = cv2.resize(test_image,(img_width,img_height))
test_image = test_image.reshape(1,img_width, img_height,3) 
result = model.predict(test_image)
plt.imshow(result, cmap="gray")
plt.show()

我遇到的类型错误是

TypeError: Invalid shape (1, 3, 3, 2048) for image data

如何修正我的评估模型并进行测试

这是模型摘要的示例

model.summary

如果这是您用来训练模型的训练图像的图像大小,您希望输入图像的形状为 (1,139,139,3)。 下一个问题是您的模型是在 RGB 还是 BGR 图像上训练的? cv2 将图像读入为 BGR。如果您的模型是在 RGB 图像上训练的,那么您需要使用

将图像从 BGR 转换为 RGB
image_rgb=cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)

下一个问题是您的模型训练所用的图像是否缩放了像素值?通常它们与

成比例
scaled_image=image/255

如果缩放了训练图像,则需要缩放输入图像。最后,为了使图像成为正确的形状,请使用

image=np.expand_dims(image, axis=0) 

这增加了 model.predict

所需的额外维度