gpt4 book ai didi

python - ValueError : Error when checking : expected flatten_1_input to have shape (None, 4, 4, 512) 但得到形状为 (1, 150, 150, 3) 的数组

转载 作者:行者123 更新时间:2023-11-30 08:59:12 24 4
gpt4 key购买 nike

我按照 this link 上的指南进行操作构建模型并在微调部分之前停止,使用以下代码在其他一些图像上测试模型:

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 给出以下输出:

_________________________________________________________________
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

我不知道如何弄清楚这意味着什么。

这是我用于创建模型的代码:

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()

我希望有人能帮助我:)

最佳答案

您在这一行中混合了 bootleneck 和图像模型逻辑:

test_model.predict_generator(...)

您将图像输入瓶颈模型的位置。这导致了形状错误。

关于python - ValueError : Error when checking : expected flatten_1_input to have shape (None, 4, 4, 512) 但得到形状为 (1, 150, 150, 3) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47143718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com