{"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://theaisummer.com/static/65961ba55109646b3aed515c7dba67cb/ee604/jax-tensorflow-pytorch.png)theaisummer.com","metadata":{}},{"cell_type":"code","source":"#Uncomment and Run when only accelerator is TPU\n#%%capture\n#!conda install -y -c conda-forge jax jaxlib flax optax\n#!conda install -y importlib-metadata","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Imports","metadata":{}},{"cell_type":"code","source":"# Importing all the libraries necessary for the project\nimport os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport cv2\nimport time\nimport random","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:52:17.821290Z","iopub.execute_input":"2022-04-09T22:52:17.821604Z","iopub.status.idle":"2022-04-09T22:52:17.869871Z","shell.execute_reply.started":"2022-04-09T22:52:17.821572Z","shell.execute_reply":"2022-04-09T22:52:17.868919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from random import randint\nimport time\nimport torch\nfrom torch.utils.data import Dataset, random_split, DataLoader\nimport torch.nn.functional as F\nimport torch.nn as nn\nfrom PIL import Image\nfrom scipy import ndimage\nimport torchvision\nfrom tqdm.notebook import tqdm\nimport tensorflow as tf\nfrom torchvision import transforms\nfrom tqdm import tqdm\nfrom flax.training import train_state\nfrom typing import Any\nimport jax.numpy as jnp\nimport jax.random\nimport functools\nimport optax\nimport flax.linen as nn\nimport jax.nn\n# to suppress warnings caused by cuda version\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:52:23.823875Z","iopub.execute_input":"2022-04-09T22:52:23.824613Z","iopub.status.idle":"2022-04-09T22:52:23.833194Z","shell.execute_reply.started":"2022-04-09T22:52:23.824566Z","shell.execute_reply":"2022-04-09T22:52:23.832141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#TPU detection and configuration\n\n\"Kaggle Team recommend using GPUs when working with JAX on Kaggle. These notebooks are compatible with the v3-8 TPUs that are provided for free in Kaggle Notebooks, but JAX was optimized for the newly updated TPU VM architecture which is not yet available on Kaggle.\"\n\nhttps://www.kaggle.com/code/nilaychauhan/dog-breed-classification-using-jax-and-flax/notebook","metadata":{}},{"cell_type":"code","source":"if 'TPU_NAME' in os.environ:\n    import requests\n    if 'TPU_DRIVER_MODE' not in globals():\n        url = 'http:' + os.environ['TPU_NAME'].split(':')[1] + ':8475/requestversion/tpu_driver_nightly'\n        resp = requests.post(url)\n        TPU_DRIVER_MODE = 1\n    from jax.config import config\n    config.FLAGS.jax_xla_backend = \"tpu_driver\"\n    config.FLAGS.jax_backend_target = os.environ['TPU_NAME']\n    print('Registered TPU:', config.FLAGS.jax_backend_target)\nelse:\n    print('No TPU detected.')","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:52:33.470793Z","iopub.execute_input":"2022-04-09T22:52:33.471567Z","iopub.status.idle":"2022-04-09T22:52:33.480776Z","shell.execute_reply.started":"2022-04-09T22:52:33.471530Z","shell.execute_reply":"2022-04-09T22:52:33.479864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shows the list of the available devices\njax.local_devices()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:52:39.179759Z","iopub.execute_input":"2022-04-09T22:52:39.180232Z","iopub.status.idle":"2022-04-09T22:52:39.187237Z","shell.execute_reply.started":"2022-04-09T22:52:39.180178Z","shell.execute_reply":"2022-04-09T22:52:39.186217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = '../input/snakeclef2022/'\nTRAIN_DIR = DATA_DIR + 'SnakeCLEF2022-small_size/SnakeCLEF2022-small_size/2013'                             \nTRAIN_CSV = DATA_DIR + '/SnakeCLEF2022-TrainMetadata.csv'     \ndata_df = pd.read_csv(TRAIN_CSV)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:52:43.336056Z","iopub.execute_input":"2022-04-09T22:52:43.336517Z","iopub.status.idle":"2022-04-09T22:52:43.828113Z","shell.execute_reply.started":"2022-04-09T22:52:43.336477Z","shell.execute_reply":"2022-04-09T22:52:43.827488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_names=data_df[\"binomial_name\"].unique()\nlabels_sorted=labels_names.sort()\nlabels = dict(zip(range(len(labels_names)),labels_names))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:52:48.253366Z","iopub.execute_input":"2022-04-09T22:52:48.253702Z","iopub.status.idle":"2022-04-09T22:52:48.287738Z","shell.execute_reply.started":"2022-04-09T22:52:48.253661Z","shell.execute_reply":"2022-04-09T22:52:48.286694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lbl=[]\npath_img=[]\n\nfor i in range(len(data_df[\"binomial_name\"])):\n    temp1=list(labels.values()).index(data_df.binomial_name[i])\n    lbl.append(temp1)\n    temp2=TRAIN_DIR + \"/\" + str(data_df.binomial_name[i]) +\"/\"+ \".jpeg\"\n    path_img.append(temp2)\n\ndata_df['file_path'] =path_img  \ndata_df['lbl'] = lbl  #lbl is the same as class_id\n\ndata_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:52:53.644827Z","iopub.execute_input":"2022-04-09T22:52:53.645421Z","iopub.status.idle":"2022-04-09T22:53:07.582001Z","shell.execute_reply.started":"2022-04-09T22:52:53.645381Z","shell.execute_reply":"2022-04-09T22:53:07.581073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#From here, I can see that my file paths won't work. With jpg or jpeg All went wrong.","metadata":{}},{"cell_type":"code","source":"data_df.iloc[2,6]","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:53:12.695234Z","iopub.execute_input":"2022-04-09T22:53:12.695601Z","iopub.status.idle":"2022-04-09T22:53:12.701904Z","shell.execute_reply.started":"2022-04-09T22:53:12.695554Z","shell.execute_reply":"2022-04-09T22:53:12.700830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Creating custom dataset class using Pytorch's Datasets and Dataloaders","metadata":{}},{"cell_type":"code","source":"class SnakeDataset(Dataset):\n    def __init__(self, df, root_dir, transform=None):\n        self.df = df\n        self.transform = transform\n        self.root_dir = root_dir\n        \n    def __len__(self):\n        return len(self.df)    \n    \n    def __getitem__(self, idx):\n        row = self.df.loc[idx]\n        img_id, img_label = row['observation_id'], row['lbl']\n        img_fname = self.root_dir + \"/\" + str(data_df.binomial_name[i]) +\"/\"+ str(img_id) + \".jpeg\"\n        img = Image.open(img_fname)\n        if self.transform:\n            img = self.transform(img)\n        return img, img_label","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:53:18.673831Z","iopub.execute_input":"2022-04-09T22:53:18.674121Z","iopub.status.idle":"2022-04-09T22:53:18.682495Z","shell.execute_reply.started":"2022-04-09T22:53:18.674091Z","shell.execute_reply":"2022-04-09T22:53:18.681172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Apply data augmentations to the dataset using Pytorch's transforms and returning data in the form of numpy arrays","metadata":{}},{"cell_type":"code","source":"IMAGE_HEIGHT = 128\nIMAGE_WIDTH = 128\n\ntraining_transform = transforms.Compose([\n    transforms.RandomAffine(degrees=(-30, 30),\n                            translate=(0.0, 0.2)),\n    transforms.RandomHorizontalFlip(),\n    transforms.Resize((IMAGE_HEIGHT,\n                        IMAGE_WIDTH)),\n    np.array])\n\ntesting_transform = transforms.Compose([\n    transforms.Resize((IMAGE_HEIGHT,\n                       IMAGE_WIDTH)),\n    np.array])\n\n\nnp.random.seed(42)\nmsk = np.random.rand(len(data_df)) < 0.8\n\ntrain_df = data_df[msk].reset_index()\nval_df = data_df[~msk].reset_index()\n\ntrain_ds = SnakeDataset(train_df, TRAIN_DIR, transform=training_transform)\nval_ds = SnakeDataset(val_df, TRAIN_DIR, transform=testing_transform)\nlen(train_ds), len(val_ds)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:53:25.746784Z","iopub.execute_input":"2022-04-09T22:53:25.747383Z","iopub.status.idle":"2022-04-09T22:53:25.818188Z","shell.execute_reply.started":"2022-04-09T22:53:25.747346Z","shell.execute_reply":"2022-04-09T22:53:25.817138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import jax\nNUM_TPUS = jax.device_count()\nBATCH_SIZE = 128\ntrain_dataloader = DataLoader(train_ds,\n                                               batch_size=BATCH_SIZE,\n                                               shuffle=True, drop_last=True,\n                                               num_workers=0)\ntest_dataloader = DataLoader(val_ds,\n                                              batch_size=BATCH_SIZE,\n                                              shuffle=True, drop_last=True,\n                                              num_workers=0)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T22:53:31.395201Z","iopub.execute_input":"2022-04-09T22:53:31.395511Z","iopub.status.idle":"2022-04-09T22:53:31.410277Z","shell.execute_reply.started":"2022-04-09T22:53:31.395472Z","shell.execute_reply":"2022-04-09T22:53:31.409192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(image_batch, label_batch) = next(iter(train_dataloader))\nprint(image_batch.shape)\nprint(label_batch.shape)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-04-09T22:54:22.638829Z","iopub.execute_input":"2022-04-09T22:54:22.639674Z","iopub.status.idle":"2022-04-09T22:54:22.731742Z","shell.execute_reply.started":"2022-04-09T22:54:22.639625Z","shell.execute_reply":"2022-04-09T22:54:22.730452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Having trouble finding a file?   Yes indeed!","metadata":{}},{"cell_type":"code","source":"from PIL import Image\n\nimage = Image.open(\"../input/snakeclef2022/SnakeCLEF2022-small_size/SnakeCLEF2022-small_size/2013/Dipsadoboa aulica/11196072.jpeg\")\nimage","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-04-09T22:54:44.585562Z","iopub.execute_input":"2022-04-09T22:54:44.586140Z","iopub.status.idle":"2022-04-09T22:54:44.623908Z","shell.execute_reply.started":"2022-04-09T22:54:44.586102Z","shell.execute_reply":"2022-04-09T22:54:44.622575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#After more than 2 hours, trying any combination of the image files. I gave up. Not a single Snake. Only Nagini below.","metadata":{}},{"cell_type":"markdown","source":"![](https://i.chzbgr.com/full/5850582528/h0D7303F0/hipster-voldemort)roflrazzi.cheezburger.com","metadata":{}}]}