Keras 使用 Resnet50 进行迁移学习失败并出现异常

标签 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 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_top=False 选项的 resnet 的最后一层已经被展平,您不需要另一个展平层。

关于Keras 使用 Resnet50 进行迁移学习失败并出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40788298/

相关文章:

neural-network - 多输出多类 Keras 模型

keras - 密是做什么的?

python-3.x - 类型错误 : type object got multiple values for keyword argument 'training'

python - Keras 在第一个 epoch 结束时显示形状错误

python-3.x - 预期输入有 4 个维度,但得到了具有形状的数组

python - Tensorflow 训练的准确性和损失与同一数据集的评估不同

python - 属性错误:模块 'resnet' 没有属性 'ResnetBuilder'

neural-network - Resnet-18 作为 Faster R-CNN 的主干

Keras:批量归一化(axis=1)的等级为 0

tensorflow - 带有lstm的蒙特卡罗经常性辍学