{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from fastai.conv_learner import *\nfrom fastai.dataset import *\n\nimport pandas as pd\nimport numpy as np\nimport os\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"MODEL_PATH = 'Dn121_v1'\nTRAIN = '../input/train/'\nTEST = '../input/test/'\nLABELS = '../input/train_labels.csv'\nSAMPLE_SUB = '../input/sample_submission.csv'\nORG_SIZE=96\nBATCH_SIZE = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59006858ecebd7f784c67780010ad30f5f21e316"},"cell_type":"code","source":"arch = dn121 \nnw = 4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f441096b8c7a4b864e743511574dc64a157d0170"},"cell_type":"code","source":"train_df = pd.read_csv(LABELS).set_index('id')\ntrain_names = train_df.index.values\ntrain_labels = np.asarray(train_df['label'].values)\nprint(\"Number of positive samples = {:.4f}%\".format(np.count_nonzero(train_labels)*100/len(train_labels)))\ntest_names = [f.replace(\".tif\",\"\") for f in os.listdir(TEST)]\ntr_n, val_n = train_test_split(train_names, test_size=0.15, random_state=42069)\nprint(len(tr_n), len(val_n))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8dd731dc8f3dcb028b0666c107cd0f57d52b1a42"},"cell_type":"code","source":"class HCDDataset(FilesDataset):\n    def __init__(self, fnames, path, transform):\n        self.train_df = train_df\n        super().__init__(fnames, transform, path)\n\n    def get_x(self, i):\n        img = open_image(os.path.join(self.path, self.fnames[i]+\".tif\"))\n        # We crop the center of the original image for faster training time\n        img = img[(ORG_SIZE-self.sz)//2:(ORG_SIZE+self.sz)//2,(ORG_SIZE-self.sz)//2:(ORG_SIZE+self.sz)//2,:]\n        return img\n\n    def get_y(self, i):\n        if (self.path == TEST): return 0\n        return self.train_df.loc[self.fnames[i]]['label']\n\n\n    def get_c(self):\n        return 2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"79b6a24c19170be3fcc4bab40526cacde3e69186"},"cell_type":"code","source":"def get_data(sz, bs):\n    aug_tfms = [RandomRotate(45, p=0.75, mode=cv2.BORDER_REFLECT),\n            RandomDihedral(),\n            RandomZoom(zoom_max=.25),\n            RandomLighting(0.15, 0.15),\n            RandomStretch(max_stretch=0.5),\n            RandomFlip()]\n    tfms = tfms_from_model(arch, sz, crop_type=CropType.NO, tfm_y=TfmType.NO,\n                           aug_tfms=aug_tfms)\n    ds = ImageData.get_ds(HCDDataset, (tr_n[:-(len(tr_n) % bs)], TRAIN),\n                          (val_n, TRAIN), tfms, test=(test_names, TEST))\n    md = ImageData(\"./\", ds, bs, num_workers=nw, classes=None)\n    return md","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"842a323dc5da24095c894c41a976fefbdda4aa69"},"cell_type":"code","source":"md = get_data(96, BATCH_SIZE)\nlearn = ConvLearner.pretrained(arch, md,precompute=False) \nlearn.opt_fn = optim.Adam","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"165fe15a2167cde1bed377933223593aaca53663"},"cell_type":"code","source":"learn.lr_find()\nlearn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"49aafa70460de6aa7f49cb5c68341c8d48153c42"},"cell_type":"code","source":"lr = 1e-5\nlearn.fit(lr, 5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e2dfd08bb0b48bb680e7c5349458680833b8b0f"},"cell_type":"code","source":"learn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cc350a50f3a2735b7023af014f9f901dadf976f"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cc350a50f3a2735b7023af014f9f901dadf976f"},"cell_type":"code","source":"lrs = np.array([1e-4, 5e-4, 1.2e-3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cc350a50f3a2735b7023af014f9f901dadf976f"},"cell_type":"code","source":"learn.fit(lrs, 1, cycle_len=6, use_clr_beta = (10,10,0.95,0.85))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f809bc37f0c8efca4df9941f75e695e2eb279b12"},"cell_type":"code","source":"learn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cc350a50f3a2735b7023af014f9f901dadf976f"},"cell_type":"code","source":"learn.fit(lrs/4, 1, cycle_len=6, use_clr_beta = (10,10,0.95,0.85))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5662dc32d23c2e67530d607bb61ca7a0630d2fb"},"cell_type":"code","source":"learn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0ebd0e7bddbe79c9238d1ffd61d4d922d329e4b"},"cell_type":"code","source":"preds_t,y_t = learn.TTA(is_test=True, n_aug=8)\npreds_t = np.stack(preds_t, axis=-1)\npreds_t = np.exp(preds_t)\npreds_t = preds_t.mean(axis=-1)[:,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cc1db5a3775b7bc151de1dab6ee6798b6b1ae030"},"cell_type":"code","source":"sample_df = pd.read_csv(SAMPLE_SUB)\nsample_list = list(sample_df.id)\npred_list = [p for p in preds_t]\npred_dic = dict((key, value) for (key, value) in zip(learn.data.test_ds.fnames,pred_list))\npred_list_cor = [pred_dic[id] for id in sample_list]\ndf = pd.DataFrame({'id':sample_list,'label':pred_list_cor})\ndf.to_csv('Fast_AI.csv'.format(MODEL_PATH), header=True, index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0744b30e0ae35aa4754c1fe5bdbb8467a118b6ef"},"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}