首页 > 解决方案 > Keras 中特征提取的 ResNet50 输入问题

问题描述

我正在使用预训练的 Resnet50 模型对图像进行简单的特征提取。但它给了我这个错误。

Error when checking input: expected input_9 to have the shape (224, 224, 3) but got array with shape (244, 244, 3)

我以为我正确地更改了形状并像本教程所说的那样为其添加了尺寸。https://www.kaggle.com/kelexu/extract-resnet-feature-using-keras

但它仍然给我上述错误。

我在这里做错了什么?

# load pre-trained resnet50
base_model = ResNet50(weights='imagenet', include_top=False,pooling=max)
x = base_model.output
input = Input(shape=(224,224,3))
x = Flatten()(input)
model = Model(inputs=input, outputs=x)
# Load in image
img = image.load_img("001.png", target_size=(244, 244))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print(x.shape) # This produces (1, 244, 244, 3)
features  = model.predict(x)
features_reduce =  features.squeeze()

标签: python-3.xkerascomputer-visionresnet

解决方案


改变

img = image.load_img("001.png", target_size=(244, 244))

img = image.load_img("001.png", target_size=(224, 224))


推荐阅读