pytorch / pytorch/vision

How to make the RegionProposalNetwork generate more proposals in FasterRCNN?

Open
#8,026 1 comment 1 reaction 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

I'm trying to update the proposal losses function of MaskRCNN to increase the recall. I'm trying to do this by adding a positive weight to the BCE function

How I create my proposal losses function:

CLASS_WEIGHTS = torch.tensor([50])

def compute_loss(
        objectness: Tensor, pred_bbox_deltas: Tensor, labels: List[Tensor], regression_targets: List[Tensor]
    ) -> Tuple[Tensor, Tensor]:
    """
    Args:
        objectness (Tensor)
        pred_bbox_deltas (Tensor)
        labels (List[Tensor])
        regression_targets (List[Tensor])

    Returns:
        objectness_loss (Tensor)
        box_loss (Tensor)
    """

    sampled_pos_inds, sampled_neg_inds = model.rpn.fg_bg_sampler(labels)
    sampled_pos_inds = torch.where(torch.cat(sampled_pos_inds, dim=0))[0]
    sampled_neg_inds = torch.where(torch.cat(sampled_neg_inds, dim=0))[0]

    sampled_inds = torch.cat([sampled_pos_inds, sampled_neg_inds], dim=0)

    objectness = objectness.flatten()

    labels = torch.cat(labels, dim=0)
    regression_targets = torch.cat(regression_targets, dim=0)

    box_loss = F.smooth_l1_loss(
        pred_bbox_deltas[sampled_pos_inds],
        regression_targets[sampled_pos_inds],
        beta=1 / 9,
        reduction="sum",
    ) / (sampled_inds.numel())

    objectness_loss = F.binary_cross_entropy_with_logits(objectness[sampled_inds], labels[sampled_inds],
                                                        pos_weight=CLASS_WEIGHTS # USE CLASS WEIGHT HERE
                                                        )
    return objectness_loss, box_loss

Then how I set the model to use this proposal losses function:

model = maskrcnn_resnet50_fpn(weights=MaskRCNN_ResNet50_FPN_Weights.DEFAULT)
model.rpn.compute_loss = compute_loss

When I train the model now:

  • the loss increases significantly (e.g. before it was 1, now it is like 50, which is expected)
  • BUT the recall stays around the same (e.g. stagnates around 0.55 after training for several epochs)

Why is this the case? How do I get the recall to improve (i.e. how do I generate more proposals)?

FYI: I already tried setting the score threshold to 0, this didn't do anything either…

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 by reading the model.rpn.compute_loss entry point used with maskrcnn_resnet50_fpn, then trace how binary_cross_entropy_with_logits and the score threshold affect proposal selection. Reproduce the reported loss and recall measurements while checking the proposal-generation settings; done means the cause is explained and a documented change produces a measurable recall improvement.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
computer-vision, machine-learning
Issue type
Bug
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.