删除最后一层并在 Keras 中插入三个 Conv2D 层
Delete last layer and insert three Conv2D layers in Keras
我在 Keras 中有一个分类模型,我在某些数据集上进行了训练。调用该模型 "classification_model"。该模型保存在 "classification.h5" 中。检测模型是相同的,只是我们删除了最后一个卷积层,并添加了三个 Conv2D
层,大小为 (3,3)
。因此,我们的检测模型 "detection_model" 应该如下所示:
detection_model = classification_model[: last_conv_index] + Conv2d + Conv2d + Conv2d。
我们如何在 Keras 中实现它?
那么,加载您的分类模型并使用 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 ...
我在 Keras 中有一个分类模型,我在某些数据集上进行了训练。调用该模型 "classification_model"。该模型保存在 "classification.h5" 中。检测模型是相同的,只是我们删除了最后一个卷积层,并添加了三个 Conv2D
层,大小为 (3,3)
。因此,我们的检测模型 "detection_model" 应该如下所示:
detection_model = classification_model[: last_conv_index] + Conv2d + Conv2d + Conv2d。
我们如何在 Keras 中实现它?
那么,加载您的分类模型并使用 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 ...