{"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":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9251895c8f9cf11d16e14d58b43609df9827affb"},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"542b09127555f02825395fc85702e8353a0912fe"},"cell_type":"code","source":"# batch size\nbs = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c13e35fdee5c1d5688cf3765e0c02524801f338"},"cell_type":"code","source":"# data path\nPATH = \"../input/\"\n\n# path to save model, as input path is read-only\nMODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0c26547c83a5df4a9408c984d9cc77b606fd7fd6"},"cell_type":"code","source":"# GPU required\ntorch.cuda.is_available()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7844a83f37222b817ca855420844875307c6bbd5"},"cell_type":"code","source":"torch.backends.cudnn.enabled","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f9b7f3b138fe7b8c5dc1994e0726fae8b841ed3e"},"cell_type":"markdown","source":"# Load the data"},{"metadata":{"trusted":true,"_uuid":"5990fba64d539d9eade41e7013fc3762b75758e4"},"cell_type":"code","source":"!head ../input/labels.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cec0a8e2f93f2ff71b76f84b119552bc88af4fd4"},"cell_type":"code","source":"# transforms applied to the images\ntfms = get_transforms(do_flip=True)\n\n# create ImageDataBunch object (images are resized to 'size')\ndata = ImageDataBunch.from_csv(PATH, folder='train', test='test', suffix='.jpg', ds_tfms=tfms,\n                               csv_labels='labels.csv', fn_col=0, label_col=1, size=128, bs=bs)\n\n# normalize\ndata.normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c1ca5d8ae63234303f33fa5480bcd97539a1cec"},"cell_type":"code","source":"data.show_batch(rows=4, figsize=(12,12))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"271bbb1f9b44f4ec876902d242ae0d7eb14a0a3b"},"cell_type":"markdown","source":"# Training: resnet34"},{"metadata":{"trusted":true,"_uuid":"18d26a530c1ee290f39802744883acb9d40eb7a6"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet34, metrics=accuracy, model_dir=MODEL_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f7f3684be2cce93090f57598b669f18c465db49c"},"cell_type":"code","source":"learn.model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"46d292c94dc37c68cb5aeb8b473aac6d7459a8d6"},"cell_type":"code","source":"learn.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee77a8605177c15fde8c7f40c9985599692accc4"},"cell_type":"code","source":"# save the weights\nlearn.save('/tmp/model/stage-1')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fa3d8d34fc0cfe85119f4c04c79564857f210469"},"cell_type":"markdown","source":"## Interpreting the results"},{"metadata":{"trusted":true,"_uuid":"38b8c4a6d3d8aa770aec9d8756667c467cb8e4ba"},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\n\nlosses, idxs = interp.top_losses()\n\nlen(data.valid_ds)==len(losses)==len(idxs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5982e0c38d96079bad19c3a58698a3024b02b45c"},"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e68f2d056500eee822aaf4fb38bd3e1977f8b46"},"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(24,24), dpi=60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4032a07d3b227f39c30cd33e902d1b3e7cf8518c"},"cell_type":"code","source":"# combinations of predicted and actual that got wrong the most often\ninterp.most_confused(min_val=3)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a19c60a752e24300c8286320b923c7ae6d64ea74"},"cell_type":"markdown","source":"## Fine-tuning the model"},{"metadata":{"trusted":true,"_uuid":"804cffe84594f766f19baded0acd82f6fd2d8bd3"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d480bf3813138d47698e73a4ff64a7f59629c5b1"},"cell_type":"code","source":"learn.fit_one_cycle(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8cf04543a65993fc8ab0367303ce8cfc3ff95010"},"cell_type":"code","source":"learn.load('/tmp/model/stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3515b303f162182ce362eb0a07bfbb16eabeaaf4"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd0d13e1601783be88cf8975f8d7fe5faee314c2"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a9078ea25ff71208007b1e997f1708600324957b"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(4, max_lr=slice(1e-6,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d872979d3b8872e4ca4123e5ba9a449694e5846"},"cell_type":"code","source":"learn.recorder.plot_lr(show_moms=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c966a0673072a621574acb26270e90ff917d5a7"},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cdc41f19f1f1275735653d942858f928761f412d"},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"deda045fe7f8f2ccc719eeb125acfe7ba4d1e87d"},"cell_type":"markdown","source":"# Comparing with normal fit"},{"metadata":{"trusted":true,"_uuid":"f5a9cb945c825ae436d8372e45df93f8d588daeb"},"cell_type":"code","source":"learn.load('/tmp/model/stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5076a18eae7b11775f7c9f3d8f1c3e5d4c4288f2"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit(4, lr=slice(1e-6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d26463475df5b7e9b1b0781e80670052c1595b6"},"cell_type":"code","source":"learn.fit(4, lr=slice(1e-6))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1b377c3a489db9d778a8d5b301fb2a5fe787142c"},"cell_type":"markdown","source":"# Training: resnet50"},{"metadata":{"trusted":true,"_uuid":"2f49a72eac10a5fb47067bd8a0cfa50c391aae39"},"cell_type":"code","source":"# transforms applied to the images\ntfms = get_transforms(do_flip=True)\n\n# create ImageDataBunch object (images are resized to 'size')\ndata = ImageDataBunch.from_csv(PATH, folder='train', test='test', suffix='.jpg', ds_tfms=tfms,\n                               csv_labels='labels.csv', fn_col=0, label_col=1, size=128, bs=bs//2)\n\n# normalize\ndata.normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dfa5614fe0160bc31811628899155cec720de920"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet50, metrics=accuracy, model_dir=MODEL_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d6440ed019d9b003dc1c51ea2f75da4e311383d"},"cell_type":"code","source":"learn.model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"025aca8c3eab8b85e84ea575180a2d119e233013"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66980f02d0219baf3f9c06eb71d5d968bc1a64ce"},"cell_type":"code","source":"learn.fit_one_cycle(5, max_lr=slice(1e-3,1e-2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77239cd97820199d2f5424e9cd89fdbfecb46f4c"},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0993f55dcb9c1b20bf0fb2f4134269aa40263552"},"cell_type":"markdown","source":"# Training: resnet101"},{"metadata":{"trusted":true,"_uuid":"a929d4ffa4ec303c6ca7f266e36cd5ae9aa4c5e9"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet101, metrics=accuracy, model_dir=MODEL_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c5d766e4ea2766731c4c49fc3a8f1704e9881ca"},"cell_type":"code","source":"learn.model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b284987b984d049903e5472abb891634d487180"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e59fad68ab4775a48df4783420b71c45fd0810a"},"cell_type":"code","source":"learn.fit_one_cycle(6, max_lr=slice(1e-3,1e-2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"22f268646febb522c677e068e5b7f2e493315564"},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e10ad10af6b788f25b08766d30920d9f62fdc4f2"},"cell_type":"markdown","source":"# Get predictions"},{"metadata":{"trusted":true,"_uuid":"e8285257440112888f31c56a69ac0b788b3511e1"},"cell_type":"code","source":"predictions = learn.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"48f5aa87b640cfce7199d85bdc78e4df47c018ec"},"cell_type":"code","source":"sample_submission_df = pd.read_csv('../input/sample_submission.csv')\nsample_submission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"185cf32f68b81c9fb5e8838a18e862a60da504ca"},"cell_type":"code","source":"submission_df = sample_submission_df.copy()\nfor i in range(len(submission_df)):\n    submission_df.iloc[i, 1:] = predictions[0][i].tolist()\nsubmission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6af8c59393354b6e231deef153cc92034deb9b3b"},"cell_type":"code","source":"# Submission\nsubmission_df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf6cc847410fa44fd6c431ba8f0156e5e5dfa28b"},"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}