class_docstr: Simplest RoI head, with only two fc layers for classification and
  regression respectively.
class_docstr_tok: ["Simplest","RoI","head",",","with","only","two","fc","layers","for","classification","and","regression","respectively","."]
docstr: Refine bboxes during training.
  
  Args:
      sampling_results (List[:obj:`SamplingResult`] or
          List[:obj:`InstanceData`]): Sampling results.
          :obj:`SamplingResult` is the real sampling results
          calculate from bbox_head, while :obj:`InstanceData` is
          fake sampling results, e.g., in Sparse R-CNN or QueryInst, etc.
      bbox_results (dict): Usually is a dictionary with keys:
  
          - `cls_score` (Tensor): Classification scores.
          - `bbox_pred` (Tensor): Box energies / deltas.
          - `rois` (Tensor): RoIs with the shape (n, 5) where the first
            column indicates batch id of each RoI.
          - `bbox_targets` (tuple):  Ground truth for proposals in a
            single image. Containing the following list of Tensors:
            (labels, label_weights, bbox_targets, bbox_weights)
      batch_img_metas (List[dict]): List of image information.
  
  Returns:
      list[:obj:`InstanceData`]: Refined bboxes of each image.
  
  Example:
      >>> # xdoctest: +REQUIRES(module:kwarray)
      >>> import numpy as np
      >>> from mmdet.models.task_modules.samplers.
      ... sampling_result import random_boxes
      >>> from mmdet.models.task_modules.samplers import SamplingResult
      >>> self = BBoxHead(reg_class_agnostic=True)
      >>> n_roi = 2
      >>> n_img = 4
      >>> scale = 512
      >>> rng = np.random.RandomState(0)
      ... batch_img_metas = [{'img_shape': (scale, scale)}
      >>>                     for _ in range(n_img)]
      >>> sampling_results = [SamplingResult.random(rng=10)
      ...                     for _ in range(n_img)]
      >>> # Create rois in the expected format
      >>> roi_boxes = random_boxes(n_roi, scale=scale, rng=rng)
      >>> img_ids = torch.randint(0, n_img, (n_roi,))
      >>> img_ids = img_ids.float()
      >>> rois = torch.cat([img_ids[:, None], roi_boxes], dim=1)
      >>> # Create other args
      >>> labels = torch.randint(0, 81, (scale,)).long()
      >>> bbox_preds = random_boxes(n_roi, scale=scale, rng=rng)
      >>> cls_score = torch.randn((scale, 81))
      ... # For each image, pretend random positive boxes are gts
      >>> bbox_targets = (labels, None, None, None)
      ... bbox_results = dict(rois=rois, bbox_pred=bbox_preds,
      ...                     cls_score=cls_score,
      ...                     bbox_targets=bbox_targets)
      >>> bboxes_list = self.refine_bboxes(sampling_results,
      ...                                  bbox_results,
      ...                                  batch_img_metas)
      >>> print(bboxes_list)
