code: def rasterize_polygons_within_box(
      polygons: List[np.ndarray], box: np.ndarray, mask_size: int
  ) -> torch.Tensor:
      """
      Rasterize the polygons into a mask image and
      crop the mask content in the given box.
      The cropped mask is resized to (mask_size, mask_size).
  
      This function is used when generating training targets for mask head in Mask R-CNN.
      Given original ground-truth masks for an image, new ground-truth mask
      training targets in the size of `mask_size x mask_size`
      must be provided for each predicted box. This function will be called to
      produce such targets.
  
      Args:
          polygons (list[ndarray[float]]): a list of polygons, which represents an instance.
          box: 4-element numpy array
          mask_size (int):
  
      Returns:
          Tensor: BoolTensor of shape (mask_size, mask_size)
      """
      # 1. Shift the polygons w.r.t the boxes
      w, h = box[2] - box[0], box[3] - box[1]
  
      polygons = copy.deepcopy(polygons)
      for p in polygons:
          p[0::2] = p[0::2] - box[0]
          p[1::2] = p[1::2] - box[1]
  
      # 2. Rescale the polygons to the new box size
      # max() to avoid division by small number
      ratio_h = mask_size / max(h, 0.1)
      ratio_w = mask_size / max(w, 0.1)
  
      if ratio_h == ratio_w:
          for p in polygons:
              p *= ratio_h
      else:
          for p in polygons:
              p[0::2] *= ratio_w
              p[1::2] *= ratio_h
  
      # 3. Rasterize the polygons with coco api
      mask = polygons_to_bitmask(polygons, mask_size, mask_size)
      mask = torch.from_numpy(mask)
      return mask
  
imports: import copy
  import itertools
  import numpy
  from typing import Any, Iterator, List, Union
  import pycocotools.mask
  import torch
  from torch import device
  from detectron2.layers.roi_align import ROIAlign
  from detectron2.utils.memory import retry_if_cuda_oom
  from .boxes import Boxes
label_desc: Use image node_id 3 for calling a global function with example usage: rasterize_polygons_within_box(polygons, box, mask_size) and returns: mask