{"cells":[{"metadata":{"trusted":true,"_uuid":"fa08d7bdb388c79378c37843c5ee1f39fe5fd199"},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom fastai import *\nfrom fastai.vision import *\nfrom torchvision.models import *\nimport os","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/train_labels.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e129919a183c7a82dbe0dfb30f100aae0f7bb17"},"cell_type":"code","source":"df_unique = pd.unique(df['label'])\ndf_unique","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b57c50b98e14747daf0daabc9f96b7ac39644ca7"},"cell_type":"code","source":"path = Path('../input')\npath.ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"34f1e1b438db6a10e22d57c4360d697e0198c322"},"cell_type":"code","source":"SZ = 96\nBS = 64\nNUM_WORKERS = 4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efdc87d64b52fb0f379adbb087513413536fb5c8"},"cell_type":"code","source":"data = (\n    ImageItemList\n        .from_csv(path, 'train_labels.csv', folder='train', suffix='.tif')\n        .random_split_by_pct()\n        .label_from_df()\n        .add_test(ImageItemList.from_folder('../input/test'))\n        .transform(get_transforms(do_flip=True, flip_vert=True, max_rotate=.0, max_zoom=.1,max_lighting=0.05, max_warp=0.), size=SZ, resize_method=ResizeMethod.SQUISH)\n        .databunch(bs=BS, num_workers=NUM_WORKERS, path = '.')\n        .normalize(imagenet_stats)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74bcdc768489a6b22198be5ba593d5a0831da5bd"},"cell_type":"code","source":"data.show_batch(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ad627b83add9dc1e70fabbee13bd9cf98896a73"},"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\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    else:\n        score=score\n    return score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"731a97091b14cdacd1ce854c3564f1cc52bc31c6"},"cell_type":"code","source":"learn = create_cnn(data, densenet161, metrics=[auc_score], ps=0.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f64a5db04073088926e04885d946ed7957f9a5a0"},"cell_type":"code","source":"# learn.lr_find()\n# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4bac0c407b10231181a126bded7308ec62a233ad"},"cell_type":"code","source":"learn.fit_one_cycle(1, 1e-3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da1be10a08175857f61e83b366e7c52aa3a9da6c","scrolled":true},"cell_type":"code","source":"learn.unfreeze()\n# learn.lr_find()\n# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1722861a9e14fa05cfa2d2a08e3e27fb19cd103a","scrolled":true},"cell_type":"code","source":"learn.fit_one_cycle(4, slice(1e-6, 1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03dc31efe8be3e91d24f586c17836422a4a697b0"},"cell_type":"code","source":"SZ = 96*2\nBS = 64//2\nNUM_WORKERS = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37f4b438fac9082ad7f4931c6abbc8314a3e585f"},"cell_type":"code","source":"data = (\n    ImageItemList\n        .from_csv(path, 'train_labels.csv', folder='train', suffix='.tif')\n        .random_split_by_pct()\n        .label_from_df()\n        .add_test(ImageItemList.from_folder('../input/test'))\n        .transform(get_transforms(do_flip=True, flip_vert=True, max_rotate=.0, max_zoom=.1,max_lighting=0.05, max_warp=0.), size=SZ, resize_method=ResizeMethod.SQUISH)\n        .databunch(bs=BS, num_workers=NUM_WORKERS, path = '.')\n        .normalize(imagenet_stats)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35da4a2dc10b017913ba26f62b8bcc8fabf68fe3"},"cell_type":"code","source":"learn.data = data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f269537a631fc3c36d9e5dd8ac653a206802c84"},"cell_type":"code","source":"# learn.lr_find()\n# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84d84bc333384b6296dd81de40831b3c9741feb6"},"cell_type":"code","source":"learn.freeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e078a636aae6d17c07cf8243bf323da38a803560"},"cell_type":"code","source":"learn.fit_one_cycle(1, 1e-3/2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec3c1bd14d27045a582d3a0c6513ac7fd95fd7f8"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0c6a30ec2f13f9d1202464a760f944c3a84f32ad"},"cell_type":"code","source":"learn.fit_one_cycle(1, slice(1e-6/2, 1e-4/2))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d270ba6c0168d71a4e26a6b4caad9e997162e2ad"},"cell_type":"markdown","source":"**Pred**"},{"metadata":{"trusted":true,"_uuid":"f1da5988e85a1f4aac7174f0b1c217b93c1c8e10"},"cell_type":"code","source":"preds = learn.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"95229447fa6fc16b39b9c94d33b52e907efea878"},"cell_type":"code","source":"df_sub = pd.read_csv('../input/sample_submission.csv').set_index('id')\ndf_sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2e777bcce6656970ff925b7e7356b4cb0b8e394"},"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":"8d58b76b542aa1fbffb94efc484cf37fec7f7c6c"},"cell_type":"code","source":"df_sub.loc[fname_cleaned,'label'] = preds[0][:,1].tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f862cc7d5672bd175aaf578aba8c349c3b96c4f"},"cell_type":"code","source":"df_sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af1747f954d39d93e5990af2402f46677ed8cf73"},"cell_type":"code","source":"df_sub.to_csv('sub.csv')","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}