class_docstr: Region Proposal Network, introduced by :paper:`Faster R-CNN`.
class_docstr_tok: ["Region","Proposal","Network",",","introduced","by",":","paper",":","`","Faster","R-CNN","`","."]
docstr: Return the losses from a set of RPN predictions and their associated ground-truth.
  
  Args:
      anchors (list[Boxes or RotatedBoxes]): anchors for each feature map, each
          has shape (Hi*Wi*A, B), where B is box dimension (4 or 5).
      pred_objectness_logits (list[Tensor]): A list of L elements.
          Element i is a tensor of shape (N, Hi*Wi*A) representing
          the predicted objectness logits for all anchors.
      gt_labels (list[Tensor]): Output of :meth:`label_and_sample_anchors`.
      pred_anchor_deltas (list[Tensor]): A list of L elements. Element i is a tensor of shape
          (N, Hi*Wi*A, 4 or 5) representing the predicted "deltas" used to transform anchors
          to proposals.
      gt_boxes (list[Tensor]): Output of :meth:`label_and_sample_anchors`.
  
  Returns:
      dict[loss name -> loss value]: A dict mapping from loss name to loss value.
          Loss names are: `loss_rpn_cls` for objectness classification and
          `loss_rpn_loc` for proposal localization.
docstr_tok: ["Return","the","losses","from","a","set","of","RPN","predictions","and","their","associated","ground-truth",".","Args",":","anchors","(","list","[","Boxes","or","RotatedBoxes","]",")",":","anchors","for","each","feature","map",",","each","has","shape","(","Hi","*","Wi","*","A",",","B",")",",","where","B","is","box","dimension","(","4","or","5",")",".","pred_objectness_logits","(","list","[","Tensor","]",")",":","A","list","of","L","elements",".","Element","i","is","a","tensor","of","shape","(","N",",","Hi","*","Wi","*","A",")","representing","the","predicted","objectness","logits","for","all","anchors",".","gt_labels","(","list","[","Tensor","]",")",":","Output","of",":","meth",":","`","label_and_sample_anchors","`",".","pred_anchor_deltas","(","list","[","Tensor","]",")",":","A","list","of","L","elements",".","Element","i","is","a","tensor","of","shape","(","N",",","Hi","*","Wi","*","A",",","4","or","5",")","representing","the","predicted","``","deltas","''","used","to","transform","anchors","to","proposals",".","gt_boxes","(","list","[","Tensor","]",")",":","Output","of",":","meth",":","`","label_and_sample_anchors","`",".","Returns",":","dict","[","loss","name","-",">","loss","value","]",":","A","dict","mapping","from","loss","name","to","loss","value",".","Loss","names","are",":","`","loss_rpn_cls","`","for","objectness","classification","and","`","loss_rpn_loc","`","for","proposal","localization","."]
code: def losses(
      self,
      anchors: List[Boxes],
      pred_objectness_logits: List[torch.Tensor],
      gt_labels: List[torch.Tensor],
      pred_anchor_deltas: List[torch.Tensor],
      gt_boxes: List[torch.Tensor],
  ) -> Dict[str, torch.Tensor]:
      """
      Return the losses from a set of RPN predictions and their associated ground-truth.
  
      Args:
          anchors (list[Boxes or RotatedBoxes]): anchors for each feature map, each
              has shape (Hi*Wi*A, B), where B is box dimension (4 or 5).
          pred_objectness_logits (list[Tensor]): A list of L elements.
              Element i is a tensor of shape (N, Hi*Wi*A) representing
              the predicted objectness logits for all anchors.
          gt_labels (list[Tensor]): Output of :meth:`label_and_sample_anchors`.
          pred_anchor_deltas (list[Tensor]): A list of L elements. Element i is a tensor of shape
              (N, Hi*Wi*A, 4 or 5) representing the predicted "deltas" used to transform anchors
              to proposals.
          gt_boxes (list[Tensor]): Output of :meth:`label_and_sample_anchors`.
  
      Returns:
          dict[loss name -> loss value]: A dict mapping from loss name to loss value.
              Loss names are: `loss_rpn_cls` for objectness classification and
              `loss_rpn_loc` for proposal localization.
      """
      num_images = len(gt_labels)
      gt_labels = torch.stack(gt_labels)  # (N, sum(Hi*Wi*Ai))
  
      # Log the number of positive/negative anchors per-image that's used in training
      pos_mask = gt_labels == 1
      num_pos_anchors = pos_mask.sum().item()
      num_neg_anchors = (gt_labels == 0).sum().item()
      storage = get_event_storage()
      storage.put_scalar(
          "rpn/num_pos_anchors", num_pos_anchors / num_images
      )
      storage.put_scalar(
          "rpn/num_neg_anchors", num_neg_anchors / num_images
      )
  
      localization_loss = _dense_box_regression_loss(
          anchors,
          self.box2box_transform,
          pred_anchor_deltas,
          gt_boxes,
          pos_mask,
          box_reg_loss_type=self.box_reg_loss_type,
          smooth_l1_beta=self.smooth_l1_beta,
      )
  
      valid_mask = gt_labels >= 0
      objectness_loss = F.binary_cross_entropy_with_logits(
          cat(pred_objectness_logits, dim=1)[valid_mask],
          gt_labels[valid_mask].to(torch.float32),
          reduction="sum",
      )
      normalizer = self.batch_size_per_image * num_images
      losses = {
          "loss_rpn_cls": objectness_loss / normalizer,
          # The original Faster R-CNN paper uses a slightly different normalizer
          # for loc loss. But it doesn't matter in practice
          "loss_rpn_loc": localization_loss / normalizer,
      }
      losses = {
          k: v * self.loss_weight.get(k, 1.0) for k, v in losses.items()
      }
      return losses
  
imports: from typing import Dict, List, Optional, Tuple, Union
  import torch
  import torch.nn.functional
  from torch import nn
  from detectron2.config import configurable
  from detectron2.layers import Conv2d, ShapeSpec, cat
  from detectron2.structures import Boxes, ImageList, Instances, pairwise_iou
  from detectron2.utils.events import get_event_storage
  from detectron2.utils.memory import retry_if_cuda_oom
  from detectron2.utils.registry import Registry
  from ..anchor_generator import build_anchor_generator
  from ..box_regression import Box2BoxTransform, _dense_box_regression_loss
  from ..matcher import Matcher
  from ..sampling import subsample_labels
  from .build import PROPOSAL_GENERATOR_REGISTRY
  from .proposal_utils import find_top_rpn_proposals
label_desc: Use image node_id 5 for calling the RPN obj's underlying member method code with example usage: obj.losses(anchors, pred_objectness_logits, gt_labels, pred_anchor_deltas, gt_boxes) and returns: losses