gpt4 book ai didi

python - 使用 FaceNet 创建三元组损失模型时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:25:25 24 4
gpt4 key购买 nike

我正在尝试使用 FacenetModel 实现三元组损失模型。我使用了 coursera 作业中提供的 Facenet 实现。

每当我编译模型时,我都会收到此错误:

ValueError: No data provided for "FaceRecoModel". Need data for each key in: ['FaceRecoModel', 'FaceRecoModel', 'FaceRecoModel']

我的代码:

def batch_generator(batch_size = 64):
while True:
pos = positiveImg[np.random.choice(len(positiveImg), batch_size)]
neg = negativeImg[np.random.choice(len(negativeImg), batch_size)]
anc = anchorsImg[np.random.choice(len(anchorsImg), batch_size)]

x_data = {'inp1': anc,
'inp2': pos,
'inp3': neg
}
y_data = {'y1': np.zeros((64,0)),
'y2': np.zeros((64,0)),
'y3': np.zeros((64,0))}
yield (x_data, y_data)

def triplet_loss(y_true, y_pred):
anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]
pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis=-1)
neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis=-1)
basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), 0.2)
loss = tf.reduce_sum(tf.maximum(basic_loss, 0.0))

return loss

def getModels():
FRmodel = keras.models.load_model('FR.h5', custom_objects={'triplet_loss': triplet_loss})

inp1 = Input((3, 96, 96), name= 'inp1')
inp2 = Input((3, 96, 96), name= 'inp2')
inp3 = Input((3, 96, 96), name= 'inp3')

pred1 = FRmodel(inp1)
pred2 = FRmodel(inp2)
pred3 = FRmodel(inp3)

inputs = [inp1, inp2, inp3]
outputs = [pred1, pred2, pred3]

model = keras.models.Model(inputs=[inp1, inp2, inp3], outputs= [pred1, pred2, pred3])

return FRmodel, model

generator = batch_generator(64)

FRmodel, my_model = getModels()
my_model.compile(optimizer = 'adam', loss = triplet_loss, metrics = ['accuracy'])
my_model.fit_generator(generator, epochs=5,steps_per_epoch=30)

预训练 Facenet 模型摘要:

FRmodel.summary() : https://codeshare.io/arxmev

my_model.summary() : https://codeshare.io/arx3N6

最佳答案

在 coursera 的论坛上找到了解决方案。这有点棘手。我必须使用 Lambda 在 keras 层包装器中添加三元组损失的欧几里德距离。根据文档:

Wraps arbitrary expression as a Layer object.

新的实现:

`

    def triplet_loss_v2(y_true, y_pred):
positive, negative = y_pred[:,0,0], y_pred[:,1,0]
margin = K.constant(0.2)
loss = K.mean(K.maximum(K.constant(0), positive - negative + margin))
return loss # shape = [1]

def euclidean_distance(vects):
x, y = vects # shape = [batch_size, 2, 1]
dist = K.sqrt(K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
return dist # shape = [batch_size, 1]

FRmodel = faceRecoModel(input_shape=(3, 96, 96))
load_weights_from_FaceNet(FRmodel)

for layer in FRmodel.layers[0: 80]:
layer.trainable = False

input_shape=(3, 96, 96)
anchor = Input(shape=input_shape, name = 'anchor')
anchorPositive = Input(shape=input_shape, name = 'anchorPositive')
anchorNegative = Input(shape=input_shape, name = 'anchorNegative')

anchorCode = FRmodel(anchor)
anchorPosCode = FRmodel(anchorPositive)
anchorNegCode = FRmodel(anchorNegative)

positive_dist = Lambda(euclidean_distance, name='pos_dist')([anchorCode, anchorPosCode])
negative_dist = Lambda(euclidean_distance, name='neg_dist')([anchorCode, anchorNegCode])
stacked_dists = Lambda(lambda vects: K.stack(vects, axis=1), name='stacked_dists')([positive_dist, negative_dist]) # shape = [batch_size, 2, 1]

tripletModel = Model([anchor, anchorPositive, anchorNegative], stacked_dists, name='triple_siamese')
tripletModel.compile(optimizer = 'adadelta', loss = triplet_loss_v2, metrics = None)

gen = batch_generator(64)
tripletModel.fit_generator(gen, epochs=1,steps_per_epoch=5)`

关于python - 使用 FaceNet 创建三元组损失模型时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53870456/

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