【问题标题】:Keras Transfer learning with Resnet50 fail with Exception使用 Resnet50 进行 Keras 迁移学习失败并出现异常
【发布时间】:2017-04-08 20:52:08
【问题描述】:

我正在使用 Resnet50 进行迁移学习。后端是张量流。 我试图在 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))

【问题讨论】:

    标签: keras keras-layer resnet


    【解决方案1】:

    带有 include_top=False 选项的最后一层 resnet 已经被展平,您不需要另一个展平层。

    【讨论】:

    • 我在按照同样的例子,似乎你不需要展平来编译,但后来我遇到了问题并选择将 input_shape 指定为 resnet。 然后我确实需要扁平化才能训练。
    【解决方案2】:

    在实例化 Resnet 时必须显式输入形状。 model = ResNet50(include_top=False, weights='imagenet',input_shape=(224,224,3))

    如果你有theano作为后端,你必须先设置通道数: model = ResNet50(include_top=False, weights='imagenet',input_shape=(3,224,224))

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 2020-02-11
      • 2017-12-21
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 2020-10-10
      相关资源
      最近更新 更多