code: def annotations_to_instances(
      annos, image_size, mask_format="polygon"
  ):
      """
      Create an :class:`Instances` object used by the models,
      from instance annotations in the dataset dict.
  
      Args:
          annos (list[dict]): a list of instance annotations in one image, each
              element for one instance.
          image_size (tuple): height, width
  
      Returns:
          Instances:
              It will contain fields "gt_boxes", "gt_classes",
              "gt_masks", "gt_keypoints", if they can be obtained from `annos`.
              This is the format that builtin models expect.
      """
      boxes = [
          BoxMode.convert(
              obj["bbox"], obj["bbox_mode"], BoxMode.XYXY_ABS
          )
          for obj in annos
      ]
      target = Instances(image_size)
      target.gt_boxes = Boxes(boxes)
  
      classes = [obj["category_id"] for obj in annos]
      classes = torch.tensor(classes, dtype=torch.int64)
      target.gt_classes = classes
  
      if len(annos) and "segmentation" in annos[0]:
          segms = [obj["segmentation"] for obj in annos]
          if mask_format == "polygon":
              # TODO check type and provide better error
              masks = PolygonMasks(segms)
          else:
              try_import_pycocotools()
              import pycocotools.mask as mask_util
  
              assert mask_format == "bitmask", mask_format
              masks = []
              for segm in segms:
                  if isinstance(segm, list):
                      # polygon
                      masks.append(
                          polygons_to_bitmask(segm, *image_size)
                      )
                  elif isinstance(segm, dict):
                      # COCO RLE
                      masks.append(mask_util.decode(segm))
                  elif isinstance(segm, np.ndarray):
                      assert (
                          segm.ndim == 2
                      ), "Expect segmentation of 2 dimensions, got {}.".format(
                          segm.ndim
                      )
                      # mask array
                      masks.append(segm)
                  else:
                      raise ValueError(
                          "Cannot convert segmentation of type '{}' to BitMasks!"
                          "Supported types are: polygons as list[list[float] or ndarray],"
                          " COCO-style RLE as a dict, or a full-image segmentation mask "
                          "as a 2D ndarray.".format(type(segm))
                      )
              # torch.from_numpy does not support array with negative stride.
              masks = BitMasks(
                  torch.stack(
                      [
                          torch.from_numpy(np.ascontiguousarray(x))
                          for x in masks
                      ]
                  )
              )
          target.gt_masks = masks
  
      if len(annos) and "keypoints" in annos[0]:
          kpts = [obj.get("keypoints", []) for obj in annos]
          target.gt_keypoints = Keypoints(kpts)
  
      # add attributes
      if len(annos) and "beziers" in annos[0]:
          beziers = [obj.get("beziers", []) for obj in annos]
          instance.beziers = torch.as_tensor(
              beziers, dtype=torch.float32
          )
  
      if len(annos) and "rec" in annos[0]:
          text = [obj.get("rec", []) for obj in annos]
          instance.text = torch.as_tensor(text, dtype=torch.int32)
  
      return target
  
imports: import os
  import logging
  import numpy
  from PIL import Image
  import torch
  from ..structures import BitMasks, Boxes, BoxMode, Instances, Keypoints, PolygonMasks, polygons_to_bitmask
  from ..transforms import instance_transforms
  from ..registry.catalog import MetadataCatalog
label_desc: Use image node_id 12 for calling a global function with example usage: annotations_to_instances(annos, image_size, mask_format) and returns: target