{"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},"cell_type":"code","source":"from fastai.vision import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"787d5cdbb1cb1e4516d7bb7f0be40439ec165f40"},"cell_type":"code","source":"df = pd.read_csv(\"../input/train_labels.csv\")\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"308e6887bb6bd0e7c0fca69cd7037ff840d4e2b1"},"cell_type":"code","source":"tfms = get_transforms(flip_vert=True, max_lighting=0.1, max_zoom=1.05, max_warp=0.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c8a266ef01b42362fc50a92328d87de3092fdff9"},"cell_type":"code","source":"np.random.seed(42)\nsrc = (ImageItemList.from_csv('../input', 'train_labels.csv', folder='train', suffix='.tif')\n       .random_split_by_pct(0.2)\n       .label_from_df(label_delim=' '))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83cad586603cbb3cc94a3c5e9179531928ea6438"},"cell_type":"code","source":"data = (src.transform(tfms, size=128)\n        .databunch().normalize(imagenet_stats))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d18f56b43080af4fc1f839df0dc9af4cd418c7a"},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(12,9))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2aaf2c85c48c2aeaec0ca416c3ebd83be1c0446d"},"cell_type":"code","source":"arch = models.resnet50","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"296ef7d862345bb5f33817dbbd9aa14627ac8384"},"cell_type":"code","source":"df['label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"853f44abc19c2844a30babbd33445e05213395eb"},"cell_type":"code","source":"PATH = \"../input/\"\nprint(\"training data\")\n!ls -l {PATH}train | grep ^[^dt] | wc -l","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4779dcb2783f25fc010b9cdd01874e1c70251e4d"},"cell_type":"code","source":"print(\"test data\")\n!ls -l {PATH}test | grep ^[^dt] | wc -l","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"728cd4a0eeb41d5dc508a875c3828dcedaad3d51"},"cell_type":"markdown","source":"**All code below this cell are Work In Progress [WIP]**"},{"metadata":{"trusted":true,"_uuid":"78cfdc087efc0f14929b92afed5fc39c3cff7d16"},"cell_type":"code","source":"from os.path import *\ncache_dir = expanduser(join('~', '.torch'))\nif not exists(cache_dir):\n    makedirs(cache_dir)\nmodels_dir = join(cache_dir, 'models')\nif not exists(models_dir):\n    makedirs(models_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a61a90f5a0d0a8fae75ec6a931f06c3d43d2914e"},"cell_type":"code","source":"!ls ~/.torch/models","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62344c2543c234ecd76f55d8a4b70f0cda9acc25"},"cell_type":"code","source":"import pathlib\narch.path = pathlib.Path('.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f675ffced791ed708aa1382284c489ab8cadf716"},"cell_type":"code","source":"!cp ../input/models/resnet50-19c8e357.pth ~/.torch/models/resnet50-19c8e357.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dc3e19d64b2b163853569875924b86375717392c"},"cell_type":"code","source":"learn = create_cnn(data,arch, path='.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a9b809a5050d23125b93c5c379b45fac4f2d848"},"cell_type":"code","source":"create_cnn??\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"894bf961c984631a79a3e8b0779f661cd8ac7465"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e89e4737502f5ec4f8f2607a4b2e08fa0809d577"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bad888c00cc106dab629bf676d4eff36e533458e"},"cell_type":"code","source":"lr = 7.6e-3\nlearn.fit_one_cycle(5,slice(lr))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"990f80a90ff5f1fa54e18c1bfc88f78634d7d6b9"},"cell_type":"code","source":"learn.save('stage-1-rn50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ee429a29634f8cbed66946cc7cace9d2fce310d"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f17dfd322b60576bc696acf1e8b86d175f27c69"},"cell_type":"code","source":"learn.lr_find()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15bba0cb1ce04b8812206053d8e9dbff59dc23dd"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66b8fa73ac35f173600d77bc9409ff7fd2ed468f"},"cell_type":"code","source":"learn.fit_one_cycle(5,slice(5e-6,lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"086eb18f31c3f204a41a8fffc6cda10b27c44fd1"},"cell_type":"code","source":"learn.save('stage-2-rn50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"156b0d0863100afc771e09cfed45a86282b44167"},"cell_type":"code","source":"data = (src.transform(tfms, size=256)\n        .databunch().normalize(imagenet_stats))\n\nlearn.data = data\ndata.train_ds[0][0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"707227d562837ff1e5bc8ac16105e3836374f0b3"},"cell_type":"code","source":"learn.freeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee90aaa418a12692c2622de3500922d1c7ca2a33"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2ae6845414df489573082f6040581b6ad904bd9"},"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.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}