class_docstr: Base class for DenseHeads.
  
  1. The ``init_weights`` method is used to initialize densehead's
  model parameters. After detector initialization, ``init_weights``
  is triggered when ``detector.init_weights()`` is called externally.
  
  2. The ``loss`` method is used to calculate the loss of densehead,
  which includes two steps: (1) the densehead model performs forward
  propagation to obtain the feature maps (2) The ``loss_by_feat`` method
  is called based on the feature maps to calculate the loss.
  
  .. code:: text
  
  loss(): forward() -> loss_by_feat()
  
  3. The ``predict`` method is used to predict detection results,
  which includes two steps: (1) the densehead model performs forward
  propagation to obtain the feature maps (2) The ``predict_by_feat`` method
  is called based on the feature maps to predict detection results including
  post-processing.
  
  .. code:: text
  
  predict(): forward() -> predict_by_feat()
  
  4. The ``loss_and_predict`` method is used to return loss and detection
  results at the same time. It will call densehead's ``forward``,
  ``loss_by_feat`` and ``predict_by_feat`` methods in order.  If one-stage is
  used as RPN, the densehead needs to return both losses and predictions.
  This predictions is used as the proposal of roihead.
  
  .. code:: text
  
  loss_and_predict(): forward() -> loss_by_feat() -> predict_by_feat()
class_docstr_tok: ["Base","class","for","DenseHeads",".","1",".","The","``","init_weights","``","method","is","used","to","initialize","densehead's","model","parameters",".","After","detector","initialization",",","``","init_weights","``","is","triggered","when","``","detector.init_weights","(",")","``","is","called","externally",".","2",".","The","``","loss","``","method","is","used","to","calculate","the","loss","of","densehead",",","which","includes","two","steps",":","(","1",")","the","densehead","model","performs","forward","propagation","to","obtain","the","feature","maps","(","2",")","The","``","loss_by_feat","``","method","is","called","based","on","the","feature","maps","to","calculate","the","loss",".","..","code",":",":","text","loss","(",")",":","forward","(",")","-",">","loss_by_feat","(",")","3",".","The","``","predict","``","method","is","used","to","predict","detection","results",",","which","includes","two","steps",":","(","1",")","the","densehead","model","performs","forward","propagation","to","obtain","the","feature","maps","(","2",")","The","``","predict_by_feat","``","method","is","called","based","on","the","feature","maps","to","predict","detection","results","including","post-processing",".","..","code",":",":","text","predict","(",")",":","forward","(",")","-",">","predict_by_feat","(",")","4",".","The","``","loss_and_predict","``","method","is","used","to","return","loss","and","detection","results","at","the","same","time",".","It","will","call","densehead","'s","``","forward","``",",","``","loss_by_feat","``","and","``","predict_by_feat","``","methods","in","order",".","If","one-stage","is","used","as","RPN",",","the","densehead","needs","to","return","both","losses","and","predictions",".","This","predictions","is","used","as","the","proposal","of","roihead",".","..","code",":",":","text","loss_and_predict","(",")",":","forward","(",")","-",">","loss_by_feat","(",")","-",">","predict_by_feat","(",")"]
docstr: Perform forward propagation of the detection head and predict
  detection results on the features of the upstream network.
  
  Args:
      x (tuple[Tensor]): Multi-level features from the
          upstream network, each is a 4D-tensor.
      batch_data_samples (List[:obj:`DetDataSample`]): The Data
          Samples. It usually includes information such as
          `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
      rescale (bool, optional): Whether to rescale the results.
          Defaults to False.
  
  Returns:
      list[obj:`InstanceData`]: Detection results of each image
      after the post process.
docstr_tok: ["Perform","forward","propagation","of","the","detection","head","and","predict","detection","results","on","the","features","of","the","upstream","network",".","Args",":","x","(","tuple","[","Tensor","]",")",":","Multi-level","features","from","the","upstream","network",",","each","is","a","4D-tensor",".","batch_data_samples","(","List","[",":","obj",":","`","DetDataSample","`","]",")",":","The","Data","Samples",".","It","usually","includes","information","such","as","`","gt_instance","`",",","`","gt_panoptic_seg","`","and","`","gt_sem_seg","`",".","rescale","(","bool",",","optional",")",":","Whether","to","rescale","the","results",".","Defaults","to","False",".","Returns",":","list","[","obj",":","`","InstanceData","`","]",":","Detection","results","of","each","image","after","the","post","process","."]
code: def predict(
      self,
      x: Tuple[Tensor],
      batch_data_samples: SampleList,
      rescale: bool = False,
  ) -> InstanceList:
      """Perform forward propagation of the detection head and predict
      detection results on the features of the upstream network.
  
      Args:
          x (tuple[Tensor]): Multi-level features from the
              upstream network, each is a 4D-tensor.
          batch_data_samples (List[:obj:`DetDataSample`]): The Data
              Samples. It usually includes information such as
              `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
          rescale (bool, optional): Whether to rescale the results.
              Defaults to False.
  
      Returns:
          list[obj:`InstanceData`]: Detection results of each image
          after the post process.
      """
      batch_img_metas = [
          data_samples.metainfo for data_samples in batch_data_samples
      ]
  
      outs = self(x)
  
      predictions = self.predict_by_feat(
          *outs, batch_img_metas=batch_img_metas, rescale=rescale
      )
      return predictions
  
imports: import copy
  from abc import ABCMeta, abstractmethod
  from inspect import signature
  from typing import List, Optional, Tuple
  import torch
  from mmcv.ops import batched_nms
  from mmengine.config import ConfigDict
  from mmengine.model import BaseModule, constant_init
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import cat_boxes, get_box_tensor, get_box_wh, scale_boxes
  from mmdet.utils import InstanceList, OptMultiConfig
  from ..test_time_augs import merge_aug_results
  from ..utils import filter_scores_and_topk, select_single_mlvl, unpack_gt_instances
label_desc: Use image node_id 7 for calling the BaseDenseHead obj's underlying member method code with example usage: obj.predict(x, batch_data_samples, rescale) and returns: predictions