{"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":{"trusted":true,"_uuid":"e899e0851a6e8c351b4f40dd54f25c19fe9600ca"},"cell_type":"code","source":"from os.path import join, exists, expanduser\nfrom os import listdir, makedirs\n\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":"a8b85f880d9019e7a5a5d02c5f1143bc086b73f7"},"cell_type":"code","source":"!cp ../input/fastai-model-48/stage3-sm.pth /tmp/.torch/models/stage3-sm.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bbcbe37f7ca89d0b0629173ff684862e71fa7fdc"},"cell_type":"code","source":"# os.mkdir('./working')\nprint(os.listdir(\"../working\"))","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 error_rate","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94e4372a88f062332434e9c68322e402e168635f"},"cell_type":"code","source":"from pathlib import Path\npath = Path('../input/histopathologic-cancer-detection');\npath = Path('.');\n\npath.ls()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"79c44899a16dbce3c540286bec58427f2b6da3f7"},"cell_type":"code","source":"df = pd.read_csv(path/'../input/histopathologic-cancer-detection/train_labels.csv'); df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4bf00b70d1295aa22531a1ec5e3f148733311c9e"},"cell_type":"code","source":"img_path = path/'../input/histopathologic-cancer-detection/train'/df.iloc[0].id\nimg = img_path.with_suffix('.tif')\nopen_image(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f47904c30922520804511751d170b9116770445"},"cell_type":"code","source":"from PIL import Image\ndf_sample = df.copy()\ndf_sample = df.sample(n=10)\nfor index, row in df_sample.iterrows():\n    file = '../input/histopathologic-cancer-detection/train/'+row.id+'.tif'\n    image = Image.open(file)\n    print(image.size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3fc5db038b1c96177ae69f547165ff0348a36ac4","scrolled":true},"cell_type":"code","source":"src = (ImageItemList.from_csv(path/'../input/histopathologic-cancer-detection',csv_name='train_labels.csv', folder='train',suffix='.tif')\n       .random_split_by_pct(0.2, seed=42)\n       .label_from_df()\n       .add_test_folder()\n      )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5fad33dc2d6755111f1d975ea6d958ba72b3f2c1"},"cell_type":"code","source":"tfms = get_transforms()\ndata = (src.transform(tfms, size=48)\n        .databunch()\n       .normalize(imagenet_stats))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a80605722102de5ffdc247fdcb108c9799dd1296"},"cell_type":"code","source":"data.show_batch(rows=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a689f054adf8065b657105261a5be568c3797d0b"},"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\ndef auc_score(y_pred,y_true,tens=True):\n    score = roc_auc_score(y_true,torch.sigmoid(y_pred)[:,1])\n    if tens:\n        score = tensor(score)\n    return score\n\nlearn = create_cnn(arch=models.resnet34, data=data, metrics=[error_rate,auc_score], model_dir='/tmp/.torch/models')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04f59878ff007b471e736bf7bf007853995f79af"},"cell_type":"code","source":"lr=0.001","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c92d662f16e9387c3a9a81a712ffbc71a87b909"},"cell_type":"code","source":"learn.fit_one_cycle(1, slice(lr))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"549914051db6791496334c2c6e6419db37255bd1"},"cell_type":"code","source":"learn.save('stage1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a74695cbee8979a7666d8b3e1ef1d889a186ab85"},"cell_type":"code","source":"learn.load(\"stage1\")\nlearn.unfreeze()\nlearn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9d4570ce67db194ef1147c3c966a8146e4e92e8f"},"cell_type":"code","source":"learn.fit_one_cycle(1, slice(1e-6, lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4efcd2277dfbae7049eeb9b1ced1ab3170b8344b"},"cell_type":"code","source":"learn.save(\"stage2\")\nlearn.load(\"stage2\")\nlearn.unfreeze()\nlearn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"801825fc95f4257214eac6e1156549c8c39ac345"},"cell_type":"code","source":"learn.fit_one_cycle(1, slice(1e-5, lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"81e33859cdbb5f7fd9d4c455c50f985d646086f4"},"cell_type":"code","source":"learn.save(\"stage3\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d78bc886e19279a3fac8b0fffa2fd4cd6655602"},"cell_type":"code","source":"learn.load(\"stage3\")\nlearn.unfreeze()\nlearn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"12cba21471108b8d49673e959a508480898472d2"},"cell_type":"code","source":"learn.fit_one_cycle(3, slice(1e-6, lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"92265138b7f7ef087f969cd69cdc71ac56c18806"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e868ca6cce17063701844543defe209090fedba2"},"cell_type":"code","source":"learn.save(\"stage3-sm\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e846122223582b9067488104e1a594a4ec47cdb8"},"cell_type":"code","source":"learn.load(\"stage3-sm\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"57c94aa8d934489307658cf4db196ecfca80c641"},"cell_type":"code","source":"tfms = get_transforms()\ndata = (src.transform(tfms, size = 96)\n       .databunch().normalize(imagenet_stats))\nlearn.data = data ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f48ee765ea66e2db16265f31feb2180038c191ea"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb64bbdf34bd69b8c52091e6ca56ab940a9f4d7b"},"cell_type":"code","source":"learn.fit_one_cycle(1, slice(1e-3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0fa0585de3fd5345dfcaf61fdd069e16ac8f3bb"},"cell_type":"code","source":"# learn.unfreeze()\nlearn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8208319eb2609e699e8fc36842222aa4ea04c652"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(4, slice(1e-6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d67d185748d44a6c6e5c2182fc87604fba2570f0"},"cell_type":"code","source":"learn.save('stage3-lg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7e9a0ef110bd96d790d62cf2f288ea60808f1c0"},"cell_type":"code","source":"# learn.fit_one_cycle(4, slice(1e-5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9d44e32ee78debfb762e20b2ca956425b9905a7"},"cell_type":"code","source":"# learn.unfreeze()\n# learn.lr_find()\n# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5691e60f9d4e0a88d6380b45fef89ed814f4cabb"},"cell_type":"code","source":"# learn.fit_one_cycle(4, slice(1e-6, lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa80027edb7a5fffc78a038ddaf4fa3f96d9cfac"},"cell_type":"code","source":"# learn.save(\"stage4-lg\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"054daa0e991e5f2e6446eace934b0fda01971263"},"cell_type":"code","source":"# pred_score = auc_score(preds,y).item()\n# print('The validation AUC is {}.'.format(pred_score))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"43eb65915e2149e5c66709c877b8b7cb3944bc64"},"cell_type":"code","source":"# submissions = pd.read_csv('../input/histopathologic-cancer-detection/sample_submission.csv')\n# id_list = list(submissions.id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74d10fa05292e2f95589ce05dfdf4fda31289d03"},"cell_type":"code","source":"# preds,y = learn.TTA(ds_type=DatasetType.Test)\n# pred_list = list(preds[:,1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"262761b80aadcf7db777179e1ccc86f3574da37f"},"cell_type":"code","source":"# pred_dict = dict((key, value.item()) for (key, value) in zip(learn.data.test_ds.items,pred_list))\n# pred_ordered = [pred_dict[Path('../input/histopathologic-cancer-detection/test/' + id + '.tif')] for id in id_list]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac71a35d0695463be6df14c821d0f788f29d129a"},"cell_type":"code","source":"# submissions = pd.DataFrame({'id':id_list,'label':pred_ordered})\n# submissions.to_csv(\"../working/my_submission_{}.csv\".format(pred_score),index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"daddb82b952dbb327f055e39773a4a362492f2d4"},"cell_type":"code","source":"# submissions.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0842256d6fe8f4cc09d738d2c5d3653c0ce43464"},"cell_type":"code","source":"# from IPython.display import HTML\n# import pandas as pd\n# import numpy as np\n# import base64\n\n# # function that takes in a dataframe and creates a text link to  \n# # download it (will only work for files < 2MB or so)\n# def create_download_link(df, title = \"Download CSV file\", filename = \"data.csv\"):  \n#     csv = df.to_csv()\n#     b64 = base64.b64encode(csv.encode())\n#     payload = b64.decode()\n#     html = '<a download=\"{filename}\" href=\"data:text/csv;base64,{payload}\" target=\"_blank\">{title}</a>'\n#     html = html.format(payload=payload,title=title,filename=filename)\n#     return HTML(html)\n\n# # create a random sample dataframe\n# df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))\n\n# # create a link to download the dataframe\n# # create_download_link(df[])\n\n# # ↓ ↓ ↓  Yay, download link! ↓ ↓ ↓ ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8072c15e3b7bf901fd3c8eb85a5430c4a16ebf33"},"cell_type":"code","source":"# df_split = np.array_split(submissions, 4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"05c6a506827ebeab8b79bf74e87e409107b564ef"},"cell_type":"code","source":"# create_download_link(submissions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b2a07d7cf85107ea0114265c34d98ab59c89c4c2"},"cell_type":"code","source":"# len(df_split)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b198f0f7a4cc6bd1543d95702e1c7ea52c2e72a6"},"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}