{"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":"pip install nnAudio","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd # Panel Data\nimport matplotlib.pyplot as plt # Make Grafics\nfrom matplotlib.gridspec import GridSpec # Axes os Graphics\nimport numpy as np #Lineal Algebra\nimport warnings #Ignore Warnings\nfrom random import shuffle\nimport tensorflow as tf #Model and Dataset\nfrom sklearn.model_selection import train_test_split\nfrom scipy import signal\nwarnings.filterwarnings(\"ignore\")\n\ntrain_labels = pd.read_csv(\"/kaggle/input/g2net-gravitational-wave-detection/training_labels.csv\")\nsample_submission = pd.read_csv(\"/kaggle/input/g2net-gravitational-wave-detection/sample_submission.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get the Path","metadata":{}},{"cell_type":"code","source":"def id2path(idx, is_train = True):\n    path = \"/kaggle/input/g2net-gravitational-wave-detection\"\n    \n    if is_train:\n        path += \"/train/\" + idx[0] + \"/\" + idx[1] + \"/\" + idx[2] + \"/\" + idx + \".npy\"\n        \n    else:\n        path += \"/test/\" + idx[0] + \"/\" + idx[1] + \"/\" + idx[2] + \"/\" + idx + \".npy\"\n        \n    return path","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Increase Dimension whit Constant Q-Transform","metadata":{}},{"cell_type":"code","source":"from nnAudio.Spectrogram import CQT1992v2\nimport torch\n\ndef increase_dimension(idx, is_train, transform = CQT1992v2(sr = 2048, hop_length = 64, fmin = 20, fmax = 500)):\n    wave = np.load(id2path(idx, is_train))\n    wave = np.concatenate(wave, axis = 0)\n    bHP, aHP = signal.butter(8, (20, 500), btype='bandpass', fs=2048)\n    window = signal.tukey(4096*3, 0.2)\n    wave *= window\n    wave = signal.filtfilt(bHP, aHP, wave)\n    wave = wave / np.max(wave)\n    wave = torch.from_numpy(wave).float()\n    image = transform(wave)\n    image = np.array(image)\n    image = np.transpose(image, (1, 2, 0))\n    \n    return image","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"increase_dimension(train_labels[\"id\"][0], is_train = True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize the data","metadata":{}},{"cell_type":"code","source":"targets = train_labels[train_labels[\"target\"] == 0][\"id\"].head(2)\nno_targets = train_labels[train_labels[\"target\"] == 1][\"id\"].head(2)\nfig = plt.figure(figsize=(15, 10))\ngs = GridSpec(4, 4, figure=fig)\n\nfor i, (target, no_target) in enumerate(zip(targets, no_targets)):\n    # Subplot para GW Found (gráfico de líneas)\n    ax1 = fig.add_subplot(gs[i*2, 0])\n    ax1.plot(np.load(id2path(target))[1, :], color='blue')\n    ax1.set_title(\"GW Found - Ligo Hanford\")\n    ax1.set_axis_off()\n\n    ax2 = fig.add_subplot(gs[i*2, 1])\n    ax2.plot(np.load(id2path(target))[2, :], color='blue')\n    ax2.set_title(\"GW Found - Ligo Livingstone\")\n    ax2.set_axis_off()\n\n    ax3 = fig.add_subplot(gs[i*2, 2])\n    ax3.plot(np.load(id2path(target))[0, :], color='blue')\n    ax3.set_title(\"GW Found - Virgo\")\n    ax3.set_axis_off()\n    \n    ax4 = fig.add_subplot(gs[i*2, 3])\n    ax4.imshow(increase_dimension(target, is_train=True))\n    ax4.set_title(\"GW Found - Spectrogram\")\n    ax4.set_axis_off()\n    \n\n    # Subplot para GW Not Found (gráfico de líneas)\n    ax5 = fig.add_subplot(gs[i*2 + 1, 0])\n    ax5.plot(np.load(id2path(no_target))[1, :], color='red')\n    ax5.set_title(\"GW Not Found - Ligo Hanford\")\n    ax5.set_axis_off()\n\n    ax6 = fig.add_subplot(gs[i*2 + 1, 1])\n    ax6.plot(np.load(id2path(no_target))[2, :], color='red')\n    ax6.set_title(\"GW Not Found - Livingstone\")\n    ax6.set_axis_off()\n\n    ax7 = fig.add_subplot(gs[i*2 + 1, 2])\n    ax7.plot(np.load(id2path(no_target))[0, :], color='red')\n    ax7.set_title(\"GW Not Found - Virgo\")\n    ax7.set_axis_off()\n\n    ax8 = fig.add_subplot(gs[i*2 + 1, 3])\n    ax8.imshow(increase_dimension(no_target, is_train=True))\n    ax8.set_title(\"GW Not Found - Spectrogram\")\n    ax8.set_axis_off()\n\nplt.tight_layout()\nplt.show()\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Batch Data","metadata":{}},{"cell_type":"code","source":"import math\nclass Dataset(tf.keras.utils.Sequence):\n    def __init__(self, data, y = None, batch_size = 256, shuffle = True):\n        self.data = data\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n        \n        if y is not None:\n            self.is_train = True\n        else:\n            self.is_train = False\n            \n        self.y = y\n        \n    def __len__(self):\n        return math.ceil(len(self.data)/ self.batch_size)\n    \n    def __getitem__(self, ids):\n        batch_data = self.data[ids * self.batch_size : (ids + 1) * self.batch_size]\n        \n        if self.y is not None:\n            batch_y = self.y[ids * self.batch_size : (ids + 1) * self.batch_size]\n            \n        batch_x = np.array([increase_dimension(x, self.is_train) for x in batch_data])\n        batch_x = np.stack(batch_x)\n        \n        if self.is_train:\n            return batch_x, batch_y\n        else:\n            return batch_x\n\n    def on_epoch_end(self):\n        if self.shuffle and self.is_train:\n            ids_y = list(zip(self.data, self.y))\n            shuffle(ids_y)\n            self.data, self.y = list(zip(*ids_y))\n        \n        ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_idx =  train_labels['id'].values\ny = train_labels['target'].values\ntest_idx = sample_submission['id'].values\n\nx_train,x_valid,y_train,y_valid = train_test_split(train_idx,y,test_size=0.05,random_state=42,stratify=y)\n\ntrain_dataset = Dataset(x_train,y_train)\nvalid_dataset = Dataset(x_valid,y_valid)\ntest_dataset = Dataset(test_idx)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install efficientnet","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n    Conv1 = tf.keras.layers.Conv2D(filters = 32, kernel_size = (3, 3) ,input_shape = [56, 193, 1], activation = \"relu\")\n    Maxpooling1 = tf.keras.layers.MaxPool2D()\n    Conv2 = tf.keras.layers.Conv2D(filters = 32,  kernel_size = (3, 3), activation = \"relu\")\n    Maxpooling2 = tf.keras.layers.MaxPool2D()\n    Conv3 = tf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), activation = \"relu\")\n    Maxpooling3 = tf.keras.layers.MaxPool2D()\n    flatten = tf.keras.layers.Flatten()\n    Dense1 = tf.keras.layers.Dense(64, activation = \"relu\")\n    dense2 = tf.keras.layers.Dense(1, activation = \"sigmoid\")\n    \n    model = tf.keras.Sequential([Conv1, Maxpooling1,Conv2, Maxpooling2, Conv3, Maxpooling3, flatten ,Dense1, dense2])\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),loss='binary_crossentropy', metrics=[tf.keras.metrics.AUC()])\n    \n    return model\n    \nmodel = create_model()\n\nfrom tensorflow.keras.utils import plot_model\n\n# Definir el modelo aquí (igual que en el código anterior)\n\n# Crear un objeto TensorBoard para visualizar el modelo\ntensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir='logs')\n\n# Entrenar el modelo con el objeto TensorBoard como argumento de callback\n# model.fit(x_train, y_train, epochs=10, callbacks=[tensorboard_callback])\n\n# Generar una imagen del diagrama de flujo del modelo\nplot_model(model, to_file='model.png', show_shapes=True)\n\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_dataset, epochs = 3, validation_data = valid_dataset)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_dataset)\npreds = preds.reshape(-1)\nsubmission = pd.DataFrame({'id':sample_submission['id'],'target':preds})\nsubmission.to_csv('submission.csv',index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}