{"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 sys\nsys.path.append('../input/tez-lib')\nsys.path.append('../input/timmmaster')","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:33.300698Z","iopub.execute_input":"2021-11-03T04:20:33.300951Z","iopub.status.idle":"2021-11-03T04:20:33.306856Z","shell.execute_reply.started":"2021-11-03T04:20:33.300923Z","shell.execute_reply":"2021-11-03T04:20:33.304006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import argparse\nimport os\n\nimport cv2\nimport albumentations\nimport albumentations.pytorch\nimport pandas as pd\nimport numpy as np\n\nimport tez\nimport timm\nimport torch\nimport torch.nn as nn\nimport torchvision\n\nfrom sklearn import metrics, model_selection, preprocessing\nfrom tez.callbacks import EarlyStopping\nfrom torch.nn import functional as F","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-03T04:20:33.309334Z","iopub.execute_input":"2021-11-03T04:20:33.309740Z","iopub.status.idle":"2021-11-03T04:20:33.318553Z","shell.execute_reply.started":"2021-11-03T04:20:33.309706Z","shell.execute_reply":"2021-11-03T04:20:33.317194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_DIR = '../input/cassava-swin-transformer-tez/'","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:33.320339Z","iopub.execute_input":"2021-11-03T04:20:33.320585Z","iopub.status.idle":"2021-11-03T04:20:33.327696Z","shell.execute_reply.started":"2021-11-03T04:20:33.320553Z","shell.execute_reply":"2021-11-03T04:20:33.326650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    image_size = 224\n    target_size = 5\n    target_col = 'label'\n    model_name = 'swin_tiny_patch4_window7_224'\n    epochs = 15\n    batch_size = 64\n    n_fold = 5\n    trn_fold = [0,1,2,3,4]","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:33.330035Z","iopub.execute_input":"2021-11-03T04:20:33.330579Z","iopub.status.idle":"2021-11-03T04:20:33.335900Z","shell.execute_reply.started":"2021-11-03T04:20:33.330543Z","shell.execute_reply":"2021-11-03T04:20:33.335247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class FlowerDataset:\n    def __init__(self, image_paths, targets, augmentations):\n        self.image_paths = image_paths\n        self.targets = targets\n        self.augmentations = augmentations\n        \n    def __len__(self):\n        return len(self.image_paths)\n\n    def __getitem__(self, item):\n        targets = self.targets[item]\n        \n        image = cv2.imread(self.image_paths[item])\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n        augmented = self.augmentations(image = image)\n        image = augmented[\"image\"]\n        \n        return {\n            \"image\": image,\n            \"targets\": targets,\n        }","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:33.337806Z","iopub.execute_input":"2021-11-03T04:20:33.338454Z","iopub.status.idle":"2021-11-03T04:20:33.346084Z","shell.execute_reply.started":"2021-11-03T04:20:33.338419Z","shell.execute_reply":"2021-11-03T04:20:33.345146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LeafModel(tez.Model):\n    def __init__(self, pretrained = True):\n        super().__init__()\n        self.model = timm.create_model(model_name = CFG.model_name, pretrained = pretrained)\n        self.n_features = self.model.head.in_features\n        self.model.head = nn.Linear(self.n_features, CFG.target_size)\n        \n        self.step_scheduler_after = \"epoch\"\n        self.step_scheduler_metric = \"valid_accuracy\"\n\n    def forward(self, image, targets=None):\n        batch_size, _, _, _ = image.shape\n\n        outputs = self.model(image)\n        \n        if targets is not None:\n            loss = nn.CrossEntropyLoss()(outputs, targets)\n            metrics = self.monitor_metrics(outputs, targets)\n            return outputs, loss, metrics\n        return outputs, None, None","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:33.347615Z","iopub.execute_input":"2021-11-03T04:20:33.347863Z","iopub.status.idle":"2021-11-03T04:20:33.356102Z","shell.execute_reply.started":"2021-11-03T04:20:33.347830Z","shell.execute_reply":"2021-11-03T04:20:33.355347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_aug = albumentations.Compose(\n        [\n            albumentations.Resize(CFG.image_size, CFG.image_size, p=1.0),\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            albumentations.pytorch.ToTensorV2()\n        ],\n        p=1.0\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:33.357507Z","iopub.execute_input":"2021-11-03T04:20:33.358013Z","iopub.status.idle":"2021-11-03T04:20:33.368509Z","shell.execute_reply.started":"2021-11-03T04:20:33.357977Z","shell.execute_reply":"2021-11-03T04:20:33.367817Z"},"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 = FlowerDataset(\n    image_paths=test_image_paths,\n    targets=test_targets,\n    augmentations=valid_aug,\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:33.369419Z","iopub.execute_input":"2021-11-03T04:20:33.371354Z","iopub.status.idle":"2021-11-03T04:20:33.382341Z","shell.execute_reply.started":"2021-11-03T04:20:33.371319Z","shell.execute_reply":"2021-11-03T04:20:33.381585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(fold):\n    model = LeafModel(pretrained = False)\n    model.load(INPUT_DIR+f'{CFG.model_name}_fold{fold}_best.bin', device=\"cuda\", weights_only=True)\n    predictions = model.predict(test_dataset, batch_size=32)\n    return predictions","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:33.484793Z","iopub.execute_input":"2021-11-03T04:20:33.485116Z","iopub.status.idle":"2021-11-03T04:20:33.490196Z","shell.execute_reply.started":"2021-11-03T04:20:33.485086Z","shell.execute_reply":"2021-11-03T04:20:33.489317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_preds = None\nfor j in range(CFG.n_fold):\n    preds = predict(j)\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-11-03T04:20:33.491872Z","iopub.execute_input":"2021-11-03T04:20:33.492678Z","iopub.status.idle":"2021-11-03T04:20:43.065547Z","shell.execute_reply.started":"2021-11-03T04:20:33.492644Z","shell.execute_reply":"2021-11-03T04:20:43.064660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_preds = final_preds.argmax(axis=1)\n\ndfx.label = final_preds\ndfx.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:43.067382Z","iopub.execute_input":"2021-11-03T04:20:43.067809Z","iopub.status.idle":"2021-11-03T04:20:43.081400Z","shell.execute_reply.started":"2021-11-03T04:20:43.067766Z","shell.execute_reply":"2021-11-03T04:20:43.080674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfx","metadata":{"execution":{"iopub.status.busy":"2021-11-03T04:20:43.082796Z","iopub.execute_input":"2021-11-03T04:20:43.083072Z","iopub.status.idle":"2021-11-03T04:20:43.101521Z","shell.execute_reply.started":"2021-11-03T04:20:43.083039Z","shell.execute_reply":"2021-11-03T04:20:43.100728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}