code: def maskrcnn_inference(x, labels):
      # type: (Tensor, List[Tensor]) -> List[Tensor]
      """
      From the results of the CNN, post process the masks
      by taking the mask corresponding to the class with max
      probability (which are of fixed size and directly output
      by the CNN) and return the masks in the mask field of the BoxList.
  
      Args:
          x (Tensor): the mask logits
          labels (list[BoxList]): bounding boxes that are used as
              reference, one for ech image
  
      Returns:
          results (list[BoxList]): one BoxList for each image, containing
              the extra field mask
      """
      # 将预测值通过sigmoid激活全部缩放到0~1之间
      mask_prob = x.sigmoid()
  
      # select masks corresponding to the predicted classes
      num_masks = x.shape[0]
      # 先记录每张图片中boxes/masks的个数
      boxes_per_image = [label.shape[0] for label in labels]
      # 在将所有图片中的masks信息拼接在一起(拼接后统一处理能够提升并行度)
      labels = torch.cat(labels)
      index = torch.arange(num_masks, device=labels.device)
      # 提取每个masks中对应预测最终类别的mask
      mask_prob = mask_prob[index, labels][:, None]
      # 最后再按照每张图片中的masks个数分离开
      mask_prob = mask_prob.split(boxes_per_image, dim=0)
  
      return mask_prob
  
imports: from typing import Optional, List, Dict, Tuple
  import torch
  from torch import Tensor
  import torch.nn.functional
  from torchvision.ops import roi_align
  from .None import det_utils
  from .None import boxes
label_desc: Use image node_id 2 for calling a global function with example usage: maskrcnn_inference(x, labels) and returns: mask_prob