class_docstr: Mask RCNN loss is computed inside the MaskRCNN model. This class is a wrapper to extract
  loss values for different heads (RPN, classification, etc.) and compute the weighted sum.
  
  Args:
      opts: command-line arguments
class_docstr_tok: ["Mask","RCNN","loss","is","computed","inside","the","MaskRCNN","model",".","This","class","is","a","wrapper","to","extract","loss","values","for","different","heads","(","RPN",",","classification",",","etc",".",")","and","compute","the","weighted","sum",".","Args",":","opts",":","command-line","arguments"]
docstr: Compute MaskRCNN loss.
  
  Args:
      input_sample: Input image tensor to the model.
      prediction: Mapping of the Maskrcnn losses.
  
  Shapes:
      input_sample: This loss function does not care about input to the model.
      prediction: Dictionary containing scalar Mask RCNN loss values. Expected keys are
          loss_classifier, loss_box_reg, loss_mask, loss_objectness, loss_rpn_box_reg.
  
  Returns:
      A mapping of (string: scalar) is returned. Output contains following keys: (total_loss,
      loss_classifier, loss_box_reg, loss_mask, loss_objectness, loss_rpn_box_reg).
docstr_tok: ["Compute","MaskRCNN","loss",".","Args",":","input_sample",":","Input","image","tensor","to","the","model",".","prediction",":","Mapping","of","the","Maskrcnn","losses",".","Shapes",":","input_sample",":","This","loss","function","does","not","care","about","input","to","the","model",".","prediction",":","Dictionary","containing","scalar","Mask","RCNN","loss","values",".","Expected","keys","are","loss_classifier",",","loss_box_reg",",","loss_mask",",","loss_objectness",",","loss_rpn_box_reg",".","Returns",":","A","mapping","of","(","string",":","scalar",")","is","returned",".","Output","contains","following","keys",":","(","total_loss",",","loss_classifier",",","loss_box_reg",",","loss_mask",",","loss_objectness",",","loss_rpn_box_reg",")","."]
code: def forward(
      self,
      input_sample: Any,
      prediction: Dict[str, Tensor],
      *args,
      **kwargs,
  ) -> Dict[str, Tensor]:
      """Compute MaskRCNN loss.
  
      Args:
          input_sample: Input image tensor to the model.
          prediction: Mapping of the Maskrcnn losses.
  
      Shapes:
          input_sample: This loss function does not care about input to the model.
          prediction: Dictionary containing scalar Mask RCNN loss values. Expected keys are
              loss_classifier, loss_box_reg, loss_mask, loss_objectness, loss_rpn_box_reg.
  
      Returns:
          A mapping of (string: scalar) is returned. Output contains following keys: (total_loss,
          loss_classifier, loss_box_reg, loss_mask, loss_objectness, loss_rpn_box_reg).
      """
  
      if not self.training:
          # MaskRCNN doesn't return the loss during validation. Therefore, we return 0.
          return {"total_loss": torch.tensor(0.0, device=self.device)}
  
      if not isinstance(prediction, Dict):
          logger.error(
              f"{self.__class__.__name__} requires prediction as a dictionary with "
              f"loss_classifier, loss_box_reg, loss_mask, loss_objectness, loss_rpn_box_reg as "
              f"mandatory keys. Got: {type(prediction)}."
          )
  
      if not {
          "loss_classifier",
          "loss_box_reg",
          "loss_mask",
          "loss_objectness",
          "loss_rpn_box_reg",
      }.issubset(prediction.keys()):
          logger.error(
              f"loss_classifier, loss_box_reg, loss_mask, loss_objectness, loss_rpn_box_reg are "
              f"required keys in {self.__class__.__name__}. Got: {prediction.keys()}"
          )
  
      total_loss = 0.0
      mask_rcnn_losses = {}
  
      for loss_key, loss_wt in zip(
          [
              "loss_classifier",
              "loss_box_reg",
              "loss_mask",
              "loss_objectness",
              "loss_rpn_box_reg",
          ],
          [
              self.classifier_weight,
              self.box_reg_weight,
              self.mask_weight,
              self.objectness_weight,
              self.rpn_box_reg,
          ],
      ):
          loss_ = prediction[loss_key] * loss_wt
          total_loss += loss_
          mask_rcnn_losses[loss_key] = loss_
      mask_rcnn_losses.update({"total_loss": total_loss})
      return mask_rcnn_losses
  
file_path: ml-cvnets/loss_fn/detection/mask_rcnn_loss.py
imports: import argparse
  from typing import Any, Dict
  import torch
  from torch import Tensor
  from loss_fn import LOSS_REGISTRY
  from loss_fn.detection.base_detection_criteria import BaseDetectionCriteria
  from utils import logger
label_desc: Use image node_id 3 for calling the MaskRCNNLoss obj's underlying member method code with example usage: obj.forward(input_sample, prediction) and returns: mask_rcnn_losses, dict