我正在尝试构建一个可以对Dog Faces进行地标检测的模型 - 如果有狗脸,则提取眼睛和鼻子的x / y坐标,否则告诉我没有狗脸。
我希望我的输出张量大小为7:
我无法弄清楚如何编写一个类似于此的自定义丢失函数:
# This is largely pseudo-code!
def custom_l1_loss(y_true, y_pred):
if y_true[0] == 1:
# Probability that there's a dog face = 1
return K.sum(K.abs(y_pred[1:] - y_true[1:]), axis=-1)
else:
# Only return the difference in probabilities that a dog face is present
return y_true[0] - y_pred[0]
问题:有没有人在Keras写过类似的条件损失函数?
答案 0 :(得分:1)
您可以提取列[0]并将其用作掩码,如下面的代码所示,以便将条件损失计算简化为一行计算。
def custom_l1_loss(y_true, y_pred):
y_true_present = y_true[...,0:1]
y_pred_present = y_pred[...,0:1]
loss = K.sum(y_true_present*K.abs(y_pred[...,1:] - y_true[...,1:]),axis=-1)+ K.sum((1-y_true_present)*K.abs(y_true_present-y_pred_present),axis=-1)
return loss