class_docstr: The roi embed head.
  
  This module is used in multi-object tracking methods, such as MaskTrack
  R-CNN.
  
  Args:
      num_convs (int): The number of convoluational layers to embed roi
          features. Defaults to 0.
      num_fcs (int): The number of fully connection layers to embed roi
          features. Defaults to 0.
      roi_feat_size (int|tuple(int)): The spatial size of roi features.
          Defaults to 7.
      in_channels (int): The input channel of roi features. Defaults to 256.
      conv_out_channels (int): The output channel of roi features after
          forwarding convoluational layers. Defaults to 256.
      with_avg_pool (bool): Whether use average pooling before passing roi
          features into fully connection layers. Defaults to False.
      fc_out_channels (int): The output channel of roi features after
          forwarding fully connection layers. Defaults to 1024.
      conv_cfg (dict): Config dict for convolution layer. Defaults to None,
          which means using conv2d.
      norm_cfg (dict): Config dict for normalization layer. Defaults to None.
      loss_match (dict): The loss function. Defaults to
          dict(type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)
      init_cfg (dict): Configuration of initialization. Defaults to None.
class_docstr_tok: ["The","roi","embed","head",".","This","module","is","used","in","multi-object","tracking","methods",",","such","as","MaskTrack","R-CNN",".","Args",":","num_convs","(","int",")",":","The","number","of","convoluational","layers","to","embed","roi","features",".","Defaults","to","0.","num_fcs","(","int",")",":","The","number","of","fully","connection","layers","to","embed","roi","features",".","Defaults","to","0.","roi_feat_size","(","int|tuple","(","int",")",")",":","The","spatial","size","of","roi","features",".","Defaults","to","7.","in_channels","(","int",")",":","The","input","channel","of","roi","features",".","Defaults","to","256.","conv_out_channels","(","int",")",":","The","output","channel","of","roi","features","after","forwarding","convoluational","layers",".","Defaults","to","256.","with_avg_pool","(","bool",")",":","Whether","use","average","pooling","before","passing","roi","features","into","fully","connection","layers",".","Defaults","to","False",".","fc_out_channels","(","int",")",":","The","output","channel","of","roi","features","after","forwarding","fully","connection","layers",".","Defaults","to","1024.","conv_cfg","(","dict",")",":","Config","dict","for","convolution","layer",".","Defaults","to","None",",","which","means","using","conv2d",".","norm_cfg","(","dict",")",":","Config","dict","for","normalization","layer",".","Defaults","to","None",".","loss_match","(","dict",")",":","The","loss","function",".","Defaults","to","dict","(","type='CrossEntropyLoss","'",",","use_sigmoid=False",",","loss_weight=1.0",")","init_cfg","(","dict",")",":","Configuration","of","initialization",".","Defaults","to","None","."]
docstr: Calculate losses.
  
  Args:
      x_split (Tensor): The embed features belonging to key image.
      ref_x_split (Tensor): The embed features belonging to ref image.
      sampling_results (List[obj:SamplingResult]): Assign results of
          all images in a batch after sampling.
      gt_instance_ids (list[Tensor]): The instance ids of gt_bboxes of
          all images in a batch, each tensor has shape (num_gt, ).
      ref_gt_instance_ids (list[Tensor]): The instance ids of gt_bboxes
          of all reference images in a batch, each tensor has shape
          (num_gt, ).
      reduction_override (str, optional): The method used to reduce the
          loss. Options are "none", "mean" and "sum".
  
  Returns:
      dict[str, Tensor]: a dictionary of loss components.
