gpt4 book ai didi

tensorflow - 如何使用孪生网络保存、恢复、预测(带有三元组损失)

转载 作者:行者123 更新时间:2023-11-30 09:47:33 25 4
gpt4 key购买 nike

我正在尝试开发一个暹罗网络来进行简单的人脸验证(以及第二阶段的识别)。我有一个我设法训练的网络,但当谈到如何保存和恢复模型+使用训练后的模型进行预测时,我有点困惑。希望该领域有经验的人可以帮助取得进步..

这是我创建暹罗网络的方法,首先......

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)

谢谢

关于tensorflow - 如何使用孪生网络保存、恢复、预测(带有三元组损失),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50558154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com