{"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":"import sys,glob\nimport librosa\nimport cv2,os\nimport numpy as np # linear algebra\nimport pandas as pd\nimport seaborn as sns\nfrom pathlib import Path\nimport os,random\nfrom scipy import signal\nfrom scipy import optimize\nimport matplotlib.pyplot as plt\nimport gc\nimport matplotlib.image as immg\nfrom tqdm.notebook import tqdm\nimport albumentations\nimport torch\nimport torch.nn as nn\nfrom torch.nn import functional as F","metadata":{"execution":{"iopub.status.busy":"2021-09-06T09:59:58.162195Z","iopub.execute_input":"2021-09-06T09:59:58.162562Z","iopub.status.idle":"2021-09-06T09:59:58.169333Z","shell.execute_reply.started":"2021-09-06T09:59:58.162531Z","shell.execute_reply":"2021-09-06T09:59:58.167899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-09-06T09:59:59.231417Z","iopub.execute_input":"2021-09-06T09:59:59.231782Z","iopub.status.idle":"2021-09-06T09:59:59.236186Z","shell.execute_reply.started":"2021-09-06T09:59:59.231752Z","shell.execute_reply":"2021-09-06T09:59:59.235284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q nnAudio","metadata":{"execution":{"iopub.status.busy":"2021-09-06T09:59:59.481169Z","iopub.execute_input":"2021-09-06T09:59:59.481529Z","iopub.status.idle":"2021-09-06T10:00:08.716021Z","shell.execute_reply.started":"2021-09-06T09:59:59.481499Z","shell.execute_reply":"2021-09-06T10:00:08.714860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/g2net-gravitational-wave-detection/training_labels.csv')\ntest = pd.read_csv('../input/g2net-gravitational-wave-detection/sample_submission.csv')\n\ndef get_train_file_path(image_id):\n    return \"../input/g2net-gravitational-wave-detection/train/{}/{}/{}/{}.npy\".format(\n        image_id[0], image_id[1], image_id[2], image_id)\n\ndef get_test_file_path(image_id):\n    return \"../input/g2net-gravitational-wave-detection/test/{}/{}/{}/{}.npy\".format(\n        image_id[0], image_id[1], image_id[2], image_id)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:08.718085Z","iopub.execute_input":"2021-09-06T10:00:08.718429Z","iopub.status.idle":"2021-09-06T10:00:09.373749Z","shell.execute_reply.started":"2021-09-06T10:00:08.718392Z","shell.execute_reply":"2021-09-06T10:00:09.372802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['file_path'] = train['id'].apply(get_train_file_path)\ntest['file_path'] = test['id'].apply(get_test_file_path)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:09.375849Z","iopub.execute_input":"2021-09-06T10:00:09.376288Z","iopub.status.idle":"2021-09-06T10:00:10.113113Z","shell.execute_reply.started":"2021-09-06T10:00:09.376248Z","shell.execute_reply":"2021-09-06T10:00:10.112193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:10.114601Z","iopub.execute_input":"2021-09-06T10:00:10.114897Z","iopub.status.idle":"2021-09-06T10:00:10.135541Z","shell.execute_reply.started":"2021-09-06T10:00:10.114870Z","shell.execute_reply":"2021-09-06T10:00:10.134009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:10.136693Z","iopub.execute_input":"2021-09-06T10:00:10.136962Z","iopub.status.idle":"2021-09-06T10:00:10.152256Z","shell.execute_reply.started":"2021-09-06T10:00:10.136936Z","shell.execute_reply":"2021-09-06T10:00:10.151314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def apply_bandpass(x, lf=25, hf=500, order=4, sr=2048):\n    sos = signal.butter(order, [lf, hf], btype=\"bandpass\", output=\"sos\", fs=sr)\n    normalization = np.sqrt((hf - lf) / (sr / 2))\n    return signal.sosfiltfilt(sos, x) / normalization","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:10.153544Z","iopub.execute_input":"2021-09-06T10:00:10.153859Z","iopub.status.idle":"2021-09-06T10:00:10.159936Z","shell.execute_reply.started":"2021-09-06T10:00:10.153829Z","shell.execute_reply":"2021-09-06T10:00:10.158819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"qtransform_params={\"sr\": 2048, \"fmin\": 20, \"fmax\": 1024, \"hop_length\": 32, \"bins_per_octave\": 8}","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:10.161150Z","iopub.execute_input":"2021-09-06T10:00:10.161430Z","iopub.status.idle":"2021-09-06T10:00:10.170579Z","shell.execute_reply.started":"2021-09-06T10:00:10.161402Z","shell.execute_reply":"2021-09-06T10:00:10.169783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom nnAudio.Spectrogram import CQT1992v2\n\ndef apply_qtransform(waves, transform=CQT1992v2(**qtransform_params)):\n    #waves = np.hstack(waves)\n    waves = waves / np.max(waves)\n    waves *= signal.tukey(4096, 0.2)\n    waves = apply_bandpass(waves, 35, 500)\n    waves = torch.from_numpy(waves).float()\n    image = transform(waves)\n    return image\n\nfor i in range(5):\n    waves = np.load(train.loc[i, 'file_path'])\n    image = apply_qtransform(waves[0])\n    target = train.loc[i, 'target']\n    plt.imshow(image[0,:,:])\n    plt.title(f\"target: {target}\")\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:10.172554Z","iopub.execute_input":"2021-09-06T10:00:10.173056Z","iopub.status.idle":"2021-09-06T10:00:11.247119Z","shell.execute_reply.started":"2021-09-06T10:00:10.173013Z","shell.execute_reply":"2021-09-06T10:00:11.246401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:11.248248Z","iopub.execute_input":"2021-09-06T10:00:11.248643Z","iopub.status.idle":"2021-09-06T10:00:11.254065Z","shell.execute_reply.started":"2021-09-06T10:00:11.248600Z","shell.execute_reply":"2021-09-06T10:00:11.253186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from https://www.kaggle.com/daisukelab/creating-fat2019-preprocessed-data\ndef mono_to_color(X, mean=None, std=None, norm_max=None, norm_min=None, eps=1e-6):\n    # Standardize\n    mean = mean or X.mean()\n    X = X - mean\n    std = std or X.std()\n    Xstd = X / (std + eps)\n    _min, _max = Xstd.min(), Xstd.max()\n    norm_max = norm_max or _max\n    norm_min = norm_min or _min\n    if (_max - _min) > eps:\n        # Normalize to [0, 255]\n        V = Xstd\n        V[V < norm_min] = norm_min\n        V[V > norm_max] = norm_max\n        V = 255 * (V - norm_min) / (norm_max - norm_min)\n        V = V.astype(np.uint8)\n    else:\n        # Just zero\n        V = np.zeros_like(Xstd, dtype=np.uint8)\n    return V\n\ndef build_spectrogram(file_loc,ax=1):\n    waves = np.load(file_loc)\n    image1,image2,image3 = apply_qtransform(waves[0]),apply_qtransform(waves[1]),apply_qtransform(waves[2])\n    M1,M2,M3 = image1.permute(1,2,0).numpy()[:,:,0],image2.permute(1,2,0).numpy()[:,:,0],image3.permute(1,2,0).numpy()[:,:,0]\n    M1,M2,M3 = mono_to_color(M1),mono_to_color(M2),mono_to_color(M3)\n    return np.concatenate([M1,M2,M3],axis=ax)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:11.255328Z","iopub.execute_input":"2021-09-06T10:00:11.255701Z","iopub.status.idle":"2021-09-06T10:00:11.268254Z","shell.execute_reply.started":"2021-09-06T10:00:11.255661Z","shell.execute_reply":"2021-09-06T10:00:11.267251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = build_spectrogram(train.loc[3566,'file_path'],1);img.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:20.301066Z","iopub.execute_input":"2021-09-06T10:00:20.301550Z","iopub.status.idle":"2021-09-06T10:00:20.336345Z","shell.execute_reply.started":"2021-09-06T10:00:20.301519Z","shell.execute_reply":"2021-09-06T10:00:20.335016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:00:20.602366Z","iopub.execute_input":"2021-09-06T10:00:20.602873Z","iopub.status.idle":"2021-09-06T10:00:20.762753Z","shell.execute_reply.started":"2021-09-06T10:00:20.602829Z","shell.execute_reply":"2021-09-06T10:00:20.761727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport zipfile\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:02:35.263048Z","iopub.execute_input":"2021-09-06T10:02:35.263412Z","iopub.status.idle":"2021-09-06T10:02:35.268246Z","shell.execute_reply.started":"2021-09-06T10:02:35.263380Z","shell.execute_reply":"2021-09-06T10:02:35.267041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:02:35.471068Z","iopub.execute_input":"2021-09-06T10:02:35.471423Z","iopub.status.idle":"2021-09-06T10:02:35.483341Z","shell.execute_reply.started":"2021-09-06T10:02:35.471392Z","shell.execute_reply":"2021-09-06T10:02:35.482216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = train['file_path'].values\nOUT_TRAIN = 'TrainG2NET.zip'","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:02:36.172164Z","iopub.execute_input":"2021-09-06T10:02:36.172525Z","iopub.status.idle":"2021-09-06T10:02:36.177438Z","shell.execute_reply.started":"2021-09-06T10:02:36.172494Z","shell.execute_reply":"2021-09-06T10:02:36.176284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_tot,x2_tot = [],[]\nbatch = 50\nwith zipfile.ZipFile(OUT_TRAIN, 'w') as img_out:\n    for idx in tqdm(range(0,len(files),batch)):\n        names = files[idx:idx+batch]\n        out = Parallel(n_jobs=-1)(delayed(build_spectrogram)(i) for i in names)\n        for s in range(len(out)):\n            img = out[s]\n            x_tot.append((img/255.0).mean())\n            x2_tot.append(((img/255.0)**2).mean()) \n            name = names[s].split('/')[-1].split('.')[0]\n            img = cv2.imencode('.png',img)[1]\n            img_out.writestr(name + '.png', img)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:02:36.873239Z","iopub.execute_input":"2021-09-06T10:02:36.873600Z","iopub.status.idle":"2021-09-06T10:03:05.358929Z","shell.execute_reply.started":"2021-09-06T10:02:36.873567Z","shell.execute_reply":"2021-09-06T10:03:05.355900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_avr =  np.array(x_tot).mean()\nimg_std =  np.sqrt(np.array(x2_tot).mean() - img_avr**2)\nprint('mean:',img_avr, ', std:', img_std)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:03:05.360113Z","iopub.status.idle":"2021-09-06T10:03:05.360570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tsfiles = test['file_path'].values\nOUT_TEST = 'TestG2NET.zip'","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:03:09.490149Z","iopub.execute_input":"2021-09-06T10:03:09.490520Z","iopub.status.idle":"2021-09-06T10:03:09.495744Z","shell.execute_reply.started":"2021-09-06T10:03:09.490486Z","shell.execute_reply":"2021-09-06T10:03:09.494524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_tot,x2_tot = [],[]\nbatch = 50\nwith zipfile.ZipFile(OUT_TEST, 'w') as img_out:\n    for idx in tqdm(range(0,len(tsfiles),batch)):\n        names = tsfiles[idx:idx+batch]\n        out = Parallel(n_jobs=-1)(delayed(build_spectrogram)(i) for i in names)\n        for s in range(len(out)):\n            img = out[s]\n            x_tot.append((img/255.0).mean())\n            x2_tot.append(((img/255.0)**2).mean()) \n            name = names[s].split('/')[-1].split('.')[0]\n            img = cv2.imencode('.png',img)[1]\n            img_out.writestr(name + '.png', img)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:03:10.350087Z","iopub.execute_input":"2021-09-06T10:03:10.350451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_avr =  np.array(x_tot).mean()\nimg_std =  np.sqrt(np.array(x2_tot).mean() - img_avr**2)\nprint('mean:',img_avr, ', std:', img_std)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T10:03:05.364257Z","iopub.status.idle":"2021-09-06T10:03:05.364676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}