如何使用孪生网络(带有三重态损失)保存、恢复、进行预测
how to save, restore, make predictions with siamese network (with triplet loss)
我正在尝试开发一个孪生网络,用于简单的人脸验证(以及第二阶段的识别)。我有一个我设法训练的网络,但是当涉及到如何保存和恢复模型+使用训练有素的模型进行预测时,我有点困惑。希望领域内有经验的人可以帮助取得进展..
下面是我创建孪生网络的方法,首先...
model = ResNet50(weights='imagenet') # get the original ResNet50 model
model.layers.pop() # Remove the last layer
for layer in model.layers:
layer.trainable = False # do not train any of original layers
x = model.get_layer('flatten_1').output
model_out = Dense(128, activation='relu', name='model_out')(x)
model_out = Lambda(lambda x: K.l2_normalize(x,axis=-1))(model_out)
new_model = Model(inputs=model.input, outputs=model_out)
# At this point, a new layer (with 128 units) added and normalization applied.
# Now create siamese network on top of this
anchor_in = Input(shape=(224, 224, 3))
positive_in = Input(shape=(224, 224, 3))
negative_in = Input(shape=(224, 224, 3))
anchor_out = new_model(anchor_in)
positive_out = new_model(positive_in)
negative_out = new_model(negative_in)
merged_vector = concatenate([anchor_out, positive_out, negative_out], axis=-1)
# Define the trainable model
siamese_model = Model(inputs=[anchor_in, positive_in, negative_in],
outputs=merged_vector)
siamese_model.compile(optimizer=Adam(lr=.0001),
loss=triplet_loss,
metrics=[dist_between_anchor_positive,
dist_between_anchor_negative])
我训练 siamese_model。当我训练它时,如果我正确地解释结果,它并不是真正在训练底层模型,它只是在训练新的连体网络(本质上,只是训练了最后一层)。
但是这个模型有 3 个输入流。训练结束后,我需要以某种方式保存此模型,使其仅需要 1 或 2 个输入,以便我可以通过计算 2 个给定图像之间的距离来执行预测。如何保存此模型并立即重复使用?
提前致谢!
附录:
如果您想知道,这里是连体模型的摘要。
siamese_model.summary()
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_2 (InputLayer) (None, 224, 224, 3) 0
__________________________________________________________________________________________________
input_3 (InputLayer) (None, 224, 224, 3) 0
__________________________________________________________________________________________________
input_4 (InputLayer) (None, 224, 224, 3) 0
__________________________________________________________________________________________________
model_1 (Model) (None, 128) 23849984 input_2[0][0]
input_3[0][0]
input_4[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 384) 0 model_1[1][0]
model_1[2][0]
model_1[3][0]
==================================================================================================
Total params: 23,849,984
Trainable params: 262,272
Non-trainable params: 23,587,712
__________________________________________________________________________________________________
您可以使用下面的代码来保存您的模型
siamese_model.save_weights(MODEL_WEIGHTS_FILE)
然后加载您需要使用的模型
siamese_model.load_weights(MODEL_WEIGHTS_FILE)
谢谢
我正在尝试开发一个孪生网络,用于简单的人脸验证(以及第二阶段的识别)。我有一个我设法训练的网络,但是当涉及到如何保存和恢复模型+使用训练有素的模型进行预测时,我有点困惑。希望领域内有经验的人可以帮助取得进展..
下面是我创建孪生网络的方法,首先...
model = ResNet50(weights='imagenet') # get the original ResNet50 model
model.layers.pop() # Remove the last layer
for layer in model.layers:
layer.trainable = False # do not train any of original layers
x = model.get_layer('flatten_1').output
model_out = Dense(128, activation='relu', name='model_out')(x)
model_out = Lambda(lambda x: K.l2_normalize(x,axis=-1))(model_out)
new_model = Model(inputs=model.input, outputs=model_out)
# At this point, a new layer (with 128 units) added and normalization applied.
# Now create siamese network on top of this
anchor_in = Input(shape=(224, 224, 3))
positive_in = Input(shape=(224, 224, 3))
negative_in = Input(shape=(224, 224, 3))
anchor_out = new_model(anchor_in)
positive_out = new_model(positive_in)
negative_out = new_model(negative_in)
merged_vector = concatenate([anchor_out, positive_out, negative_out], axis=-1)
# Define the trainable model
siamese_model = Model(inputs=[anchor_in, positive_in, negative_in],
outputs=merged_vector)
siamese_model.compile(optimizer=Adam(lr=.0001),
loss=triplet_loss,
metrics=[dist_between_anchor_positive,
dist_between_anchor_negative])
我训练 siamese_model。当我训练它时,如果我正确地解释结果,它并不是真正在训练底层模型,它只是在训练新的连体网络(本质上,只是训练了最后一层)。
但是这个模型有 3 个输入流。训练结束后,我需要以某种方式保存此模型,使其仅需要 1 或 2 个输入,以便我可以通过计算 2 个给定图像之间的距离来执行预测。如何保存此模型并立即重复使用?
提前致谢!
附录:
如果您想知道,这里是连体模型的摘要。
siamese_model.summary()
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_2 (InputLayer) (None, 224, 224, 3) 0
__________________________________________________________________________________________________
input_3 (InputLayer) (None, 224, 224, 3) 0
__________________________________________________________________________________________________
input_4 (InputLayer) (None, 224, 224, 3) 0
__________________________________________________________________________________________________
model_1 (Model) (None, 128) 23849984 input_2[0][0]
input_3[0][0]
input_4[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 384) 0 model_1[1][0]
model_1[2][0]
model_1[3][0]
==================================================================================================
Total params: 23,849,984
Trainable params: 262,272
Non-trainable params: 23,587,712
__________________________________________________________________________________________________
您可以使用下面的代码来保存您的模型 siamese_model.save_weights(MODEL_WEIGHTS_FILE)
然后加载您需要使用的模型 siamese_model.load_weights(MODEL_WEIGHTS_FILE)
谢谢