{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# fastai training with resnet34\nfastai is a great tool to create a strong baseline quickly. My idea is to convert the numpy files to images and train a model with an imagenet-pretrained model. Let's see if this approach gets us anywhere :) \n\n### I will smile for every upvote :) ","metadata":{"papermill":{"duration":0.052055,"end_time":"2021-02-24T04:34:38.361345","exception":false,"start_time":"2021-02-24T04:34:38.30929","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom fastai.vision.all import *\nimport pickle\nimport os\nimport torch","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":4.169069,"end_time":"2021-02-24T04:35:14.564093","exception":false,"start_time":"2021-02-24T04:35:10.395024","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-01T10:11:57.450982Z","iopub.execute_input":"2021-07-01T10:11:57.451323Z","iopub.status.idle":"2021-07-01T10:11:58.535282Z","shell.execute_reply.started":"2021-07-01T10:11:57.451253Z","shell.execute_reply":"2021-07-01T10:11:58.534323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/g2net-gravitational-wave-detection/training_labels.csv')\ntest_df = pd.read_csv('../input/g2net-gravitational-wave-detection/sample_submission.csv')","metadata":{"papermill":{"duration":0.519139,"end_time":"2021-02-24T04:35:18.946309","exception":false,"start_time":"2021-02-24T04:35:18.42717","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-01T10:11:58.537267Z","iopub.execute_input":"2021-07-01T10:11:58.537613Z","iopub.status.idle":"2021-07-01T10:11:59.021785Z","shell.execute_reply.started":"2021-07-01T10:11:58.537585Z","shell.execute_reply":"2021-07-01T10:11:59.020839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.sample(frac=1).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:11:59.025368Z","iopub.execute_input":"2021-07-01T10:11:59.025711Z","iopub.status.idle":"2021-07-01T10:11:59.07546Z","shell.execute_reply.started":"2021-07-01T10:11:59.025679Z","shell.execute_reply":"2021-07-01T10:11:59.074545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def id2path(id, is_test):\n    a, b, c = id[0], id[1], id[2]\n    if is_test: return f'../input/g2net-gravitational-wave-detection/test/{a}/{b}/{c}/{id}.npy'\n    return f'../input/g2net-gravitational-wave-detection/train/{a}/{b}/{c}/{id}.npy'","metadata":{"papermill":{"duration":4.157218,"end_time":"2021-02-24T04:35:23.420229","exception":false,"start_time":"2021-02-24T04:35:19.263011","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-01T10:11:59.076899Z","iopub.execute_input":"2021-07-01T10:11:59.077292Z","iopub.status.idle":"2021-07-01T10:11:59.083254Z","shell.execute_reply.started":"2021-07-01T10:11:59.07725Z","shell.execute_reply":"2021-07-01T10:11:59.082008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def id2img(id, is_test):\n    fname = id2path(id, is_test)\n    img = np.load(fname).reshape(3,64,64).transpose(1,2,0)\n    img = (img - img.min()) / (img.max() - img.min())\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:11:59.084797Z","iopub.execute_input":"2021-07-01T10:11:59.085296Z","iopub.status.idle":"2021-07-01T10:11:59.094322Z","shell.execute_reply.started":"2021-07-01T10:11:59.085251Z","shell.execute_reply":"2021-07-01T10:11:59.093395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class NumpyDataset(torch.utils.data.Dataset):\n    def __init__(self, df, is_test=False):\n        self.df,self.is_test = df,is_test\n        \n    def __getitem__(self, i):\n        image_id = self.df['id'].loc[i]\n        img = id2img(image_id, self.is_test).transpose(2,0,1)\n        if self.is_test:\n            tgt = 0 if i < 10 else 1\n            return (torch.tensor(img, dtype=torch.float), torch.tensor(tgt, dtype=torch.long))\n        else:\n            tgt = self.df['target'].loc[i]\n            return (torch.tensor(img, dtype=torch.float), torch.tensor(tgt, dtype=torch.long))\n    \n    def __len__(self): return len(self.df)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:11:59.095796Z","iopub.execute_input":"2021-07-01T10:11:59.096202Z","iopub.status.idle":"2021-07-01T10:11:59.108369Z","shell.execute_reply.started":"2021-07-01T10:11:59.096161Z","shell.execute_reply":"2021-07-01T10:11:59.107307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cut = int(0.8 * len(train))\ntrain_df = train[:cut].reset_index(drop=True)\nvalid_df = train[cut:].reset_index(drop=True)\nlen(train_df), len(valid_df)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:11:59.109922Z","iopub.execute_input":"2021-07-01T10:11:59.110273Z","iopub.status.idle":"2021-07-01T10:11:59.124332Z","shell.execute_reply.started":"2021-07-01T10:11:59.110229Z","shell.execute_reply":"2021-07-01T10:11:59.123253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = NumpyDataset(train_df, is_test=False)\nvalid_ds = NumpyDataset(train_df, is_test=False)\ntest_ds = NumpyDataset(test_df, is_test=True)","metadata":{"papermill":{"duration":0.28233,"end_time":"2021-02-24T04:35:26.014346","exception":false,"start_time":"2021-02-24T04:35:25.732016","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-01T10:11:59.128303Z","iopub.execute_input":"2021-07-01T10:11:59.128602Z","iopub.status.idle":"2021-07-01T10:11:59.133278Z","shell.execute_reply.started":"2021-07-01T10:11:59.128573Z","shell.execute_reply":"2021-07-01T10:11:59.132011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = DataLoaders.from_dsets(train_ds, valid_ds)\ndls.c = 1","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:11:59.135892Z","iopub.execute_input":"2021-07-01T10:11:59.136682Z","iopub.status.idle":"2021-07-01T10:11:59.145229Z","shell.execute_reply.started":"2021-07-01T10:11:59.136475Z","shell.execute_reply":"2021-07-01T10:11:59.144285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, loss_func=BCEWithLogitsLossFlat(), metrics=RocAucBinary())","metadata":{"papermill":{"duration":5.522109,"end_time":"2021-02-24T04:35:31.85855","exception":false,"start_time":"2021-02-24T04:35:26.336441","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-01T10:11:59.149006Z","iopub.execute_input":"2021-07-01T10:11:59.149403Z","iopub.status.idle":"2021-07-01T10:12:01.884918Z","shell.execute_reply.started":"2021-07-01T10:11:59.149341Z","shell.execute_reply":"2021-07-01T10:12:01.88404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(1, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:12:01.88627Z","iopub.execute_input":"2021-07-01T10:12:01.886623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('model')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"test_dl = DataLoader(test_ds, bs=256, shuffle=False, drop_last=False)\npreds, _ = learn.get_preds(dl=test_dl)\ntest_df.target = np.array(preds)\ntest_df.to_csv('submission.csv', index=False)\ntest_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}