Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除最后一层并在Keras中插入三个Conv2D层_Python_Machine Learning_Keras_Conv Neural Network_Keras Layer - Fatal编程技术网

Python 删除最后一层并在Keras中插入三个Conv2D层

Python 删除最后一层并在Keras中插入三个Conv2D层,python,machine-learning,keras,conv-neural-network,keras-layer,Python,Machine Learning,Keras,Conv Neural Network,Keras Layer,我在Keras中有一个分类模型,我在一些数据集上进行了训练。将该模型称为“分类模型”。该模型保存在“classification.h5”中。检测模型相同,只是我们删除了最后一个卷积层,并添加了三个大小为(3,3)的Conv2D层。因此,我们的检测模型“检测模型”应该如下所示: 检测模型=分类模型[:最后一个索引]+Conv2d+Conv2d+Conv2d 我们如何在Keras中实现这一点 那么,加载您的分类模型并用于构建新模型: model = load_model("classificatio

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

检测模型=分类模型[:最后一个索引]+Conv2d+Conv2d+Conv2d


我们如何在Keras中实现这一点

那么,加载您的分类模型并用于构建新模型:

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 ...

那么,加载您的分类模型并使用构建您的新模型:

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:模型的输出张量必须是Keras张量。发现:@Alem您确定已将参数传递到层,即
(conv)
?我忘记了最后一个参数。我现在修好了。非常感谢。分类中的预训练权重会发生什么情况。h5现在在新_模型中吗?@Alem在
新_模型中使用的
模型
层也会在保存
新_模型
时被保存,如果您训练
新_模型
,这些层会被更新。但是,很明显,“classification.h5”中的权重不会改变,您可以随时使用此文件加载原始模型。我按照您的说明操作,得到了一个错误:TypeError:模型的输出张量必须是Keras张量。发现:@Alem您确定已将参数传递到层,即
(conv)
?我忘记了最后一个参数。我现在修好了。非常感谢。分类中的预训练权重会发生什么情况。h5现在在新_模型中吗?@Alem在
新_模型中使用的
模型
层也会在保存
新_模型
时被保存,如果您训练
新_模型
,这些层会被更新。但是,“classification.h5”中的权重显然不会改变,您可以随时使用此文件加载原始模型。