docstr_tok: ["Calculate","losses",".","Args",":","x_split","(","Tensor",")",":","The","embed","features","belonging","to","key","image",".","ref_x_split","(","Tensor",")",":","The","embed","features","belonging","to","ref","image",".","sampling_results","(","List","[","obj",":","SamplingResult","]",")",":","Assign","results","of","all","images","in","a","batch","after","sampling",".","gt_instance_ids","(","list","[","Tensor","]",")",":","The","instance","ids","of","gt_bboxes","of","all","images","in","a","batch",",","each","tensor","has","shape","(","num_gt",",",")",".","ref_gt_instance_ids","(","list","[","Tensor","]",")",":","The","instance","ids","of","gt_bboxes","of","all","reference","images","in","a","batch",",","each","tensor","has","shape","(","num_gt",",",")",".","reduction_override","(","str",",","optional",")",":","The","method","used","to","reduce","the","loss",".","Options","are","``","none","''",",","``","mean","''","and","``","sum","''",".","Returns",":","dict","[","str",",","Tensor","]",":","a","dictionary","of","loss","components","."]
code: def loss_by_feat(
      self,
      x_split: Tuple[Tensor],
      ref_x_split: Tuple[Tensor],
      sampling_results: List[SamplingResult],
      gt_instance_ids: List[Tensor],
      ref_gt_instance_ids: List[Tensor],
      reduction_override: Optional[str] = None,
  ) -> dict:
      """Calculate losses.
  
      Args:
          x_split (Tensor): The embed features belonging to key image.
          ref_x_split (Tensor): The embed features belonging to ref image.
          sampling_results (List[obj:SamplingResult]): Assign results of
              all images in a batch after sampling.
          gt_instance_ids (list[Tensor]): The instance ids of gt_bboxes of
              all images in a batch, each tensor has shape (num_gt, ).
          ref_gt_instance_ids (list[Tensor]): The instance ids of gt_bboxes
              of all reference images in a batch, each tensor has shape
              (num_gt, ).
          reduction_override (str, optional): The method used to reduce the
              loss. Options are "none", "mean" and "sum".
  
      Returns:
          dict[str, Tensor]: a dictionary of loss components.
      """
      track_id_targets, track_id_weights = self.get_targets(
          sampling_results, gt_instance_ids, ref_gt_instance_ids
      )
      assert isinstance(track_id_targets, list)
      assert isinstance(track_id_weights, list)
      assert len(track_id_weights) == len(track_id_targets)
  
      losses = defaultdict(list)
      similarity_logits = []
      for one_x, one_ref_x in zip(x_split, ref_x_split):
          similarity_logit = embed_similarity(
              one_x, one_ref_x, method="dot_product"
          )
          dummy = similarity_logit.new_zeros(one_x.shape[0], 1)
          similarity_logit = torch.cat((dummy, similarity_logit), dim=1)
          similarity_logits.append(similarity_logit)
      assert isinstance(similarity_logits, list)
      assert len(similarity_logits) == len(track_id_targets)
  
      for similarity_logit, track_id_target, track_id_weight in zip(
          similarity_logits, track_id_targets, track_id_weights
      ):
          avg_factor = max(
              torch.sum(track_id_target > 0).float().item(), 1.0
          )
          if similarity_logit.numel() > 0:
              loss_match = self.loss_match(
                  similarity_logit,
                  track_id_target,
                  track_id_weight,
                  avg_factor=avg_factor,
                  reduction_override=reduction_override,
              )
              if isinstance(loss_match, dict):
                  for key, value in loss_match.items():
                      losses[key].append(value)
              else:
                  losses["loss_match"].append(loss_match)
  
              valid_index = track_id_weight > 0
              valid_similarity_logit = similarity_logit[valid_index]
              valid_track_id_target = track_id_target[valid_index]
              if self.custom_activation:
                  match_accuracy = self.loss_match.get_accuracy(
                      valid_similarity_logit, valid_track_id_target
                  )
                  for key, value in match_accuracy.items():
                      losses[key].append(value)
              else:
                  losses["match_accuracy"].append(
                      accuracy(
                          valid_similarity_logit, valid_track_id_target
                      )
                  )
  
      for key, value in losses.items():
          losses[key] = sum(losses[key]) / len(similarity_logits)
      return losses
  
imports: from collections import defaultdict
  from typing import List, Optional, Tuple
  import torch
  import torch.nn
  from mmcv.cnn import ConvModule
  from mmengine.model import BaseModule
  from torch import Tensor
  from torch.nn.modules.utils import _pair
  from mmdet.models.losses import accuracy
  from mmdet.models.task_modules import SamplingResult
  from mmdet.models.task_modules.tracking import embed_similarity
  from mmdet.registry import MODELS
label_desc: Use image node_id 8 for calling the RoIEmbedHead obj's underlying member method code with example usage: obj.loss_by_feat(x_split, ref_x_split, sampling_results, gt_instance_ids, ref_gt_instance_ids, reduction_override) and returns: losses