在尝试将模型转换为tflite或.pb文件时出错:
ValueError:未知损失function:triplet_loss
我检查了在StackOverflow和GitHub中发布的不同解决方案,但它们都不适合我的代码。
#calculates triplet loss
def triplet_loss(y_true, y_pred, alpha = 0.2):
anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]
# triplet loss formula
pos_dist = tf.reduce_sum( tf.square(tf.subtract(anchor, positive)) )
neg_dist = tf.reduce_sum( tf.square(tf.subtract(anchor, negative)) )
basic_loss = pos_dist - neg_dist + alpha
loss = tf.maximum(basic_loss, 0.0)
return loss
# load the model
model = load_model('facenet_model/model.h5', custom_objects={'triplet_loss': triplet_loss})我认为我必须对三重态损失函数做一些修改,以解决价值错误。
发布于 2021-09-28 06:00:19
在前两个步骤中使用tf.reduce_sum()的轴参数等于-1。这可能会解决你的问题。
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 = pos_dist - neg_dist + alpha
loss = tf.reduce_sum(tf.maximum(basic_loss,0.0))https://stackoverflow.com/questions/57719800
复制相似问题