class_docstr: Grid roi head for Grid R-CNN.
  
  https://arxiv.org/abs/1811.12030
docstr: Run forward function and calculate loss for box head in training.
code: def _bbox_forward_train(
      self, x, sampling_results, gt_bboxes, gt_labels, img_metas
  ):
      """Run forward function and calculate loss for box head in training."""
      bbox_results = super(GridRoIHead, self)._bbox_forward_train(
          x, sampling_results, gt_bboxes, gt_labels, img_metas
      )
  
      # Grid head forward and loss
      sampling_results = self._random_jitter(
          sampling_results, 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)
  
      grid_targets = self.grid_head.get_targets(
          sampling_results, self.train_cfg
      )
      grid_targets = grid_targets[sample_idx]
  
      loss_grid = self.grid_head.loss(grid_pred, grid_targets)
  
      bbox_results["loss_bbox"].update(loss_grid)
      return bbox_results
  
file_path: insightface/detection/scrfd/mmdet/models/roi_heads/grid_roi_head.py
imports: import torch
  from mmdet.core import bbox2result, bbox2roi
  from ..builder import HEADS, build_head, build_roi_extractor
  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_forward_train(x, sampling_results, gt_bboxes, gt_labels, img_metas) and returns: bbox_results, bbox_results