我按照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()
我希望有人可以帮助我:)
答案 0 :(得分:1)
尝试通过GlobalAveragePooling2D或GlobalMaxPooling2D替换Flatten图层
答案 1 :(得分:1)
您在此行中混合了bootleneck和图像模型逻辑:
test_model.predict_generator(...)
将图像输送到瓶颈模型的位置。这导致了形状错误。
答案 2 :(得分:0)
添加
ss.CharAt(i - 1)
data_format='channels_last'
中的如果后端使用Tensorflow,则
将起作用,
model.add()
如果后端正在使用theano。