{"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":"## What is this?\nAttempt ot create a baseline model with data from [here](https://www.kaggle.com/thedrcat/g2net-train-images-with-gpwy-sample/)\nand [here](https://www.kaggle.com/thedrcat/g2net-test-imgs-with-gwpy/) ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-08-18T12:52:12.090135Z","iopub.execute_input":"2021-08-18T12:52:12.090524Z","iopub.status.idle":"2021-08-18T12:52:12.131533Z","shell.execute_reply.started":"2021-08-18T12:52:12.090437Z","shell.execute_reply":"2021-08-18T12:52:12.130800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install spacy==3.1.1","metadata":{"execution":{"iopub.status.busy":"2021-08-18T12:52:12.145812Z","iopub.execute_input":"2021-08-18T12:52:12.146079Z","iopub.status.idle":"2021-08-18T12:52:37.926148Z","shell.execute_reply.started":"2021-08-18T12:52:12.146053Z","shell.execute_reply":"2021-08-18T12:52:37.925102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!yes Y|conda install -c fastai fastai=2.4.1\n!pip install fastai==2.4.1","metadata":{"execution":{"iopub.status.busy":"2021-08-18T12:52:37.931302Z","iopub.execute_input":"2021-08-18T12:52:37.933594Z","iopub.status.idle":"2021-08-18T12:54:00.846401Z","shell.execute_reply.started":"2021-08-18T12:52:37.933550Z","shell.execute_reply":"2021-08-18T12:54:00.845398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pandas.core.frame import DataFrame\nfrom fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2021-08-18T12:54:00.848400Z","iopub.execute_input":"2021-08-18T12:54:00.848770Z","iopub.status.idle":"2021-08-18T12:54:02.567020Z","shell.execute_reply.started":"2021-08-18T12:54:00.848715Z","shell.execute_reply":"2021-08-18T12:54:02.566152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get the files","metadata":{}},{"cell_type":"code","source":"path = Path(\"../input\")","metadata":{"execution":{"iopub.status.busy":"2021-08-18T12:54:02.568489Z","iopub.execute_input":"2021-08-18T12:54:02.568813Z","iopub.status.idle":"2021-08-18T12:54:02.604275Z","shell.execute_reply.started":"2021-08-18T12:54:02.568778Z","shell.execute_reply":"2021-08-18T12:54:02.603230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## getfiles(path: Path, ext)","metadata":{}},{"cell_type":"code","source":"def getfiles(path: Path, ext):\n    \"Get numpy files in `path` recursively, only in `folders`, if specified.\"\n    return L(path.glob(f'**/*.{ext}'))","metadata":{"execution":{"iopub.status.busy":"2021-08-18T12:54:02.606498Z","iopub.execute_input":"2021-08-18T12:54:02.606761Z","iopub.status.idle":"2021-08-18T12:54:02.850364Z","shell.execute_reply.started":"2021-08-18T12:54:02.606716Z","shell.execute_reply":"2021-08-18T12:54:02.849402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfiles = getfiles(path/'g2net-train-images-with-gpwy-sample', 'png')","metadata":{"execution":{"iopub.status.busy":"2021-08-18T12:54:02.851852Z","iopub.execute_input":"2021-08-18T12:54:02.852377Z","iopub.status.idle":"2021-08-18T13:03:58.542260Z","shell.execute_reply.started":"2021-08-18T12:54:02.852334Z","shell.execute_reply":"2021-08-18T13:03:58.541424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(files)","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:58.543602Z","iopub.execute_input":"2021-08-18T13:03:58.543962Z","iopub.status.idle":"2021-08-18T13:03:58.583062Z","shell.execute_reply.started":"2021-08-18T13:03:58.543924Z","shell.execute_reply":"2021-08-18T13:03:58.582318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files[-1]","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:58.585896Z","iopub.execute_input":"2021-08-18T13:03:58.586265Z","iopub.status.idle":"2021-08-18T13:03:58.623425Z","shell.execute_reply.started":"2021-08-18T13:03:58.586227Z","shell.execute_reply":"2021-08-18T13:03:58.622524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files[-1].name.replace(\".png\", \"\")","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:58.625599Z","iopub.execute_input":"2021-08-18T13:03:58.626051Z","iopub.status.idle":"2021-08-18T13:03:58.662597Z","shell.execute_reply.started":"2021-08-18T13:03:58.626013Z","shell.execute_reply":"2021-08-18T13:03:58.661832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get labels","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(path/'g2net-gravitational-wave-detection/training_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:58.663856Z","iopub.execute_input":"2021-08-18T13:03:58.664330Z","iopub.status.idle":"2021-08-18T13:03:59.025621Z","shell.execute_reply.started":"2021-08-18T13:03:58.664292Z","shell.execute_reply":"2021-08-18T13:03:59.024786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(1)","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:59.028406Z","iopub.execute_input":"2021-08-18T13:03:59.028667Z","iopub.status.idle":"2021-08-18T13:03:59.081304Z","shell.execute_reply.started":"2021-08-18T13:03:59.028642Z","shell.execute_reply":"2021-08-18T13:03:59.080503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_df = df.loc[df.id. isin([f.name.replace(\".png\", \"\") for f in files])]","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:59.082574Z","iopub.execute_input":"2021-08-18T13:03:59.082948Z","iopub.status.idle":"2021-08-18T13:03:59.501679Z","shell.execute_reply.started":"2021-08-18T13:03:59.082909Z","shell.execute_reply":"2021-08-18T13:03:59.500848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_df.head(1)","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:59.503016Z","iopub.execute_input":"2021-08-18T13:03:59.503364Z","iopub.status.idle":"2021-08-18T13:03:59.544708Z","shell.execute_reply.started":"2021-08-18T13:03:59.503327Z","shell.execute_reply":"2021-08-18T13:03:59.543841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating the dataloader","metadata":{}},{"cell_type":"code","source":"filtered_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:59.546220Z","iopub.execute_input":"2021-08-18T13:03:59.546564Z","iopub.status.idle":"2021-08-18T13:03:59.583887Z","shell.execute_reply.started":"2021-08-18T13:03:59.546529Z","shell.execute_reply":"2021-08-18T13:03:59.582911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = filtered_df[:20000]","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:59.585375Z","iopub.execute_input":"2021-08-18T13:03:59.585848Z","iopub.status.idle":"2021-08-18T13:03:59.620673Z","shell.execute_reply.started":"2021-08-18T13:03:59.585799Z","shell.execute_reply":"2021-08-18T13:03:59.619824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = path/'g2net-train-images-with-gpwy-sample/kaggle/tmp/train'\n\nds = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                get_x=ColReader('id', pref=train_path, suff='.png'),\n                splitter=TrainTestSplitter(),\n                get_y=ColReader('target'),\n                item_tfms=Resize(460),\n                batch_tfms=aug_transforms(size=224, flip_vert=True,\n                                          max_lighting=0.1, max_zoom=1.05, \n                                          max_warp=0.))","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:59.622048Z","iopub.execute_input":"2021-08-18T13:03:59.622499Z","iopub.status.idle":"2021-08-18T13:03:59.660585Z","shell.execute_reply.started":"2021-08-18T13:03:59.622463Z","shell.execute_reply":"2021-08-18T13:03:59.659782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds.summary(filtered_df)","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:03:59.661966Z","iopub.execute_input":"2021-08-18T13:03:59.662543Z","iopub.status.idle":"2021-08-18T13:04:39.110415Z","shell.execute_reply.started":"2021-08-18T13:03:59.662505Z","shell.execute_reply":"2021-08-18T13:04:39.109452Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ds.dataloaders(filtered_df, bs=256)","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:04:39.111765Z","iopub.execute_input":"2021-08-18T13:04:39.112127Z","iopub.status.idle":"2021-08-18T13:04:40.576305Z","shell.execute_reply.started":"2021-08-18T13:04:39.112087Z","shell.execute_reply":"2021-08-18T13:04:40.575452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:04:40.577476Z","iopub.execute_input":"2021-08-18T13:04:40.577833Z","iopub.status.idle":"2021-08-18T13:04:45.443801Z","shell.execute_reply.started":"2021-08-18T13:04:40.577799Z","shell.execute_reply":"2021-08-18T13:04:45.443007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How does a simple model like resnet would perform?","metadata":{}},{"cell_type":"markdown","source":"## The Learner","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34,\n                    loss_func=CrossEntropyLossFlat(), metrics=[RocAucBinary(axis=0)]).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:04:45.444900Z","iopub.execute_input":"2021-08-18T13:04:45.445223Z","iopub.status.idle":"2021-08-18T13:04:51.949355Z","shell.execute_reply.started":"2021-08-18T13:04:45.445190Z","shell.execute_reply":"2021-08-18T13:04:51.948474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train","metadata":{}},{"cell_type":"code","source":"def show_me_lrs(learn):\n    suggestions = namedtuple('Suggestions', [\"min\", \"steep\",\n                                            \"valley\", \"slide\"])\n    lr_min, lr_steep,lr_valley, lr_slide = learn.lr_find(suggest_funcs=(minimum, steep,valley, slide))\n    suggested_lrs = suggestions(lr_min, lr_steep, lr_valley, lr_slide)\n    \n    print(f\"Minimum/10:\\t{lr_min:.2e}\\\n          \\nSteepest point:\\t{lr_steep:.2e}\\\n          \\nLongest valley:\\t{lr_valley:.2e}\\\n          \\nSlide interval:\\t{lr_slide:.2e}\")\n    \n    return suggested_lrs","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:04:51.950774Z","iopub.execute_input":"2021-08-18T13:04:51.951283Z","iopub.status.idle":"2021-08-18T13:04:51.988876Z","shell.execute_reply.started":"2021-08-18T13:04:51.951244Z","shell.execute_reply":"2021-08-18T13:04:51.987971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(2, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2021-08-18T13:04:51.990072Z","iopub.execute_input":"2021-08-18T13:04:51.990424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"suggested_lrs = show_me_lrs(learn)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(5, lr_max=slice(suggested_lrs.slide/10, suggested_lrs.slide))","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":"## Save trained model","metadata":{}},{"cell_type":"code","source":"learn.export(\"./final\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}