{"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\n# import os\n# for 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","execution":{"iopub.status.busy":"2021-08-04T12:38:54.690363Z","iopub.execute_input":"2021-08-04T12:38:54.690774Z","iopub.status.idle":"2021-08-04T12:38:54.696478Z","shell.execute_reply.started":"2021-08-04T12:38:54.690726Z","shell.execute_reply":"2021-08-04T12:38:54.694956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n\n#For Image Augmentation\nimport albumentations\n\n#Std import for numbers and dataframe\nimport numpy as np # linear algebra\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:54.697778Z","iopub.execute_input":"2021-08-04T12:38:54.698070Z","iopub.status.idle":"2021-08-04T12:38:54.710396Z","shell.execute_reply.started":"2021-08-04T12:38:54.698041Z","shell.execute_reply":"2021-08-04T12:38:54.709627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AlbumentationsTransform(RandTransform):\n    \"A transform handler for multiple `Albumentation` transforms\"\n    split_idx,order=None,2\n    def __init__(self, train_aug, valid_aug): store_attr()\n    \n    def before_call(self, b, split_idx):\n        self.idx = split_idx\n    \n    def encodes(self, img: PILImage):\n        if self.idx == 0:\n            aug_img = self.train_aug(image=np.array(img))['image']\n        else:\n            aug_img = self.valid_aug(image=np.array(img))['image']\n        return PILImage.create(aug_img)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:54.711892Z","iopub.execute_input":"2021-08-04T12:38:54.712489Z","iopub.status.idle":"2021-08-04T12:38:54.724584Z","shell.execute_reply.started":"2021-08-04T12:38:54.712439Z","shell.execute_reply":"2021-08-04T12:38:54.723750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_aug(): return 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.ShiftScaleRotate(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.CoarseDropout(p=0.5),\n            albumentations.Cutout(p=0.5)\n])\n\ndef get_valid_aug(): return albumentations.Compose([\n    albumentations.CenterCrop(256,256, p=1.),\n    albumentations.Resize(256,256)\n], p=1.)\n\nitem_tfms = AlbumentationsTransform(get_train_aug(), get_valid_aug())\n","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:54.725773Z","iopub.execute_input":"2021-08-04T12:38:54.726202Z","iopub.status.idle":"2021-08-04T12:38:54.743130Z","shell.execute_reply.started":"2021-08-04T12:38:54.726163Z","shell.execute_reply":"2021-08-04T12:38:54.742305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_x(df): return TRAIN_DIR/df['image_id']\ndef get_y(df): return df['label']\ndef splitter(df):\n    train = df.index[~df['is_val']].tolist()\n    valid = df.index[df['is_val']].tolist()\n    return train,valid","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:54.744183Z","iopub.execute_input":"2021-08-04T12:38:54.744571Z","iopub.status.idle":"2021-08-04T12:38:54.756325Z","shell.execute_reply.started":"2021-08-04T12:38:54.744534Z","shell.execute_reply":"2021-08-04T12:38:54.755231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load the pickle file you exported previously\npath = Path('/kaggle/input/casava-modelpkl')\npath.ls(file_exts='.pkl')\n\nlearn_inf = load_learner(path/'export_best_model.pkl')","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:54.758381Z","iopub.execute_input":"2021-08-04T12:38:54.759235Z","iopub.status.idle":"2021-08-04T12:38:54.980790Z","shell.execute_reply.started":"2021-08-04T12:38:54.759175Z","shell.execute_reply":"2021-08-04T12:38:54.979919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('/kaggle/input/cassava-leaf-disease-classification')\nDATA_DIR = path\nTRAIN_DIR = DATA_DIR/'train_images'\nTEST_DIR = DATA_DIR/'test_images'\nTRAIN_DIR,TEST_DIR","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:54.982411Z","iopub.execute_input":"2021-08-04T12:38:54.983147Z","iopub.status.idle":"2021-08-04T12:38:54.990457Z","shell.execute_reply.started":"2021-08-04T12:38:54.983086Z","shell.execute_reply":"2021-08-04T12:38:54.989395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = pd.read_csv(DATA_DIR/'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:54.991774Z","iopub.execute_input":"2021-08-04T12:38:54.992057Z","iopub.status.idle":"2021-08-04T12:38:55.008955Z","shell.execute_reply.started":"2021-08-04T12:38:54.992029Z","shell.execute_reply":"2021-08-04T12:38:55.007712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:55.010978Z","iopub.execute_input":"2021-08-04T12:38:55.011764Z","iopub.status.idle":"2021-08-04T12:38:55.024550Z","shell.execute_reply.started":"2021-08-04T12:38:55.011715Z","shell.execute_reply":"2021-08-04T12:38:55.023571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_copy = sample_df.copy()\nsample_copy['image_id'] = sample_copy['image_id'].apply(lambda x: f'../test_images/{x}')","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:55.026009Z","iopub.execute_input":"2021-08-04T12:38:55.026541Z","iopub.status.idle":"2021-08-04T12:38:55.033088Z","shell.execute_reply.started":"2021-08-04T12:38:55.026507Z","shell.execute_reply":"2021-08-04T12:38:55.032166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = learn_inf.dls.test_dl(sample_copy)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:55.034448Z","iopub.execute_input":"2021-08-04T12:38:55.035119Z","iopub.status.idle":"2021-08-04T12:38:55.051807Z","shell.execute_reply.started":"2021-08-04T12:38:55.035073Z","shell.execute_reply":"2021-08-04T12:38:55.050846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds, _ = learn_inf.tta(dl=test_dl, n=15, beta=0)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:55.053401Z","iopub.execute_input":"2021-08-04T12:38:55.053999Z","iopub.status.idle":"2021-08-04T12:38:58.808014Z","shell.execute_reply.started":"2021-08-04T12:38:55.053959Z","shell.execute_reply":"2021-08-04T12:38:58.806926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:58.809522Z","iopub.execute_input":"2021-08-04T12:38:58.809838Z","iopub.status.idle":"2021-08-04T12:38:58.830486Z","shell.execute_reply.started":"2021-08-04T12:38:58.809804Z","shell.execute_reply":"2021-08-04T12:38:58.829394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df['label'] = preds.argmax(dim=-1).numpy()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:58.831879Z","iopub.execute_input":"2021-08-04T12:38:58.832189Z","iopub.status.idle":"2021-08-04T12:38:58.839452Z","shell.execute_reply.started":"2021-08-04T12:38:58.832157Z","shell.execute_reply":"2021-08-04T12:38:58.838313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:38:58.841001Z","iopub.execute_input":"2021-08-04T12:38:58.841393Z","iopub.status.idle":"2021-08-04T12:38:58.854725Z","shell.execute_reply.started":"2021-08-04T12:38:58.841357Z","shell.execute_reply":"2021-08-04T12:38:58.853542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}