I am new to keras and tensorflow . How do I go about implementing a custom loss function while doing object detection , right now I have 5 parameters - 4 for bounding box coordinates and 1 for whether the object is present or not . Loss function should return square of difference between coordinates if object is present else if object is absent it should return a huge value as loss . This is the code I am tring right now:
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
Here I am masking other values to obtain the last value which is 1000--> object present 0 --> object absent. Is there any other better way to do this?
The value of loss when I am running this in Kaggle decreases rapidly , by 2nd epoch the loss becomes 0.