{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Setup Environment"},{"metadata":{"trusted":true},"cell_type":"code","source":"# !pip install ../input/fastai2-offline/timm-0.2.1-py3-none-any.whl ../input/fastai2-offline/wwf-0.0.3-py3-none-any.whl -q","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom fastai.vision.all import *\n# from wwf.vision.timm import *\n# from fastai.callback.cutmix import *\n\nimport os","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Import Train Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/cassava-leaf-disease-classification')\n\n# 2019 and 2020 merge data path\n# data_path = Path('../input/cassava-datasetv2/cassava_datasetv2')\ndata_path = Path('../input/cassava-leaf-disease-merged')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_df = pd.read_csv(data_path/'train.csv')\ntrain_df = pd.read_csv(data_path/'merged.csv')\ntrain_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Remove the duplicate image according to this [discussion](https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/198202)\n* '1562043567.jpg' and '3551135685.jpg' (mislabel)\n* '2252529694.jpg' and '911861181.jpg' (duplicate)\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_df = train_df[~train_df['image_id'].isin(['1562043567.jpg', '3551135685.jpg', '2252529694.jpg'])]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Create Dataloaders"},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_x(r):\n#     return data_path/'train_images'/r['image_id']\n    return data_path/'train'/r['image_id']\n\ndef get_y(r):\n    return r['label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_data(size=224,bs=128,data_df=train_df):\n    dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                       splitter=RandomSplitter(seed=42),\n                       get_x=get_x, \n                       get_y=get_y,\n                       item_tfms = RandomResizedCrop(size=size, min_scale=0.75, ratio=(1.,1.)),\n                       batch_tfms = [*aug_transforms(size=size, flip_vert=True),Normalize.from_stats(*imagenet_stats)]\n                      )\n    return dblock.dataloaders(data_df,bs=bs)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Get dataloader and show the data"},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = get_data(size=112)\ndls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Create learner"},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists('/root/.cache/torch/hub/checkpoints/'):\n        os.makedirs('/root/.cache/torch/hub/checkpoints/')\n!cp '../input/resnet50/resnet50.pth' '/root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth'\n# !cp '../input/timm-pretrained-efficientnet/efficientnet/efficientnet_b0_ra-3dd342df.pth' '/root/.cache/torch/hub/checkpoints/efficientnet_b0_ra-3dd342df.pth'\n# !cp '../input/timm-pretrained-efficientnet/efficientnet/efficientnet_b3_ra2-cf984f9c.pth' '/root/.cache/torch/hub/checkpoints/efficientnet_b3_ra2-cf984f9c.pth'\n# !cp '../input/timm-pretrained-efficientnet/efficientnet/tf_efficientnet_b3_ns-9d44bf68.pth' '/root/.cache/torch/hub/checkpoints/tf_efficientnet_b3_ns-9d44bf68.pth'\n# !cp '../input/timm-pretrained-resnet/resnet/seresnext26d_32x4d-80fa48a3.pth' '/root/.cache/torch/hub/checkpoints/seresnext26d_32x4d-80fa48a3.pth'\n# !cp '../input/timm-pretrained-resnet/resnet/wide_resnet50_2-95faca4d.pth' '/root/.cache/torch/hub/checkpoints/wide_resnet50_2-95faca4d.pth'\n# !cp '../input/timm-pretrained-resnet/resnet/resnext50_32x4d_ra-d733960d.pth' '/root/.cache/torch/hub/checkpoints/resnext50_32x4d_ra-d733960d.pth'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"loss_func = LabelSmoothingCrossEntropy()\ncbs = MixUp","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dls, resnet50, loss_func=loss_func, metrics=accuracy, cbs=cbs).to_native_fp16()\n\n# learn = timm_learner(dls, 'efficientnet_b3', loss_func=loss_func, metrics=accuracy, cbs=cbs).to_native_fp16()","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.fine_tune(20, base_lr=1e-2 , freeze_epochs=8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"resize to 224"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.dls = get_data()\nlearn.dls.show_batch()","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.fine_tune(20, freeze_epochs=8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = learn.to_native_fp32()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Make Submission file"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv(path/'sample_submission.csv')\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"prediction using test time augmentation(TTA)"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data_path = submission_df['image_id'].apply(lambda x: path/'test_images'/x)\ntst_dl = learn.dls.test_dl(test_data_path)\npredictions = learn.tta(dl = tst_dl, n=10, beta=0)\n\nsubmission_df['label'] = np.argmax(predictions[0],axis=1)\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_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}