code: """
  Transforms and data augmentation for sequence level images, bboxes and masks.
  
  Mostly copy-paste from https://github.com/Epiphqny/VisTR/blob/master/datasets/transforms.py
  """
  import random
  
  import PIL
  import torch
  import torchvision.transforms as T
  import torchvision.transforms.functional as F
  
  from util.box_ops import box_xyxy_to_cxcywh, box_iou
  from util.misc import interpolate
  import numpy as np
  from numpy import random as rand
  from PIL import Image
  import cv2
  import pdb
  
  def bbox_overlaps(bboxes1, bboxes2, mode='iou', eps=1e-6):
      assert mode in ['iou', 'iof']
      bboxes1 = bboxes1.astype(np.float32)
      bboxes2 = bboxes2.astype(np.float32)
      rows = bboxes1.shape[0]
      cols = bboxes2.shape[0]
      ious = np.zeros((rows, cols), dtype=np.float32)
      if rows * cols == 0:
          return ious
      exchange = False
      if bboxes1.shape[0] > bboxes2.shape[0]:
          bboxes1, bboxes2 = bboxes2, bboxes1
          ious = np.zeros((cols, rows), dtype=np.float32)
          exchange = True
      area1 = (bboxes1[:, 2] - bboxes1[:, 0]) * (bboxes1[:, 3] - bboxes1[:, 1])
      area2 = (bboxes2[:, 2] - bboxes2[:, 0]) * (bboxes2[:, 3] - bboxes2[:, 1])
      for i in range(bboxes1.shape[0]):
          x_start = np.maximum(bboxes1[i, 0], bboxes2[:, 0])
          y_start = np.maximum(bboxes1[i, 1], bboxes2[:, 1])
          x_end = np.minimum(bboxes1[i, 2], bboxes2[:, 2])
          y_end = np.minimum(bboxes1[i, 3], bboxes2[:, 3])
          overlap = np.maximum(x_end - x_start, 0) * np.maximum(y_end - y_start, 0)
          if mode == 'iou':
              union = area1[i] + area2 - overlap
          else:
              union = area1[i] if not exchange else area2
          union = np.maximum(union, eps)
          ious[i, :] = overlap / union
      if exchange:
          ious = ious.T
      return ious
  
  
  def crop(image, target, region):
      cropped_image = F.crop(image, *region)
  
      target = target.copy()
      i, j, h, w = region
  
      # should we do something wrt the original size?
      target["size"] = torch.tensor([h, w])
  
      fields = ["labels", "area", "iscrowd"]
  
      if "boxes" in target:
          boxes = target["boxes"]
          max_size = torch.as_tensor([w, h], dtype=torch.float32)
          cropped_boxes = boxes - torch.as_tensor([j, i, j, i])
          cropped_boxes = torch.min(cropped_boxes.reshape(-1, 2, 2), max_size)
          cropped_boxes = cropped_boxes.clamp(min=0)
          area = (cropped_boxes[:, 1, :] - cropped_boxes[:, 0, :]).prod(dim=1)
          target["boxes"] = cropped_boxes.reshape(-1, 4)
          target["area"] = area
          fields.append("boxes")
  
      if "masks" in target:
          # FIXME should we update the area here if there are no boxes?
          target['masks'] = target['masks'][:, i:i + h, j:j + w]
          fields.append("masks")
  
      # remove elements for which the boxes or masks that have zero area
      if "boxes" in target or "masks" in target:
          # favor boxes selection when defining which elements to keep
          # this is compatible with previous implementation
          if "boxes" in target:
              cropped_boxes = target['boxes'].reshape(-1, 2, 2)
              keep = torch.all(cropped_boxes[:, 1, :] > cropped_boxes[:, 0, :], dim=1)
          else:
              keep = target['masks'].flatten(1).any(1)
  
          for field in fields:
              target[field] = target[field][keep]
  
      return cropped_image, target
  
  
  def hflip(image, target):
      flipped_image = F.hflip(image)
  
      w, h = image.size
  
      target = target.copy()
      if "boxes" in target:
          boxes = target["boxes"]
          boxes = boxes[:, [2, 1, 0, 3]] * torch.as_tensor([-1, 1, -1, 1]) + torch.as_tensor([w, 0, w, 0])
          target["boxes"] = boxes
  
      if "masks" in target:
          target['masks'] = target['masks'].flip(-1)
  
      return flipped_image, target
  
  
  def resize(image, target, size, max_size=None):
      # size can be min_size (scalar) or (w, h) tuple
  
      def get_size_with_aspect_ratio(image_size, size, max_size=None):
          w, h = image_size
          if max_size is not None:
              min_original_size = float(min((w, h)))
              max_original_size = float(max((w, h)))
              if max_original_size / min_original_size * size > max_size:
                  size = int(round(max_size * min_original_size / max_original_size))
  
          if (w <= h and w == size) or (h <= w and h == size):
              return (h, w)
  
          if w < h:
              ow = size
              oh = int(size * h / w)
          else:
              oh = size
              ow = int(size * w / h)
  
          return (oh, ow)
  
      def get_size(image_size, size, max_size=None):
          if isinstance(size, (list, tuple)):
              return size[::-1]
          else:
              return get_size_with_aspect_ratio(image_size, size, max_size)
  
      try:
          size = get_size(image.size, size, max_size)
      except:
          pdb.set_trace()
      rescaled_image = F.resize(image, size)
  
      if target is None:
          return rescaled_image, None
  
      ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(rescaled_image.size, image.size))
      ratio_width, ratio_height = ratios
  
      target = target.copy()
      if "boxes" in target:
          boxes = target["boxes"]
          scaled_boxes = boxes * torch.as_tensor([ratio_width, ratio_height, ratio_width, ratio_height])
          target["boxes"] = scaled_boxes
  
      if "area" in target:
          area = target["area"]
          scaled_area = area * (ratio_width * ratio_height)
          target["area"] = scaled_area
  
      h, w = size
      target["size"] = torch.tensor([h, w])
  
      if "masks" in target:
          target['masks'] = interpolate(
              target['masks'][:, None].float(), size, mode="nearest")[:, 0] > 0.5
  
      return rescaled_image, target
  
  
  def pad(image, target, padding):
      # assumes that we only pad on the bottom right corners
      padded_image = F.pad(image, (0, 0, padding[0], padding[1]))
      if target is None:
          return padded_image, None
      target = target.copy()
      # should we do something wrt the original size?
      target["size"] = torch.tensor(padded_image.size[::-1])
      if "masks" in target:
          target['masks'] = torch.nn.functional.pad(target['masks'], (0, padding[0], 0, padding[1]))
      return padded_image, target
  
  
  class RandomCrop(object):
      def __init__(self, size):
          self.size = size
  
      def __call__(self, img, target):
          region = T.RandomCrop.get_params(img, self.size)
          return crop(img, target, region)
  
  
  class RandomSizeCrop(object):
      def __init__(self, min_size: int, max_size: int):
          self.min_size = min_size
          self.max_size = max_size
  
      def __call__(self, img: PIL.Image.Image, ref_img: PIL.Image.Image, target: dict, ref_target: dict):
          w = random.randint(self.min_size, min(img.width, self.max_size))
          h = random.randint(self.min_size, min(img.height, self.max_size))
          region = T.RandomCrop.get_params(img, [h, w])
          img, target = crop(img, target, region)
          ref_img, ref_target = crop(ref_img, ref_target, region)
          return img, ref_img, target, ref_target
  
  
  class CenterCrop(object):
      def __init__(self, size):
          self.size = size
  
      def __call__(self, img, target):
          image_width, image_height = img.size
          crop_height, crop_width = self.size
          crop_top = int(round((image_height - crop_height) / 2.))
          crop_left = int(round((image_width - crop_width) / 2.))
          return crop(img, target, (crop_top, crop_left, crop_height, crop_width))
  
  
  class RandomContrast(object):
      def __init__(self, lower=0.5, upper=1.5):
          self.lower = lower
          self.upper = upper
          assert self.upper >= self.lower, "contrast upper must be >= lower."
          assert self.lower >= 0, "contrast lower must be non-negative."
      def __call__(self, image, ref_image, target, ref_target):
          
          if rand.randint(2):
              image *= rand.uniform(self.lower, self.upper)
              ref_image *= rand.uniform(self.lower, self.upper)
          return image, ref_image, target, ref_target
  
  class RandomBrightness(object):
      def __init__(self, delta=32):
          assert delta >= 0.0
          assert delta <= 255.0
          self.delta = delta
      def __call__(self, image, target):
          if rand.randint(2):
              delta = rand.uniform(-self.delta, self.delta)
              image += delta
          return image, target
  
  class RandomSaturation(object):
      def __init__(self, lower=0.5, upper=1.5):
          self.lower = lower
          self.upper = upper
          assert self.upper >= self.lower, "contrast upper must be >= lower."
          assert self.lower >= 0, "contrast lower must be non-negative."
  
      def __call__(self, image, ref_image, target, ref_target):
          if rand.randint(2):
              image[:, :, 1] *= rand.uniform(self.lower, self.upper)
              ref_image[:, :, 1] *= rand.uniform(self.lower, self.upper)
          return image, ref_image, target, ref_target
  
  class RandomHue(object): #
      def __init__(self, delta=18.0):
          assert delta >= 0.0 and delta <= 360.0
          self.delta = delta
  
      def __call__(self, image, ref_image, target, ref_target):
          if rand.randint(2):
              image[:, :, 0] += rand.uniform(-self.delta, self.delta)
              image[:, :, 0][image[:, :, 0] > 360.0] -= 360.0
              image[:, :, 0][image[:, :, 0] < 0.0] += 360.0
              ref_image[:, :, 0] += rand.uniform(-self.delta, self.delta)
              ref_image[:, :, 0][ref_image[:, :, 0] > 360.0] -= 360.0
              ref_image[:, :, 0][ref_image[:, :, 0] < 0.0] += 360.0
          return image, ref_image, target, ref_target
  
  class RandomLightingNoise(object):
      def __init__(self):
          self.perms = ((0, 1, 2), (0, 2, 1),
                        (1, 0, 2), (1, 2, 0),
                        (2, 0, 1), (2, 1, 0))
      def __call__(self, image, target):
          if rand.randint(2):
              swap = self.perms[rand.randint(len(self.perms))]
              shuffle = SwapChannels(swap)  # shuffle channels
              image = shuffle(image)
          return image, target
  
  class ConvertColor(object):
      def __init__(self, current='BGR', transform='HSV'):
          self.transform = transform
          self.current = current
  
      def __call__(self, image, ref_image, target, ref_target):
          if self.current == 'BGR' and self.transform == 'HSV':
              image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
              ref_image = cv2.cvtColor(ref_image, cv2.COLOR_BGR2HSV)
          elif self.current == 'HSV' and self.transform == 'BGR':
              image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
              ref_image = cv2.cvtColor(ref_image, cv2.COLOR_HSV2BGR)
          else:
              raise NotImplementedError
          return image, ref_image, target, ref_target
  
  class SwapChannels(object):
      def __init__(self, swaps):
          self.swaps = swaps
      def __call__(self, image):
          image = image[:, :, self.swaps]
          return image
  
  class PhotometricDistort(object):
      def __init__(self):
          self.pd = [
              RandomContrast(),
              ConvertColor(transform='HSV'),
              RandomSaturation(),
              RandomHue(),
              ConvertColor(current='HSV', transform='BGR'),
              RandomContrast()
          ]
          self.rand_brightness = RandomBrightness()
          self.rand_light_noise = RandomLightingNoise()
      
      def __call__(self, image, ref_image, target, ref_target):
          image = np.asarray(image).astype('float32')
          ref_image = np.asarray(ref_image).astype('float32')
          image, target = self.rand_brightness(image, target)
          ref_image, ref_target = self.rand_brightness(ref_image, ref_target)
          if rand.randint(2):
              distort = Compose(self.pd[:-1])
          else:
              distort = Compose(self.pd[1:])
          image, ref_image, target, ref_target = distort(image, ref_image, target, ref_target)
          image, target = self.rand_light_noise(image, target)
          ref_image, ref_target = self.rand_light_noise(ref_image, ref_target)
          image = Image.fromarray(image.astype('uint8'))
          ref_image = Image.fromarray(ref_image.astype('uint8'))
          return image, ref_image, target, ref_target
  
  class RandomHorizontalFlip(object):
      def __init__(self, p=0.5):
          self.p = p
  
      def __call__(self, img, ref_img, target, ref_target):
          if random.random() < self.p:
              img, target = hflip(img, target)
              ref_img, ref_target = hflip(ref_img, ref_target)
          return img, ref_img, target, ref_target
  
  class RandomVerticalFlip(object):
      def __init__(self, p=0.5):
          self.p = p
  
      def __call__(self, img, target):
          if random.random() < self.p:
              return vflip(img, target)
          return img, target
  
  
  class RandomResize(object):
      def __init__(self, sizes, max_size=None):
          assert isinstance(sizes, (list, tuple))
          self.sizes = sizes
          self.max_size = max_size
  
      def __call__(self, img, ref_img, target=None, ref_target=None):
          size = random.choice(self.sizes)
          img, target = resize(img, target, size, self.max_size)
          ref_img, ref_target = resize(ref_img, ref_target, size, self.max_size)
          return img, ref_img, target, ref_target
  
  
  class RandomPad(object):
      def __init__(self, max_pad):
          self.max_pad = max_pad
  
      def __call__(self, img, target):
          pad_x = random.randint(0, self.max_pad)
          pad_y = random.randint(0, self.max_pad)
          return pad(img, target, (pad_x, pad_y))
  
  
  class RandomSelect(object):
      """
      Randomly selects between transforms1 and transforms2,
      with probability p for transforms1 and (1 - p) for transforms2
      """
      def __init__(self, transforms1, transforms2, p=0.5):
          self.transforms1 = transforms1
          self.transforms2 = transforms2
          self.p = p
  
      def __call__(self, img, target):
          if random.random() < self.p:
              return self.transforms1(img, target)
          return self.transforms2(img, target)
  
  
  class ToTensor(object):
      def __call__(self, image, ref_image, target, ref_target):
          return F.to_tensor(image), F.to_tensor(ref_image), target, ref_target
  
  
  class RandomErasing(object):
  
      def __init__(self, *args, **kwargs):
          self.eraser = T.RandomErasing(*args, **kwargs)
  
      def __call__(self, img, target):
          return self.eraser(img), target
  
  
  class Normalize(object):
      def __init__(self, mean, std):
          self.mean = mean
          self.std = std
  
      def __call__(self, image, ref_image, target=None, ref_target=None):
          image = F.normalize(image, mean=self.mean, std=self.std)
          ref_image = F.normalize(ref_image, mean=self.mean, std=self.std)
          if target is None:
              return image, None, ref_image, None
          target = target.copy()
          h, w = image.shape[-2:]
          if "boxes" in target:
              boxes = target["boxes"]
              boxes = box_xyxy_to_cxcywh(boxes)
              boxes = boxes / torch.tensor([w, h, w, h], dtype=torch.float32)
              target["boxes"] = boxes
          ref_target = ref_target.copy()
          if "boxes" in ref_target:
              boxes = ref_target["boxes"]
              boxes = box_xyxy_to_cxcywh(boxes)
              boxes = boxes / torch.tensor([w, h, w, h], dtype=torch.float32)
              ref_target["boxes"] = boxes
          return image, ref_image, target, ref_target
  
  
  class Compose(object):
      def __init__(self, transforms):
          self.transforms = transforms
  
      def __call__(self, image, ref_image, target, ref_target):
          for t in self.transforms:
              image, ref_image, target, ref_target = t(image, ref_image, target, ref_target)
          return image, ref_image, target, ref_target
  
      def __repr__(self):
          format_string = self.__class__.__name__ + "("
          for t in self.transforms:
              format_string += "\n"
              format_string += "    {0}".format(t)
          format_string += "\n)"
          return format_string
apis: numpy.random.uniform numpy.zeros torch.as_tensor torch.nn.functional.pad torch.tensor numpy.asarray torch.all numpy.maximum numpy.random.randint numpy.minimum