【问题标题】:Fine-tune InceptionV3 model not working as expected微调 InceptionV3 模型未按预期工作
【发布时间】:2019-05-19 14:05:17
【问题描述】:

尝试在 InceptionV3 中使用迁移学习(微调),移除最后一层,关闭所有层的训练,并添加单个密集层。当我再次查看摘要时,我没有看到我添加的层,并且没有得到期望。

RuntimeError: 你试图在 dense_7 上调用 count_params,但是 层没有建立。您可以通过以下方式手动构建它: dense_7.build(batch_input_shape).

from keras import applications
pretrained_model = applications.inception_v3.InceptionV3(weights = "imagenet", include_top=False, input_shape = (299, 299, 3))

from keras.layers import Dense
for layer in pretrained_model.layers:
  layer.trainable = False

pretrained_model.layers.pop()

layer = (Dense(2, activation='sigmoid'))
pretrained_model.layers.append(layer)

再次查看摘要给出了上述异常。

pretrained_model.summary()

想训练编译和拟合模型,但是

pretrained_model.compile(optimizer=RMSprop(lr=0.0001), 
              loss = 'sparse_categorical_crossentropy', metrics = ['acc'])

上面一行给出了这个错误,

无法解释优化器标识符:

【问题讨论】:

  • 您不能在layers 属性上使用pop() 来修改架构。 Thisthis 可能会有所帮助。

标签: keras deep-learning keras-layer


【解决方案1】:

您正在使用pop 弹出全连接层,例如网络末端的Dense。但这已经通过参数include top = False 完成。所以你只需要用include_top = False初始化Inception,添加最后的Dense层。另外,由于是InceptionV3,建议在InceptionV3输出后加上GlobalAveragePooling2D(),以减少过拟合。这是一个代码,

from keras import applications
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D

pretrained_model = applications.inception_v3.InceptionV3(weights = "imagenet", include_top=False, input_shape = (299, 299, 3))


x = pretrained_model.output
x = GlobalAveragePooling2D()(x) #Highly reccomended

layer = Dense(2, activation='sigmoid')(x)

model = Model(input=pretrained_model.input, output=layer)

for layer in pretrained_model.layers:
  layer.trainable = False

model.summary()

这应该会为您提供所需的模型进行微调。

【讨论】:

    猜你喜欢
    • 2019-05-26
    • 2015-08-20
    • 1970-01-01
    • 2019-03-07
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多