📅  最后修改于: 2023-12-03 15:28:31.243000             🧑  作者: Mango
在 Keras 中,我们可以轻松地重命名一个模型的所有层。但如果我们只需要重命名模型的最后一层,我们怎么办呢?下面是一些简单的步骤来改变 Keras 模型的最后一层的名称。
首先,我们需要加载一个已训练的 Keras 模型。对于本例,我们将加载 VGG16 神经网络模型,这是一个在大型图像识别竞赛中获胜的模型之一。以下是加载 VGG16 模型的代码:
from keras.applications import VGG16
model = VGG16(weights='imagenet', include_top=True)
第二步是查看模型的结构。我们需要了解模型的最后一层的名称,以便能够重命名它。以下是打印 VGG16 模型结构的代码片段:
print(model.summary())
这将输出以下信息:
Model: "vgg16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 224, 224, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
_________________________________________________________________
flatten (Flatten) (None, 25088) 0
_________________________________________________________________
fc1 (Dense) (None, 4096) 102764544
_________________________________________________________________
fc2 (Dense) (None, 4096) 16781312
_________________________________________________________________
predictions (Dense) (None, 1000) 4097000
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
_________________________________________________________________
在这里,我们可以看到模型的最后一层的名称是 predictions
。
有两种方法可以重命名模型的最后一层。第一种方法是定义一个新的 Keras 模型,该模型与先前的模型相同,但最后一层的名称已更改。下面是使用 Model()
函数定义新模型的代码片段:
from keras.models import Model
new_model = Model(inputs=model.input, outputs=model.get_layer('fc2').output)
new_model.summary()
在这里,我们将新模型的输入设置为原始模型的输入,并将输出设置为原始模型中名为 fc2
的层的输出。这将创建一个新模型,其最后一层的名称为 fc2
。在其他方面,新模型与原始模型相同。
第二种方法是通过 model.layers
属性访问模型的所有层,并更改最后一层的名称。这种方法更简单,更直观,只需要一行 Python 代码,如下所示:
model.layers[-1].name = 'new_predictions'
在这里,我们将模型的最后一层的名称更改为 new_predictions
。这种方法不会创建新模型,而是直接在原始模型中改名。
在进行任何下一步之前,我们需要验证模型的最后一层的名称是否已成功更改。以下是打印已更改的 VGG16 模型结构的代码:
print(new_model.summary())
或者
print(model.summary())
这会返回以下信息:
Model: "vgg16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 224, 224, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
_________________________________________________________________
flatten (Flatten) (None, 25088) 0
_________________________________________________________________
fc1 (Dense) (None, 4096) 102764544
_________________________________________________________________
fc2 (Dense) (None, 4096) 16781312
_________________________________________________________________
new_predictions (Dense) (None, 1000) 4097000
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
_________________________________________________________________
从输出中,您可以看到 VGG16 模型的最后一层的名称已成功更改为 new_predictions
。
这就是重命名 Keras 模型的最后一层的简单步骤。