首页 > 解决方案 > 删除最后一层,在 Keras 中插入三个 Conv2D 层

问题描述

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

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

我们如何在 Keras 中实现它?

标签: pythonmachine-learningkerasconv-neural-networkkeras-layer

解决方案


好吧,加载您的分类模型并使用Keras 函数式 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 ...

推荐阅读