docstr_tok: ["Refine","bboxes","during","training",".","Args",":","sampling_results","(","List","[",":","obj",":","`","SamplingResult","`","]","or","List","[",":","obj",":","`","InstanceData","`","]",")",":","Sampling","results",".",":","obj",":","`","SamplingResult","`","is","the","real","sampling","results","calculate","from","bbox_head",",","while",":","obj",":","`","InstanceData","`","is","fake","sampling","results",",","e.g.",",","in","Sparse","R-CNN","or","QueryInst",",","etc",".","bbox_results","(","dict",")",":","Usually","is","a","dictionary","with","keys",":","-","`","cls_score","`","(","Tensor",")",":","Classification","scores",".","-","`","bbox_pred","`","(","Tensor",")",":","Box","energies","\/","deltas",".","-","`","rois","`","(","Tensor",")",":","RoIs","with","the","shape","(","n",",","5",")","where","the","first","column","indicates","batch","id","of","each","RoI",".","-","`","bbox_targets","`","(","tuple",")",":","Ground","truth","for","proposals","in","a","single","image",".","Containing","the","following","list","of","Tensors",":","(","labels",",","label_weights",",","bbox_targets",",","bbox_weights",")","batch_img_metas","(","List","[","dict","]",")",":","List","of","image","information",".","Returns",":","list","[",":","obj",":","`","InstanceData","`","]",":","Refined","bboxes","of","each","image",".","Example",":",">",">",">","#","xdoctest",":","+REQUIRES","(","module",":","kwarray",")",">",">",">","import","numpy","as","np",">",">",">","from","mmdet.models.task_modules.samplers",".","...","sampling_result","import","random_boxes",">",">",">","from","mmdet.models.task_modules.samplers","import","SamplingResult",">",">",">","self","=","BBoxHead","(","reg_class_agnostic=True",")",">",">",">","n_roi","=","2",">",">",">","n_img","=","4",">",">",">","scale","=","512",">",">",">","rng","=","np.random.RandomState","(","0",")","...","batch_img_metas","=","[","{","'img_shape","'",":","(","scale",",","scale",")","}",">",">",">","for","_","in","range","(","n_img",")","]",">",">",">","sampling_results","=","[","SamplingResult.random","(","rng=10",")","...","for","_","in","range","(","n_img",")","]",">",">",">","#","Create","rois","in","the","expected","format",">",">",">","roi_boxes","=","random_boxes","(","n_roi",",","scale=scale",",","rng=rng",")",">",">",">","img_ids","=","torch.randint","(","0",",","n_img",",","(","n_roi",",",")",")",">",">",">","img_ids","=","img_ids.float","(",")",">",">",">","rois","=","torch.cat","(","[","img_ids","[",":",",","None","]",",","roi_boxes","]",",","dim=1",")",">",">",">","#","Create","other","args",">",">",">","labels","=","torch.randint","(","0",",","81",",","(","scale",",",")",")",".long","(",")",">",">",">","bbox_preds","=","random_boxes","(","n_roi",",","scale=scale",",","rng=rng",")",">",">",">","cls_score","=","torch.randn","(","(","scale",",","81",")",")","...","#","For","each","image",",","pretend","random","positive","boxes","are","gts",">",">",">","bbox_targets","=","(","labels",",","None",",","None",",","None",")","...","bbox_results","=","dict","(","rois=rois",",","bbox_pred=bbox_preds",",","...","cls_score=cls_score",",","...","bbox_targets=bbox_targets",")",">",">",">","bboxes_list","=","self.refine_bboxes","(","sampling_results",",","...","bbox_results",",","...","batch_img_metas",")",">",">",">","print","(","bboxes_list",")"]
code: def refine_bboxes(
      self,
      sampling_results: Union[List[SamplingResult], InstanceList],
      bbox_results: dict,
      batch_img_metas: List[dict],
  ) -> InstanceList:
      """Refine bboxes during training.
  
      Args:
          sampling_results (List[:obj:`SamplingResult`] or
              List[:obj:`InstanceData`]): Sampling results.
              :obj:`SamplingResult` is the real sampling results
              calculate from bbox_head, while :obj:`InstanceData` is
              fake sampling results, e.g., in Sparse R-CNN or QueryInst, etc.
          bbox_results (dict): Usually is a dictionary with keys:
  
              - `cls_score` (Tensor): Classification scores.
              - `bbox_pred` (Tensor): Box energies / deltas.
              - `rois` (Tensor): RoIs with the shape (n, 5) where the first
                column indicates batch id of each RoI.
              - `bbox_targets` (tuple):  Ground truth for proposals in a
                single image. Containing the following list of Tensors:
                (labels, label_weights, bbox_targets, bbox_weights)
          batch_img_metas (List[dict]): List of image information.
  
      Returns:
          list[:obj:`InstanceData`]: Refined bboxes of each image.
  
      Example:
          >>> # xdoctest: +REQUIRES(module:kwarray)
          >>> import numpy as np
          >>> from mmdet.models.task_modules.samplers.
          ... sampling_result import random_boxes
          >>> from mmdet.models.task_modules.samplers import SamplingResult
          >>> self = BBoxHead(reg_class_agnostic=True)
          >>> n_roi = 2
          >>> n_img = 4
          >>> scale = 512
          >>> rng = np.random.RandomState(0)
          ... batch_img_metas = [{'img_shape': (scale, scale)}
          >>>                     for _ in range(n_img)]
          >>> sampling_results = [SamplingResult.random(rng=10)
          ...                     for _ in range(n_img)]
          >>> # Create rois in the expected format
          >>> roi_boxes = random_boxes(n_roi, scale=scale, rng=rng)
          >>> img_ids = torch.randint(0, n_img, (n_roi,))
          >>> img_ids = img_ids.float()
          >>> rois = torch.cat([img_ids[:, None], roi_boxes], dim=1)
          >>> # Create other args
          >>> labels = torch.randint(0, 81, (scale,)).long()
          >>> bbox_preds = random_boxes(n_roi, scale=scale, rng=rng)
          >>> cls_score = torch.randn((scale, 81))
          ... # For each image, pretend random positive boxes are gts
          >>> bbox_targets = (labels, None, None, None)
          ... bbox_results = dict(rois=rois, bbox_pred=bbox_preds,
          ...                     cls_score=cls_score,
          ...                     bbox_targets=bbox_targets)
          >>> bboxes_list = self.refine_bboxes(sampling_results,
          ...                                  bbox_results,
          ...                                  batch_img_metas)
          >>> print(bboxes_list)
      """
      pos_is_gts = [res.pos_is_gt for res in sampling_results]
      # bbox_targets is a tuple
      labels = bbox_results["bbox_targets"][0]
      cls_scores = bbox_results["cls_score"]
      rois = bbox_results["rois"]
      bbox_preds = bbox_results["bbox_pred"]
      if self.custom_activation:
          # TODO: Create a SeasawBBoxHead to simplified logic in BBoxHead
          cls_scores = self.loss_cls.get_activation(cls_scores)
      if cls_scores.numel() == 0:
          return None
      if cls_scores.shape[-1] == self.num_classes + 1:
          # remove background class
          cls_scores = cls_scores[:, :-1]
      elif cls_scores.shape[-1] != self.num_classes:
          raise ValueError(
              "The last dim of `cls_scores` should equal to "
              "`num_classes` or `num_classes + 1`,"
              f"but got {cls_scores.shape[-1]}."
          )
      labels = torch.where(
          labels == self.num_classes, cls_scores.argmax(1), labels
      )
  
      img_ids = rois[:, 0].long().unique(sorted=True)
      assert img_ids.numel() <= len(batch_img_metas)
  
      results_list = []
      for i in range(len(batch_img_metas)):
          inds = torch.nonzero(rois[:, 0] == i, as_tuple=False).squeeze(
              dim=1
          )
          num_rois = inds.numel()
  
          bboxes_ = rois[inds, 1:]
          label_ = labels[inds]
          bbox_pred_ = bbox_preds[inds]
          img_meta_ = batch_img_metas[i]
          pos_is_gts_ = pos_is_gts[i]
  
          bboxes = self.regress_by_class(
              bboxes_, label_, bbox_pred_, img_meta_
          )
          # filter gt bboxes
          pos_keep = 1 - pos_is_gts_
          keep_inds = pos_is_gts_.new_ones(num_rois)
          keep_inds[: len(pos_is_gts_)] = pos_keep
          results = InstanceData(
              bboxes=bboxes[keep_inds.type(torch.bool)]
          )
          results_list.append(results)
  
      return results_list
  
imports: from typing import List, Optional, Tuple, Union
  import torch
  import torch.nn
  import torch.nn.functional
  from mmengine.config import ConfigDict
  from mmengine.model import BaseModule
  from mmengine.structures import InstanceData
  from torch import Tensor
  from torch.nn.modules.utils import _pair
  from mmdet.models.layers import multiclass_nms
  from mmdet.models.losses import accuracy
  from mmdet.models.task_modules.samplers import SamplingResult
  from mmdet.models.utils import empty_instances, multi_apply
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures.bbox import get_box_tensor, scale_boxes
  from mmdet.utils import ConfigType, InstanceList, OptMultiConfig
label_desc: Use image node_id 12 for calling the BBoxHead obj's underlying member method code with example usage: obj.refine_bboxes(sampling_results, bbox_results, batch_img_metas) and returns: results_list, None