{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1dda351ec55b056c6a96bcdfe382d8ab9320216","collapsed":true},"cell_type":"code","source":"from fastai.imports import *\nfrom fastai.torch_imports import *\nfrom fastai.transforms import *\nfrom fastai.conv_learner import *\nfrom fastai.model import *\nfrom fastai.dataset import *\nfrom fastai.sgdr import *\nfrom fastai.plots import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dad32823aa57f291697bc65c4c96529caa4af9b8","collapsed":true},"cell_type":"code","source":"torch.cuda.set_device(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"86499105422eecf3d2ff8add530c0566d7791e2d"},"cell_type":"code","source":"torch.backends.cudnn.enabled","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cbef1973a13f541cd25b1a0f93e9d6a6313a8899","collapsed":true},"cell_type":"code","source":"PATH = '../input/'\nsz = 224\narch = resnet101\nbs = 128","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"218d6f80b1efb5058c0455da2d4fba77cb7296c5"},"cell_type":"code","source":"!ln -s {PATH}train\n!ln -s {PATH}test\n!ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"067e356967231a708a96db471e3b92b59ae62b68","collapsed":true},"cell_type":"code","source":"label_csv = f'{PATH}labels.csv'\nn = len(list(open(label_csv))) - 1 # header is not counted (-1)\nval_idxs = get_cv_idxs(n) # random 20% data for validation set","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b247f5c13bffc5643b5bbb8504e319a017929d2","collapsed":true},"cell_type":"code","source":"def get_data(sz,bs):\n    tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)\n    data = ImageClassifierData.from_csv('.', 'train', label_csv, test_name='test',\n                                       val_idxs=val_idxs, suffix='.jpg', tfms=tfms, bs=bs)\n    return data if sz > 300 else data.resize(340, 'tmp')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0c2c138075d7bdacce8b6f60aef8075fc247dac8"},"cell_type":"code","source":"data = get_data(sz,bs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"821e26a7a279a4155d0907fff6cdc096e942041b"},"cell_type":"code","source":"fn = f'{PATH}' + data.trn_ds.fnames[0]; fn\nimg = PIL.Image.open(fn); img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b17594e1a5a1c729c57aca44cc78234c5a08e1dc"},"cell_type":"code","source":"learn = ConvLearner.pretrained(arch,data,precompute=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03ee5b3993f4986f197c7f37b9aded663052cd45"},"cell_type":"code","source":"learning_rate = learn.lr_find()\nlearn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27c1b3fe3fdb2ddee93d5ce5339fd77c015fdbfd"},"cell_type":"code","source":"learn.fit(1e-2, 5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a43461716963ddd0a993a4a7cb80f66fd719919f","collapsed":true},"cell_type":"code","source":"from sklearn import metrics","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e12a15ccb616f9701afe7841012f0793bf7a4acb"},"cell_type":"code","source":"#submitting...\nlog_preds, y = learn.TTA(is_test=True) # use test dataset rather than validation dataset\nprobs = np.mean(np.exp(log_preds),0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ccfe3b55dd3c55fdcb6ec93cc6941cc8f437cf21"},"cell_type":"code","source":"probs.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66a8db1e9e9e99bfe789647dfc4c37dc103a04b7","collapsed":true},"cell_type":"code","source":"df = pd.DataFrame(probs)\ndf.columns = data.classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77c5487541df3427c4149a2b282638046d038f92"},"cell_type":"code","source":"df.insert(0, 'id', [o[5:-4] for o in data.test_ds.fnames])\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"15b679158250639db6f2ec1562c627f9216bf887"},"cell_type":"code","source":"\nwd = '/kaggle/working/'\ndef clean_up(wd=wd):\n    \"\"\"\n    Delete all temporary directories and symlinks in working directory (wd)\n    \"\"\"\n    for root, dirs, files in os.walk(wd):\n        try:\n            for d in dirs:\n                if os.path.islink(d):\n                    os.unlink(d)\n                else:\n                    shutil.rmtree(d)\n            for f in files:\n                if os.path.islink(f):\n                    os.unlink(f)\n                else:\n                    print(f)\n        except FileNotFoundError as e:\n            print(e)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0efb38686bfdf1ad7b08e9f501e5b0a87d8c68e8","collapsed":true},"cell_type":"code","source":"#SUBM = f'..input/subm/'\n#os.makedirs(SUBM, exist_ok=True)\n#df.to_csv(f'{SUBM}subm.gz', compression='gzip', index=False)\ndf.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc12da8b5d1e7598233590bf6f5269ca98487fbb"},"cell_type":"code","source":"clean_up()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"57d458bcd250ee371ecfe26b045e239a1c91d64a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}