class_docstr: This class stores the segmentation masks for all objects in one image, in the form of polygons.
  
  Attributes:
      polygons: list[list[ndarray]]. Each ndarray is a float64 vector representing a polygon.
class_docstr_tok: ["This","class","stores","the","segmentation","masks","for","all","objects","in","one","image",",","in","the","form","of","polygons",".","Attributes",":","polygons",":","list","[","list","[","ndarray","]","]",".","Each","ndarray","is","a","float64","vector","representing","a","polygon","."]
docstr: Crop each mask by the given box, and resize results to (mask_size, mask_size).
  This can be used to prepare training targets for Mask R-CNN.
  
  Args:
      boxes (Tensor): Nx4 tensor storing the boxes for each mask
      mask_size (int): the size of the rasterized mask.
  
  Returns:
      Tensor: A bool tensor of shape (N, mask_size, mask_size), where
      N is the number of predicted boxes for this image.
docstr_tok: ["Crop","each","mask","by","the","given","box",",","and","resize","results","to","(","mask_size",",","mask_size",")",".","This","can","be","used","to","prepare","training","targets","for","Mask","R-CNN",".","Args",":","boxes","(","Tensor",")",":","Nx4","tensor","storing","the","boxes","for","each","mask","mask_size","(","int",")",":","the","size","of","the","rasterized","mask",".","Returns",":","Tensor",":","A","bool","tensor","of","shape","(","N",",","mask_size",",","mask_size",")",",","where","N","is","the","number","of","predicted","boxes","for","this","image","."]
code: def crop_and_resize(
      self, boxes: torch.Tensor, mask_size: int
  ) -> torch.Tensor:
      """
      Crop each mask by the given box, and resize results to (mask_size, mask_size).
      This can be used to prepare training targets for Mask R-CNN.
  
      Args:
          boxes (Tensor): Nx4 tensor storing the boxes for each mask
          mask_size (int): the size of the rasterized mask.
  
      Returns:
          Tensor: A bool tensor of shape (N, mask_size, mask_size), where
          N is the number of predicted boxes for this image.
      """
      assert len(boxes) == len(self), "{} != {}".format(
          len(boxes), len(self)
      )
  
      device = boxes.device
      # Put boxes on the CPU, as the polygon representation is not efficient GPU-wise
      # (several small tensors for representing a single instance mask)
      boxes = boxes.to(torch.device("cpu"))
  
      results = [
          rasterize_polygons_within_box(poly, box.numpy(), mask_size)
          for poly, box in zip(self.polygons, boxes)
      ]
      """
          poly: list[list[float]], the polygons for one instance
          box: a tensor of shape (4,)
          """
      if len(results) == 0:
          return torch.empty(
              0, mask_size, mask_size, dtype=torch.bool, device=device
          )
      return torch.stack(results, dim=0).to(device=device)
  
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 10 for calling the PolygonMasks obj's underlying member method code with example usage: obj.crop_and_resize(boxes, mask_size) and returns: torch, torch