【问题标题】:Delete last layer and insert three Conv2D layers in Keras删除最后一层,在 Keras 中插入三个 Conv2D 层
【发布时间】:2019-02-25 18:09:55
【问题描述】:

我在 Keras 中有一个用于分类的模型,我在一些数据集上进行了训练。将该模型称为“classification_model”。该模型保存在“classification.h5”中。用于检测的模型是相同的,只是我们删除了最后一个卷积层,并添加了三个大小为(3,3)Conv2D 层。因此,我们的检测模型“detection_model”应该如下所示:

detection_model = 分类模型[: last_conv_index] + Conv2d + Conv2d + Conv2d。

我们如何在 Keras 中实现它?

【问题讨论】:

    标签: python machine-learning keras conv-neural-network keras-layer


    【解决方案1】:

    好吧,加载你的分类模型并使用Keras functional API 来构建你的新模型:

    model = load_model("classification.h5")
    
    last_conv_layer_output = model.layers[last_conv_index].output
    conv = Conv2D(...)(last_conv_layer_output)
    conv = Conv2D(...)(conv)
    output = Conv2D(...)(conv)
    
    new_model = Model(model.inputs, output)
    
    # compile the new model and save it ...
    

    【讨论】:

    • 我按照您的说明进行操作,但出现错误:TypeError: Output tensors to a Model must be Keras tensors。找到:
    • @Alem 你确定你已经将参数传递给层,即(conv)
    • 我忘记了最后一个。我现在修好了。非常感谢。
    • classification.h5 中的预训练权重现在在 new_model 中会发生什么?
    • @Alem 在new_model 中使用的model 层,将在您保存new_model 时保存,并在您训练new_model 时更新。但是,显然“classification.h5”中的权重不会改变,您可以随时使用此文件加载原始模型。
    猜你喜欢
    • 2017-12-31
    • 2019-01-03
    • 2017-03-31
    • 2020-04-29
    • 1970-01-01
    • 2022-12-03
    • 2017-11-09
    • 2020-09-08
    • 1970-01-01
    相关资源
    最近更新 更多