我正在尝试使用基于 Keras 2 incepctionV3 的训练模型来预测图像以进行测试。我的原始模型运行良好,然后我尝试创建具有指定 input_shape (299,299,3) 的模型
base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299,299,3))
训练过程看起来不错,但是当我尝试使用它来预测图像时,它会导致此错误。
ValueError:检查时出错:预期 input_1 的形状为 (None, 299, 299, 3) 但数组的形状为 (1, 229, 229, 3)
import sys
import argparse
import numpy as np
from PIL import Image
from io import BytesIO
from keras.preprocessing import image
from keras.models import load_model
from keras.applications.inception_v3 import preprocess_input
target_size = (229, 229) #fixed size for InceptionV3 architecture
def predict(model, img, target_size):
"""Run model prediction on image
Args:
model: keras model
img: PIL format image
target_size: (w,h) tuple
Returns:
list of predicted labels and their probabilities
"""
if img.size != target_size:
img = img.resize(target_size)
x = image.img_to_array(img)
print(x.shape)
print("model input",model.inputs)
print("model output",model.outputs)
x = np.expand_dims(x, axis=0)
#x = x[None,:,:,:]
print(x.shape)
x = preprocess_input(x)
print(x.shape)
preds = model.predict(x)
print('Predicted:',preds)
return preds[0]
这是打印出来的
(229, 229, 3)
('model input', [<tf.Tensor 'input_1:0' shape=(?, 299, 299, 3) dtype=float32>])
('model output', [<tf.Tensor 'dense_2/Softmax:0' shape=(?, 5) dtype=float32>])
(1, 229, 229, 3)
(1, 229, 229, 3)
(1,299,299,3) 表示 299 X 299 中的 1 张图像,具有 3 个通道。在这种情况下,我的训练模型 (None,299,299,3) 的预期输入是什么意思?如何从 (299,299,3) 创建 (None,299,299,3)?