class_docstr: Bbox head used in CrowdDet.
  
  .. code-block:: none
  
                                    /-> cls convs_1 -> cls fcs_1 -> cls_1
                                 |--
                                 |  \-> reg convs_1 -> reg fcs_1 -> reg_1
                                 |
                                 |  /-> cls convs_2 -> cls fcs_2 -> cls_2
      shared convs -> shared fcs |--
                                 |  \-> reg convs_2 -> reg fcs_2 -> reg_2
                                 |
                                 |                     ...
                                 |
                                 |  /-> cls convs_k -> cls fcs_k -> cls_k
                                 |--
                                    \-> reg convs_k -> reg fcs_k -> reg_k
  
  
  Args:
      num_instance (int): The number of branches after shared fcs.
          Defaults to 2.
      with_refine (bool): Whether to use refine module. Defaults to False.
      num_shared_convs (int): The number of shared convs. Defaults to 0.
      num_shared_fcs (int): The number of shared fcs. Defaults to 2.
      num_cls_convs (int): The number of cls convs. Defaults to 0.
      num_cls_fcs (int): The number of cls fcs. Defaults to 0.
      num_reg_convs (int): The number of reg convs. Defaults to 0.
      num_reg_fcs (int): The number of reg fcs. Defaults to 0.
      conv_out_channels (int): The number of conv out channels.
          Defaults to 256.
      fc_out_channels (int): The number of fc out channels. Defaults to 1024.
      init_cfg (dict or list[dict], optional): Initialization config dict.
          Defaults to None.
docstr: Transform a single image's features extracted from the head into
  bbox results.
  
  Args:
      roi (Tensor): Boxes to be transformed. Has shape (num_boxes, 5).
          last dimension 5 arrange as (batch_index, x1, y1, x2, y2).
      cls_score (Tensor): Box scores, has shape
          (num_boxes, num_classes + 1).
      bbox_pred (Tensor): Box energies / deltas. has shape
          (num_boxes, num_classes * 4).
      img_meta (dict): image information.
      rescale (bool): If True, return boxes in original image space.
          Defaults to False.
      rcnn_test_cfg (obj:`ConfigDict`): `test_cfg` of Bbox Head.
          Defaults to None
  
  Returns:
      :obj:`InstanceData`: Detection results of each image.
      Each item usually contains following keys.
  
          - scores (Tensor): Classification scores, has a shape
            (num_instance, )
          - labels (Tensor): Labels of bboxes, has a shape
            (num_instances, ).
          - bboxes (Tensor): Has a shape (num_instances, 4),
            the last dimension 4 arrange as (x1, y1, x2, y2).
