docstr: Get segmentation masks from mask_preds and bboxes.
  
  Args:
      mask_preds (Tensor): Predicted foreground masks, has shape
          (n, num_classes, h, w).
      bboxes (Tensor): Predicted bboxes, has shape (n, 4)
      labels (Tensor): Labels of bboxes, has shape (n, )
      img_meta (dict): image information.
      rcnn_test_cfg (obj:`ConfigDict`): `test_cfg` of Bbox Head.
          Defaults to None.
      rescale (bool): If True, return boxes in original image space.
          Defaults to False.
      activate_map (book): Whether get results with augmentations test.
          If True, the `mask_preds` will not process with sigmoid.
          Defaults to False.
  
  Returns:
      Tensor: Encoded masks, has shape (n, img_w, img_h)
  
  Example:
      >>> from mmengine.config import Config
      >>> from mmdet.models.roi_heads.mask_heads.fcn_mask_head import *  # NOQA
      >>> N = 7  # N = number of extracted ROIs
      >>> C, H, W = 11, 32, 32
      >>> # Create example instance of FCN Mask Head.
      >>> self = FCNMaskHead(num_classes=C, num_convs=0)
      >>> inputs = torch.rand(N, self.in_channels, H, W)
      >>> mask_preds = self.forward(inputs)
      >>> # Each input is associated with some bounding box
      >>> bboxes = torch.Tensor([[1, 1, 42, 42 ]] * N)
      >>> labels = torch.randint(0, C, size=(N,))
      >>> rcnn_test_cfg = Config({'mask_thr_binary': 0, })
      >>> ori_shape = (H * 4, W * 4)
      >>> scale_factor = (1, 1)
      >>> rescale = False
      >>> img_meta = {'scale_factor': scale_factor,
      ...             'ori_shape': ori_shape}
      >>> # Encoded masks are a list for each category.
      >>> encoded_masks = self._get_seg_masks_single(
      ...     mask_preds, bboxes, labels,
      ...     img_meta, rcnn_test_cfg, rescale)
      >>> assert encoded_masks.size()[0] == N
      >>> assert encoded_masks.size()[1:] == ori_shape
