{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom IPython.core.interactiveshell import InteractiveShell\nInteractiveShell.ast_node_interactivity = \"all\"\n\nfrom fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Path to the data\n\nTRAIN_FILE_PATH = '/kaggle/input/cassava-leaf-disease-classification/train_images'\nTRAIN_CSV_PATH = '/kaggle/input/cassava-leaf-disease-classification/train.csv'\nSUBMISSION_PATH = '/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(TRAIN_CSV_PATH)\nsub_df = pd.read_csv(SUBMISSION_PATH)\ntrain_df.head()\nsub_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Inspecting the target distribution\nimport seaborn as sns\ntrain_df['label'].value_counts(normalize=True)\nsns.countplot(train_df['label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_X, val_X, x, y = train_test_split(train_df['image_id'], train_df['label'], stratify=train_df['label'], test_size=0.3, random_state=11) \nval_idxs = val_X.index\ntrain_idxs = train_X.index","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"item_tfms = Resize(512)\n# Image augmentation\nbatch_tfms = [RandomResizedCrop(size=224), *aug_transforms(mult=1.0, do_flip=True, max_rotate=30.0),\n                                    Normalize.from_stats(*imagenet_stats)]\n\n\ndblock = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                    splitter = IndexSplitter(val_idxs),\n                    get_x = ColReader(0,pref=f'{TRAIN_FILE_PATH}/',suff=''),\n                    get_y = ColReader(1, pref=f'{TRAIN_FILE_PATH}/'),\n                    item_tfms = item_tfms,\n                    batch_tfms = batch_tfms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = dblock.dataloaders(train_df,bs=64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# mixup\nmixup=MixUp(0.4)\nmodel = xresnet18()\nlearn = Learner(dls, model,loss_func=LabelSmoothingCrossEntropy(), metrics=accuracy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(20, 3e-3, cbs=mixup)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#\nTEST_FILE_PATH = '/kaggle/input/cassava-leaf-disease-classification/test_images'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"_sample_df = sub_df.copy()\ntest_files = TEST_FILE_PATH + '/' + _sample_df['image_id']\ntest_dl = dls.test_dl(test_files)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# getting our prediction\npreds, _ = learn.tta(dl=test_dl, n=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df['label'] = preds.argmax(dim=-1).numpy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}