{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-21T05:34:42.128893Z","iopub.execute_input":"2021-07-21T05:34:42.129327Z","iopub.status.idle":"2021-07-21T05:34:42.13388Z","shell.execute_reply.started":"2021-07-21T05:34:42.129274Z","shell.execute_reply":"2021-07-21T05:34:42.132818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom pathlib import Path\nfrom sklearn.model_selection import StratifiedKFold\nimport glob\n\ndef predict_batch(self, item, rm_type_tfms=None, with_input=False):\n    dl = self.dls.test_dl(item, rm_type_tfms=rm_type_tfms, num_workers=os.cpu_count())\n    ret = self.get_preds(dl=dl)\n    return ret\n\nLearner.predict_batch = predict_batch","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:34:43.171982Z","iopub.execute_input":"2021-07-21T05:34:43.172333Z","iopub.status.idle":"2021-07-21T05:34:46.043316Z","shell.execute_reply.started":"2021-07-21T05:34:43.17228Z","shell.execute_reply":"2021-07-21T05:34:46.042396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/d-li14/efficientnetv2.pytorch.git effnet","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:34:50.062338Z","iopub.execute_input":"2021-07-21T05:34:50.062706Z","iopub.status.idle":"2021-07-21T05:34:51.591448Z","shell.execute_reply.started":"2021-07-21T05:34:50.062676Z","shell.execute_reply":"2021-07-21T05:34:51.590449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/g2net-gravitational-wave-detection/training_labels.csv\")\ndf[\"id\"] = df[\"id\"] + \".png\"\n\nskf = StratifiedKFold(n_splits=5)\nt = df.target\n\nfor k, (train_index, test_index) in enumerate(skf.split(np.zeros(len(t)), t)):\n    train = df.loc[train_index]\n    test = df.loc[test_index]\n    \n    df_new = pd.DataFrame()\n    \n    train.reset_index(drop=True, inplace=True)\n    df_new = pd.concat([train, pd.DataFrame(np.array([False] * len(train)))], axis=1)\n    df_new.columns = [\"image\", \"labels\", \"is_valid\"]\n    \n    test.reset_index(drop=True, inplace=True)\n    test = pd.concat([test, pd.DataFrame(np.array([True] * len(test)))], axis=1)\n    test.columns = [\"image\", \"labels\", \"is_valid\"]\n    \n    df_new = pd.concat([df_new, test], axis=0)\n    df_new.sort_values(by=\"image\", inplace=True)\n    df_new.reset_index(drop=True, inplace=True)\n    \n    df_new.to_csv(f\"train_fold_{k}.csv\", header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:34:51.668395Z","iopub.execute_input":"2021-07-21T05:34:51.668758Z","iopub.status.idle":"2021-07-21T05:35:02.46411Z","shell.execute_reply.started":"2021-07-21T05:34:51.668723Z","shell.execute_reply":"2021-07-21T05:35:02.463027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:35:02.465811Z","iopub.execute_input":"2021-07-21T05:35:02.466182Z","iopub.status.idle":"2021-07-21T05:35:02.486342Z","shell.execute_reply.started":"2021-07-21T05:35:02.466141Z","shell.execute_reply":"2021-07-21T05:35:02.485469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nImage.open(\"../input/g2net-melspec-image/g2net_melspec/00000e74ad.png\")","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:39:23.047688Z","iopub.execute_input":"2021-07-21T05:39:23.048036Z","iopub.status.idle":"2021-07-21T05:39:23.066294Z","shell.execute_reply.started":"2021-07-21T05:39:23.048005Z","shell.execute_reply":"2021-07-21T05:39:23.065424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source = Path(\"../input/g2net-melspec-image/\")\n\ndatablock = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                     splitter=ColSplitter(),\n                     get_x=ColReader(0, pref=source/\"g2net_melspec\"),\n                     get_y=ColReader(1),\n                     item_tfms=Resize((56, 128)),\n                     batch_tfms=[\n                         IntToFloatTensor,\n                         Normalize.from_stats(*imagenet_stats)\n                     ])\n\ndf_final = pd.read_csv(\"train_fold_0.csv\")\ndls = datablock.dataloaders(df_final, bs=64, num_workers=os.cpu_count())\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:43:46.126192Z","iopub.execute_input":"2021-07-21T05:43:46.126564Z","iopub.status.idle":"2021-07-21T05:44:41.351528Z","shell.execute_reply.started":"2021-07-21T05:43:46.126528Z","shell.execute_reply":"2021-07-21T05:44:41.350691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch \nimport torch.nn as nn\nfrom effnet.effnetv2 import effnetv2_m\n\ncnn_model = effnetv2_m(num_classes=2)\n\ntry:\n    del learn\n    torch.cuda.empty_cache()\nexcept Exception:\n    pass\n\n# Skip using FP16 since P100 does not have Tensor Cores. \nlearn = Learner(dls, cnn_model, metrics=[accuracy, F1Score(), RocAucBinary()])\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:49:18.538219Z","iopub.execute_input":"2021-07-21T05:49:18.540765Z","iopub.status.idle":"2021-07-21T05:49:57.391261Z","shell.execute_reply.started":"2021-07-21T05:49:18.540724Z","shell.execute_reply":"2021-07-21T05:49:57.390404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(1, lr_max=3e-5)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:48:49.536301Z","iopub.execute_input":"2021-07-21T05:48:49.536631Z","iopub.status.idle":"2021-07-21T05:49:16.285318Z","shell.execute_reply.started":"2021-07-21T05:48:49.536599Z","shell.execute_reply":"2021-07-21T05:49:16.281797Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(2, slice(1e-5, 3e-5))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(2, slice(1e-6, 6e-6))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export(\"/kaggle/working/effnetv2_m_melspec.pkl\")","metadata":{},"execution_count":null,"outputs":[]}]}