如何保存、恢复并使用孪生网络(使用三元组损失)进行预测

我正在尝试开发一个用于简单人脸验证(以及第二阶段的人脸识别)的孪生网络。我已经训练了一个网络,但对于如何保存和恢复模型以及如何使用训练后的模型进行预测感到有些困惑。希望领域内的有经验的人士能帮助我取得进展…

以下是我创建孪生网络的方式…

model = ResNet50(weights='imagenet')   # get the original ResNet50 modelmodel.layers.pop()   # Remove the last layerfor layer in model.layers:    layer.trainable = False   # do not train any of original layersx = model.get_layer('flatten_1').outputmodel_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 thisanchor_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 modelsiamese_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个输入,以便通过计算给定两张图片之间的距离来进行预测。我该如何保存这个模型并现在重用它呢?

提前感谢!

补充说明:

如果你想知道,这里是孪生模型的摘要。

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,984Trainable params: 262,272Non-trainable params: 23,587,712__________________________________________________________________________________________________

回答:

你可以使用下面的代码来保存你的模型siamese_model.save_weights(MODEL_WEIGHTS_FILE)

然后要加载你的模型,你需要使用siamese_model.load_weights(MODEL_WEIGHTS_FILE)

谢谢

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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