def rpn_loss(rpn_loc, rpn_score, gt_rpn_loc, gt_rpn_score):
  rpn_cls_loss = F.cross_entropy(rpn_score, gt_rpn_score.long(), ignore_index = -1)

  print(rpn_cls_loss)

  pos = gt_rpn_score > 0

  mask = pos.unsqueeze(1).expand_as(rpn_loc)

  # extract bounding boxes from positive labels

  mask_loc_preds = rpn_loc[mask].view(-1, 4)

  mask_loc_targets = gt_rpn_loc[mask].view(-1, 4)

  x = torch.abs(mask_loc_targets - mask_loc_preds)

  rpn_loc_loss = ((x < 1).float() * 0.5 * x**2) + ((x >= 1).float() * (x-0.5))

  # apply loss

  x = torch.abs(mask_loc_targets - mask_loc_preds)

  rpn_loc_loss = ((x < 1).float() * 0.5 * x**2) + ((x >= 1).float() * (x-0.5))

  # Combine and apply our class loss, using a regularization parameter

  rpn_lambda = 10.

  N_reg = (gt_rpn_score >0).float().sum()

  rpn_loc_loss = rpn_loc_loss.sum() / N_reg

  rpn_loss = rpn_cls_loss + (rpn_lambda * rpn_loc_loss)

  return rpn_loss