docstr_tok: ["Transform","a","single","image","'s","features","extracted","from","the","head","into","bbox","results",".","Args",":","roi","(","Tensor",")",":","Boxes","to","be","transformed",".","Has","shape","(","num_boxes",",","5",")",".","last","dimension","5","arrange","as","(","batch_index",",","x1",",","y1",",","x2",",","y2",")",".","cls_score","(","Tensor",")",":","Box","scores",",","has","shape","(","num_boxes",",","num_classes","+","1",")",".","bbox_pred","(","Tensor",")",":","Box","energies","\/","deltas",".","has","shape","(","num_boxes",",","num_classes","*","4",")",".","img_meta","(","dict",")",":","image","information",".","rescale","(","bool",")",":","If","True",",","return","boxes","in","original","image","space",".","Defaults","to","False",".","rcnn_test_cfg","(","obj",":","`","ConfigDict","`",")",":","`","test_cfg","`","of","Bbox","Head",".","Defaults","to","None","Returns",":",":","obj",":","`","InstanceData","`",":","Detection","results","of","each","image",".","Each","item","usually","contains","following","keys",".","-","scores","(","Tensor",")",":","Classification","scores",",","has","a","shape","(","num_instance",",",")","-","labels","(","Tensor",")",":","Labels","of","bboxes",",","has","a","shape","(","num_instances",",",")",".","-","bboxes","(","Tensor",")",":","Has","a","shape","(","num_instances",",","4",")",",","the","last","dimension","4","arrange","as","(","x1",",","y1",",","x2",",","y2",")","."]
code: def _predict_by_feat_single(
      self,
      roi: Tensor,
      cls_score: Tensor,
      bbox_pred: Tensor,
      img_meta: dict,
      rescale: bool = False,
      rcnn_test_cfg: Optional[ConfigDict] = None,
  ) -> InstanceData:
      """Transform a single image's features extracted from the head into
      bbox results.
  
      Args:
          roi (Tensor): Boxes to be transformed. Has shape (num_boxes, 5).
              last dimension 5 arrange as (batch_index, x1, y1, x2, y2).
          cls_score (Tensor): Box scores, has shape
              (num_boxes, num_classes + 1).
          bbox_pred (Tensor): Box energies / deltas. has shape
              (num_boxes, num_classes * 4).
          img_meta (dict): image information.
          rescale (bool): If True, return boxes in original image space.
              Defaults to False.
          rcnn_test_cfg (obj:`ConfigDict`): `test_cfg` of Bbox Head.
              Defaults to None
  
      Returns:
          :obj:`InstanceData`: Detection results of each image.
          Each item usually contains following keys.
  
              - scores (Tensor): Classification scores, has a shape
                (num_instance, )
              - labels (Tensor): Labels of bboxes, has a shape
                (num_instances, ).
              - bboxes (Tensor): Has a shape (num_instances, 4),
                the last dimension 4 arrange as (x1, y1, x2, y2).
      """
  
      cls_score = cls_score.reshape(-1, self.num_classes + 1)
      bbox_pred = bbox_pred.reshape(-1, 4)
      roi = roi.repeat_interleave(self.num_instance, dim=0)
  
      results = InstanceData()
      if roi.shape[0] == 0:
          return empty_instances(
              [img_meta],
              roi.device,
              task_type="bbox",
              instance_results=[results],
          )[0]
  
      scores = (
          cls_score.softmax(dim=-1) if cls_score is not None else None
      )
      img_shape = img_meta["img_shape"]
      bboxes = self.bbox_coder.decode(
          roi[..., 1:], bbox_pred, max_shape=img_shape
      )
  
      if rescale and bboxes.size(0) > 0:
          assert img_meta.get("scale_factor") is not None
          scale_factor = bboxes.new_tensor(
              img_meta["scale_factor"]
          ).repeat((1, 2))
          bboxes = (
              bboxes.view(bboxes.size(0), -1, 4) / scale_factor
          ).view(bboxes.size()[0], -1)
  
      if rcnn_test_cfg is None:
          # This means that it is aug test.
          # It needs to return the raw results without nms.
          results.bboxes = bboxes
          results.scores = scores
      else:
          roi_idx = np.tile(
              np.arange(bboxes.shape[0] / self.num_instance)[:, None],
              (1, self.num_instance),
          ).reshape(-1, 1)[:, 0]
          roi_idx = (
              torch.from_numpy(roi_idx).to(bboxes.device).reshape(-1, 1)
          )
          bboxes = torch.cat([bboxes, roi_idx], dim=1)
          det_bboxes, det_scores = self.set_nms(
              bboxes,
              scores[:, 1],
              rcnn_test_cfg.score_thr,
              rcnn_test_cfg.nms["iou_threshold"],
              rcnn_test_cfg.max_per_img,
          )
  
          results.bboxes = det_bboxes[:, :-1]
          results.scores = det_scores
          results.labels = torch.zeros_like(det_scores)
  
      return results
  
imports: from typing import List, Optional, Tuple, Union
  import numpy
  import torch
  import torch.nn.functional
  from mmcv.cnn import ConvModule
  from mmengine.config import ConfigDict
  from mmengine.structures import InstanceData
  from torch import Tensor, nn
  from mmdet.models.roi_heads.bbox_heads.bbox_head import BBoxHead
  from mmdet.models.task_modules.samplers import SamplingResult
  from mmdet.models.utils import empty_instances
  from mmdet.registry import MODELS
  from mmdet.structures.bbox import bbox_overlaps
label_desc: Use image node_id 7 for calling the MultiInstanceBBoxHead obj's underlying member method code with example usage: obj._predict_by_feat_single(roi, cls_score, bbox_pred, img_meta, rescale, rcnn_test_cfg) and returns: results, empty_instances