ValueError:检查时出错:预期flatten_1_input具有形状(无,4、4、512),但数组的形状为(1、150、150、3) [英] ValueError: Error when checking : expected flatten_1_input to have shape (None, 4, 4, 512) but got array with shape (1, 150, 150, 3)
问题描述
我在上遵循了该指南.此链接以建立模型,并在微调部分之前停止,以使用以下代码在其他一些图像上测试该模型:
I followed the guide at this link to build a model and stopped before the finetuning part to test the model on some other images using the following code:
img_width, img_height = 150, 150
batch_size = 1
test_model = load_model('dog_cat_model.h5')
validation_data_dir = "test1"
test_datagen = ImageDataGenerator(rescale=1. / 255)
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
shuffle=False,
class_mode='binary')
predictions = test_model.predict_generator(validation_generator, len(validation_generator.filenames));
for i in range(len(validation_generator.filenames)):
print(validation_generator.filenames[i], ": ", predictions[i])
但是出现以下错误:
ValueError: Error when checking : expected flatten_1_input to have shape (None, 4, 4, 512) but got array with shape (1, 150, 150, 3)
打印test_model.summary给出以下输出:
printing test_model.summary gives the following output:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_1 (Flatten) (None, 8192) 0
_________________________________________________________________
dense_1 (Dense) (None, 256) 2097408
_________________________________________________________________
dropout_1 (Dropout) (None, 256) 0
_________________________________________________________________
dense_2 (Dense) (None, 1) 257
=================================================================
Total params: 2,097,665
Trainable params: 2,097,665
Non-trainable params: 0
_________________________________________________________________
None
我也不知道如何弄清楚这是什么意思.
And I am clueless how to figure out what this means.
这是我用来创建模型的代码:
Here is the code I used for creating the model:
img_width, img_height = 150, 150
top_model_weights_path = 'bottleneck_fc_model.h5'
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
batch_size = 16
train_samples = 2000
validation_samples = 800
epochs = 50
def save_bottlebeck_features():
datagen = ImageDataGenerator(rescale=1. / 255)
# build the VGG16 network
model = applications.VGG16(include_top=False, weights='imagenet')
train_generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=None,
shuffle=False)
validation_generator = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=None,
shuffle=False)
predict_size_train = int(math.ceil(train_samples / batch_size))
bottleneck_features_train = model.predict_generator(train_generator, predict_size_train)
np.save('bottleneck_features_train.npy', bottleneck_features_train)
predict_size_validation = int(math.ceil(validation_samples / batch_size))
bottleneck_features_validation = model.predict_generator(validation_generator, predict_size_validation)
np.save('bottleneck_features_validation.npy', bottleneck_features_validation)
def train_top_model():
train_data = np.load('bottleneck_features_train.npy')
train_labels = np.array([0] * (train_samples // 2) + [1] * (train_samples // 2))
validation_data = np.load('bottleneck_features_validation.npy')
validation_labels = np.array([0] * (validation_samples // 2) + [1] * (validation_samples // 2))
model = Sequential()
model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(train_data, train_labels,
epochs=epochs,
batch_size=batch_size,
validation_data=(validation_data, validation_labels))
model.save_weights(top_model_weights_path)
model.save('dog_cat_model.h5')
save_bottlebeck_features()
train_top_model()
我希望有人可以帮助我:)
I hope someone can help me out :)
推荐答案
您在这一行中混合了bootleneck和图像模型逻辑:
You mixed bootleneck and image model logic in this line:
test_model.predict_generator(...)
将图像馈送到瓶颈模型的位置.这导致形状错误.
Where you fed images to bottleneck model. This caused a shape error.
这篇关于ValueError:检查时出错:预期flatten_1_input具有形状(无,4、4、512),但数组的形状为(1、150、150、3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!