class_docstr: The RoI head for `Prime Sample Attention in Object Detection
  <https://arxiv.org/abs/1904.04821>`_.
docstr: Perform forward propagation and loss calculation of the bbox head on
  the features of the upstream network.
  
  Args:
      x (tuple[Tensor]): List of multi-level img features.
      sampling_results (list["obj:`SamplingResult`]): Sampling results.
  
  Returns:
      dict[str, Tensor]: Usually returns a dictionary with keys:
  
      - `cls_score` (Tensor): Classification scores.
      - `bbox_pred` (Tensor): Box energies / deltas.
      - `bbox_feats` (Tensor): Extract bbox RoI features.
      - `loss_bbox` (dict): A dictionary of bbox loss components.
docstr_tok: ["Perform","forward","propagation","and","loss","calculation","of","the","bbox","head","on","the","features","of","the","upstream","network",".","Args",":","x","(","tuple","[","Tensor","]",")",":","List","of","multi-level","img","features",".","sampling_results","(","list","[","``","obj",":","`","SamplingResult","`","]",")",":","Sampling","results",".","Returns",":","dict","[","str",",","Tensor","]",":","Usually","returns","a","dictionary","with","keys",":","-","`","cls_score","`","(","Tensor",")",":","Classification","scores",".","-","`","bbox_pred","`","(","Tensor",")",":","Box","energies","\/","deltas",".","-","`","bbox_feats","`","(","Tensor",")",":","Extract","bbox","RoI","features",".","-","`","loss_bbox","`","(","dict",")",":","A","dictionary","of","bbox","loss","components","."]
code: def bbox_loss(
      self,
      x: Tuple[Tensor],
      sampling_results: List[SamplingResult],
      neg_label_weights: List[Tensor] = None,
  ) -> dict:
      """Perform forward propagation and loss calculation of the bbox head on
      the features of the upstream network.
  
      Args:
          x (tuple[Tensor]): List of multi-level img features.
          sampling_results (list["obj:`SamplingResult`]): Sampling results.
  
      Returns:
          dict[str, Tensor]: Usually returns a dictionary with keys:
  
          - `cls_score` (Tensor): Classification scores.
          - `bbox_pred` (Tensor): Box energies / deltas.
          - `bbox_feats` (Tensor): Extract bbox RoI features.
          - `loss_bbox` (dict): A dictionary of bbox loss components.
      """
      rois = bbox2roi([res.priors for res in sampling_results])
      bbox_results = self._bbox_forward(x, rois)
      bbox_targets = self.bbox_head.get_targets(
          sampling_results, self.train_cfg
      )
  
      # neg_label_weights obtained by sampler is image-wise, mapping back to
      # the corresponding location in label weights
      if neg_label_weights[0] is not None:
          label_weights = bbox_targets[1]
          cur_num_rois = 0
          for i in range(len(sampling_results)):
              num_pos = sampling_results[i].pos_inds.size(0)
              num_neg = sampling_results[i].neg_inds.size(0)
              label_weights[
                  cur_num_rois
                  + num_pos : cur_num_rois
                  + num_pos
                  + num_neg
              ] = neg_label_weights[i]
              cur_num_rois += num_pos + num_neg
  
      cls_score = bbox_results["cls_score"]
      bbox_pred = bbox_results["bbox_pred"]
  
      # Apply ISR-P
      isr_cfg = self.train_cfg.get("isr", None)
      if isr_cfg is not None:
          bbox_targets = isr_p(
              cls_score,
              bbox_pred,
              bbox_targets,
              rois,
              sampling_results,
              self.bbox_head.loss_cls,
              self.bbox_head.bbox_coder,
              **isr_cfg,
              num_class=self.bbox_head.num_classes,
          )
      loss_bbox = self.bbox_head.loss(
          cls_score, bbox_pred, rois, *bbox_targets
      )
  
      # Add CARL Loss
      carl_cfg = self.train_cfg.get("carl", None)
      if carl_cfg is not None:
          loss_carl = carl_loss(
              cls_score,
              bbox_targets[0],
              bbox_pred,
              bbox_targets[2],
              self.bbox_head.loss_bbox,
              **carl_cfg,
              num_class=self.bbox_head.num_classes,
          )
          loss_bbox.update(loss_carl)
  
      bbox_results.update(loss_bbox=loss_bbox)
      return bbox_results
  
file_path: mmdetection/mmdet/models/roi_heads/pisa_roi_head.py
imports: from typing import List, Tuple
  from torch import Tensor
  from mmdet.models.task_modules import SamplingResult
  from mmdet.registry import MODELS
  from mmdet.structures import DetDataSample
  from mmdet.structures.bbox import bbox2roi
  from mmdet.utils import InstanceList
  from ..losses.pisa_loss import carl_loss, isr_p
  from ..utils import unpack_gt_instances
  from .standard_roi_head import StandardRoIHead
label_desc: Use image node_id 2 for calling the PISARoIHead obj's underlying member method code with example usage: obj.bbox_loss(x, sampling_results, neg_label_weights) and returns: bbox_results