class_docstr: Bbox head used in CrowdDet.
  
  .. code-block:: none
  
                                    /-> cls convs_1 -> cls fcs_1 -> cls_1
                                 |--
                                 |  \-> reg convs_1 -> reg fcs_1 -> reg_1
                                 |
                                 |  /-> cls convs_2 -> cls fcs_2 -> cls_2
      shared convs -> shared fcs |--
                                 |  \-> reg convs_2 -> reg fcs_2 -> reg_2
                                 |
                                 |                     ...
                                 |
                                 |  /-> cls convs_k -> cls fcs_k -> cls_k
                                 |--
                                    \-> reg convs_k -> reg fcs_k -> reg_k
  
  
  Args:
      num_instance (int): The number of branches after shared fcs.
          Defaults to 2.
      with_refine (bool): Whether to use refine module. Defaults to False.
      num_shared_convs (int): The number of shared convs. Defaults to 0.
      num_shared_fcs (int): The number of shared fcs. Defaults to 2.
      num_cls_convs (int): The number of cls convs. Defaults to 0.
      num_cls_fcs (int): The number of cls fcs. Defaults to 0.
      num_reg_convs (int): The number of reg convs. Defaults to 0.
      num_reg_fcs (int): The number of reg fcs. Defaults to 0.
      conv_out_channels (int): The number of conv out channels.
          Defaults to 256.
      fc_out_channels (int): The number of fc out channels. Defaults to 1024.
      init_cfg (dict or list[dict], optional): Initialization config dict.
          Defaults to None.
docstr: Calculate the emd loss.
  
  Note:
      This implementation is modified from https://github.com/Purkialo/
      CrowdDet/blob/master/lib/det_oprs/loss_opr.py
  
  Args:
      bbox_pred_0 (Tensor): Part of regression prediction results, has
          shape (batch_size * num_proposals_single_image, 4), the last
          dimension 4 represents [tl_x, tl_y, br_x, br_y].
      cls_score_0 (Tensor): Part of classification prediction results,
          has shape (batch_size * num_proposals_single_image,
          (num_classes + 1)), where 1 represents the background.
      bbox_pred_1 (Tensor): The other part of regression prediction
          results, has shape (batch_size*num_proposals_single_image, 4).
      cls_score_1 (Tensor):The other part of classification prediction
          results, has shape (batch_size * num_proposals_single_image,
          (num_classes + 1)).
      targets (Tensor):Regression target for all proposals in a
          batch, has shape (batch_size * num_proposals_single_image,
          4 * k), the last dimension 4 represents [tl_x, tl_y, br_x,
          br_y], k represents the number of prediction boxes generated
          by each proposal box.
      labels (Tensor): Gt_labels for all proposals in a batch, has
          shape (batch_size * num_proposals_single_image, k).
  
  Returns:
      torch.Tensor: The calculated loss.
