{"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":"markdown","source":"# Prepare Environment","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport pandas as pd\nimport math\nimport cv2\nimport librosa \nimport librosa.display\nimport IPython.display as ipd \nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\n\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' \n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras import backend as K","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:39.251563Z","iopub.execute_input":"2022-02-25T15:26:39.251836Z","iopub.status.idle":"2022-02-25T15:26:39.259614Z","shell.execute_reply.started":"2022-02-25T15:26:39.251806Z","shell.execute_reply":"2022-02-25T15:26:39.258807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def merge_history(hlist):\n    history = {}\n    for k in hlist[0].history.keys():\n        history[k] = sum([h.history[k] for h in hlist], [])\n    return history","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:39.265198Z","iopub.execute_input":"2022-02-25T15:26:39.266182Z","iopub.status.idle":"2022-02-25T15:26:39.271641Z","shell.execute_reply.started":"2022-02-25T15:26:39.266143Z","shell.execute_reply":"2022-02-25T15:26:39.270878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vis_training(h, start=1):\n    epoch_range = range(start, len(h['loss'])+1)\n    s = slice(start-1, None)\n\n    plt.figure(figsize=[14,4])\n\n    n = int(len(h.keys()) / 2)\n\n    for i in range(n):\n        k = list(h.keys())[i]\n        plt.subplot(1,n,i+1)\n        plt.plot(epoch_range, h[k][s], label='Training')\n        plt.plot(epoch_range, h['val_' + k][s], label='Validation')\n        plt.xlabel('Epoch'); plt.ylabel(k); plt.title(k)\n        plt.grid()\n        plt.legend()\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:39.273327Z","iopub.execute_input":"2022-02-25T15:26:39.273665Z","iopub.status.idle":"2022-02-25T15:26:39.282369Z","shell.execute_reply.started":"2022-02-25T15:26:39.273629Z","shell.execute_reply":"2022-02-25T15:26:39.281617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/g2net-gravitational-wave-detection/training_labels.csv')\nprint(train.shape, '\\n')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:39.283645Z","iopub.execute_input":"2022-02-25T15:26:39.284085Z","iopub.status.idle":"2022-02-25T15:26:39.664455Z","shell.execute_reply.started":"2022-02-25T15:26:39.284041Z","shell.execute_reply":"2022-02-25T15:26:39.663642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generators","metadata":{}},{"cell_type":"code","source":"SPEC_PATH = '../input/g2net-spectrograms-41x9/spectrograms'\n\nclass DataGenerator(keras.utils.Sequence):\n    \n    def __init__(self, df, batch_size=32, img_size=(41, 9), shuffle=True, is_train=True):\n        self.df = df\n        self.n = len(df)\n        self.batch_size = batch_size\n        self.img_size = img_size\n        self.shuffle = shuffle\n        self.is_train = is_train\n        self.on_epoch_end()\n        \n    def on_epoch_end(self):\n        self.indices = np.arange(self.n)\n        if self.shuffle == True:\n            np.random.shuffle(self.indices)   \n    \n    def __len__(self):\n        # Determine batches per epoch\n        return math.ceil( self.n / self.batch_size )\n    \n    def __getitem__(self, batch_index):\n        # Get and return a single batch of data\n        start = batch_index * self.batch_size\n        end = (batch_index + 1) * self.batch_size\n        \n        indices = self.indices[start:end]\n        \n        return self.__data_generation(indices)\n    \n    def __data_generation(self, batch_indices):\n        batch_size = len(batch_indices)\n        \n        X = np.zeros(shape=(batch_size, self.img_size[0], self.img_size[1], 3))\n        y = np.zeros(batch_size)\n        \n        for i, idx in enumerate(batch_indices):\n            ID = self.df.id.values[idx]\n            y[i] = self.df.target.values[idx]\n            \n            SET = 'train_spec' if self.is_train else 'test_spec'\n            path = f'{SPEC_PATH}/{SET}/{ID}.npy'\n            data_array = np.load(path)\n            \n            X[i,:,:,:] = data_array\n            \n        return X, y\n    \n\nGENERATOR_TEST = True\n\nif GENERATOR_TEST:\n    temp_gen = DataGenerator(train, batch_size=8, shuffle=False)\n    X,y = temp_gen.__getitem__(0)\n\n    print(X.shape)\n    print(y)\n    \n    # Display one spectrogram\n    librosa.display.specshow(X[0, :, :, 0])\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:39.665608Z","iopub.execute_input":"2022-02-25T15:26:39.665869Z","iopub.status.idle":"2022-02-25T15:26:39.80223Z","shell.execute_reply.started":"2022-02-25T15:26:39.665834Z","shell.execute_reply":"2022-02-25T15:26:39.801407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, valid_df = train_test_split(train, test_size=0.2, random_state=1)\n\nprint(train_df.shape)\nprint(valid_df.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:39.806884Z","iopub.execute_input":"2022-02-25T15:26:39.807252Z","iopub.status.idle":"2022-02-25T15:26:39.950016Z","shell.execute_reply.started":"2022-02-25T15:26:39.807209Z","shell.execute_reply":"2022-02-25T15:26:39.949159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataGenerator(train_df, batch_size=2048, shuffle=True)\nvalid_loader = DataGenerator(valid_df, batch_size=2048, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:39.951623Z","iopub.execute_input":"2022-02-25T15:26:39.951927Z","iopub.status.idle":"2022-02-25T15:26:39.968619Z","shell.execute_reply.started":"2022-02-25T15:26:39.951872Z","shell.execute_reply":"2022-02-25T15:26:39.967769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build CNN","metadata":{}},{"cell_type":"code","source":"np.random.seed(1)\n\ncnn = Sequential()\n\ncnn.add(Conv2D(64, (3,3), activation = 'relu', padding = 'same', input_shape=(41,9,3)))\ncnn.add(Conv2D(64, (3,3), activation = 'relu', padding = 'same'))\ncnn.add(MaxPooling2D(2,2))\ncnn.add(Dropout(0.20))\ncnn.add(BatchNormalization())\n\ncnn.add(Conv2D(128, (3,3), activation = 'relu', padding = 'same'))\ncnn.add(Conv2D(128, (3,3), activation = 'relu', padding = 'same'))\ncnn.add(MaxPooling2D(2,2))\ncnn.add(Dropout(0.20))\ncnn.add(BatchNormalization())\n\ncnn.add(Conv2D(256, (3,3), activation = 'relu', padding = 'same'))\ncnn.add(Conv2D(256, (3,3), activation = 'relu', padding = 'same'))\ncnn.add(MaxPooling2D(2,2))\ncnn.add(Dropout(0.20))\ncnn.add(BatchNormalization())\n\ncnn.add(Flatten())\n\ncnn.add(Dense(128, activation='relu'))\ncnn.add(Dropout(0.20))\ncnn.add(BatchNormalization())\n\ncnn.add(Dense(64, activation='relu'))\ncnn.add(Dropout(0.20))\ncnn.add(BatchNormalization())\n\ncnn.add(Dense(1, activation='sigmoid'))\n\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:39.970087Z","iopub.execute_input":"2022-02-25T15:26:39.97036Z","iopub.status.idle":"2022-02-25T15:26:42.358606Z","shell.execute_reply.started":"2022-02-25T15:26:39.970323Z","shell.execute_reply":"2022-02-25T15:26:42.357937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Network","metadata":{}},{"cell_type":"code","source":"%%time\n\nopt = tf.keras.optimizers.Adam(0.01)\ncnn.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])\n\nh1 = cnn.fit(train_loader, epochs=10, validation_data=valid_loader, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T04:44:17.56736Z","iopub.execute_input":"2022-02-25T04:44:17.57059Z","iopub.status.idle":"2022-02-25T04:54:45.246242Z","shell.execute_reply.started":"2022-02-25T04:44:17.570548Z","shell.execute_reply":"2022-02-25T04:54:45.245553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = merge_history([h1])\nvis_training(history)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.set_value(cnn.optimizer.learning_rate, 0.001)\n\nh2 = cnn.fit(train_loader, epochs=10, validation_data=valid_loader, verbose=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = merge_history([h1,h2])\nvis_training(history)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.save('my_model.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Predictions","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/g2net-spectrograms-41x9/spectrograms/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:51.454274Z","iopub.execute_input":"2022-02-25T15:26:51.454533Z","iopub.status.idle":"2022-02-25T15:26:51.59904Z","shell.execute_reply.started":"2022-02-25T15:26:51.454502Z","shell.execute_reply":"2022-02-25T15:26:51.59821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader = DataGenerator(submission, is_train=False, batch_size=2048, shuffle=False)\n\npred = cnn.predict(test_loader)\n\nsubmission['target'] = pred\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:53.310819Z","iopub.execute_input":"2022-02-25T15:26:53.311555Z","iopub.status.idle":"2022-02-25T15:51:58.238415Z","shell.execute_reply.started":"2022-02-25T15:26:53.311515Z","shell.execute_reply":"2022-02-25T15:51:58.237646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:51:58.241087Z","iopub.execute_input":"2022-02-25T15:51:58.241296Z","iopub.status.idle":"2022-02-25T15:51:58.782052Z","shell.execute_reply.started":"2022-02-25T15:51:58.24127Z","shell.execute_reply":"2022-02-25T15:51:58.781275Z"},"trusted":true},"execution_count":null,"outputs":[]}]}