pytorch / pytorch/vision

Class weight for CrossEntropy loss in Faster RCNN

Open
#7,167 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

🚀 The feature

Hi, in order to give more importance to some classes, add a parameter to implement class weight in torchvision.models.detection.roi_heads.fastrcnn_loss.

Motivation, pitch

It would be useful in unbalanced datasets

Alternatives

I propose something like that:

def fastrcnn_loss(class_logits, box_regression, labels, regression_targets, pos_weight):
    # type: (Tensor, Tensor, List[Tensor], List[Tensor], pos_weight) -> Tuple[Tensor, Tensor]
    """
    Computes the loss for Faster R-CNN.
    Args:
        class_logits (Tensor)
        box_regression (Tensor)
        labels (list[BoxList])
        regression_targets (Tensor)
        pos_weight (Tensor)

    Returns:
        classification_loss (Tensor)
        box_loss (Tensor)
    """

    labels = torch.cat(labels, dim=0)
    regression_targets = torch.cat(regression_targets, dim=0)
    pos_weight = pos_weight.cuda() if labels.is_cuda else pos_weight
    classification_loss = F.cross_entropy(class_logits, labels, weight=pos_weight)

    # get indices that correspond to the regression targets for
    # the corresponding ground truth labels, to be used with
    # advanced indexing
    sampled_pos_inds_subset = torch.where(labels > 0)[0]
    labels_pos = labels[sampled_pos_inds_subset]
    N, num_classes = class_logits.shape
    box_regression = box_regression.reshape(N, box_regression.size(-1) // 4, 4)

    box_loss = F.smooth_l1_loss(
        box_regression[sampled_pos_inds_subset, labels_pos],
        regression_targets[sampled_pos_inds_subset],
        beta=1 / 9,
        reduction="sum",
    )
    box_loss = box_loss / labels.numel()

    return classification_loss, 
Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in torchvision/models/detection/roi_heads.py at fastrcnn_loss and trace its callers to determine how a class-weight parameter would be supplied. Review the existing CrossEntropy loss path and the proposed behavior in the issue. Done means Faster R-CNN can apply class weights for classification loss without changing box regression behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision, machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.