class_docstr: Base class of Two-stage Panoptic Segmentor.
  
  As well as the components in TwoStageDetector, Panoptic Segmentor has extra
  semantic_head and panoptic_fusion_head.
class_docstr_tok: ["Base","class","of","Two-stage","Panoptic","Segmentor",".","As","well","as","the","components","in","TwoStageDetector",",","Panoptic","Segmentor","has","extra","semantic_head","and","panoptic_fusion_head","."]
docstr: Args:
      batch_inputs (Tensor): Input images of shape (N, C, H, W).
          These should usually be mean centered and std scaled.
      batch_data_samples (list[:obj:`DetDataSample`]): The batch
          data samples. It usually includes information such
          as `gt_instance` or `gt_panoptic_seg` or `gt_sem_seg`.
  
  Returns:
      dict: A dictionary of loss components.
docstr_tok: ["Args",":","batch_inputs","(","Tensor",")",":","Input","images","of","shape","(","N",",","C",",","H",",","W",")",".","These","should","usually","be","mean","centered","and","std","scaled",".","batch_data_samples","(","list","[",":","obj",":","`","DetDataSample","`","]",")",":","The","batch","data","samples",".","It","usually","includes","information","such","as","`","gt_instance","`","or","`","gt_panoptic_seg","`","or","`","gt_sem_seg","`",".","Returns",":","dict",":","A","dictionary","of","loss","components","."]
code: def loss(
      self, batch_inputs: Tensor, batch_data_samples: SampleList
  ) -> dict:
      """
      Args:
          batch_inputs (Tensor): Input images of shape (N, C, H, W).
              These should usually be mean centered and std scaled.
          batch_data_samples (list[:obj:`DetDataSample`]): The batch
              data samples. It usually includes information such
              as `gt_instance` or `gt_panoptic_seg` or `gt_sem_seg`.
  
      Returns:
          dict: A dictionary of loss components.
      """
      x = self.extract_feat(batch_inputs)
  
      losses = dict()
  
      # RPN forward and loss
      if self.with_rpn:
          proposal_cfg = self.train_cfg.get(
              "rpn_proposal", self.test_cfg.rpn
          )
          rpn_data_samples = copy.deepcopy(batch_data_samples)
          # set cat_id of gt_labels to 0 in RPN
          for data_sample in rpn_data_samples:
              data_sample.gt_instances.labels = torch.zeros_like(
                  data_sample.gt_instances.labels
              )
  
          rpn_losses, rpn_results_list = self.rpn_head.loss_and_predict(
              x, rpn_data_samples, proposal_cfg=proposal_cfg
          )
          # avoid get same name with roi_head loss
          keys = rpn_losses.keys()
          for key in list(keys):
              if "loss" in key and "rpn" not in key:
                  rpn_losses[f"rpn_{key}"] = rpn_losses.pop(key)
          losses.update(rpn_losses)
      else:
          # TODO: Not support currently, should have a check at Fast R-CNN
          assert (
              batch_data_samples[0].get("proposals", None) is not None
          )
          # use pre-defined proposals in InstanceData for the second stage
          # to extract ROI features.
          rpn_results_list = [
              data_sample.proposals
              for data_sample in batch_data_samples
          ]
  
      roi_losses = self.roi_head.loss(
          x, rpn_results_list, batch_data_samples
      )
      losses.update(roi_losses)
  
      semantic_loss = self.semantic_head.loss(x, batch_data_samples)
      losses.update(semantic_loss)
  
      return losses
  
file_path: mmdetection/mmdet/models/detectors/panoptic_two_stage_segmentor.py
imports: import copy
  from typing import List
  import torch
  from mmengine.structures import PixelData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.utils import ConfigType, OptConfigType, OptMultiConfig
  from .two_stage import TwoStageDetector
label_desc: Use image node_id 4 for calling the TwoStagePanopticSegmentor obj's underlying member method code with example usage: obj.loss(batch_inputs, batch_data_samples) and returns: losses