Machine learning 如何基于微调VGGNet16创建子模型
为了找到两幅图像之间的相似性,设计了以下网络结构 最初,我使用VGGNet16并移除分类头:Machine learning 如何基于微调VGGNet16创建子模型,machine-learning,deep-learning,conv-neural-network,transfer-learning,vgg-net,Machine Learning,Deep Learning,Conv Neural Network,Transfer Learning,Vgg Net,为了找到两幅图像之间的相似性,设计了以下网络结构 最初,我使用VGGNet16并移除分类头: vgg_model = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(img_width, img_height, channels))) 之后,我设置参数layer.trainable=False,这样网络将作为一个特征提取器工作 我向网络传递了两个不同的图像: encoded_left = vgg_mo
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)
在只训练度量网络之后,对于微调卷积层,我设置最后一个conva块进行训练。因此,在第二个训练阶段,与度量网络一起训练最后一个卷积块
现在我想把这个经过微调的网络用于另一个目的。以下是网络摘要:
__________________________________________________________________________________________________
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)
我希望这能帮助其他人