question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to calculate xy-keypoints during training?

See original GitHub issue

I’d like to implement a new loss for the keypoint head. For this loss, I need the xy-coordinates of the predicted keypoints. However, in _forward_keypoint and keypoint_rcnn_loss, I only have access to keypoint_logits. To convert keypoint_logits into xy-keypoints, I think I need to apply the steps in keypoint_rcnn_inference.

Now I have two questions:

  1. Is this approach correct so far?
  2. For the conversion from keypoint_logits into xy-keypoints, I need to specify the bounding boxes of the instances. In keypoint_rcnn_inference the predicted boxes from the box head are used. However, during training I don’t have access to this information yet. Can I just use the proposal boxes?

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
maxfrei750commented, Sep 13, 2020

@cognitiveRobot I used the function heatmaps_to_keypoints with the proposed bounding boxes and implemented a custom_loss_function which operates on the xy-keypoints:

def custom_keypoint_rcnn_loss(pred_keypoint_logits, instances):
    """
    Arguments:
        pred_keypoint_logits (Tensor): A tensor of shape (N, K, S, S) where N is the total number
            of instances in the batch, K is the number of keypoints, and S is the side length
            of the keypoint heatmap. The values are spatial logits.
        instances (list[Instances]): A list of M Instances, where M is the batch size.
            These instances are predictions from the model
            that are in 1:1 correspondence with pred_keypoint_logits.
            Each Instances should contain a `gt_keypoints` field containing a `structures.Keypoint`
            instance.
    Returns a scalar tensor containing the loss.
    """

    device = pred_keypoint_logits.device

    # flatten all bboxes from all images together (list[Boxes] -> Rx4 tensor)
    bboxes_flat = cat([b.proposal_boxes.tensor for b in instances], dim=0)

    keypoint_results = heatmaps_to_keypoints(pred_keypoint_logits, bboxes_flat)
    num_instances_per_image = [len(i) for i in instances]
    keypoint_results = keypoint_results.split(num_instances_per_image, dim=0)

    instance_losses = torch.Tensor().to(device)
    instance_weights = torch.Tensor().to(device)

    for keypoint_results_per_image, instances_per_image in zip(keypoint_results, instances):
        # keypoint_results_per_image is (num instances)x(num keypoints)x(x, y, score, prob)
        keypoints_gt_xy = instances_per_image.gt_keypoints.tensor[:, :, :2]
        keypoints_xy = keypoint_results_per_image[:, :, [0, 1]]

        keypoints_p = keypoint_results_per_image[:, :, 3]
        instance_weights = torch.cat([instance_weights, keypoints_p.sum(1)])
        instance_losses_per_image = torch.Tensor().to(device)

        for instance_keypoints_xy, instance_keypoints_gt_xy in zip(keypoints_xy, keypoints_gt_xy):

            # You need to implement the custom_loss_function yourself.
            instance_loss = custom_loss_function(instance_keypoints_xy, instance_keypoints_gt_xy)

            instance_losses_per_image = torch.cat(
                [instance_losses_per_image, instance_loss.unsqueeze(0)]
            )

        instance_losses = torch.cat([instance_losses, instance_losses_per_image])

    keypoint_loss = torch.sum(instance_losses * instance_weights)

    # Maybe you need normalization.
    # normalizer = sum(num_instances_per_image)
    # keypoint_loss /= normalizer

    return keypoint_loss

Ultimately, I did not end up using this loss function but stuck to the default keypoint loss, so this function is not very well tested. Also, it is obviously shamelessly copied from various places in the original code.

0reactions
cognitiveRobotcommented, Sep 13, 2020

@maxfrei750, thanks for sharing. If I test, I will let you know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Calculate Your Training Heart Rate Zones - ACTIVE
The key to getting results is elevating your heart rate into the correct training zone, so effort matches goals. Learn how to calculate...
Read more >
5 Easy Ways to Measure the ROI of Training
The traditional ROI formula for training is the program benefits (net profit) minus the training costs and then divided by the program costs....
Read more >
How to Calculate Sweat Rate - TrainingPeaks
Determining your sweat rate is the first step in creating a successful hydration strategy. Here's how to calculate sweat rate and why it's...
Read more >
Training Video: Calculating FTEs - VAWA MEI
This is a recording of the webinar, “ Calculating FTEs for your OVW Semi-Annual Progress Report,” that was presented on July 9, 2021,...
Read more >
Runner's World's Training Pace Calculator
Calculate your running training paces - just enter a recent race time into our training pace calculator and we'll do the rest.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found