{"cells":[{"metadata":{"_uuid":"86a9f063d7b4b71aca2ee1813a0bd36671551f15"},"cell_type":"markdown","source":"# Minimal, stripped-down version using Fast.ai\nIf you're new to Kaggle like me, you may have noticed the following frustrations:\n1. No feedback loop; no ability to know what you've done wrong, or how to fix it, when something doesn't happen as expected.\n2. Even worse, it looks like it's doing something for 30 minutes, and then you stumble into frustration #1.\n\nSo I took this [excellent kernel](https://www.kaggle.com/mentalwanderer/minimal-fast-ai-kit-for-image-classification) and stripped it down to only the bare necessities. Depending on your settings, it should take a half-hour to an hour to run.  From here, you and I can get fancier if we like, but at least we have run a successful submission on Kaggle.\n\nNOTE THAT NOTHING IN HERE SHOULD BE CONSIDERED A BEST PRACTICE, OR EVEN NECESSARILY 'ACCEPTABLE PRACTICE'. In particular, I'm totally skipping EDA - which is a huge and fundamental part of any successful data science project, but I'm literally just focusing on the mechanics of getting a working Kaggle submission."},{"metadata":{"_uuid":"66ba8baf0a9169ffa65cb5d929f9a7c71a110a07"},"cell_type":"markdown","source":"# Import packages"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nfrom torchvision.models import * \n\nimport os\nimport pathlib\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bc506c3e1586e44fc51f717f3a3e99778cd06219"},"cell_type":"markdown","source":"# Data loading and preparation"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"np.random.seed(123)\n\npath = Path(\"../input\")\n\ntfms = get_transforms(do_flip = True,flip_vert = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"91b943362c6451ea74111faff902ce73d0bccb85"},"cell_type":"code","source":"sz = 36\ndata = ImageDataBunch.from_csv(path, folder = 'train', csv_labels = \"train_labels.csv\",\n                               test = 'test',suffix=\".tif\", size = sz, ds_tfms = tfms)\ndata.path = pathlib.Path('.')\nstats = data.batch_stats()\ndata.normalize(stats)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b66e8bb47853bf47cdd98fdfed31900b70f84d1b"},"cell_type":"markdown","source":"# Model creation"},{"metadata":{"trusted":true,"_uuid":"ff61e57537f8f611a2efc11e2e7b846179e00c10"},"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\ndef auc_score(y_pred,y_true,tens=True):\n    score = roc_auc_score(y_true,torch.sigmoid(y_pred)[:,1])\n    if tens:\n        score = tensor(score)\n    return score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"038f7bb44f5e850827e718aade8715d44e33e35e"},"cell_type":"code","source":"learn = create_cnn(data,resnet18, pretrained = True,metrics = [auc_score ,accuracy])\nlearn.fit(epochs=3)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"885a8278d83b8a0aa6f3573a30391bab7a4fe68a"},"cell_type":"markdown","source":"# Generating submission"},{"metadata":{"_uuid":"cf9128cc20cb44b48b580d65c23f66dd2a6fe81f"},"cell_type":"markdown","source":"As the order of the images loaded into `data` is not necessarily the same order as that in the required submission, we will need to rearrange the predictions on the test set.\n\nThe desired order for submission is that of `sample_submission.csv`. while the order of the test set loaded into `data` can be accessed by calling `learn.data.test_ds.items`. We will first create a dictionary assigning each image in the test to its prediction by the model, then call the keys in the order in  `sample_submission.csv`."},{"metadata":{"trusted":true,"_uuid":"c9359e1fa590e6415e4917ec4e4284308cc516e5"},"cell_type":"code","source":"submissions = pd.read_csv('../input/sample_submission.csv')\nid_list = list(submissions.id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"20e01bf94c2cdd18346b3ec8b59398da167a83ca"},"cell_type":"code","source":"preds,y = learn.TTA(ds_type=DatasetType.Test)\npred_list = list(preds[:,1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82d007d385515fdb237233eb601ce7764b7e9649"},"cell_type":"code","source":"print(learn.data.test_ds.items)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e4341d2799ebc69def00767f10330dfc1a3524e0"},"cell_type":"code","source":"pred_dict = dict((key, value.item()) for (key, value) in zip(learn.data.test_ds.items,pred_list))\npred_ordered = [pred_dict[Path('../input/test/' + id + '.tif')] for id in id_list]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78e3e5fd06a4383ad81fe285579937f49333769f"},"cell_type":"code","source":"import time # Adding int(time.time() at end of file name so it's unique)\nsubmissions = pd.DataFrame({'id':id_list,'label':pred_ordered})\nsubmissions.to_csv(\"submission_weightdecay_{}.csv\".format(int(time.time())),index = False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"56eaf8be52be9665272c073b3e5b86986514fffc"},"cell_type":"markdown","source":"# What now?\n\n* Thanks again to [mentalwanderer](https://www.kaggle.com/mentalwanderer) for excellent kernel (who is not responsible for anything stupid I've done), and to the Fast.ai team for awesome and simple code.\n* Check out mentalwanderer's original notebook for lots more detail and things that will provide a better end result: https://www.kaggle.com/mentalwanderer/minimal-fast-ai-kit-for-image-classification\n* Check out the [Fast.ai library](https://docs.fast.ai/index.html) and [the v3 course](https://course.fast.ai/index.html)."}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}