{"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":"markdown","source":"# Inference for 🌿Herbarium with Lightning⚡Flash\n\n\n**This is just inference version fo the original work: https://www.kaggle.com/jirkaborovec/herbarium-eda-baseline-flash-efficientnet**\n\nSee our story: [Best Practices to Rank on Kaggle Competition with PyTorch Lightning and Grid.ai Spot Instances](https://devblog.pytorchlightning.ai/best-practices-to-rank-on-kaggle-competition-with-pytorch-lightning-and-grid-ai-spot-instances-54aa5248aa8e)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-15T09:52:20.579025Z","iopub.execute_input":"2022-02-15T09:52:20.579391Z","iopub.status.idle":"2022-02-15T09:52:20.583893Z","shell.execute_reply.started":"2022-02-15T09:52:20.579358Z","shell.execute_reply":"2022-02-15T09:52:20.583227Z"}}},{"cell_type":"code","source":"! ls -l /kaggle/input/\n\nimage_size = (512, 512)\nnormalize = True","metadata":{"execution":{"iopub.status.busy":"2022-05-12T10:56:49.558413Z","iopub.execute_input":"2022-05-12T10:56:49.559076Z","iopub.status.idle":"2022-05-12T10:56:50.242593Z","shell.execute_reply.started":"2022-05-12T10:56:49.558952Z","shell.execute_reply":"2022-05-12T10:56:50.241722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Browse test images ","metadata":{}},{"cell_type":"code","source":"import os\nimport json\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nPATH_DATASET = \"/kaggle/input/herbarium-2022-fgvc9\"","metadata":{"execution":{"iopub.status.busy":"2022-05-12T10:56:50.247658Z","iopub.execute_input":"2022-05-12T10:56:50.247876Z","iopub.status.idle":"2022-05-12T10:56:50.252197Z","shell.execute_reply.started":"2022-05-12T10:56:50.24784Z","shell.execute_reply":"2022-05-12T10:56:50.251148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nwith open(os.path.join(PATH_DATASET, \"test_metadata.json\")) as fp:\n    test_data = json.load(fp)\n\nprint(len(test_data))\ndf_test = pd.DataFrame(test_data).set_index(\"image_id\")\ndisplay(df_test.head())","metadata":{"execution":{"iopub.status.busy":"2022-05-12T10:56:50.253668Z","iopub.execute_input":"2022-05-12T10:56:50.253994Z","iopub.status.idle":"2022-05-12T10:56:51.039984Z","shell.execute_reply.started":"2022-05-12T10:56:50.253956Z","shell.execute_reply":"2022-05-12T10:56:51.038784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference with Lightning⚡Flash\n","metadata":{}},{"cell_type":"code","source":"!pip install -q 'lightning-flash[image]' --find-links /kaggle/input/herbarium-eda-baseline-flash-efficientnet/frozen_packages/ --no-index\n!pip install -q timm -U --find-links /kaggle/input/herbarium-submissions/packages/ --no-index\n!pip uninstall -y wandb","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-12T10:56:51.041943Z","iopub.execute_input":"2022-05-12T10:56:51.042215Z","iopub.status.idle":"2022-05-12T10:57:25.15549Z","shell.execute_reply.started":"2022-05-12T10:56:51.042179Z","shell.execute_reply":"2022-05-12T10:57:25.154648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport flash\nfrom flash.image import ImageClassificationData, ImageClassifier","metadata":{"execution":{"iopub.status.busy":"2022-05-12T10:57:25.157062Z","iopub.execute_input":"2022-05-12T10:57:25.157621Z","iopub.status.idle":"2022-05-12T10:57:37.130724Z","shell.execute_reply.started":"2022-05-12T10:57:25.157576Z","shell.execute_reply":"2022-05-12T10:57:37.130035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1. Load the task ⚙️","metadata":{}},{"cell_type":"code","source":"ls /kaggle/input/herbariumflash/cont-v4.ckpt","metadata":{"execution":{"iopub.status.busy":"2022-05-12T10:57:37.132058Z","iopub.execute_input":"2022-05-12T10:57:37.132288Z","iopub.status.idle":"2022-05-12T10:57:37.805624Z","shell.execute_reply.started":"2022-05-12T10:57:37.132255Z","shell.execute_reply":"2022-05-12T10:57:37.804802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ImageClassifier.load_from_checkpoint(\n    \"/kaggle/input/herbariumflash/cont-v4.ckpt\"\n).eval()\n\ndel model.train_metrics, model.val_metrics, model.test_metrics\nmodel = model.cuda()","metadata":{"execution":{"iopub.status.busy":"2022-05-12T11:02:20.46623Z","iopub.execute_input":"2022-05-12T11:02:20.466744Z","iopub.status.idle":"2022-05-12T11:02:41.540616Z","shell.execute_reply.started":"2022-05-12T11:02:20.466704Z","shell.execute_reply":"2022-05-12T11:02:41.539791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Trainer Args\nGPUS = int(torch.cuda.is_available())  # Set to 1 if GPU is enabled for notebook\ntrainer = flash.Trainer(gpus=GPUS)","metadata":{"_kg_hide-output":false,"execution":{"iopub.status.busy":"2022-05-11T16:00:11.573889Z","iopub.status.idle":"2022-05-11T16:00:11.574945Z","shell.execute_reply.started":"2022-05-11T16:00:11.574462Z","shell.execute_reply":"2022-05-11T16:00:11.574496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. Run predictions 🎉","metadata":{}},{"cell_type":"code","source":"from dataclasses import dataclass\nfrom torchvision import transforms as T\nfrom typing import Tuple, Callable\nfrom flash.core.data.io.input_transform import InputTransform\n\n\n@dataclass\nclass ImageClassificationInputTransform(InputTransform):\n\n    image_size: Tuple[int, int] = image_size\n    image_color_mean: Tuple[float, float] = (0.781, 0.759, 0.710)\n    image_color_std: Tuple[float, float] = (0.241, 0.245, 0.249)\n\n    def input_per_sample_transform(self):\n        tfsm = [\n            T.Resize(self.image_size),\n            T.ToTensor(),\n        ]\n\n        if normalize:\n            tfsm.append(T.Normalize(self.image_color_mean, self.image_color_std))\n\n        return T.Compose(tfsm)\n\n\n    def target_per_sample_transform(self) -> Callable:\n        return torch.as_tensor","metadata":{"execution":{"iopub.status.busy":"2022-05-11T16:00:11.576724Z","iopub.status.idle":"2022-05-11T16:00:11.577903Z","shell.execute_reply.started":"2022-05-11T16:00:11.577491Z","shell.execute_reply":"2022-05-11T16:00:11.577524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-11T16:00:11.580073Z","iopub.status.idle":"2022-05-11T16:00:11.581455Z","shell.execute_reply.started":"2022-05-11T16:00:11.581112Z","shell.execute_reply":"2022-05-11T16:00:11.581149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(df_test))\n\ndatamodule = ImageClassificationData.from_data_frame(\n    input_field=\"file_name\",\n    predict_data_frame=df_test,\n    predict_images_root=os.path.join(PATH_DATASET, \"test_images\"),\n    predict_transform=ImageClassificationInputTransform,\n    batch_size=64,\n    transform_kwargs={\"image_size\": image_size},\n    num_workers=os.cpu_count(),\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-11T16:00:11.582812Z","iopub.status.idle":"2022-05-11T16:00:11.584302Z","shell.execute_reply.started":"2022-05-11T16:00:11.583945Z","shell.execute_reply":"2022-05-11T16:00:11.583985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.inference_mode():\n    predictions = []\n    for lbs in trainer.predict(model, datamodule=datamodule, output=\"labels\"):\n        predictions += lbs","metadata":{"execution":{"iopub.status.busy":"2022-05-11T16:00:11.585864Z","iopub.status.idle":"2022-05-11T16:00:11.587297Z","shell.execute_reply.started":"2022-05-11T16:00:11.586809Z","shell.execute_reply":"2022-05-11T16:00:11.586845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\"id\": df_test.index, \"Predicted\": predictions}).set_index(\"id\")\nsubmission.to_csv(\"submission.csv\")\n\n! head submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-05-11T16:00:11.588935Z","iopub.status.idle":"2022-05-11T16:00:11.590109Z","shell.execute_reply.started":"2022-05-11T16:00:11.58977Z","shell.execute_reply":"2022-05-11T16:00:11.589811Z"},"trusted":true},"execution_count":null,"outputs":[]}]}