0

I've create a custom loss function for a YOLO model in Keras. However, I get different results with the loss function during training than I do when using it outside of training.

Here is my custom loss:

@tf.function 
def yolo_loss(y_pred,y_true):

    pred_boxes = y_pred[...,:4]
    true_boxes = y_true[...,:4]

    # use boxes to get proper xy and wh as from the paper
    pred_cx,pred_cy = get_all_centers(pred_boxes)
    true_cx,true_cy = get_all_centers(true_boxes)

    pred_w,pred_h = get_wh(pred_boxes)
    true_w,true_h = get_wh(true_boxes)

    # create masks
    pred_confs = y_pred[...,4]
    true_confs = y_true[...,4] # this is equivalent to 1obj from the paper... it will be 1 if there is supposed to be something there


    obj = true_confs.__copy__()
    noobj = tf.where(obj==0,1,0)
    noobj = tf.cast(noobj,tf.float32)

    pred_clss  = y_pred[...,5:]
    true_clss  = y_true[...,5:]

    xy_loss = K.sum(obj*(
            ((pred_cx-true_cx)**2)
            +
            ((pred_cy-true_cy)**2)))

    wh_loss = K.sum(obj*(((pred_w-true_w)**2)+
                         ((pred_h-true_h)**2)))

    loc_loss = xy_loss+wh_loss

    conf_loss =K.sum(obj*(pred_confs-true_confs)**2)\
               +\
               K.sum(noobj*(pred_confs-true_confs)**2)


    cls_loss = K.sum(obj*(K.sum((pred_clss-true_clss)**2,axis=-1)))

    loss = loc_loss+conf_loss+cls_loss


    tf.print(xy_loss)
    tf.print(wh_loss)
    tf.print(conf_loss)
    tf.print(cls_loss)

    return loss

The 4 numbers represent the different parts of the loss function. Code is available here: https://github.com/GoldwinXS/custom_yolo

outputs look like this (I've removed some of the text before the issue occurs):

640/1000 [==================>...........] - ETA: 8s - loss: 237.2000

0

0

231

0

768/1000 [======================>.......] - ETA: 5s - loss: 236.1667

0

0

235

0

896/1000 [=========================>....] - ETA: 2s - loss: 236.0000

0

0

194

0

1000/1000 [==============================] - 25s 25ms/step - loss: 231.6320

and then when calling my custom loss after training I get:

1561.9729

694.685425

1846

9304.88672

One should be able to reproduce this by running MyYoloTest.py

Does anyone have any clue why this could be happening?

EDIT: After much pain I've discovered that y_pred is coming into the loss function with shape TensorShape([None, None, None, None]) whereas y_true has the expected shape: TensorShape([None, 3, 3, 8])

Does anyone have any ideas as to why this would be the case?

gold
  • 1
  • 1
  • 1
  • Could it be that the helper functions like `get_wh` are for some reason not available to tensorflow when it runs? Try defining all functions you defined as a local definition/import modules inside of your function. [see here for local functions](https://stackoverflow.com/a/1414320/6301103) – jaaq Jan 31 '20 at 07:10
  • I rewrote everything explicitly (so no helper functions) and I still get the same weirdness – gold Jan 31 '20 at 13:35

0 Answers0