text: import torch
  import random
  import numpy as np
  
  from PIL import Image, ImageOps, ImageFilter
  
  
  class Normalize(object):
      """Normalize a tensor image with mean and standard deviation.
      Args:
          mean (tuple): means for each channel.
          std (tuple): standard deviations for each channel.
      """
  
      def __init__(self, mean=(0.0, 0.0, 0.0), std=(1.0, 1.0, 1.0)):
          self.mean = mean
          self.std = std
  
      def __call__(self, sample):
          img = sample["image"]
          mask = sample["label"]
          img = np.array(img).astype(np.float32)
          mask = np.array(mask).astype(np.float32)
          img /= 255.0
          img -= self.mean
          img /= self.std
          if "center" not in sample.keys():
              return {"image": img, "label": mask}
          else:
              center = sample["center"]
              x_reg = sample["x_reg"]
              y_reg = sample["y_reg"]
              center = np.array(center).astype(np.float32)
              x_reg = np.array(x_reg).astype(np.float32)
              y_reg = np.array(y_reg).astype(np.float32)
              return {
                  "image": img,
                  "label": mask,
                  "center": center,
                  "x_reg": x_reg,
                  "y_reg": y_reg,
              }
  
  
  class ToTensor(object):
      """Convert ndarrays in sample to Tensors."""
  
      def __call__(self, sample):
          # swap color axis because
          # numpy image: H x W x C
          # torch image: C X H X W
          img = sample["image"]
          mask = sample["label"]
          img = np.array(img).astype(np.float32).transpose((2, 0, 1))
          mask = np.array(mask).astype(np.float32)
  
          img = torch.from_numpy(img).float()
          mask = torch.from_numpy(mask).float()
  
          if "center" not in sample.keys():
              return {"image": img, "label": mask}
          else:
              center = sample["center"]
              x_reg = sample["x_reg"]
              y_reg = sample["y_reg"]
              center = np.array(center).astype(np.float32)
              x_reg = np.array(x_reg).astype(np.float32)
              y_reg = np.array(y_reg).astype(np.float32)
              center = torch.from_numpy(center).float()
              x_reg = torch.from_numpy(x_reg).float()
              y_reg = torch.from_numpy(y_reg).float()
              return {
                  "image": img,
                  "label": mask,
                  "center": center,
                  "x_reg": x_reg,
                  "y_reg": y_reg,
              }
  
  
  class RandomHorizontalFlip(object):
      def __call__(self, sample):
          img = sample["image"]
          mask = sample["label"]
          if "center" in sample.keys():
              center = sample["center"]
              x_reg = sample["x_reg"]
              y_reg = sample["y_reg"]
              if random.random() < 0.5:
                  img = img.transpose(Image.FLIP_LEFT_RIGHT)
                  mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
                  center = center.transpose(Image.FLIP_LEFT_RIGHT)
                  x_reg = x_reg.transpose(Image.FLIP_LEFT_RIGHT)
                  y_reg = y_reg.transpose(Image.FLIP_LEFT_RIGHT)
              return {
                  "image": img,
                  "label": mask,
                  "center": center,
                  "x_reg": x_reg,
                  "y_reg": y_reg,
              }
          else:
              if random.random() < 0.5:
                  img = img.transpose(Image.FLIP_LEFT_RIGHT)
                  mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
              return {"image": img, "label": mask}
  
  
  class RandomRotate(object):
      def __init__(self, degree):
          self.degree = degree
  
      def __call__(self, sample):
          img = sample["image"]
          mask = sample["label"]
          rotate_degree = random.uniform(-1 * self.degree, self.degree)
          img = img.rotate(rotate_degree, Image.BILINEAR)
          mask = mask.rotate(rotate_degree, Image.NEAREST)
  
          return {"image": img, "label": mask}
  
  
  class RandomGaussianBlur(object):
      def __call__(self, sample):
          img = sample["image"]
          mask = sample["label"]
          if random.random() < 0.5:
              img = img.filter(ImageFilter.GaussianBlur(radius=random.random()))
  
          if "center" in sample.keys():
              center = sample["center"]
              x_reg = sample["x_reg"]
              y_reg = sample["y_reg"]
              return {
                  "image": img,
                  "label": mask,
                  "center": center,
                  "x_reg": x_reg,
                  "y_reg": y_reg,
              }
          else:
              return {"image": img, "label": mask}
  
  
  class RandomScaleCrop(object):
      def __init__(self, base_size, crop_size, fill=0):
          self.base_size = base_size
          self.crop_size = crop_size
          self.fill = fill
  
      def __call__(self, sample):
          img = sample["image"]
          mask = sample["label"]
          panoptic = False
          if "center" in sample.keys():
              center = sample["center"]
              x_reg = sample["x_reg"]
              y_reg = sample["y_reg"]
              panoptic = True
          # random scale (short edge)
          short_size = random.randint(
              int(self.base_size * 0.5), int(self.base_size * 2.0)
          )
          w, h = img.size
          if h > w:
              ow = short_size
              oh = int(1.0 * h * ow / w)
          else:
              oh = short_size
              ow = int(1.0 * w * oh / h)
          img = img.resize((ow, oh), Image.BILINEAR)
          mask = mask.resize((ow, oh), Image.NEAREST)
          if panoptic:
              center = center.resize((ow, oh), Image.NEAREST)
              x_reg = x_reg.resize((ow, oh), Image.NEAREST)
              y_reg = y_reg.resize((ow, oh), Image.NEAREST)
  
          # pad crop
          if short_size < self.crop_size:
              padh = self.crop_size - oh if oh < self.crop_size else 0
              padw = self.crop_size - ow if ow < self.crop_size else 0
              img = ImageOps.expand(img, border=(0, 0, padw, padh), fill=0)
              mask = ImageOps.expand(
                  mask, border=(0, 0, padw, padh), fill=self.fill
              )
              if panoptic:
                  center = ImageOps.expand(
                      center, border=(0, 0, padw, padh), fill=0
                  )
                  x_reg = ImageOps.expand(
                      x_reg, border=(0, 0, padw, padh), fill=0
                  )
                  y_reg = ImageOps.expand(
                      y_reg, border=(0, 0, padw, padh), fill=0
                  )
          # random crop crop_size
          w, h = img.size
          x1 = random.randint(0, w - self.crop_size)
          y1 = random.randint(0, h - self.crop_size)
          if panoptic:
              img = img.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
              mask = mask.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              center = center.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              x_reg = x_reg.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              y_reg = y_reg.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              return {
                  "image": img,
                  "label": mask,
                  "center": center,
                  "x_reg": x_reg,
                  "y_reg": y_reg,
              }
          else:
              img = img.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
              mask = mask.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              return {"image": img, "label": mask}
  
  
  class FixScaleCrop(object):
      def __init__(self, crop_size):
          self.crop_size = crop_size
  
      def __call__(self, sample):
          img = sample["image"]
          mask = sample["label"]
          panoptic = False
          if "center" in sample.keys():
              center = sample["center"]
              x_reg = sample["x_reg"]
              y_reg = sample["y_reg"]
              panoptic = True
          w, h = img.size
          if w > h:
              oh = self.crop_size
              ow = int(1.0 * w * oh / h)
          else:
              ow = self.crop_size
              oh = int(1.0 * h * ow / w)
          img = img.resize((ow, oh), Image.BILINEAR)
          mask = mask.resize((ow, oh), Image.NEAREST)
          if panoptic:
              center = center.resize((ow, oh), Image.NEAREST)
              x_reg = x_reg.resize((ow, oh), Image.NEAREST)
              y_reg = y_reg.resize((ow, oh), Image.NEAREST)
  
          # center crop
          w, h = img.size
          x1 = int(round((w - self.crop_size) / 2.0))
          y1 = int(round((h - self.crop_size) / 2.0))
          if panoptic:
              img = img.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
              mask = mask.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              center = center.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              x_reg = x_reg.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              y_reg = y_reg.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              return {
                  "image": img,
                  "label": mask,
                  "center": center,
                  "x_reg": x_reg,
                  "y_reg": y_reg,
              }
          else:
              img = img.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
              mask = mask.crop(
                  (x1, y1, x1 + self.crop_size, y1 + self.crop_size)
              )
              return {"image": img, "label": mask}
  
  
  class FixedResize(object):
      def __init__(self, size):
          self.size = (size, size)  # size: (h, w)
  
      def __call__(self, sample):
          img = sample["image"]
          mask = sample["label"]
  
          assert img.size == mask.size
  
          img = img.resize(self.size, Image.BILINEAR)
          mask = mask.resize(self.size, Image.NEAREST)
  
          if "center" in sample.keys():
              center = sample["center"]
              x_reg = sample["x_reg"]
              y_reg = sample["y_reg"]
              center = center.resize(self.size, Image.NEAREST)
              x_reg = x_reg.resize(self.size, Image.NEAREST)
              y_reg = y_reg.resize(self.size, Image.NEAREST)
              return {
                  "image": img,
                  "label": mask,
                  "center": center,
                  "x_reg": x_reg,
                  "y_reg": y_reg,
              }
          else:
              return {"image": img, "label": mask}
  