{"cells":[{"metadata":{"trusted":true,"_uuid":"45049a1462ed4344cd93f50a9c45c38b7eb6db93"},"cell_type":"code","source":"# Put these at the top of every notebook, to get automatic reloading and inline plotting\n%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"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.\n\nPATH = \"../input/humpback-whale-identification/\"\n\nMODEL_NAME = 'resnet34'\nTRAIN = '../input/humpback-whale-identification/train/'\nTEST = '../input/humpback-whale-identification/test/'\nLABELS = '../input/humpback-whale-identification/train.csv'\nSAMPLE_SUB = '../input/humpback-whale-identification/sample_submission.csv'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"beb845ec6e732620644db314d616dd129a447814"},"cell_type":"code","source":"print(os.listdir(\"../input/humpback-whale-identification/test/\"))\n      ","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from fastai.vision import *\nfrom fastai.metrics import accuracy\nfrom fastai.basic_data import *\nimport pandas as pd\nimport numpy as np\nimport torch\nfrom utils import *\nprint(os.listdir(\"../input/\"))\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3b50d86bbba1e43285674e41a9208b0daba18d78"},"cell_type":"code","source":"!pip show fastai","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a8442e0605b251a278c9a3d39701656bf9a7dc2"},"cell_type":"code","source":"cache_dir = os.path.expanduser(os.path.join('~', '.torch'))\nif not os.path.exists(cache_dir):\n    os.makedirs(cache_dir)\nmodels_dir = os.path.join(cache_dir, 'models')\nif not os.path.exists(models_dir):\n    os.makedirs(models_dir)\n\n# copy time!\n!cp ../input/resnet34/resnet34.pth /tmp/.torch/models/resnet34-333f7ec4.pth\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a89dd3505b633c665545c56eb06472de91d95df"},"cell_type":"code","source":"def apk(actual, predicted, k=10):\n    if len(predicted)>k:\n        predicted = predicted[:k]\n\n    score = 0.0\n    num_hits = 0.0\n\n    for i,p in enumerate(predicted):\n        if p in actual and p not in predicted[:i]:\n            num_hits += 1.0\n            score += num_hits / (i+1.0)\n\n    if not actual:\n        return 0.0\n\n    return score / min(len(actual), k)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bdd66fb1ef1bb95827e6c02924c7fb357179e9e5"},"cell_type":"code","source":"def mapk(actual, predicted, k=10):\n    return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca0274f9cf41fb6f7cf71d0756c04407d47e535d"},"cell_type":"code","source":"def map5(preds, targs):\n    predicted_idxs = preds.sort(descending=True)[1]\n    top_5 = predicted_idxs[:, :5]\n    res = mapk([[t] for t in targs.cpu().numpy()], top_5.cpu().numpy(), 5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"42ff5aec446b5008858a38b1b8ae8ad7a6a89ad0"},"cell_type":"code","source":"def top_5_pred_labels(preds, classes):\n    top_5 = top_5_preds(preds)\n    labels = []\n    for i in range(top_5.shape[0]):\n        labels.append(' '.join([classes[idx] for idx in top_5[i]]))\n    return labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0540ed78415fc22592accec4efd27dcbd383c4e0"},"cell_type":"code","source":"def top_5_preds(preds): return np.argsort(preds.numpy())[:, ::-1][:, :5]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d9687806f73c4989a97dfd8fe8687d1f3ed062e"},"cell_type":"code","source":"def top_5_pred_labels(preds, classes):\n    top_5 = top_5_preds(preds)\n    labels = []\n    for i in range(top_5.shape[0]):\n        labels.append(' '.join([classes[idx] for idx in top_5[i]]))\n    return labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c76d2219479b5d24ece943171d068b5d4ddc87bf"},"cell_type":"code","source":"def create_submission(preds, data, name, classes=None):\n    if not classes: classes = data.classes\n    sub = pd.DataFrame({'Image': [path.name for path in data.test_ds.x.items]})\n    sub['Id'] = top_5_pred_labels(preds, classes)\n    sub.to_csv(f'subs/{name}.csv.gz', index=False, compression='gzip')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ea55a5e9657746c4bf4a971a5fa43f40bad0993"},"cell_type":"code","source":"!pip show fastai","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"153ce890c3216cb2e53baf30fa388ccd820a2547"},"cell_type":"code","source":"\n!pwd\n!ls\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e164e04c5d8a6045ac87ae596cae3f6b3bf2803b"},"cell_type":"code","source":"df = pd.read_csv('../input/humpback-whale-identification/train.csv')\ndf.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f7afa09ae95b00a507715630bac3148b12e361d"},"cell_type":"code","source":"df.Id.value_counts().head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1554017dd60826503071d03a1cf0b2aa5aef40ed"},"cell_type":"code","source":"(df.Id == 'new_whale').mean()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1743a589015438080f51242dbcab00f6b1ba2304"},"cell_type":"code","source":"(df.Id.value_counts() == 1).mean()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9af72cce1a3478a7c7c16eaa91d2bbe731f185df"},"cell_type":"code","source":"df.Id.nunique()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2470932ca57d537a625ba481868efae07022889a"},"cell_type":"code","source":"df.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13e0b3e043098d4ffce57a7ca1f44082e6f4c189"},"cell_type":"code","source":"fn2label = {row[1].Image: row[1].Id for row in df.iterrows()}\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"270fd44ccea4340baaa067dbebfd4dd8db302d02"},"cell_type":"code","source":"SZ = 224\nBS = 64\nNUM_WORKERS = 0\nSEED=0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b343a69421d1f5ef0407dd858bc8ff3472fbfcda"},"cell_type":"code","source":"data = (\n    ImageItemList\n        .from_folder(TRAIN)\n        .random_split_by_pct(seed=SEED)\n        .label_from_func(lambda path: fn2label[path.name])\n        .add_test(ImageItemList.from_folder(TEST))\n        .transform(get_transforms(do_flip=False, max_zoom=1, max_warp=0, max_rotate=2), size=SZ, resize_method=ResizeMethod.SQUISH)\n        .databunch(bs=BS, num_workers=NUM_WORKERS)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f8eb301551aff8fa2fbe421cf0ef7396e9f3524"},"cell_type":"code","source":"data.show_batch(rows=3)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0fb16a9da705f1e9a1af5e1d615215d2c756554f"},"cell_type":"code","source":"name = f'res34-{SZ}'\nimport pathlib\ndata.path = pathlib.Path('.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35393c4084b2e62756a2c533acf2f12a8fbc16cd"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet34, metrics=[accuracy, map5])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7cc9e170a9f745bf188065e04b0ae9247fb472c"},"cell_type":"code","source":"learn.fit_one_cycle(2)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb105aeb25c82bd28289663a5c56c5875bf7a1f0"},"cell_type":"code","source":"learn.recorder.plot_losses()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"143a068e9a00e945f55c4012b191a04e4603d719"},"cell_type":"code","source":"learn.save(f'{name}-stage-1')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8b98592f3ffbbb40e36406c5d5c4b321d79a0d4"},"cell_type":"code","source":"learn.unfreeze()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"476c76f0c0ca7915e1f0e21b889084ae1fc82479"},"cell_type":"code","source":"learn.recorder.plot()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11ea5620ec958d27e2d875ae9e300737bdb457ce"},"cell_type":"code","source":"max_lr = 1e-4\nlrs = [max_lr/100, max_lr/10, max_lr]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7af2754cdaafdf0a7e902af8b8f46ce7a025787a"},"cell_type":"code","source":"learn.fit_one_cycle(5, lrs)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c94d1cc070468fa71629805f174338e888ca44f8"},"cell_type":"code","source":"learn.save(f'{name}-stage-2')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b19a524f695e0759d0ba4a3ee7bd6a0432ea035"},"cell_type":"code","source":"learn.recorder.plot_losses()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9a343c337cfb41a370a25be92c6c8e65ab95fa1"},"cell_type":"code","source":"preds, _ = learn.get_preds(DatasetType.Test)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df1267edf0a55605779cd4fffa6d6138df51d749"},"cell_type":"code","source":"mkdir -p subs\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"752d9c84ca3bef1c134ca5a36b0b9389bc5f31ac"},"cell_type":"code","source":"create_submission(preds, learn.data, name)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5a962210df905ae1e625adb0fabbc00ea01e77e"},"cell_type":"code","source":"pd.read_csv(f'subs/{name}.csv.gz').head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f75fef6e29c4995062ed34514c3d46d954c3dfb9"},"cell_type":"code","source":"!kaggle competitions submit -c humpback-whale-identification -f subs/{name}.csv.gz -m \"{name}\"\n","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}