我刚开始接触keras和tensorflow。在目标检测时如何实现自定义损失函数,目前我有5个参数——4个用于边界框坐标,1个用于确定物体是否存在。如果物体存在,则损失函数应返回坐标差的平方;否则,应返回一个巨大的值作为损失。 这是我正在尝试的代码:
def loss_func(y_true,y_pred):
mask = np.array([False, False, False,False,True]) # check column of the class of object
mask1 = np.array([True, True, True,True,False]) # get the columns of the coordinates of B box
check_class = K.mean(K.square(tf.subtract(tf.boolean_mask(y_true,mask),tf.boolean_mask(y_pred,mask))))
mean_square = K.mean(K.square(tf.subtract(tf.boolean_mask(y_true,mask1),tf.boolean_mask(y_pred,mask1))))
value=K.mean(tf.boolean_mask(y_pred,mask))
return value*mean_square + check_class
我在这里掩盖其他值,以获得最后一个值,即1000->对象存在0->对象不存在。是否有其他更好的方法来做到这一点?
当我在Kaggle上运行时,损失值迅速下降,到第二个纪元时,损失变为0。