{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Creating a fastai pipeline\n\nAdapted from : https://www.kaggle.com/slm37102/cassava-leaf-disease-classification-fastai"},{"metadata":{},"cell_type":"markdown","source":"## Initial setup"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -Uqq fastai","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\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"try:\n    print(torch.cuda.get_device_name(0))\nexcept Exception as e:\n    print(e)\n    print(\"Please enable gpu to run. Or comment this cell.\")\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Import train data"},{"metadata":{"trusted":true},"cell_type":"code","source":"data_path = Path(\"../input/plant-pathology-2021-fgvc8\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"str(data_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/plant-pathology-2021-fgvc8/train.csv\")\ntrain_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So the labels are verbose and sometimes multiple labels are also present in the data."},{"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']\n\ndef get_y(r):\n    return r['labels'].split(' ')","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, MultiCategoryBlock),\n                       splitter=RandomSplitter(seed=42),\n                       get_x=get_x,\n                       get_y=get_y,\n                       item_tfms=RandomResizedCrop(128, min_scale=0.35),\n                       batch_tfms = [*aug_transforms(size=size, flip_vert=True), Normalize.from_stats(*imagenet_stats)])\n    return dblock.dataloaders(data_df, bs=bs)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Lets take a look"},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = get_data()\ndls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating a learner"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dls, resnet18, metrics=partial(accuracy_multi, thresh=0.2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.model = learn.model.cuda()","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":"try:\n    learn.fine_tune(2, base_lr=1e-2, freeze_epochs=4)\nexcept Exception as e:\n    print(e)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Make Submission file"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv(data_path/'sample_submision.csv')\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## prediction using TTA"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data_path = submission_df[\"image\"].apply(lambda x: data_path/'test_images'/x)\ntst_dl = learn.dls.test_dl(test_data_path)\npredictions = learn.tta(dl=tst_dl, n=10, beta=0)\n\nprint(predictions)\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":{"trusted":true},"cell_type":"code","source":"learn.show_results()","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}