{"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\nimport glob\nimport matplotlib.pyplot as plt\nfrom fastai.vision.all import *\nimport librosa\nimport librosa.display\n\n%matplotlib inline\n\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-01T12:27:55.660852Z","iopub.execute_input":"2021-07-01T12:27:55.661259Z","iopub.status.idle":"2021-07-01T12:27:59.328573Z","shell.execute_reply.started":"2021-07-01T12:27:55.661155Z","shell.execute_reply":"2021-07-01T12:27:59.327719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/g2net-gravitational-wave-detection/training_labels.csv')\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:47:38.332452Z","iopub.execute_input":"2021-07-01T12:47:38.332821Z","iopub.status.idle":"2021-07-01T12:47:38.683075Z","shell.execute_reply.started":"2021-07-01T12:47:38.332791Z","shell.execute_reply":"2021-07-01T12:47:38.682138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:27:59.86732Z","iopub.execute_input":"2021-07-01T12:27:59.867697Z","iopub.status.idle":"2021-07-01T12:27:59.882755Z","shell.execute_reply.started":"2021-07-01T12:27:59.867658Z","shell.execute_reply":"2021-07-01T12:27:59.88179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss = pd.read_csv('../input/g2net-gravitational-wave-detection/sample_submission.csv')\nprint(ss.shape)\nss.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:27:59.884316Z","iopub.execute_input":"2021-07-01T12:27:59.884694Z","iopub.status.idle":"2021-07-01T12:28:00.0484Z","shell.execute_reply.started":"2021-07-01T12:27:59.884657Z","shell.execute_reply":"2021-07-01T12:28:00.047493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fns = glob.glob('../input/g2net-gravitational-wave-detection/train/*/*/*/*.npy',recursive=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:28:00.049633Z","iopub.execute_input":"2021-07-01T12:28:00.049939Z","iopub.status.idle":"2021-07-01T12:30:10.210375Z","shell.execute_reply.started":"2021-07-01T12:28:00.04991Z","shell.execute_reply":"2021-07-01T12:30:10.209487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_df = pd.DataFrame()\npath_df['path'] = fns\npath_df['id'] = path_df['path'].apply(lambda x: x.split('/')[-1].split('.')[0])\npath_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:30:10.211673Z","iopub.execute_input":"2021-07-01T12:30:10.21201Z","iopub.status.idle":"2021-07-01T12:30:10.844549Z","shell.execute_reply.started":"2021-07-01T12:30:10.211975Z","shell.execute_reply":"2021-07-01T12:30:10.84362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.merge(train,path_df,on='id')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:47:50.857999Z","iopub.execute_input":"2021-07-01T12:47:50.858311Z","iopub.status.idle":"2021-07-01T12:47:51.399725Z","shell.execute_reply.started":"2021-07-01T12:47:50.858281Z","shell.execute_reply":"2021-07-01T12:47:51.398747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train1 = train.copy(deep=True)\n\ntrain = train.sample(n=10000).reset_index(drop=True)\n\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:47:59.092414Z","iopub.execute_input":"2021-07-01T12:47:59.092791Z","iopub.status.idle":"2021-07-01T12:47:59.193665Z","shell.execute_reply.started":"2021-07-01T12:47:59.092756Z","shell.execute_reply":"2021-07-01T12:47:59.192646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pad_array(arr):\n    h,w = arr.shape\n    return np.pad(arr, [(0, 128-h), (0, 9-w)])","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:30:11.462108Z","iopub.execute_input":"2021-07-01T12:30:11.462533Z","iopub.status.idle":"2021-07-01T12:30:11.467732Z","shell.execute_reply.started":"2021-07-01T12:30:11.462498Z","shell.execute_reply":"2021-07-01T12:30:11.466739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_item(_):\n    return train.path.values\n\ndef get_x(fn):\n    arr = np.load(fn)\n    #spec1 = get_spec(arr[0,:])\n    #spec1 = pad_array(spec1)\n    #spec2 = get_spec(arr[1,:])\n    #spec2 = pad_array(spec2)\n    #spec3 = get_spec(arr[2,:])\n    #spec3 = pad_array(spec3)\n    \n    spec1 = generate_spec(arr[0,:])\n    spec2 = generate_spec(arr[1,:])\n    spec3 = generate_spec(arr[2,:])\n    \n    img = np.concatenate((spec1,spec2,spec3),axis=-1)\n        \n    return img\n\ndef get_y(fn):\n    return int(train.loc[train['path']==fn,'target'])\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:30:11.469564Z","iopub.execute_input":"2021-07-01T12:30:11.46999Z","iopub.status.idle":"2021-07-01T12:30:11.477624Z","shell.execute_reply.started":"2021-07-01T12:30:11.469954Z","shell.execute_reply":"2021-07-01T12:30:11.476871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_spec(arr):\n    y = arr\n    y = (y - y.min())/y.max()\n    S = librosa.feature.melspectrogram(y=y.reshape(-1).astype('float'),n_mels=128,fmax=8000)\n    S = librosa.power_to_db(S,ref=np.max)\n    S = np.abs(S)\n    S = (((S - S.min())/S.max())*255).astype(np.uint8)\n    return S\n","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:30:11.480038Z","iopub.execute_input":"2021-07-01T12:30:11.48039Z","iopub.status.idle":"2021-07-01T12:30:11.488142Z","shell.execute_reply.started":"2021-07-01T12:30:11.480355Z","shell.execute_reply":"2021-07-01T12:30:11.487322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(\n    blocks=(ImageBlock(cls=PILImageBW),CategoryBlock),\n    get_items=get_item,\n    get_x=get_x,\n    get_y=get_y, \n    splitter=RandomSplitter(valid_pct=0.20,seed=2021),\n    batch_tfms=[*aug_transforms(size=64)] # Resize\n)\n\n#dblock.summary('')","metadata":{"execution":{"iopub.status.busy":"2021-07-01T12:48:07.12928Z","iopub.execute_input":"2021-07-01T12:48:07.129611Z","iopub.status.idle":"2021-07-01T12:48:07.13537Z","shell.execute_reply.started":"2021-07-01T12:48:07.129565Z","shell.execute_reply":"2021-07-01T12:48:07.134377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = dblock.dataloaders(Path(''), bs=512)\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T13:26:17.666864Z","iopub.execute_input":"2021-07-01T13:26:17.667232Z","iopub.status.idle":"2021-07-01T13:26:31.704394Z","shell.execute_reply.started":"2021-07-01T13:26:17.667195Z","shell.execute_reply":"2021-07-01T13:26:31.703469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls,resnet18,metrics=[RocAucBinary(),accuracy])","metadata":{"execution":{"iopub.status.busy":"2021-07-01T13:26:38.56251Z","iopub.execute_input":"2021-07-01T13:26:38.562881Z","iopub.status.idle":"2021-07-01T13:26:38.911915Z","shell.execute_reply.started":"2021-07-01T13:26:38.562828Z","shell.execute_reply":"2021-07-01T13:26:38.910913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T13:26:45.015544Z","iopub.execute_input":"2021-07-01T13:26:45.015935Z"},"trusted":true},"execution_count":null,"outputs":[]}]}