class_docstr: Base class for two-stage detectors.
  
  Two-stage detectors typically consisting of a region proposal network and a
  task-specific regression head.
class_docstr_tok: ["Base","class","for","two-stage","detectors",".","Two-stage","detectors","typically","consisting","of","a","region","proposal","network","and","a","task-specific","regression","head","."]
docstr: Calculate losses from a batch of inputs and data samples.
  
  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: ["Calculate","losses","from","a","batch","of","inputs","and","data","samples",".","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:
      """Calculate losses from a batch of inputs and data samples.
  
      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:
          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)
  
      return losses
  
file_path: mmdetection/mmdet/models/detectors/two_stage.py
imports: import copy
  import warnings
  from typing import List, Tuple, Union
  import torch
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.utils import ConfigType, OptConfigType, OptMultiConfig
  from .base import BaseDetector
label_desc: Use image node_id 7 for calling the TwoStageDetector obj's underlying member method code with example usage: obj.loss(batch_inputs, batch_data_samples) and returns: losses