如何基于微调的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)

在只训练度量网络后,为了微调卷积层,我设置了最后一个卷积块进行训练。因此,在第二个训练阶段,除了度量网络外,最后一个卷积块也被训练。

现在我想将这个微调后的网络用于其他目的。这里是网络摘要:

__________________________________________________________________________________________________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,517Trainable params: 13,158,787Non-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.`

请指导我哪里做错了。


回答:

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注