我在Keras中有一个用于分类的模型,已经在某个数据集上进行了训练。我们称这个模型为“classification_model”。该模型被保存为“classification.h5”。用于检测的模型与此相同,只是我们删除了最后一个卷积层,并添加了三个大小为(3,3)
的Conv2D
层。因此,我们的检测模型“detection_model”应该如下所示:
detection_model = classification_model[: last_conv_index] + Conv2d + Conv2d + Conv2d.
我们如何在Keras中实现这个操作呢?
回答:
好吧,加载你的分类模型,并使用Keras函数式API来构建你的新模型:
model = load_model("classification.h5")last_conv_layer_output = model.layers[last_conv_index].outputconv = Conv2D(...)(last_conv_layer_output)conv = Conv2D(...)(conv)output = Conv2D(...)(conv)new_model = Model(model.inputs, output)# 编译新模型并保存它 ...