docstr_tok: ["Get","segmentation","masks","from","mask_preds","and","bboxes",".","Args",":","mask_preds","(","Tensor",")",":","Predicted","foreground","masks",",","has","shape","(","n",",","num_classes",",","h",",","w",")",".","bboxes","(","Tensor",")",":","Predicted","bboxes",",","has","shape","(","n",",","4",")","labels","(","Tensor",")",":","Labels","of","bboxes",",","has","shape","(","n",",",")","img_meta","(","dict",")",":","image","information",".","rcnn_test_cfg","(","obj",":","`","ConfigDict","`",")",":","`","test_cfg","`","of","Bbox","Head",".","Defaults","to","None",".","rescale","(","bool",")",":","If","True",",","return","boxes","in","original","image","space",".","Defaults","to","False",".","activate_map","(","book",")",":","Whether","get","results","with","augmentations","test",".","If","True",",","the","`","mask_preds","`","will","not","process","with","sigmoid",".","Defaults","to","False",".","Returns",":","Tensor",":","Encoded","masks",",","has","shape","(","n",",","img_w",",","img_h",")","Example",":",">",">",">","from","mmengine.config","import","Config",">",">",">","from","mmdet.models.roi_heads.mask_heads.fcn_mask_head","import","*","#","NOQA",">",">",">","N","=","7","#","N","=","number","of","extracted","ROIs",">",">",">","C",",","H",",","W","=","11",",","32",",","32",">",">",">","#","Create","example","instance","of","FCN","Mask","Head",".",">",">",">","self","=","FCNMaskHead","(","num_classes=C",",","num_convs=0",")",">",">",">","inputs","=","torch.rand","(","N",",","self.in_channels",",","H",",","W",")",">",">",">","mask_preds","=","self.forward","(","inputs",")",">",">",">","#","Each","input","is","associated","with","some","bounding","box",">",">",">","bboxes","=","torch.Tensor","(","[","[","1",",","1",",","42",",","42","]","]","*","N",")",">",">",">","labels","=","torch.randint","(","0",",","C",",","size=","(","N",",",")",")",">",">",">","rcnn_test_cfg","=","Config","(","{","'mask_thr_binary","'",":","0",",","}",")",">",">",">","ori_shape","=","(","H","*","4",",","W","*","4",")",">",">",">","scale_factor","=","(","1",",","1",")",">",">",">","rescale","=","False",">",">",">","img_meta","=","{","'scale_factor","'",":","scale_factor",",","...","'ori_shape","'",":","ori_shape","}",">",">",">","#","Encoded","masks","are","a","list","for","each","category",".",">",">",">","encoded_masks","=","self._get_seg_masks_single","(","...","mask_preds",",","bboxes",",","labels",",","...","img_meta",",","rcnn_test_cfg",",","rescale",")",">",">",">","assert","encoded_masks.size","(",")","[","0","]","==","N",">",">",">","assert","encoded_masks.size","(",")","[","1",":","]","==","ori_shape"]
code: def _predict_by_feat_single(
      self,
      mask_preds: Tensor,
      bboxes: Tensor,
      labels: Tensor,
      img_meta: dict,
      rcnn_test_cfg: ConfigDict,
      rescale: bool = False,
      activate_map: bool = False,
  ) -> Tensor:
      """Get segmentation masks from mask_preds and bboxes.
  
      Args:
          mask_preds (Tensor): Predicted foreground masks, has shape
              (n, num_classes, h, w).
          bboxes (Tensor): Predicted bboxes, has shape (n, 4)
          labels (Tensor): Labels of bboxes, has shape (n, )
          img_meta (dict): image information.
          rcnn_test_cfg (obj:`ConfigDict`): `test_cfg` of Bbox Head.
              Defaults to None.
          rescale (bool): If True, return boxes in original image space.
              Defaults to False.
          activate_map (book): Whether get results with augmentations test.
              If True, the `mask_preds` will not process with sigmoid.
              Defaults to False.
  
      Returns:
          Tensor: Encoded masks, has shape (n, img_w, img_h)
  
      Example:
          >>> from mmengine.config import Config
          >>> from mmdet.models.roi_heads.mask_heads.fcn_mask_head import *  # NOQA
          >>> N = 7  # N = number of extracted ROIs
          >>> C, H, W = 11, 32, 32
          >>> # Create example instance of FCN Mask Head.
          >>> self = FCNMaskHead(num_classes=C, num_convs=0)
          >>> inputs = torch.rand(N, self.in_channels, H, W)
          >>> mask_preds = self.forward(inputs)
          >>> # Each input is associated with some bounding box
          >>> bboxes = torch.Tensor([[1, 1, 42, 42 ]] * N)
          >>> labels = torch.randint(0, C, size=(N,))
          >>> rcnn_test_cfg = Config({'mask_thr_binary': 0, })
          >>> ori_shape = (H * 4, W * 4)
          >>> scale_factor = (1, 1)
          >>> rescale = False
          >>> img_meta = {'scale_factor': scale_factor,
          ...             'ori_shape': ori_shape}
          >>> # Encoded masks are a list for each category.
          >>> encoded_masks = self._get_seg_masks_single(
          ...     mask_preds, bboxes, labels,
          ...     img_meta, rcnn_test_cfg, rescale)
          >>> assert encoded_masks.size()[0] == N
          >>> assert encoded_masks.size()[1:] == ori_shape
      """
      scale_factor = bboxes.new_tensor(img_meta["scale_factor"]).repeat(
          (1, 2)
      )
      img_h, img_w = img_meta["ori_shape"][:2]
      device = bboxes.device
  
      if not activate_map:
          mask_preds = mask_preds.sigmoid()
      else:
          # In AugTest, has been activated before
          mask_preds = bboxes.new_tensor(mask_preds)
  
      if rescale:  # in-placed rescale the bboxes
          bboxes /= scale_factor
      else:
          w_scale, h_scale = scale_factor[0, 0], scale_factor[0, 1]
          img_h = np.round(img_h * h_scale.item()).astype(np.int32)
          img_w = np.round(img_w * w_scale.item()).astype(np.int32)
  
      N = len(mask_preds)
      # The actual implementation split the input into chunks,
      # and paste them chunk by chunk.
      if device.type == "cpu":
          # CPU is most efficient when they are pasted one by one with
          # skip_empty=True, so that it performs minimal number of
          # operations.
          num_chunks = N
      else:
          # GPU benefits from parallelism for larger chunks,
          # but may have memory issue
          # the types of img_w and img_h are np.int32,
          # when the image resolution is large,
          # the calculation of num_chunks will overflow.
          # so we need to change the types of img_w and img_h to int.
          # See https://github.com/open-mmlab/mmdetection/pull/5191
          num_chunks = int(
              np.ceil(
                  N
                  * int(img_h)
                  * int(img_w)
                  * BYTES_PER_FLOAT
                  / GPU_MEM_LIMIT
              )
          )
          assert (
              num_chunks <= N
          ), "Default GPU_MEM_LIMIT is too small; try increasing it"
      chunks = torch.chunk(torch.arange(N, device=device), num_chunks)
  
      threshold = rcnn_test_cfg.mask_thr_binary
      im_mask = torch.zeros(
          N,
          img_h,
          img_w,
          device=device,
          dtype=torch.bool if threshold >= 0 else torch.uint8,
      )
  
      if not self.class_agnostic:
          mask_preds = mask_preds[range(N), labels][:, None]
  
      for inds in chunks:
          masks_chunk, spatial_inds = _do_paste_mask(
              mask_preds[inds],
              bboxes[inds],
              img_h,
              img_w,
              skip_empty=device.type == "cpu",
          )
  
          if threshold >= 0:
              masks_chunk = (masks_chunk >= threshold).to(
                  dtype=torch.bool
              )
          else:
              # for visualization and debugging
              masks_chunk = (masks_chunk * 255).to(dtype=torch.uint8)
  
          im_mask[(inds,) + spatial_inds] = masks_chunk
      return im_mask
  
imports: from typing import List, Tuple
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule, build_conv_layer, build_upsample_layer
  from mmcv.ops.carafe import CARAFEPack
  from mmengine.config import ConfigDict
  from mmengine.model import BaseModule, ModuleList
  from mmengine.structures import InstanceData
  from torch import Tensor
  from torch.nn.modules.utils import _pair
  from mmdet.models.task_modules.samplers import SamplingResult
  from mmdet.models.utils import empty_instances
  from mmdet.registry import MODELS
  from mmdet.structures.mask import mask_target
  from mmdet.utils import ConfigType, InstanceList, OptConfigType, OptMultiConfig
label_desc: Use image node_id 7 for calling the FCNMaskHead obj's underlying member method code with example usage: obj._predict_by_feat_single(mask_preds, bboxes, labels, img_meta, rcnn_test_cfg, rescale, activate_map) and returns: im_mask