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

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

我正在尝试使用预保留的 CNN (VGG16),但我不断收到以下错误:

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

这是我的完整代码:

import numpy as np 
import keras 
from keras import backend as K 
from keras.models import Sequential 
from keras.layers import Activation 
from keras.layers.core import Dense, Flatten 
from keras.optimizers import Adam 
from keras.metrics import categorical_crossentropy 
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization 
from keras.layers.convolutional import *

train_path = "/DATA/train"
valid_path = "/DATA/valid"
test_path = "/DATA/test"
#creating the training, testing, and validation sets 
trainBatches = ImageDataGenerator().flow_from_directory(train_path, target_size=(244,244), classes=['classU', 'classH'], batch_size=20)
valBatches = ImageDataGenerator().flow_from_directory(valid_path, target_size=(244,244), classes=['classU', 'classH'], batch_size=2)
testBatches = ImageDataGenerator().flow_from_directory(test_path, target_size=(244,244), classes=['classU', 'classH'], batch_size=2)
#loading the model & removing the top layer 
model = Sequential() 
for layer in vgg16_model.layers[:-1]:
    model.add(layer)

#Fixing the weights 
for layer in model.layers:
    layer.trainable = False

#adding the new classier 
model.add(Dense(2, activation = 'softmax'))


model.compile(Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit_generator(trainBatches, steps_per_epoch=89, validation_data=valBatches, validation_steps=11, epochs=5, verbose=2)

但我不知道我遇到了什么错误。我认为 ImageDataGenerator() 会处理具有正确尺寸的 data/batches 代。我缺少什么?

本例中的 VGG 模型期望图像为 (224, 224),而您的图像生成器目标为 (244, 244),因此您的输入形状不匹配。您应该将目标大小调整为预期的形状。 documentation 详细说明了预期的输入,它还有一个选项 include_top 可以为您删除最后一层,因此您不必手动执行。