def compute_loss(pred_encoded, gt_encoded, loss_weights=[1., 1., 1., 1.]): # Expand tuples score, pos_offsets, dim_offsets, ang_offsets = pred_encoded heatmaps, gt_pos_offsets, gt_dim_offsets, gt_ang_offsets, mask = gt_encoded score_weight, pos_weight, dim_weight, ang_weight = loss_weights # Compute losses score_loss = huber_loss(score, heatmaps) pos_loss = huber_loss(pos_offsets, gt_pos_offsets, mask.unsqueeze(2)) dim_loss = huber_loss(dim_offsets, gt_dim_offsets, mask.unsqueeze(2)) ang_loss = huber_loss(ang_offsets, gt_ang_offsets, mask.unsqueeze(2)) # Combine loss total_loss = score_loss * score_weight + pos_loss * pos_weight \ + dim_loss * dim_weight + ang_loss * ang_weight # Store scalar losses in a dictionary loss_dict = { 'score': float(score_loss), 'position': float(pos_loss), 'dimension': float(dim_loss), 'angle': float(ang_loss), 'total': float(total_loss) } total_loss = score_loss return total_loss, loss_dict
def compute_loss(pred_encoded, gt_encoded, loss_weights=[1., 1., 1., 1.]): # Expand tuples score, pos_offsets, dim_offsets, ang_offsets = pred_encoded labels, sqr_dists, gt_pos_offsets, gt_dim_offsets, gt_ang_offsets = gt_encoded score_weight, pos_weight, dim_weight, ang_weight = loss_weights # Compute losses score_loss = oft.model.loss.balanced_cross_entropy_loss(score, labels) pos_loss = huber_loss(pos_offsets, gt_pos_offsets, labels.unsqueeze(2)) dim_loss = huber_loss(dim_offsets, gt_dim_offsets, labels.unsqueeze(2)) ang_loss = huber_loss(ang_offsets, gt_ang_offsets, labels.unsqueeze(2)) # Combine loss total_loss = score_loss * score_weight + pos_loss * pos_weight \ + dim_loss * dim_weight + ang_loss * ang_weight # Store scalar losses in a dictionary loss_dict = { 'score': float(score_loss), 'position': float(pos_loss), 'dimension': float(dim_loss), 'angle': float(ang_loss), 'total': float(total_loss) } total_loss = score_loss return total_loss, loss_dict