class_docstr: The ROI heads that implement :paper:`Cascade R-CNN`.
class_docstr_tok: ["The","ROI","heads","that","implement",":","paper",":","`","Cascade","R-CNN","`","."]
docstr: Match proposals with groundtruth using the matcher at the given stage.
  Label the proposals as foreground or background based on the match.
  
  Args:
      proposals (list[Instances]): One Instances for each image, with
          the field "proposal_boxes".
      stage (int): the current stage
      targets (list[Instances]): the ground truth instances
  
  Returns:
      list[Instances]: the same proposals, but with fields "gt_classes" and "gt_boxes"
docstr_tok: ["Match","proposals","with","groundtruth","using","the","matcher","at","the","given","stage",".","Label","the","proposals","as","foreground","or","background","based","on","the","match",".","Args",":","proposals","(","list","[","Instances","]",")",":","One","Instances","for","each","image",",","with","the","field","``","proposal_boxes","''",".","stage","(","int",")",":","the","current","stage","targets","(","list","[","Instances","]",")",":","the","ground","truth","instances","Returns",":","list","[","Instances","]",":","the","same","proposals",",","but","with","fields","``","gt_classes","''","and","``","gt_boxes","''"]
code: def _match_and_label_boxes(self, proposals, stage, targets):
      """
      Match proposals with groundtruth using the matcher at the given stage.
      Label the proposals as foreground or background based on the match.
  
      Args:
          proposals (list[Instances]): One Instances for each image, with
              the field "proposal_boxes".
          stage (int): the current stage
          targets (list[Instances]): the ground truth instances
  
      Returns:
          list[Instances]: the same proposals, but with fields "gt_classes" and "gt_boxes"
      """
      num_fg_samples, num_bg_samples = [], []
      for proposals_per_image, targets_per_image in zip(
          proposals, targets
      ):
          match_quality_matrix = pairwise_iou(
              targets_per_image.gt_boxes,
              proposals_per_image.proposal_boxes,
          )
          # proposal_labels are 0 or 1
          matched_idxs, proposal_labels = self.proposal_matchers[stage](
              match_quality_matrix
          )
          if len(targets_per_image) > 0:
              gt_classes = targets_per_image.gt_classes[matched_idxs]
              # Label unmatched proposals (0 label from matcher) as background (label=num_classes)
              gt_classes[proposal_labels == 0] = self.num_classes
              gt_boxes = targets_per_image.gt_boxes[matched_idxs]
          else:
              gt_classes = (
                  torch.zeros_like(matched_idxs) + self.num_classes
              )
              gt_boxes = Boxes(
                  targets_per_image.gt_boxes.tensor.new_zeros(
                      (len(proposals_per_image), 4)
                  )
              )
          proposals_per_image.gt_classes = gt_classes
          proposals_per_image.gt_boxes = gt_boxes
  
          num_fg_samples.append((proposal_labels == 1).sum().item())
          num_bg_samples.append(
              proposal_labels.numel() - num_fg_samples[-1]
          )
  
      # Log the number of fg/bg samples in each stage
      storage = get_event_storage()
      storage.put_scalar(
          "stage{}/roi_head/num_fg_samples".format(stage),
          sum(num_fg_samples) / len(num_fg_samples),
      )
      storage.put_scalar(
          "stage{}/roi_head/num_bg_samples".format(stage),
          sum(num_bg_samples) / len(num_bg_samples),
      )
      return proposals
  
imports: from typing import List
  import torch
  from torch import nn
  from torch.autograd.function import Function
  from detectron2.config import configurable
  from detectron2.layers import ShapeSpec
  from detectron2.structures import Boxes, Instances, pairwise_iou
  from detectron2.utils.events import get_event_storage
  from ..box_regression import Box2BoxTransform
  from ..matcher import Matcher
  from ..poolers import ROIPooler
  from .box_head import build_box_head
  from .fast_rcnn import FastRCNNOutputLayers, fast_rcnn_inference
  from .roi_heads import ROI_HEADS_REGISTRY, StandardROIHeads
label_desc: Use image node_id 6 for calling the CascadeROIHeads obj's underlying member method code with example usage: obj._match_and_label_boxes(proposals, stage, targets) and returns: proposals