docstr_tok: ["Calculate","the","emd","loss",".","Note",":","This","implementation","is","modified","from","https",":","\/\/github.com\/Purkialo\/","CrowdDet\/blob\/master\/lib\/det_oprs\/loss_opr.py","Args",":","bbox_pred_0","(","Tensor",")",":","Part","of","regression","prediction","results",",","has","shape","(","batch_size","*","num_proposals_single_image",",","4",")",",","the","last","dimension","4","represents","[","tl_x",",","tl_y",",","br_x",",","br_y","]",".","cls_score_0","(","Tensor",")",":","Part","of","classification","prediction","results",",","has","shape","(","batch_size","*","num_proposals_single_image",",","(","num_classes","+","1",")",")",",","where","1","represents","the","background",".","bbox_pred_1","(","Tensor",")",":","The","other","part","of","regression","prediction","results",",","has","shape","(","batch_size","*","num_proposals_single_image",",","4",")",".","cls_score_1","(","Tensor",")",":","The","other","part","of","classification","prediction","results",",","has","shape","(","batch_size","*","num_proposals_single_image",",","(","num_classes","+","1",")",")",".","targets","(","Tensor",")",":","Regression","target","for","all","proposals","in","a","batch",",","has","shape","(","batch_size","*","num_proposals_single_image",",","4","*","k",")",",","the","last","dimension","4","represents","[","tl_x",",","tl_y",",","br_x",",","br_y","]",",","k","represents","the","number","of","prediction","boxes","generated","by","each","proposal","box",".","labels","(","Tensor",")",":","Gt_labels","for","all","proposals","in","a","batch",",","has","shape","(","batch_size","*","num_proposals_single_image",",","k",")",".","Returns",":","torch.Tensor",":","The","calculated","loss","."]
code: def emd_loss(
      self,
      bbox_pred_0: Tensor,
      cls_score_0: Tensor,
      bbox_pred_1: Tensor,
      cls_score_1: Tensor,
      targets: Tensor,
      labels: Tensor,
  ) -> Tensor:
      """Calculate the emd loss.
  
      Note:
          This implementation is modified from https://github.com/Purkialo/
          CrowdDet/blob/master/lib/det_oprs/loss_opr.py
  
      Args:
          bbox_pred_0 (Tensor): Part of regression prediction results, has
              shape (batch_size * num_proposals_single_image, 4), the last
              dimension 4 represents [tl_x, tl_y, br_x, br_y].
          cls_score_0 (Tensor): Part of classification prediction results,
              has shape (batch_size * num_proposals_single_image,
              (num_classes + 1)), where 1 represents the background.
          bbox_pred_1 (Tensor): The other part of regression prediction
              results, has shape (batch_size*num_proposals_single_image, 4).
          cls_score_1 (Tensor):The other part of classification prediction
              results, has shape (batch_size * num_proposals_single_image,
              (num_classes + 1)).
          targets (Tensor):Regression target for all proposals in a
              batch, has shape (batch_size * num_proposals_single_image,
              4 * k), the last dimension 4 represents [tl_x, tl_y, br_x,
              br_y], k represents the number of prediction boxes generated
              by each proposal box.
          labels (Tensor): Gt_labels for all proposals in a batch, has
              shape (batch_size * num_proposals_single_image, k).
  
      Returns:
          torch.Tensor: The calculated loss.
      """
  
      bbox_pred = torch.cat([bbox_pred_0, bbox_pred_1], dim=1).reshape(
          -1, bbox_pred_0.shape[-1]
      )
      cls_score = torch.cat([cls_score_0, cls_score_1], dim=1).reshape(
          -1, cls_score_0.shape[-1]
      )
      targets = targets.reshape(-1, 4)
      labels = labels.long().flatten()
  
      # masks
      valid_masks = labels >= 0
      fg_masks = labels > 0
  
      # multiple class
      bbox_pred = bbox_pred.reshape(-1, self.num_classes, 4)
      fg_gt_classes = labels[fg_masks]
      bbox_pred = bbox_pred[fg_masks, fg_gt_classes - 1, :]
  
      # loss for regression
      loss_bbox = self.loss_bbox(bbox_pred, targets[fg_masks])
      loss_bbox = loss_bbox.sum(dim=1)
  
      # loss for classification
      labels = labels * valid_masks
      loss_cls = self.loss_cls(cls_score, labels)
  
      loss_cls[fg_masks] = loss_cls[fg_masks] + loss_bbox
      loss = loss_cls.reshape(-1, 2).sum(dim=1)
      return loss.reshape(-1, 1)
  
imports: from typing import List, Optional, Tuple, Union
  import numpy
  import torch
  import torch.nn.functional
  from mmcv.cnn import ConvModule
  from mmengine.config import ConfigDict
  from mmengine.structures import InstanceData
  from torch import Tensor, nn
  from mmdet.models.roi_heads.bbox_heads.bbox_head import BBoxHead
  from mmdet.models.task_modules.samplers import SamplingResult
  from mmdet.models.utils import empty_instances
  from mmdet.registry import MODELS
  from mmdet.structures.bbox import bbox_overlaps
label_desc: Use image node_id 6 for calling the MultiInstanceBBoxHead obj's underlying member method code with example usage: obj.emd_loss(bbox_pred_0, cls_score_0, bbox_pred_1, cls_score_1, targets, labels) and returns: loss