code: def cityscapes_wrapper_factory(dataset, target_keys):
      if any(
          target_type in dataset.target_type
          for target_type in ["polygon", "color"]
      ):
          raise_not_supported(
              "`Cityscapes` dataset with `target_type=['polygon', 'color', ...]`"
          )
  
      def instance_segmentation_wrapper(mask):
          # See https://github.com/mcordts/cityscapesScripts/blob/8da5dd00c9069058ccc134654116aac52d4f6fa2/cityscapesscripts/preparation/json2instanceImg.py#L7-L21
          data = pil_image_to_mask(mask)
          masks = []
          labels = []
          for id in data.unique():
              masks.append(data == id)
              label = id
              if label >= 1_000:
                  label //= 1_000
              labels.append(label)
          return dict(
              masks=tv_tensors.Mask(torch.stack(masks)),
              labels=torch.stack(labels),
          )
  
      def wrapper(idx, sample):
          image, target = sample
  
          target = wrap_target_by_type(
              target,
              target_types=dataset.target_type,
              type_wrappers={
                  "instance": instance_segmentation_wrapper,
                  "semantic": pil_image_to_mask,
              },
          )
  
          return image, target
  
      return wrapper
  
imports: from __future__ import annotations
  import collections.abc
  import contextlib
  from collections import defaultdict
  from copy import copy
  import torch
  from torchvision import datasets, tv_tensors
  from torchvision.transforms.v2 import functional
label_desc: Use image node_id 19 for calling a global function with example usage: cityscapes_wrapper_factory(dataset, target_keys) and returns: wrapper, dict, image,  target