{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\nfrom fastai.vision import *\nfrom fastai.metrics import *\nfrom fastai import *\nfrom os import *\n\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport random\nfrom sklearn.utils import shuffle","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb86f3d02014b6aadae039e6cb4ea9e0c6c04915"},"cell_type":"code","source":"model_path='.'\npath='../input/'\ntrain_folder=f'{path}train'\ntest_folder=f'{path}test'\ntrain_lbl=f'{path}train_labels.csv'\nORG_SIZE=96\n\nbs=64\nnum_workers=None # Apprently 2 cpus per kaggle node, so 4 threads I think\nsz=96","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"182c44c32d0ffe1446d650d46477da8e70be417b"},"cell_type":"code","source":"df_train=pd.read_csv(train_lbl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a776e17b649f740902a1ca9cbc4c59a17e6b8d2"},"cell_type":"code","source":"tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=.0, max_zoom=1.1,\n                      max_lighting=0.05, max_warp=0.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"172f46d7caa0f1e7ce5eaf7e99d7f51c2da2ae6d"},"cell_type":"code","source":"data = ImageDataBunch.from_csv(path,csv_labels=train_lbl,folder='train',valid_pct=0.2, ds_tfms=tfms, size=sz, suffix='.tif',test=test_folder,bs=bs);\nstats=data.batch_stats()        \ndata.normalize(stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"580cd55cbeac91e238d391dbd9b8daad5ae50e67"},"cell_type":"code","source":"data.show_batch(rows=5, figsize=(12,9))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a94c875786a5acc8f801ba5cd9e139465f829687"},"cell_type":"markdown","source":"## Model training (resnet34)"},{"metadata":{"trusted":true,"_uuid":"413f309920b28870bb98312b24194181437622a9"},"cell_type":"code","source":"from sklearn.metrics import roc_auc_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd8e537e896a0af82ddf8c050731b57f31e640bd"},"cell_type":"code","source":"def 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    else:\n        score=score\n    return score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08d74379a62fc8f8b4b596381d69443436ef5977"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet34, metrics=[auc_score], model_dir=\"/tmp/model/\", ps=0.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"698696bfaab0d835ef625956752f946f60a7392d"},"cell_type":"code","source":"learn.fit_one_cycle(1, 1e-3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15a1d6eeaefb069f05066ed7127a87838c1be702"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44627fdc744fd61a1d558e64e18c1e1a8d098cf4"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e2125710047e80a7adb5d138c2a9690a6771ad3"},"cell_type":"code","source":"learn.fit_one_cycle(5, slice(1e-4,1e-1)) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e6aca94b7ebf4567806e9692c3a57c2a4690d80"},"cell_type":"code","source":"learn.save('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3d90019072ba65371aedd9c42ee483984bd88be"},"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":"927a423e0f00146b3a7871f3d9259ab62e5728dc"},"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(10,10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"361edecd055d84cb94fb3892654f7341192b118b"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1948f61800c39d577af4b89ac0d500ee96e3bac6"},"cell_type":"code","source":"learn.fit_one_cycle(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4211dde474bfa1cda541d8f516362d152f2b4e03"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb8ff87fcb2ac4a78d14a7dd7116f0ea0162c0ca"},"cell_type":"code","source":"learn.fit_one_cycle(5, slice(5e-5,5e-3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73e827200e2d11c2086fab7ed12f6263038caa7d"},"cell_type":"code","source":"learn.save('stage-2')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8fdc50f33160e4447994b4f50acd3af4cf758b5d"},"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":"e41ddfb1b9826063b5f41c256e3d9b89072f267e"},"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(10,10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f0dcc62f375fe899b09a8e3fa94a57299d21fa7"},"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(15,5))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c94ee17baa9c72f075d190009b3984bc0bfb690d"},"cell_type":"markdown","source":"## Predict on test set and submission"},{"metadata":{"trusted":true,"_uuid":"d7e4010a69836cfb0e4a921e8978f0e47420c99d"},"cell_type":"code","source":"preds,y=learn.get_preds()\npred_score=auc_score(preds,y)\npred_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f6a848c4ab6b4aa4756284f6bd56a1efcd9305f9"},"cell_type":"code","source":"preds_test,y_test=learn.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6001df891ce13e3298c5cdb26d3ad953caad8c1c"},"cell_type":"code","source":"sub=pd.read_csv(f'{path}sample_submission.csv').set_index('id')\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"faeab7667f94ad5ed8a7bc38c3b4f6e6981d00e9"},"cell_type":"code","source":"clean_fname=np.vectorize(lambda fname: str(fname).split('/')[-1].split('.')[0])\nfname_cleaned=clean_fname(data.test_ds.items)\nfname_cleaned=fname_cleaned.astype(str)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5455663c419794e514413d6214c11ec18940120e"},"cell_type":"code","source":"sub.loc[fname_cleaned,'label']=to_np(preds_test[:,1])\nsub.to_csv(f'/kaggle/working/submission_{pred_score}.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"469dc7afef19149492adfec642ff08b6365ed792"},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}