{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:02:21.09349Z","iopub.execute_input":"2022-05-17T00:02:21.093764Z","iopub.status.idle":"2022-05-17T00:02:21.09859Z","shell.execute_reply.started":"2022-05-17T00:02:21.093735Z","shell.execute_reply":"2022-05-17T00:02:21.09794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image, ImageDraw\nimport collections\nimport glob \nfrom datetime import datetime as dt\nimport gc\nimport json\n\nimport matplotlib.image as mpimg\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:01:55.432998Z","iopub.execute_input":"2022-05-17T00:01:55.433875Z","iopub.status.idle":"2022-05-17T00:01:55.443378Z","shell.execute_reply.started":"2022-05-17T00:01:55.433825Z","shell.execute_reply":"2022-05-17T00:01:55.442749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/fungiclef2022/DF20-train_metadata.csv', delimiter=',')\npd.set_option('display.max_columns', None)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:12:20.535467Z","iopub.execute_input":"2022-05-17T00:12:20.535802Z","iopub.status.idle":"2022-05-17T00:12:22.80239Z","shell.execute_reply.started":"2022-05-17T00:12:20.535768Z","shell.execute_reply":"2022-05-17T00:12:22.801499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:19:52.638694Z","iopub.execute_input":"2022-05-17T00:19:52.639082Z","iopub.status.idle":"2022-05-17T00:19:52.648804Z","shell.execute_reply.started":"2022-05-17T00:19:52.639041Z","shell.execute_reply":"2022-05-17T00:19:52.648226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Habitat\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:02:38.65446Z","iopub.execute_input":"2022-05-17T00:02:38.655203Z","iopub.status.idle":"2022-05-17T00:02:38.707012Z","shell.execute_reply.started":"2022-05-17T00:02:38.655155Z","shell.execute_reply":"2022-05-17T00:02:38.70605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Code by Taha07  https://www.kaggle.com/taha07/data-scientists-jobs-analysis-visualization/notebook\n\nfrom wordcloud import WordCloud\nfrom wordcloud import STOPWORDS\nstopwords = set(STOPWORDS)\nwordcloud = WordCloud(background_color = 'red',\n                      height =2000,\n                      width = 2000\n                     ).generate(str(train[\"Habitat\"]))\nplt.rcParams['figure.figsize'] = (12,12)\nplt.axis(\"off\")\nplt.imshow(wordcloud)\nplt.title(\"Fungi Habitat\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:02:45.816447Z","iopub.execute_input":"2022-05-17T00:02:45.816893Z","iopub.status.idle":"2022-05-17T00:02:48.477182Z","shell.execute_reply.started":"2022-05-17T00:02:45.816856Z","shell.execute_reply":"2022-05-17T00:02:48.476569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Handle Missing Values to plot a Map. ","metadata":{}},{"cell_type":"code","source":"# Lets first handle numerical features with nan value\nnumerical_nan = [feature for feature in train.columns if train[feature].isna().sum()>1 and train[feature].dtypes!='O']\nnumerical_nan","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-17T00:02:52.325093Z","iopub.execute_input":"2022-05-17T00:02:52.325671Z","iopub.status.idle":"2022-05-17T00:02:53.029566Z","shell.execute_reply.started":"2022-05-17T00:02:52.325618Z","shell.execute_reply":"2022-05-17T00:02:53.028699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Code by Taha07  https://www.kaggle.com/taha07/data-scientists-jobs-analysis-visualization/notebook\n\nfrom wordcloud import WordCloud\nfrom wordcloud import STOPWORDS\nstopwords = set(STOPWORDS)\nwordcloud = WordCloud(background_color = 'blue',\n                      height =2000,\n                      width = 2000\n                     ).generate(str(train[\"kingdom\"]))\nplt.rcParams['figure.figsize'] = (12,12)\nplt.axis(\"off\")\nplt.imshow(wordcloud)\nplt.title(\"FungiCLEF Kingdom\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:02:57.07443Z","iopub.execute_input":"2022-05-17T00:02:57.07495Z","iopub.status.idle":"2022-05-17T00:02:58.582756Z","shell.execute_reply.started":"2022-05-17T00:02:57.074897Z","shell.execute_reply":"2022-05-17T00:02:58.582179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[numerical_nan].isna().sum()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-17T00:03:04.74723Z","iopub.execute_input":"2022-05-17T00:03:04.747724Z","iopub.status.idle":"2022-05-17T00:03:04.762021Z","shell.execute_reply.started":"2022-05-17T00:03:04.747669Z","shell.execute_reply":"2022-05-17T00:03:04.76136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Replacing the numerical Missing Values\n\nfor feature in numerical_nan:\n    ## We will replace by using median since there are outliers\n    median_value=train[feature].median()\n    \n    train[feature].fillna(median_value,inplace=True)\n    \ntrain[numerical_nan].isnull().sum()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-17T00:03:10.069619Z","iopub.execute_input":"2022-05-17T00:03:10.071512Z","iopub.status.idle":"2022-05-17T00:03:10.126379Z","shell.execute_reply.started":"2022-05-17T00:03:10.07146Z","shell.execute_reply":"2022-05-17T00:03:10.125341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#I don't know why infraspecificEpithet has still Missing Values.","metadata":{}},{"cell_type":"code","source":"#Code by Parul Pandey  https://www.kaggle.com/parulpandey/a-guide-to-handling-missing-values-in-python\n\n# imputing with a constant\n\nfrom sklearn.impute import SimpleImputer\ntrain_constant = train.copy()\n#setting strategy to 'constant' \nmean_imputer = SimpleImputer(strategy='constant') # imputing using constant value\ntrain_constant.iloc[:,:] = mean_imputer.fit_transform(train_constant)\ntrain_constant.isnull().sum()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-17T00:03:15.801052Z","iopub.execute_input":"2022-05-17T00:03:15.80133Z","iopub.status.idle":"2022-05-17T00:03:20.368349Z","shell.execute_reply.started":"2022-05-17T00:03:15.801289Z","shell.execute_reply":"2022-05-17T00:03:20.367529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# categorical features with missing values\ncategorical_nan = [feature for feature in train.columns if train[feature].isna().sum()>0 and train[feature].dtypes=='O']\nprint(categorical_nan)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-17T00:03:24.590247Z","iopub.execute_input":"2022-05-17T00:03:24.590547Z","iopub.status.idle":"2022-05-17T00:03:25.293076Z","shell.execute_reply.started":"2022-05-17T00:03:24.590518Z","shell.execute_reply":"2022-05-17T00:03:25.292193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# replacing missing values in categorical features\nfor feature in categorical_nan:\n    train[feature] = train[feature].fillna('None')","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:03:32.052536Z","iopub.execute_input":"2022-05-17T00:03:32.052857Z","iopub.status.idle":"2022-05-17T00:03:32.523222Z","shell.execute_reply.started":"2022-05-17T00:03:32.052826Z","shell.execute_reply":"2022-05-17T00:03:32.522364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[categorical_nan].isna().sum()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-17T00:03:37.882856Z","iopub.execute_input":"2022-05-17T00:03:37.883136Z","iopub.status.idle":"2022-05-17T00:03:38.310358Z","shell.execute_reply.started":"2022-05-17T00:03:37.883102Z","shell.execute_reply":"2022-05-17T00:03:38.309633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\nimport folium\n\n#Code by Varshini PJ  user: varshinipj\n\nfig = px.scatter_mapbox(train,\n# Here, plotly gets, (x,y) coordinates\nlat=\"Latitude\",\nlon=\"Longitude\",\ntext='level0Name',\n\n                #Here, plotly detects color of series\n                size=\"class_id\",\n                color = \"phylum\",\n                labels=\"level0Name\",\n\n                zoom=14.5,\n                center={\"lat\":56.975158, \"lon\":9.285525},\n                height=600,\n                width=800)\nfig.update_layout(mapbox_style='stamen-toner')\nfig.update_layout(margin={\"r\": 0, \"t\": 0, \"l\": 0, \"b\": 0})\nfig.update_layout(title_text=\"FungiCLEF 2022\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:23:04.831107Z","iopub.execute_input":"2022-05-17T00:23:04.831804Z","iopub.status.idle":"2022-05-17T00:23:07.046439Z","shell.execute_reply.started":"2022-05-17T00:23:04.831754Z","shell.execute_reply":"2022-05-17T00:23:07.045637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/fungiclef2022/FungiCLEF2022_test_metadata.csv', delimiter=',')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:04:16.532928Z","iopub.execute_input":"2022-05-17T00:04:16.533234Z","iopub.status.idle":"2022-05-17T00:04:16.890795Z","shell.execute_reply.started":"2022-05-17T00:04:16.533192Z","shell.execute_reply":"2022-05-17T00:04:16.8899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"Substrate\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:04:21.660666Z","iopub.execute_input":"2022-05-17T00:04:21.661116Z","iopub.status.idle":"2022-05-17T00:04:21.686339Z","shell.execute_reply.started":"2022-05-17T00:04:21.661076Z","shell.execute_reply":"2022-05-17T00:04:21.685659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Code by Taha07  https://www.kaggle.com/taha07/data-scientists-jobs-analysis-visualization/notebook\n\nfrom wordcloud import WordCloud\nfrom wordcloud import STOPWORDS\nstopwords = set(STOPWORDS)\nwordcloud = WordCloud(background_color = 'green',\n                      height =2000,\n                      width = 2000\n                     ).generate(str(test[\"Substrate\"]))\nplt.rcParams['figure.figsize'] = (12,12)\nplt.axis(\"off\")\nplt.imshow(wordcloud)\nplt.title(\"Fungi substrate\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:04:26.412408Z","iopub.execute_input":"2022-05-17T00:04:26.412956Z","iopub.status.idle":"2022-05-17T00:04:28.166493Z","shell.execute_reply.started":"2022-05-17T00:04:26.412905Z","shell.execute_reply":"2022-05-17T00:04:28.165606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val = pd.read_csv('/kaggle/input/fungiclef2022/DF20-val_metadata.csv', delimiter=',')\nval.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:04:34.02684Z","iopub.execute_input":"2022-05-17T00:04:34.027408Z","iopub.status.idle":"2022-05-17T00:04:34.310684Z","shell.execute_reply.started":"2022-05-17T00:04:34.027372Z","shell.execute_reply":"2022-05-17T00:04:34.309861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install kornia","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-17T00:04:39.198022Z","iopub.execute_input":"2022-05-17T00:04:39.1983Z","iopub.status.idle":"2022-05-17T00:04:49.778647Z","shell.execute_reply.started":"2022-05-17T00:04:39.19827Z","shell.execute_reply":"2022-05-17T00:04:49.777814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\nimport torch\nimport torchvision\nimport kornia as K","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:04:51.708255Z","iopub.execute_input":"2022-05-17T00:04:51.708571Z","iopub.status.idle":"2022-05-17T00:04:51.713227Z","shell.execute_reply.started":"2022-05-17T00:04:51.70854Z","shell.execute_reply":"2022-05-17T00:04:51.7126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_bgr: np.array = cv2.imread('../input/fungiclef2022/DF21-images-300/DF21_300/1-3343234404.JPG')  # HxWxC / np.uint8\nimg_rgb: np.array = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)\n\nplt.imshow(img_rgb); plt.axis('off');","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:05:01.980126Z","iopub.execute_input":"2022-05-17T00:05:01.981014Z","iopub.status.idle":"2022-05-17T00:05:02.670281Z","shell.execute_reply.started":"2022-05-17T00:05:01.980973Z","shell.execute_reply":"2022-05-17T00:05:02.669382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_bgr: np.array = cv2.imread('../input/fungiclef2022/DF21-images-300/DF21_300/0-3008822344.JPG')  # HxWxC / np.uint8\nimg_rgb: np.array = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)\n\nplt.imshow(img_rgb); plt.axis('off');","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:05:15.187055Z","iopub.execute_input":"2022-05-17T00:05:15.189505Z","iopub.status.idle":"2022-05-17T00:05:15.635196Z","shell.execute_reply.started":"2022-05-17T00:05:15.189465Z","shell.execute_reply":"2022-05-17T00:05:15.634344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_bgr: np.array = cv2.imread('../input/fungiclef2022/DF21-images-300/DF21_300/0-3008822345.JPG')  # HxWxC / np.uint8\nimg_rgb: np.array = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)\n\nplt.imshow(img_rgb); plt.axis('off');","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:05:22.20031Z","iopub.execute_input":"2022-05-17T00:05:22.200644Z","iopub.status.idle":"2022-05-17T00:05:22.626669Z","shell.execute_reply.started":"2022-05-17T00:05:22.200613Z","shell.execute_reply":"2022-05-17T00:05:22.625462Z"},"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/fungiclef2022/DF21-images-300/DF21_300/0-3008822387.JPG')  # CxHxW / torch.uint8\nx_rgb = x_rgb.unsqueeze(0)  # BxCxHxW\nprint(x_rgb.shape);","metadata":{"execution":{"iopub.status.busy":"2022-05-17T00:05:29.023154Z","iopub.execute_input":"2022-05-17T00:05:29.023976Z","iopub.status.idle":"2022-05-17T00:05:29.037888Z","shell.execute_reply.started":"2022-05-17T00:05:29.023932Z","shell.execute_reply":"2022-05-17T00:05:29.037045Z"},"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-05-17T00:05:34.495301Z","iopub.execute_input":"2022-05-17T00:05:34.495626Z","iopub.status.idle":"2022-05-17T00:05:34.500465Z","shell.execute_reply.started":"2022-05-17T00:05:34.495592Z","shell.execute_reply":"2022-05-17T00:05:34.49984Z"},"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-05-17T00:05:39.122921Z","iopub.execute_input":"2022-05-17T00:05:39.123483Z","iopub.status.idle":"2022-05-17T00:05:39.129076Z","shell.execute_reply.started":"2022-05-17T00:05:39.123446Z","shell.execute_reply":"2022-05-17T00:05:39.128287Z"},"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-05-17T00:05:44.009862Z","iopub.execute_input":"2022-05-17T00:05:44.010418Z","iopub.status.idle":"2022-05-17T00:05:44.015126Z","shell.execute_reply.started":"2022-05-17T00:05:44.010383Z","shell.execute_reply":"2022-05-17T00:05:44.014369Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:05:48.273427Z","iopub.execute_input":"2022-05-17T00:05:48.273711Z","iopub.status.idle":"2022-05-17T00:05:49.278179Z","shell.execute_reply.started":"2022-05-17T00:05:48.273682Z","shell.execute_reply":"2022-05-17T00:05:49.277568Z"},"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\n#from kornia.geometry import bbox_to_mask  ##Deprecated\nfrom kornia.geometry.bbox 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/fungiclef2022/DF20-300px/DF20_300/2237851965-148421.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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:05:55.012121Z","iopub.execute_input":"2022-05-17T00:05:55.012861Z","iopub.status.idle":"2022-05-17T00:05:55.368163Z","shell.execute_reply.started":"2022-05-17T00:05:55.012822Z","shell.execute_reply":"2022-05-17T00:05:55.367378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Since I got: IndexError: index 302 is out of bounds for dimension 1 with size 302\n\nI changed evething to 200","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([[[200,10],[230,10],[230,250],[200,250]]])\nkeypoints = torch.tensor([[[200, 115], [200, 116]]])\nmask = bbox_to_mask(torch.tensor([[[155,0],[200,0],[200,200],[155,200]]]), 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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:06:10.234251Z","iopub.execute_input":"2022-05-17T00:06:10.235043Z","iopub.status.idle":"2022-05-17T00:06:10.398233Z","shell.execute_reply.started":"2022-05-17T00:06:10.235003Z","shell.execute_reply":"2022-05-17T00:06:10.39722Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:06:15.608566Z","iopub.execute_input":"2022-05-17T00:06:15.608893Z","iopub.status.idle":"2022-05-17T00:06:15.769637Z","shell.execute_reply.started":"2022-05-17T00:06:15.608858Z","shell.execute_reply":"2022-05-17T00:06:15.768822Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:06:21.495414Z","iopub.execute_input":"2022-05-17T00:06:21.496274Z","iopub.status.idle":"2022-05-17T00:06:21.729031Z","shell.execute_reply.started":"2022-05-17T00:06:21.496215Z","shell.execute_reply":"2022-05-17T00:06:21.728095Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:06:27.370144Z","iopub.execute_input":"2022-05-17T00:06:27.370461Z","iopub.status.idle":"2022-05-17T00:06:27.473067Z","shell.execute_reply.started":"2022-05-17T00:06:27.370432Z","shell.execute_reply":"2022-05-17T00:06:27.47224Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:06:34.273641Z","iopub.execute_input":"2022-05-17T00:06:34.273964Z","iopub.status.idle":"2022-05-17T00:06:34.447562Z","shell.execute_reply.started":"2022-05-17T00:06:34.273929Z","shell.execute_reply":"2022-05-17T00:06:34.446681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install kornia_moons","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-17T00:06:39.532288Z","iopub.execute_input":"2022-05-17T00:06:39.532591Z","iopub.status.idle":"2022-05-17T00:06:50.025037Z","shell.execute_reply.started":"2022-05-17T00:06:39.53256Z","shell.execute_reply":"2022-05-17T00:06:50.024022Z"},"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-05-17T00:06:52.828795Z","iopub.execute_input":"2022-05-17T00:06:52.829135Z","iopub.status.idle":"2022-05-17T00:06:52.837649Z","shell.execute_reply.started":"2022-05-17T00:06:52.829097Z","shell.execute_reply":"2022-05-17T00:06:52.836776Z"},"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/fungiclef2022/DF20-300px/DF20_300/2237851963-3.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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-17T00:06:58.064845Z","iopub.execute_input":"2022-05-17T00:06:58.065138Z","iopub.status.idle":"2022-05-17T00:06:59.136196Z","shell.execute_reply.started":"2022-05-17T00:06:58.065108Z","shell.execute_reply":"2022-05-17T00:06:59.135373Z"},"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":{}}]}