如何基于微调的 VGGNet16 创建子模型

How to create a sub-model based on fine-tuned VGGNet16

为了寻找两张图片之间的相似性,设计了以下网络架构。

最初,我拿了VGGNet16,去掉了分类头:

vgg_model = VGG16(weights="imagenet", include_top=False,
input_tensor=Input(shape=(img_width, img_height, channels)))

之后,我设置参数layer.trainable = False,这样网络就会作为特征提取器工作。

我向网络传递了两个不同的图像:

encoded_left = vgg_model(input_left)
encoded_right = vgg_model(input_right)

这将产生两个特征向量。然后对于分类(无论它们是否相似),我使用了一个度量网络,它由 2 个卷积层、池化层和 4 个全连接层组成。

merge(encoded_left, encoded_right) -> conv-pool -> conv-pool -> reshape -> dense * 4 -> output

因此,模型看起来像:

model = Model(inputs=[left_image, right_image], outputs=output)

仅训练度量网络后,为了微调卷积层,我将最后一个卷积块设置为训练。因此,在第二个训练阶段,随着metric network,最后一个卷积块也被训练了。

现在我想将这个微调的网络用于另一个目的。这是网络摘要:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_1 (InputLayer)            (None, 224, 224, 3)  0
__________________________________________________________________________________________________
input_2 (InputLayer)            (None, 224, 224, 3)  0
__________________________________________________________________________________________________
vgg16 (Model)                   (None, 7, 7, 512)    14714688    input_1[0][0]
                                                                 input_2[0][0]
__________________________________________________________________________________________________
Merged_feature_map (Concatenate (None, 7, 7, 1024)   0           vgg16[1][0]
                                                                 vgg16[2][0]
__________________________________________________________________________________________________
mnet_conv1 (Conv2D)             (None, 7, 7, 1024)   4195328     Merged_feature_map[0][0]
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, 7, 7, 1024)   4096        mnet_conv1[0][0]
__________________________________________________________________________________________________
activation_1 (Activation)       (None, 7, 7, 1024)   0           batch_normalization_1[0][0]
__________________________________________________________________________________________________
mnet_pool1 (MaxPooling2D)       (None, 3, 3, 1024)   0           activation_1[0][0]
__________________________________________________________________________________________________
mnet_conv2 (Conv2D)             (None, 3, 3, 2048)   8390656     mnet_pool1[0][0]
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, 3, 3, 2048)   8192        mnet_conv2[0][0]
__________________________________________________________________________________________________
activation_2 (Activation)       (None, 3, 3, 2048)   0           batch_normalization_2[0][0]
__________________________________________________________________________________________________
mnet_pool2 (MaxPooling2D)       (None, 1, 1, 2048)   0           activation_2[0][0]
__________________________________________________________________________________________________
reshape_1 (Reshape)             (None, 1, 2048)      0           mnet_pool2[0][0]
__________________________________________________________________________________________________
fc1 (Dense)                     (None, 1, 256)       524544      reshape_1[0][0]
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, 1, 256)       1024        fc1[0][0]
__________________________________________________________________________________________________
activation_3 (Activation)       (None, 1, 256)       0           batch_normalization_3[0][0]
__________________________________________________________________________________________________
fc2 (Dense)                     (None, 1, 128)       32896       activation_3[0][0]
__________________________________________________________________________________________________
batch_normalization_4 (BatchNor (None, 1, 128)       512         fc2[0][0]
__________________________________________________________________________________________________
activation_4 (Activation)       (None, 1, 128)       0           batch_normalization_4[0][0]
__________________________________________________________________________________________________
fc3 (Dense)                     (None, 1, 64)        8256        activation_4[0][0]
__________________________________________________________________________________________________
batch_normalization_5 (BatchNor (None, 1, 64)        256         fc3[0][0]
__________________________________________________________________________________________________
activation_5 (Activation)       (None, 1, 64)        0           batch_normalization_5[0][0]
__________________________________________________________________________________________________
fc4 (Dense)                     (None, 1, 1)         65          activation_5[0][0]
__________________________________________________________________________________________________
batch_normalization_6 (BatchNor (None, 1, 1)         4           fc4[0][0]
__________________________________________________________________________________________________
activation_6 (Activation)       (None, 1, 1)         0           batch_normalization_6[0][0]
__________________________________________________________________________________________________
reshape_2 (Reshape)             (None, 1)            0           activation_6[0][0]
==================================================================================================
Total params: 27,880,517
Trainable params: 13,158,787
Non-trainable params: 14,721,730

由于 VGGNet 的最后一个卷积块已经在自定义数据集上进行了训练,因此我想在图层上切割网络:

__________________________________________________________________________________________________
vgg16 (Model)                   (None, 7, 7, 512)    14714688    input_1[0][0]
                                                                 input_2[0][0]
__________________________________________________________________________________________________

并将其用作强大的特征提取器。对于此任务,我加载了微调模型:

model = load_model('model.h5')

然后尝试将新模型创建为:

new_model = Model(Input(shape=(img_width, img_height, channels)), model.layers[2].output)

这会导致以下错误:

`AttributeError: Layer vgg16 has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `get_output_at(node_index)` instead.`

请告诉我哪里做错了。

我尝试了几种方法,但以下方法非常有效。而不是创建新模型:

model = load_model('model.h5')
new_model = Model(Input(shape=(img_width, img_height, channels)), model.layers[2].output)

我使用了以下方式:

model = load_model('model.h5')
sub_model = Sequential()
for layer in model.get_layer('vgg16').layers:
    sub_model.add(layer)

希望这对其他人有所帮助。