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

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

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

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

使用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中创建了一个多类分类项目。该项目可以对…

发表回复

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