{"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":"tez_path = '../input/tez-lib/'\neffnet_path = '../input/efficientnet-pytorch'\nimport sys\nsys.path.append(tez_path)\nsys.path.append(effnet_path)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-27T06:11:18.086847Z","iopub.execute_input":"2021-08-27T06:11:18.087178Z","iopub.status.idle":"2021-08-27T06:11:18.091365Z","shell.execute_reply.started":"2021-08-27T06:11:18.087149Z","shell.execute_reply":"2021-08-27T06:11:18.090422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport albumentations\nimport pandas as pd\nimport numpy as np\n\nimport tez\nfrom tez.datasets import ImageDataset\n\nimport torch\nimport torch.nn as nn\nfrom torch.nn import functional as F\n\n\nfrom efficientnet_pytorch import EfficientNet","metadata":{"execution":{"iopub.status.busy":"2021-08-27T06:11:18.916459Z","iopub.execute_input":"2021-08-27T06:11:18.916904Z","iopub.status.idle":"2021-08-27T06:11:18.966693Z","shell.execute_reply.started":"2021-08-27T06:11:18.916866Z","shell.execute_reply":"2021-08-27T06:11:18.965659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LeafModel(tez.Model):\n    def __init__(self, num_classes):\n        super().__init__()\n\n        self.effnet = EfficientNet.from_name(\"efficientnet-b3\")\n        self.dropout = nn.Dropout(0.1)\n        self.out = nn.Linear(1536, num_classes)\n        self.step_scheduler_after = \"epoch\"\n\n    def forward(self, image, targets=None):\n        batch_size, _, _, _ = image.shape\n\n        x = self.effnet.extract_features(image)\n        x = F.adaptive_avg_pool2d(x, 1).reshape(batch_size, -1)\n        outputs = self.out(self.dropout(x))\n        return outputs, None, None","metadata":{"execution":{"iopub.status.busy":"2021-08-27T06:11:19.977630Z","iopub.execute_input":"2021-08-27T06:11:19.977978Z","iopub.status.idle":"2021-08-27T06:11:19.983939Z","shell.execute_reply.started":"2021-08-27T06:11:19.977948Z","shell.execute_reply":"2021-08-27T06:11:19.983088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# augmentations taken from: https://www.kaggle.com/khyeh0719/pytorch-efficientnet-baseline-inference-tta\ntest_aug = albumentations.Compose([\n    albumentations.RandomResizedCrop(256, 256),\n    albumentations.Transpose(p=0.5),\n    albumentations.HorizontalFlip(p=0.5),\n    albumentations.VerticalFlip(p=0.5),\n    albumentations.HueSaturationValue(\n        hue_shift_limit=0.2, \n        sat_shift_limit=0.2,\n        val_shift_limit=0.2, \n        p=0.5\n    ),\n    albumentations.RandomBrightnessContrast(\n        brightness_limit=(-0.1,0.1), \n        contrast_limit=(-0.1, 0.1), \n        p=0.5\n    ),\n    albumentations.Normalize(\n        mean=[0.485, 0.456, 0.406], \n        std=[0.229, 0.224, 0.225], \n        max_pixel_value=255.0, \n        p=1.0\n    )\n], p=1.)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T06:11:20.921746Z","iopub.execute_input":"2021-08-27T06:11:20.922073Z","iopub.status.idle":"2021-08-27T06:11:20.928142Z","shell.execute_reply.started":"2021-08-27T06:11:20.922043Z","shell.execute_reply":"2021-08-27T06:11:20.927308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfx = pd.read_csv(\"../input/cassava-leaf-disease-classification/sample_submission.csv\")\nimage_path = \"../input/cassava-leaf-disease-classification/test_images/\"\ntest_image_paths = [os.path.join(image_path, x) for x in dfx.image_id.values]\n# fake targets\ntest_targets = dfx.label.values\ntest_dataset = ImageDataset(\n    image_paths=test_image_paths,\n    targets=test_targets,\n    augmentations=test_aug,\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T06:11:21.817206Z","iopub.execute_input":"2021-08-27T06:11:21.817521Z","iopub.status.idle":"2021-08-27T06:11:21.835980Z","shell.execute_reply.started":"2021-08-27T06:11:21.817492Z","shell.execute_reply":"2021-08-27T06:11:21.835205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dfx = pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")\nmodel = LeafModel(num_classes=train_dfx.label.nunique())\nmodel.load(\"../input/modeldata/model.bin\")","metadata":{"execution":{"iopub.status.busy":"2021-08-27T06:11:22.935240Z","iopub.execute_input":"2021-08-27T06:11:22.935553Z","iopub.status.idle":"2021-08-27T06:11:30.323006Z","shell.execute_reply.started":"2021-08-27T06:11:22.935523Z","shell.execute_reply":"2021-08-27T06:11:30.322023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run inference 5 times\nfinal_preds = None\nfor j in range(5):\n    preds = model.predict(test_dataset, batch_size=32, n_jobs=-1)\n    temp_preds = None\n    for p in preds:\n        if temp_preds is None:\n            temp_preds = p\n        else:\n            temp_preds = np.vstack((temp_preds, p))\n    if final_preds is None:\n        final_preds = temp_preds\n    else:\n        final_preds += temp_preds\nfinal_preds /= 5","metadata":{"execution":{"iopub.status.busy":"2021-08-27T06:11:31.520893Z","iopub.execute_input":"2021-08-27T06:11:31.521227Z","iopub.status.idle":"2021-08-27T06:11:33.329673Z","shell.execute_reply.started":"2021-08-27T06:11:31.521199Z","shell.execute_reply":"2021-08-27T06:11:33.328726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_preds = final_preds.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T06:11:34.426930Z","iopub.execute_input":"2021-08-27T06:11:34.427260Z","iopub.status.idle":"2021-08-27T06:11:34.432181Z","shell.execute_reply.started":"2021-08-27T06:11:34.427227Z","shell.execute_reply":"2021-08-27T06:11:34.430857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfx.label = final_preds","metadata":{"execution":{"iopub.status.busy":"2021-08-27T06:11:35.470135Z","iopub.execute_input":"2021-08-27T06:11:35.470452Z","iopub.status.idle":"2021-08-27T06:11:35.474782Z","shell.execute_reply.started":"2021-08-27T06:11:35.470423Z","shell.execute_reply":"2021-08-27T06:11:35.473831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfx.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T06:11:36.389837Z","iopub.execute_input":"2021-08-27T06:11:36.390207Z","iopub.status.idle":"2021-08-27T06:11:36.399345Z","shell.execute_reply.started":"2021-08-27T06:11:36.390173Z","shell.execute_reply":"2021-08-27T06:11:36.398418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}