class_docstr: Implementation of `Grid RoI Head <https://arxiv.org/abs/1811.12030>`_
  
  Args:
      grid_roi_extractor (:obj:`ConfigDict` or dict): Config of
          roi extractor.
      grid_head (:obj:`ConfigDict` or dict): Config of grid head
class_docstr_tok: ["Implementation","of","`","Grid","RoI","Head","<","https",":","\/\/arxiv.org\/abs\/1811.12030",">","`","_","Args",":","grid_roi_extractor","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","roi","extractor",".","grid_head","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","grid","head"]
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.
      batch_img_metas (list[dict], optional): Meta information of each
          image, e.g., image size, scaling factor, etc.
  
  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",".","batch_img_metas","(","list","[","dict","]",",","optional",")",":","Meta","information","of","each","image",",","e.g.",",","image","size",",","scaling","factor",",","etc",".","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],
      batch_img_metas: Optional[List[dict]] = 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.
          batch_img_metas (list[dict], optional): Meta information of each
              image, e.g., image size, scaling factor, etc.
  
      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.
      """
      assert batch_img_metas is not None
      bbox_results = super().bbox_loss(x, sampling_results)
  
      # Grid head forward and loss
      sampling_results = self._random_jitter(
          sampling_results, batch_img_metas
      )
      pos_rois = bbox2roi([res.pos_bboxes for res in sampling_results])
  
      # GN in head does not support zero shape input
      if pos_rois.shape[0] == 0:
          return bbox_results
  
      grid_feats = self.grid_roi_extractor(
          x[: self.grid_roi_extractor.num_inputs], pos_rois
      )
      if self.with_shared_head:
          grid_feats = self.shared_head(grid_feats)
      # Accelerate training
      max_sample_num_grid = self.train_cfg.get("max_num_grid", 192)
      sample_idx = torch.randperm(grid_feats.shape[0])[
          : min(grid_feats.shape[0], max_sample_num_grid)
      ]
      grid_feats = grid_feats[sample_idx]
      grid_pred = self.grid_head(grid_feats)
  
      loss_grid = self.grid_head.loss(
          grid_pred, sample_idx, sampling_results, self.train_cfg
      )
  
      bbox_results["loss_bbox"].update(loss_grid)
      return bbox_results
  
file_path: mmdetection/mmdet/models/roi_heads/grid_roi_head.py
imports: from typing import List, Optional, Tuple
  import torch
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox2roi
  from mmdet.utils import ConfigType, InstanceList
  from ..task_modules.samplers import SamplingResult
  from ..utils.misc import unpack_gt_instances
  from .standard_roi_head import StandardRoIHead
label_desc: Use image node_id 5 for calling the GridRoIHead obj's underlying member method code with example usage: obj.bbox_loss(x, sampling_results, batch_img_metas) and returns: bbox_results, bbox_results