尝试实现keras模型时出现以下错误:
“ ValueError:”平面“输入的形状未完全定义 (获得(无,无,512)。请确保传递完整的“ input_shape”或 模型中第一层的参数“ batch_input_shape”。“
这里可能是什么问题?
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
weights_path = '../keras/examples/vgg16_weights.h5'
top_model_weights_path = 'fc_model.h5'
img_width, img_height = 240, 320
train_data_dir = 'datasetmini/train'
validation_data_dir = 'datasetmini/VALIDATION'
nb_train_samples = nb_train_samples
nb_validation_samples = nb_validation_samples
epochs = epochs
batch_size = batch_size
model = applications.VGG16(weights='imagenet', include_top=False)
print('Model loaded.')
top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
top_model.load_weights(top_model_weights_path)
model.add(top_model)
for layer in model.layers[:25]:
layer.trainable = False
model.compile(loss='binary_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
epochs=epochs,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)
答案 0 :(得分:1)
根据Unable to fine tune Keras vgg16 model - input shape issue。事实证明,在加载keras预训练模型时应指定input_shape
。
而且keras.engine.training.Model
也不能add
个新层,因此应该建立一个新的keras.engine.sequential.Sequential
模型,然后使用add
。
MWE:
from keras import applications
from keras import Sequential
from keras.layers import Flatten, Dense, Dropout
from keras import optimizers
import numpy as np
img_width, img_height = 240, 320
model = applications.VGG16(include_top=False, weights=None, input_shape=(img_width, img_height, 3))
print('Model loaded.')
top_model = Sequential()
top_model.add(Flatten())
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
#top_model.load_weights(top_model_weights_path)
##this will fail
##AttributeError: 'Model' object has no attribute 'add'
#model.add(top_model)
new_model = Sequential()
new_model.add(model)
new_model.add(top_model)
for layer in new_model.layers[:25]:
layer.trainable = False
new_model.compile(loss='binary_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
batch_size = 8
X = np.random.randn(batch_size,240,320,3)
Y = np.random.randn(batch_size, 1)
new_model.train_on_batch(X, Y)