docstr: Refine bboxes during training.
  
  Args:
      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",":","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, bbox_results: dict, batch_img_metas: List[dict]
  ) -> InstanceList:
      """Refine bboxes during training.
  
      Args:
          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)
      """
      # bbox_targets is a tuple
      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]}."
          )
  
      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
          )
  
          bboxes_ = rois[inds, 1:]
          bbox_pred_ = bbox_preds[inds]
          img_meta_ = batch_img_metas[i]
  
          bboxes = self.regress(bboxes_, bbox_pred_, img_meta_)
  
          # don't filter gt bboxes like D2
          results = InstanceData(bboxes=bboxes)
          results_list.append(results)
  
      return results_list
  
imports: import json
  from typing import List, Optional
  import torch
  from mmengine.config import ConfigDict
  from mmengine.structures import InstanceData
  from torch import Tensor
  from torch.nn import functional
  from mmdet.models.layers import multiclass_nms
  from mmdet.models.losses import accuracy
  from mmdet.models.roi_heads.bbox_heads import Shared2FCBBoxHead
  from mmdet.models.utils import empty_instances
  from mmdet.registry import MODELS
  from mmdet.structures.bbox import get_box_tensor, scale_boxes
  from mmdet.utils import ConfigType, InstanceList
label_desc: Use image node_id 6 for calling the DeticBBoxHead obj's underlying member method code with example usage: obj.refine_bboxes(bbox_results, batch_img_metas) and returns: results_list, None