{"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\ntez_path = '../input/tez-modified-tqdm/'\neffnet_path = '../input/pytorch-efficientnet'\nimport sys,glob\nsys.path.append(tez_path)\nsys.path.append(effnet_path)\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\nimport matplotlib.pyplot as plt\nimport gc\nimport matplotlib.image as immg\nfrom tqdm.notebook import tqdm\nimport albumentations\nimport tez\nimport torch\nimport torch.nn as nn\nfrom torch.nn import functional as F\nfrom efficientnet_pytorch import EfficientNet","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:38.654646Z","iopub.execute_input":"2021-09-02T18:35:38.655017Z","iopub.status.idle":"2021-09-02T18:35:44.450086Z","shell.execute_reply.started":"2021-09-02T18:35:38.654983Z","shell.execute_reply":"2021-09-02T18:35:44.448896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:44.451849Z","iopub.execute_input":"2021-09-02T18:35:44.452194Z","iopub.status.idle":"2021-09-02T18:35:44.456563Z","shell.execute_reply.started":"2021-09-02T18:35:44.452160Z","shell.execute_reply":"2021-09-02T18:35:44.455517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q nnAudio","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:44.458321Z","iopub.execute_input":"2021-09-02T18:35:44.458720Z","iopub.status.idle":"2021-09-02T18:35:54.327435Z","shell.execute_reply.started":"2021-09-02T18:35:44.458689Z","shell.execute_reply":"2021-09-02T18:35:54.326423Z"},"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-02T18:35:54.329412Z","iopub.execute_input":"2021-09-02T18:35:54.329709Z","iopub.status.idle":"2021-09-02T18:35:55.092243Z","shell.execute_reply.started":"2021-09-02T18:35:54.329679Z","shell.execute_reply":"2021-09-02T18:35:55.091145Z"},"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-02T18:35:55.093859Z","iopub.execute_input":"2021-09-02T18:35:55.094196Z","iopub.status.idle":"2021-09-02T18:35:55.887406Z","shell.execute_reply.started":"2021-09-02T18:35:55.094164Z","shell.execute_reply":"2021-09-02T18:35:55.886413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:55.888631Z","iopub.execute_input":"2021-09-02T18:35:55.888929Z","iopub.status.idle":"2021-09-02T18:35:55.910281Z","shell.execute_reply.started":"2021-09-02T18:35:55.888899Z","shell.execute_reply":"2021-09-02T18:35:55.909374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:55.911629Z","iopub.execute_input":"2021-09-02T18:35:55.911941Z","iopub.status.idle":"2021-09-02T18:35:55.925207Z","shell.execute_reply.started":"2021-09-02T18:35:55.911912Z","shell.execute_reply":"2021-09-02T18:35:55.924098Z"},"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-02T18:35:55.926776Z","iopub.execute_input":"2021-09-02T18:35:55.927073Z","iopub.status.idle":"2021-09-02T18:35:55.934247Z","shell.execute_reply.started":"2021-09-02T18:35:55.927044Z","shell.execute_reply":"2021-09-02T18:35:55.933238Z"},"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 = 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)\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-02T18:35:55.937248Z","iopub.execute_input":"2021-09-02T18:35:55.937721Z","iopub.status.idle":"2021-09-02T18:35:56.916147Z","shell.execute_reply.started":"2021-09-02T18:35:55.937687Z","shell.execute_reply":"2021-09-02T18:35:56.915117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:56.917703Z","iopub.execute_input":"2021-09-02T18:35:56.917991Z","iopub.status.idle":"2021-09-02T18:35:56.923931Z","shell.execute_reply.started":"2021-09-02T18:35:56.917955Z","shell.execute_reply":"2021-09-02T18:35:56.922966Z"},"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-02T18:35:56.925307Z","iopub.execute_input":"2021-09-02T18:35:56.925616Z","iopub.status.idle":"2021-09-02T18:35:56.939475Z","shell.execute_reply.started":"2021-09-02T18:35:56.925587Z","shell.execute_reply":"2021-09-02T18:35:56.938253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = build_spectrogram(train.loc[3566,'file_path']);img.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:56.940951Z","iopub.execute_input":"2021-09-02T18:35:56.941328Z","iopub.status.idle":"2021-09-02T18:35:56.996714Z","shell.execute_reply.started":"2021-09-02T18:35:56.941273Z","shell.execute_reply":"2021-09-02T18:35:56.995526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:56.998043Z","iopub.execute_input":"2021-09-02T18:35:56.998358Z","iopub.status.idle":"2021-09-02T18:35:57.151692Z","shell.execute_reply.started":"2021-09-02T18:35:56.998327Z","shell.execute_reply":"2021-09-02T18:35:57.150599Z"},"trusted":true},"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-02T18:35:57.153017Z","iopub.execute_input":"2021-09-02T18:35:57.153308Z","iopub.status.idle":"2021-09-02T18:35:57.157791Z","shell.execute_reply.started":"2021-09-02T18:35:57.153280Z","shell.execute_reply":"2021-09-02T18:35:57.156817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:57.159235Z","iopub.execute_input":"2021-09-02T18:35:57.159687Z","iopub.status.idle":"2021-09-02T18:35:57.176880Z","shell.execute_reply.started":"2021-09-02T18:35:57.159652Z","shell.execute_reply":"2021-09-02T18:35:57.175727Z"},"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-02T18:35:57.178150Z","iopub.execute_input":"2021-09-02T18:35:57.178511Z","iopub.status.idle":"2021-09-02T18:35:57.188194Z","shell.execute_reply.started":"2021-09-02T18:35:57.178476Z","shell.execute_reply":"2021-09-02T18:35:57.187001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tsfiles)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T18:35:57.189745Z","iopub.execute_input":"2021-09-02T18:35:57.190231Z","iopub.status.idle":"2021-09-02T18:35:57.202764Z","shell.execute_reply.started":"2021-09-02T18:35:57.190188Z","shell.execute_reply":"2021-09-02T18:35:57.201617Z"},"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-02T18:36:03.750256Z","iopub.execute_input":"2021-09-02T18:36:03.750709Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}