{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://pbs.twimg.com/media/E_uXFk_WYAE_6n3.png)twitter.com","metadata":{}},{"cell_type":"code","source":"!pip install kornia","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:22:40.945904Z","iopub.execute_input":"2022-04-09T23:22:40.946650Z","iopub.status.idle":"2022-04-09T23:22:50.190687Z","shell.execute_reply.started":"2022-04-09T23:22:40.946532Z","shell.execute_reply":"2022-04-09T23:22:50.189604Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd\n\nimport cv2\nfrom matplotlib import pyplot as plt\n\nimport torch\nimport torchvision\nimport kornia as K","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:28:20.289573Z","iopub.execute_input":"2022-04-09T23:28:20.289912Z","iopub.status.idle":"2022-04-09T23:28:20.295123Z","shell.execute_reply.started":"2022-04-09T23:28:20.289878Z","shell.execute_reply":"2022-04-09T23:28:20.294093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Load an image with OpenCV","metadata":{}},{"cell_type":"code","source":"img_bgr: np.array = cv2.imread('../input/snakeclef2022/SnakeCLEF2022-medium_size/SnakeCLEF2022-medium_size/2004/Python_molurus/1610154.jpg')  # HxWxC / np.uint8\nimg_rgb: np.array = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)\n\nplt.imshow(img_rgb); plt.axis('off');","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:29:51.712471Z","iopub.execute_input":"2022-04-09T23:29:51.712805Z","iopub.status.idle":"2022-04-09T23:29:51.992178Z","shell.execute_reply.started":"2022-04-09T23:29:51.712736Z","shell.execute_reply":"2022-04-09T23:29:51.991472Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Load an image with Torchvision\n\nIt returns the images in a torch.Tensor in the shape (C,H,W)","metadata":{}},{"cell_type":"code","source":"x_rgb: torch.tensor = torchvision.io.read_image('../input/snakeclef2022/SnakeCLEF2022-medium_size/SnakeCLEF2022-medium_size/2004/Python_molurus/1610154.jpg')  # CxHxW / torch.uint8\nx_rgb = x_rgb.unsqueeze(0)  # BxCxHxW\nprint(x_rgb.shape);","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:31:01.124088Z","iopub.execute_input":"2022-04-09T23:31:01.124696Z","iopub.status.idle":"2022-04-09T23:31:01.159443Z","shell.execute_reply.started":"2022-04-09T23:31:01.124657Z","shell.execute_reply":"2022-04-09T23:31:01.158708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Load an image with Kornia\n\n\"The utility is kornia.image_to_tensor which casts a numpy.ndarray to a torch.Tensor and permutes the channels to leave the image ready for being used with any other PyTorch or Kornia component. The image is casted into a 4D torch.Tensor with zero-copy.\"\n\nhttps://kornia-tutorials.readthedocs.io/en/latest/hello_world_tutorial.html","metadata":{}},{"cell_type":"code","source":"#Code by https://kornia-tutorials.readthedocs.io/en/latest/hello_world_tutorial.html\n\nx_bgr: torch.tensor = K.image_to_tensor(img_bgr)  # CxHxW / torch.uint8\nx_bgr = x_bgr.unsqueeze(0)  # 1xCxHxW\nprint(f\"convert from '{img_bgr.shape}' to '{x_bgr.shape}'\")","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:31:48.607782Z","iopub.execute_input":"2022-04-09T23:31:48.608089Z","iopub.status.idle":"2022-04-09T23:31:48.616685Z","shell.execute_reply.started":"2022-04-09T23:31:48.608053Z","shell.execute_reply":"2022-04-09T23:31:48.615599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Convert from BGR to RGB with a kornia.color component.","metadata":{}},{"cell_type":"code","source":"x_rgb: torch.tensor = K.color.bgr_to_rgb(x_bgr)  # 1xCxHxW / torch.uint8","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:32:32.523154Z","iopub.execute_input":"2022-04-09T23:32:32.523471Z","iopub.status.idle":"2022-04-09T23:32:32.531534Z","shell.execute_reply.started":"2022-04-09T23:32:32.523434Z","shell.execute_reply":"2022-04-09T23:32:32.530812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Visualize an image with Matplotib","metadata":{}},{"cell_type":"code","source":"img_bgr: np.array = K.tensor_to_image(x_bgr)\nimg_rgb: np.array = K.tensor_to_image(x_rgb)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:33:04.940963Z","iopub.execute_input":"2022-04-09T23:33:04.941775Z","iopub.status.idle":"2022-04-09T23:33:04.949330Z","shell.execute_reply.started":"2022-04-09T23:33:04.941732Z","shell.execute_reply":"2022-04-09T23:33:04.948374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by https://kornia-tutorials.readthedocs.io/en/latest/hello_world_tutorial.html\n\nfig, axs = plt.subplots(1, 2, figsize=(32, 16))\naxs = axs.ravel()\n\naxs[0].axis('off')\naxs[0].imshow(img_rgb)\n\naxs[1].axis('off')\naxs[1].imshow(img_bgr)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:33:40.047680Z","iopub.execute_input":"2022-04-09T23:33:40.048233Z","iopub.status.idle":"2022-04-09T23:33:41.241984Z","shell.execute_reply.started":"2022-04-09T23:33:40.048191Z","shell.execute_reply":"2022-04-09T23:33:41.241295Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by https://kornia-tutorials.readthedocs.io/en/latest/data_augmentation_sequential.html\n\nfrom kornia import augmentation as K\nfrom kornia.augmentation import AugmentationSequential\nfrom kornia.geometry import bbox_to_mask\nfrom kornia.utils import image_to_tensor, tensor_to_image\nfrom torchvision.transforms import transforms\n\nto_tensor = transforms.ToTensor()\nto_pil = transforms.ToPILImage()\n\ndef plot_resulting_image(img, bbox, keypoints, mask):\n    img = img * mask\n    img_draw = cv2.polylines(np.array(to_pil(img)), bbox.numpy(), isClosed=True, color=(255, 0, 0))\n    for k in keypoints[0]:\n        img_draw = cv2.circle(img_draw, tuple(k.numpy()[:2]), radius=6, color=(255, 0, 0), thickness=-1)\n    return img_draw\n\nimg = cv2.imread(\"../input/snakeclef2022/SnakeCLEF2022-medium_size/SnakeCLEF2022-medium_size/2004/Python_molurus/1610154.jpg\", cv2.IMREAD_COLOR)\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nh, w = img.shape[:2]\n\nimg_tensor = image_to_tensor(img).float() / 255.\nplt.imshow(img); plt.axis('off');","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:35:16.228924Z","iopub.execute_input":"2022-04-09T23:35:16.229680Z","iopub.status.idle":"2022-04-09T23:35:16.457015Z","shell.execute_reply.started":"2022-04-09T23:35:16.229641Z","shell.execute_reply":"2022-04-09T23:35:16.456028Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Define Augmentation Sequential and Different Labels","metadata":{}},{"cell_type":"code","source":"#Code by https://kornia-tutorials.readthedocs.io/en/latest/data_augmentation_sequential.html\n\naug_list = AugmentationSequential(\n    K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0),\n    K.RandomAffine(360, [0.1, 0.1], [0.7, 1.2], [30., 50.], p=1.0),\n    K.RandomPerspective(0.5, p=1.0),\n    data_keys=[\"input\", \"bbox\", \"keypoints\", \"mask\"],\n    return_transform=False,\n    same_on_batch=False,\n)\n\nbbox = torch.tensor([[[355,10],[660,10],[660,250],[355,250]]])\nkeypoints = torch.tensor([[[465, 115], [545, 116]]])\nmask = bbox_to_mask(torch.tensor([[[155,0],[500,0],[500,300],[155,300]]]), w, h).float()##I had to reduce. Original was [155,0],[900,0],[900,400],[155,400]]\n\nimg_out = plot_resulting_image(img_tensor, bbox, keypoints, mask)\nplt.imshow(img_out); plt.axis('off');","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:39:32.727741Z","iopub.execute_input":"2022-04-09T23:39:32.728232Z","iopub.status.idle":"2022-04-09T23:39:32.921453Z","shell.execute_reply.started":"2022-04-09T23:39:32.728180Z","shell.execute_reply":"2022-04-09T23:39:32.920523Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Forward Computations","metadata":{}},{"cell_type":"code","source":"#Code by https://kornia-tutorials.readthedocs.io/en/latest/data_augmentation_sequential.html\n\nout_tensor = aug_list(img_tensor, bbox.float(), keypoints.float(), mask)\nimg_out = plot_resulting_image(\n    out_tensor[0][0],\n    out_tensor[1].int(),\n    out_tensor[2].int(),\n    out_tensor[3][0],\n)\nplt.imshow(img_out); plt.axis('off');","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:41:55.315634Z","iopub.execute_input":"2022-04-09T23:41:55.315996Z","iopub.status.idle":"2022-04-09T23:41:55.659822Z","shell.execute_reply.started":"2022-04-09T23:41:55.315958Z","shell.execute_reply":"2022-04-09T23:41:55.658920Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Inverse Transformations","metadata":{}},{"cell_type":"code","source":"out_tensor_inv = aug_list.inverse(*out_tensor)\nimg_out = plot_resulting_image(\n    out_tensor_inv[0][0],\n    out_tensor_inv[1].int(),\n    out_tensor_inv[2].int(),\n    out_tensor_inv[3][0],\n)\nplt.imshow(img_out); plt.axis('off');","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:42:47.552298Z","iopub.execute_input":"2022-04-09T23:42:47.552601Z","iopub.status.idle":"2022-04-09T23:42:47.780761Z","shell.execute_reply.started":"2022-04-09T23:42:47.552571Z","shell.execute_reply":"2022-04-09T23:42:47.779639Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by https://kornia-tutorials.readthedocs.io/en/latest/data_patch_sequential.html\n\nfrom kornia.augmentation import PatchSequential, ImageSequential\n\npseq = PatchSequential(\n    ImageSequential(\n        K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.5),\n        K.RandomPerspective(0.2, p=0.5),\n        K.RandomSolarize(0.1, 0.1, p=0.5),\n    ),\n    K.RandomAffine(15, [0.1, 0.1], [0.7, 1.2], [0., 20.], p=0.5),\n    K.RandomPerspective(0.2, p=0.5),\n    ImageSequential(\n        K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.5),\n        K.RandomPerspective(0.2, p=0.5),\n        K.RandomSolarize(0.1, 0.1, p=0.5),\n    ),\n    K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.5),\n    K.RandomAffine(15, [0.1, 0.1], [0.7, 1.2], [0., 20.], p=0.5),\n    K.RandomPerspective(0.2, p=0.5),\n    K.RandomSolarize(0.1, 0.1, p=0.5),\n    K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.5),\n    K.RandomAffine(15, [0.1, 0.1], [0.7, 1.2], [0., 20.], p=0.5),\n    ImageSequential(\n        K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.5),\n        K.RandomPerspective(0.2, p=0.5),\n        K.RandomSolarize(0.1, 0.1, p=0.5),\n    ),\n    K.RandomSolarize(0.1, 0.1, p=0.5),\n    K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.5),\n    K.RandomAffine(15, [0.1, 0.1], [0.7, 1.2], [0., 20.], p=0.5),\n    K.RandomPerspective(0.2, p=0.5),\n    K.RandomSolarize(0.1, 0.1, p=0.5),\n    patchwise_apply=True,\n    same_on_batch=True,\n)\nout_tensor = pseq(img_tensor[None].repeat(2, 1, 1, 1))\nto_pil(torch.cat([out_tensor[0], out_tensor[1]], dim=2))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:43:45.724997Z","iopub.execute_input":"2022-04-09T23:43:45.725276Z","iopub.status.idle":"2022-04-09T23:43:46.063923Z","shell.execute_reply.started":"2022-04-09T23:43:45.725250Z","shell.execute_reply":"2022-04-09T23:43:46.063022Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Patch Augmentation Sequential rocks!\n\nPatch Augmentation Sequential with patchwise_apply=False\n\nIf patchwise_apply=False, all the args will be combined and applied as one pipeline for each patch.\n\nhttps://kornia-tutorials.readthedocs.io/en/latest/data_patch_sequential.html","metadata":{}},{"cell_type":"code","source":"#Code by https://kornia-tutorials.readthedocs.io/en/latest/data_patch_sequential.html\n\npseq = PatchSequential(\n    K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.75),\n    K.RandomElasticTransform(alpha=(4., 4.)),\n    patchwise_apply=False,\n    same_on_batch=False\n)\nout_tensor = pseq(img_tensor[None].repeat(2, 1, 1, 1))\nto_pil(torch.cat([out_tensor[0], out_tensor[1]], dim=2))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:44:21.987491Z","iopub.execute_input":"2022-04-09T23:44:21.988191Z","iopub.status.idle":"2022-04-09T23:44:22.890569Z","shell.execute_reply.started":"2022-04-09T23:44:21.988139Z","shell.execute_reply":"2022-04-09T23:44:22.889688Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install kornia_moons","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:45:26.351335Z","iopub.execute_input":"2022-04-09T23:45:26.351635Z","iopub.status.idle":"2022-04-09T23:45:35.298159Z","shell.execute_reply.started":"2022-04-09T23:45:26.351603Z","shell.execute_reply":"2022-04-09T23:45:35.296778Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Aziz Amindzhanov  https://www.kaggle.com/code/azizdzhon/kornia-moons-imc-2022\n\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport cv2\nimport torch\nimport kornia as K\nfrom typing import List\nimport matplotlib.pyplot as plt\n\nfrom kornia_moons.feature import *","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:45:58.903160Z","iopub.execute_input":"2022-04-09T23:45:58.903467Z","iopub.status.idle":"2022-04-09T23:45:58.916680Z","shell.execute_reply.started":"2022-04-09T23:45:58.903433Z","shell.execute_reply":"2022-04-09T23:45:58.915906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Aziz Amindzhanov  https://www.kaggle.com/code/azizdzhon/kornia-moons-imc-2022\n\nimg = cv2.cvtColor(cv2.imread('../input/snakeclef2022/SnakeCLEF2022-medium_size/SnakeCLEF2022-medium_size/2004/Python_molurus/1610154.jpg'), cv2.COLOR_BGR2RGB)\n\ndet = cv2.ORB_create(500)\nkps, descs = det.detectAndCompute(img, None)\n\nout_img = cv2.drawKeypoints(img, kps, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)\nplt.imshow(out_img)\n\n\nlafs = laf_from_opencv_ORB_kpts(kps)\nvisualize_LAF(K.image_to_tensor(img, False), lafs, 0)\n\nkps_back = opencv_ORB_kpts_from_laf(lafs)\nout_img2 = cv2.drawKeypoints(img, kps_back, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)\nplt.imshow(out_img2)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T23:48:10.816563Z","iopub.execute_input":"2022-04-09T23:48:10.816873Z","iopub.status.idle":"2022-04-09T23:48:12.056248Z","shell.execute_reply.started":"2022-04-09T23:48:10.816818Z","shell.execute_reply":"2022-04-09T23:48:12.055020Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgements:\n\nKornia AI is on the mission to leverage and democratize the next generation of Computer Vision tools and Deep Learning libraries within the context of an Open Source community.\n\nhttps://kornia.readthedocs.io/en/latest/\n\nAziz Amindzhanov https://www.kaggle.com/code/azizdzhon/kornia-moons-imc-2022","metadata":{}}]}