使用Resnet50的Keras转移学习异常失败

使用Resnet50的Keras转移学习异常失败,keras,keras-layer,resnet,Keras,Keras Layer,Resnet,我正在使用Resnet50进行迁移学习。后端是tensorflow。 我尝试在Resnet上再堆叠三层,但失败了,错误如下: Exception: The shape of the input to "Flatten" is not fully defined (got (None, None, 2048). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer

我正在使用Resnet50进行迁移学习。后端是tensorflow。 我尝试在Resnet上再堆叠三层,但失败了,错误如下:

Exception: The shape of the input to "Flatten" is not fully defined (got (None, None, 2048). 
Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
堆叠两个模型的代码如下所示:

model = ResNet50(include_top=False, weights='imagenet')

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 = Model(input=model.input, output=top_model(model.output))

带有include\u top=False选项的resnet的最后一层已展平,您不需要另一个展平层。

在实例化resnet时,必须显式输入\u形状。

model=ResNet50(包括_top=False,weights='imagenet',输入_shape=(224,3))

如果您的后端为theano,则必须将通道数设置为first:

model=ResNet50(包括_top=False,weights='imagenet',输入_shape=(3224224))

你确定吗?我遵循的是同一个示例,似乎不需要展平来编译,但后来我遇到了问题,选择将输入形状指定给resnet。然后为了训练,我确实需要压平身体。