{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from fastai.tabular import *\nfrom fastai.callbacks import ReduceLROnPlateauCallback, EarlyStoppingCallback, SaveModelCallback\nfrom sklearn.metrics import roc_auc_score\nimport gc","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"dense161 = pd.read_csv(\"../input/cancer-densenet161-v2-for-ensemble/validation_0.976066529750824.csv\")\ndense161_test = pd.read_csv(\"../input/cancer-densenet161-v2-for-ensemble/submission_0.976066529750824.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9af48639e78a91e2ba898b36371219bb21520a0d"},"cell_type":"code","source":"dense201 = pd.read_csv(\"../input/cancer-densenet201-v2-for-ensemble/validation_0.9749373197555542.csv\")\ndense201_test = pd.read_csv(\"../input/cancer-densenet201-v2-for-ensemble/submission_0.9749373197555542.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e339585d89603c386952ef9016f3c43c9a14d62"},"cell_type":"code","source":"res50 = pd.read_csv(\"../input/cancer-resnet50-v2-for-ensemble/validation_0.9727705717086792.csv\")\nres50_test = pd.read_csv(\"../input/cancer-resnet50-v2-for-ensemble/submission_0.9727705717086792.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"199a7027c84d2223fcf38f4bbabf6052779148bb"},"cell_type":"code","source":"#trydf = pd.DataFrame({'dense161':dense161.ground_truth_label, \n#                      'dense201':dense201.ground_truth_label, \n#                      'res50':res50.ground_truth_label})\n#trydf['1and2'] = trydf.dense161==trydf.dense201\n#trydf['2and3'] = trydf.res50==trydf.dense201\n#trydf['1and2'].nunique() == 1\n#trydf['2and3'].nunique() == 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"655e7d57b5ae5dcc002103f19b4fa41878b74eaa"},"cell_type":"code","source":"def softmax_df(df, model_name, test=False):\n    if test:\n            df[model_name+'_0'] = np.exp(df['pred_0'])\n            df[model_name+'_1'] = np.exp(df['pred_1'])\n    else:\n        df[model_name+'_0'] = np.exp(df['val_0'])\n        df[model_name+'_1'] = np.exp(df['val_1'])\n    df[model_name+'sum'] = df[model_name+'_0'] + df[model_name+'_1']\n    df[model_name+'softmax'] = df[model_name+'_1'] / df[model_name+'sum']\n    return df[model_name+'softmax']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"21b800f63bc655d9bf411745b2a1fa69e7d6619f"},"cell_type":"code","source":"#dense161_sm = softmax_df(dense161, 'dense161')\n#dense201_sm = softmax_df(dense201, 'dense201')\n#res50_sm = softmax_df(res50, 'res50')\n#dense161_sm_test = softmax_df(dense161_test, 'dense161_test', True)\n#dense201_sm_test = softmax_df(dense201_test, 'dense201_test', True)\n#res50_sm_test = softmax_df(res50_test, 'res50_test', True)\n#train = pd.DataFrame({'dense161_sm':dense161_sm, \"dense201_sm\":dense201_sm, \"res50_sm\":res50_sm, \"y\":dense161.ground_truth_label})\n#test = pd.DataFrame({'dense161_sm':dense161_sm_test, \"dense201_sm\":dense201_sm_test, \"res50_sm\":res50_sm_test})\n#test.y=0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f95709582b14adc4b077797e24cef68a851b57bd"},"cell_type":"code","source":"dense161.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6019fb9c025e9406157718c64c0526d3c494aa32"},"cell_type":"code","source":"train = pd.DataFrame({'dense161_0':dense161.val_0, 'dense161_1':dense161.val_1, \n                      'dense201_0':dense201.val_0, 'dense201_1':dense201.val_1,\n                      'res50_0':res50.val_0, 'res50_1':res50.val_1,\n                      \"y\":dense161.ground_truth_label})\ntest = pd.DataFrame({'dense161_0':dense161_test.pred_0, 'dense161_1':dense161_test.pred_1, \n                      'dense201_0':dense201_test.pred_0, 'dense201_1':dense201_test.pred_1,\n                      'res50_0':res50_test.pred_0, 'res50_1':res50_test.pred_1})\ntest.y=0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1300fb85681f87a72ac321208114a29ae7f7f17a"},"cell_type":"code","source":"dep_var = 'y'\n#cont_names = ['dense161_sm','dense201_sm', 'res50_sm']\ncont_names = ['dense161_0', 'dense161_1', 'dense201_0', 'dense201_1', 'res50_0','res50_1']\n\ndata = (TabularList.from_df(train, cont_names=cont_names)\n            .split_by_rand_pct(seed=47)\n            .label_from_df(cols=dep_var)\n            .add_test(TabularList.from_df(test, cont_names=cont_names))\n            .databunch())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a0added86fd182df8b2932c1d8657238798eda9"},"cell_type":"code","source":"def roc_score(inp, target):\n    _, indices = inp.max(1)\n    return torch.Tensor([roc_auc_score(target, indices)])[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d3362e0ea6537dff650a5ec91ef436b8cbd6904b"},"cell_type":"code","source":"learn = tabular_learner(data, layers=[10, 10, 10], metrics=[accuracy, roc_score],  ps=0.5, wd=1e-1, model_dir='./').to_fp16()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"25995ec3c1af9e1d1498a087f4012a9dd681a3c9"},"cell_type":"code","source":"#learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"249ae0c86988073b2811b3b7dc9f7cf129d75caf"},"cell_type":"code","source":"#learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d9950102737036f6546cfe97408421f2eb922752"},"cell_type":"code","source":"from fastai.callbacks import ReduceLROnPlateauCallback, EarlyStoppingCallback, SaveModelCallback\nES = EarlyStoppingCallback(learn, monitor='roc_score',patience = 5)\nRLR = ReduceLROnPlateauCallback(learn, monitor='roc_score',patience = 2)\nSAVEML = SaveModelCallback(learn, every='improvement', monitor='roc_score', name='best')\nlearn.fit_one_cycle(20, 1e-3, callbacks = [ES, RLR, SAVEML])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d21da543f27ad25adc7151d9185d295596c46d26"},"cell_type":"code","source":"learn.load('best')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0d919430a8485e228bc4cdae2e3a3d45f1a95bf"},"cell_type":"code","source":"preds, _ = learn.get_preds(DatasetType.Test)\npreds = torch.softmax(preds, dim=1)[:, 1].numpy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a393daf12d019c532225ec4a43c7bbb5e37f0743"},"cell_type":"code","source":"auc_val = learn.validate()[2].item()\nauc_val","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"753e1ac830b9755947a866457c086f804626df49"},"cell_type":"code","source":"sub = pd.read_csv(\"../input/histopathologic-cancer-detection/sample_submission.csv\")\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f0e297d7bb7e7572d4873b629697965069be6167"},"cell_type":"code","source":"sub['label'] = preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3be638d7f611dbe579379ad08e932fd5c1b0cf7e"},"cell_type":"code","source":"sub.to_csv(f'submission_{auc_val}.csv', header=True, index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb3de895990e381e1834eb963b7700ae5befe88b"},"cell_type":"code","source":"sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cc73749dc61423bb222771efa09da8d673df245a"},"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}