{"cells":[{"metadata":{"_uuid":"afe597eae01375fbcabbd3402f83b9988dc6513e"},"cell_type":"markdown","source":"### Thanks to authors of these notebooks for helpful examples of how to imitate the fast.ai MOOC code within a kernel\n\nhttps://www.kaggle.com/stefanbuenten/dog-breed-test-with-fastai\n\nhttps://www.kaggle.com/hortonhearsafoo/fast-ai-lesson-1"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\nfrom fastai.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 *\nimport numpy as np\nimport pandas as pd\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c06b2b4b0e0907e5c2e3abcbdf3c68e286ddb0f"},"cell_type":"code","source":"labels = pd.read_csv(\"../input/labels.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"509fbe6993d338e084486fa611bcab72914b7068"},"cell_type":"code","source":"## validation data\n#val_idxs = get_cv_idxs(labels.shape[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06b8634bde8fb3e7fc01515e1677bdd86532d605"},"cell_type":"code","source":"PATH = \"../input/\"\nTMP_PATH = \"/tmp/tmp\"\nMODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f5d5e207bb74cd6c0e440b8cd06a0036cea2b940"},"cell_type":"code","source":"arch=resnet101\nsz=224\nbs=48","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"173983c95985258f5a645242b8e499fc6c84fceb"},"cell_type":"code","source":"tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fca99b524082eceb6645b699fab06662099aacf3"},"cell_type":"code","source":"data = ImageClassifierData.from_csv(path=PATH, folder=\"train\", csv_fname=f\"{PATH}labels.csv\", tfms=tfms, suffix=\".jpg\", test_name=\"test\",bs=bs,num_workers=4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"decd0016fd9b31392688ff7f7c73e36fc9227951"},"cell_type":"code","source":"learn = ConvLearner.pretrained(arch, data, precompute=True, tmp_name=TMP_PATH, models_name=MODEL_PATH, ps=0.4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6c48d235c1b29d5dd4649833509804a46cc262a"},"cell_type":"code","source":"lrf = learn.lr_find()\nlearn.sched.plot()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0d6447e46aa718e4a0bf0b10a18b82172a51f3c"},"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(PATH, 'train', f'{PATH}labels.csv', test_name='test', suffix='.jpg',tfms=tfms, bs=bs, num_workers=4)\n    return data if sz > 300 else data.resize(sz, '/tmp')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"302eeb83933b14389f6c6f018982038577dcad88"},"cell_type":"code","source":"##without augmentation\nlearn.fit(1e-1, 5)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73e1bc0e93e5b216a665564589711b72f0c5baee"},"cell_type":"code","source":"##with augmentation\nlearn.precompute=False\nlearn.fit(1e-1, 5, cycle_len=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5814d8bd33915d5d5980825ccbaa59657395351f"},"cell_type":"code","source":"## increase size trick\nlearn.set_data(get_data(299,bs))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa5db5cde59e754b5191d1c5b64146c930da87cf"},"cell_type":"code","source":"##and now resized\nlearn.fit(1e-1, 3, cycle_len=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58eaa096a7fa38e89dc2d537147e0ca572a590a7"},"cell_type":"code","source":"learn.fit(1e-1,3,cycle_len=1,cycle_mult=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f262e624e275203c0ccc48dd80cf1c91ac3edef"},"cell_type":"code","source":"learn.fit(1e-1, 1, cycle_len=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b228dbd0e5302504586af101b01258310ac72968"},"cell_type":"code","source":"## unfreezing doesnt help in this case....","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a5643c22daaea0b8a695d412cf590151185d888e"},"cell_type":"code","source":"from sklearn.metrics import log_loss\nlog_preds, y = learn.TTA()\nprobs = np.mean(np.exp(log_preds), 0)\naccuracy_np(probs, y), log_loss(y, probs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"703998e911dd404d5bb983a1ffa7b459ff3f3242"},"cell_type":"code","source":"log_preds_test, y_test = learn.TTA(is_test=True)\nprobs_test = np.mean(np.exp(log_preds_test), 0)\ndf = pd.DataFrame(probs_test)\ndf.columns = data.classes\ndf.insert(0, \"id\", [e[5:-4] for e in data.test_ds.fnames])\ndf.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]}],"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}