docstr: rasterize_polygons_within_box (used in training)
  and
  paste_masks_in_image (used in inference)
  should be inverse operations to each other.
  
  This function runs several implementation of the above two operations and prints
  the reconstruction error.
docstr_tok: ["rasterize_polygons_within_box","(","used","in","training",")","and","paste_masks_in_image","(","used","in","inference",")","should","be","inverse","operations","to","each","other",".","This","function","runs","several","implementation","of","the","above","two","operations","and","prints","the","reconstruction","error","."]
code: def test_crop_paste_consistency(self):
      """
      rasterize_polygons_within_box (used in training)
      and
      paste_masks_in_image (used in inference)
      should be inverse operations to each other.
  
      This function runs several implementation of the above two operations and prints
      the reconstruction error.
      """
  
      anns = self.coco.loadAnns(
          self.coco.getAnnIds(iscrowd=False)
      )  # avoid crowd annotations
  
      selected_anns = anns[:100]
  
      ious = []
      for ann in tqdm.tqdm(selected_anns):
          results = self.process_annotation(ann)
          ious.append([k[2] for k in results])
  
      ious = np.array(ious)
      mean_ious = ious.mean(axis=0)
      table = []
      res_dic = defaultdict(dict)
      for row, iou in zip(results, mean_ious):
          table.append((row[0], row[1], iou))
          res_dic[row[0]][row[1]] = iou
      print(
          tabulate(
              table,
              headers=["rasterize", "paste", "iou"],
              tablefmt="simple",
          )
      )
      # assert that the reconstruction is good:
      self.assertTrue(res_dic["polygon"]["aligned"] > 0.94)
      self.assertTrue(res_dic["roialign"]["aligned"] > 0.95)
  
imports: import contextlib
  import io
  import numpy
  import unittest
  from collections import defaultdict
  import torch
  import tqdm
  from fvcore.common.benchmark import benchmark
  from pycocotools.coco import COCO
  from tabulate import tabulate
  from torch.nn import functional
  from detectron2.data import MetadataCatalog
  from detectron2.layers.mask_ops import pad_masks, paste_mask_in_image_old, paste_masks_in_image, scale_boxes
  from detectron2.structures import BitMasks, Boxes, BoxMode, PolygonMasks
  from detectron2.structures.masks import polygons_to_bitmask
  from detectron2.utils.file_io import PathManager
  from detectron2.utils.testing import random_boxes
label_desc: Use image node_id 2 for calling the TestMaskCropPaste obj's underlying member method code with example usage: obj.test_crop_paste_consistency() without return types