{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "from torchvision import io\n",
    "from semseg.augmentations import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_images(image1, image2):\n",
    "    images = torch.cat([image1, image2], dim=2)\n",
    "    images = Image.fromarray(images.permute(1, 2, 0).numpy())\n",
    "    return images\n",
    "\n",
    "def show_image_labels(image, label, img, lbl):\n",
    "    images = torch.cat([image, img], dim=2)\n",
    "    labels = torch.cat([label, lbl], dim=2)\n",
    "    image_labels = torch.cat([images, labels], dim=1)\n",
    "    image_labels = Image.fromarray(image_labels.permute(1, 2, 0).numpy())\n",
    "    return image_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_path = '../assests/image_labels/Seq05VD_f05100.png'\n",
    "label_path = '../assests/image_labels/Seq05VD_f05100_L.png'\n",
    "image = io.read_image(img_path)\n",
    "label = io.read_image(label_path)\n",
    "show_images(image, label)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pixel-level Transforms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ColorJitter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = ColorJitter(brightness=0.5)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = ColorJitter(contrast=0.5)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = ColorJitter(saturation=0.5)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = ColorJitter(hue=0.5)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## AdjustGamma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = AdjustGamma(gamma=0.5, gain=1)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## AdjustSharpness"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomAdjustSharpness(10, p=1.0)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## AutoContrast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomAutoContrast(p=1.0)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GaussianBlur"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomGaussianBlur(5, p=1.0)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Grayscale"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# channel size remains to 3 instead of 1\n",
    "img, lbl = RandomGrayscale(p=1.0)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Equalize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Equalize()(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Posterize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Posterize(1)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Posterize(7)(image, label)\n",
    "show_images(image, img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Spatial-level Transforms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Affine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Affine(angle=10)(image, label)\n",
    "show_image_labels(image, label, img, lbl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Affine(translate=[30, 10])(image, label)\n",
    "show_image_labels(image, label, img, lbl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Affine(scale=0.5)(image, label)\n",
    "show_image_labels(image, label, img, lbl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Affine(scale=2.0)(image, label)\n",
    "show_image_labels(image, label, img, lbl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Affine(shear=[-10, 10])(image, label)\n",
    "show_image_labels(image, label, img, lbl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HorizontalFlip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomHorizontalFlip(p=1.0)(image, label)\n",
    "show_image_labels(image, label, img, lbl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## VerticalFlip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomVerticalFlip(p=1.0)(image, label)\n",
    "show_image_labels(image, label, img, lbl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RandomRotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomRotation(30, p=1.0, seg_fill=0)(image, label)\n",
    "show_image_labels(image, label, img, lbl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CenterCrop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = CenterCrop((320, 320))(image, label)\n",
    "show_images(image, label)   # original "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "show_images(img, lbl)  # center crop to (320, 320)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RandomCrop\n",
    "\n",
    "Crop at random location. (Like a zoom-in effect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomCrop((320, 320), p=1.0)(image, label)\n",
    "print(image.shape, label.shape)\n",
    "show_images(image, label)   # original "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pad\n",
    "\n",
    "Pad the image to given size."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Pad((1024, 1024), seg_fill=0)(image, label)\n",
    "print(image.shape, label.shape)\n",
    "show_images(image, label)   # original "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if pad output size is less than original, then crop the image start from the corner\n",
    "# not the same as RandomCrop; which will crop at random location instead of from the corner\n",
    "img, lbl = Pad((480, 640), seg_fill=0)(image, label)\n",
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ResizePad\n",
    "\n",
    "Nearly the same as Pad but uses resize instead of crop in **Pad**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = ResizePad((1024, 1024), seg_fill=0)(image, label)\n",
    "print(image.shape, label.shape)\n",
    "show_images(image, label)   # original "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = ResizePad((480, 640), seg_fill=0)(image, label)\n",
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Resize\n",
    "\n",
    "Resize the shortest image size to the target size while maintaing the aspect ratio. And also make it divisible by stride 32. \n",
    "\n",
    "Notes: This will make the image sizes not equal; which is not suitable to use in training."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Resize((512, 512))(image, label)\n",
    "print(image.shape, label.shape)\n",
    "show_images(image, label)   # original "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# as u can see, the other side is not 512 but maintains the aspect ratio\n",
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = Resize((1024, 1024))(image, label)\n",
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RandomResizedCrop\n",
    "\n",
    "Combination of **RandomCrop** and **ResizePad**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomResizedCrop((512, 512), (0.5, 2.0), seg_fill=0)(image, label)\n",
    "print(image.shape, label.shape)\n",
    "show_images(image, label)   # original "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# as u can see, the other side is not 512 but maintains the aspect ratio\n",
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomResizedCrop((1024, 1024), (0.5, 2.0), seg_fill=0)(image, label)\n",
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomResizedCrop((1024, 1024), (0.5, 2.0), seg_fill=0)(image, label)\n",
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, lbl = RandomResizedCrop((1024, 1024), (0.5, 2.0), seg_fill=0)(image, label)\n",
    "print(img.shape, lbl.shape)\n",
    "show_images(img, lbl)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "78184fe1b8a3f830767e8814b2b01c36fc7c8ac521e39cb583cd3fce210fee57"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
