{"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":"# Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:41.675142Z","iopub.execute_input":"2021-08-15T13:00:41.675976Z","iopub.status.idle":"2021-08-15T13:00:41.683871Z","shell.execute_reply.started":"2021-08-15T13:00:41.675902Z","shell.execute_reply":"2021-08-15T13:00:41.683177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/g2net-gravitational-wave-detection/sample_submission.csv')\ntraining_labels = pd.read_csv('../input/g2net-gravitational-wave-detection/training_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:41.685397Z","iopub.execute_input":"2021-08-15T13:00:41.685932Z","iopub.status.idle":"2021-08-15T13:00:42.245017Z","shell.execute_reply.started":"2021-08-15T13:00:41.685858Z","shell.execute_reply":"2021-08-15T13:00:42.243960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:42.247241Z","iopub.execute_input":"2021-08-15T13:00:42.247545Z","iopub.status.idle":"2021-08-15T13:00:42.261566Z","shell.execute_reply.started":"2021-08-15T13:00:42.247516Z","shell.execute_reply":"2021-08-15T13:00:42.260690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Demonstration of the Data","metadata":{}},{"cell_type":"code","source":"my_tensor = np.load('../input/g2net-gravitational-wave-detection/train/0/0/0/00000e74ad.npy', allow_pickle=True)\nmy_tensor = np.load('../input/g2net-gravitational-wave-detection/test/0/0/0/00024887b5.npy', allow_pickle=True)\n\nmax_of_rows = my_tensor.max(axis=1)\nmy_tensor = my_tensor / max_of_rows[:, np.newaxis]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:42.263358Z","iopub.execute_input":"2021-08-15T13:00:42.263737Z","iopub.status.idle":"2021-08-15T13:00:42.282702Z","shell.execute_reply.started":"2021-08-15T13:00:42.263706Z","shell.execute_reply":"2021-08-15T13:00:42.281440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = training_labels.id[0]\ntarget = training_labels.target[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:42.284005Z","iopub.execute_input":"2021-08-15T13:00:42.284310Z","iopub.status.idle":"2021-08-15T13:00:42.288727Z","shell.execute_reply.started":"2021-08-15T13:00:42.284282Z","shell.execute_reply":"2021-08-15T13:00:42.287763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(num=0, figsize= (9,9))\nfor i in range(3):\n    plt.subplot(3, 1, i+1)\n    plt.title(f'target: {target}, label: {label}')\n    plt.plot(my_tensor[i])","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:42.289989Z","iopub.execute_input":"2021-08-15T13:00:42.290282Z","iopub.status.idle":"2021-08-15T13:00:42.698462Z","shell.execute_reply.started":"2021-08-15T13:00:42.290254Z","shell.execute_reply":"2021-08-15T13:00:42.697411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# What the neural net sees after GlobalAveragePooling2D()","metadata":{}},{"cell_type":"code","source":"my_tensor = np.load('../input/g2net-gravitational-wave-detection/train/0/0/0/00000e74ad.npy', allow_pickle=True)\nmy_tensor = np.load('../input/g2net-gravitational-wave-detection/test/0/0/0/0003259f74.npy', allow_pickle=True)\n\nprint(my_tensor.shape)\n\nmax_of_rows = my_tensor.max(axis=1)\nmy_tensor = my_tensor / max_of_rows[:, np.newaxis]\n\nd1 = my_tensor[0]\nd2 = my_tensor[1]\nd3 = my_tensor[2]\n\nd1 = d1.reshape(64, 64, 1)\nd2 = d2.reshape(64, 64, 1)\nd3 = d3.reshape(64, 64, 1)\n\nd = np.concatenate((d1,d2,d3), axis=2)\n\nplt.imshow(d)\n\nD = layers.GlobalAveragePooling2D()(d.reshape(1, 64, 64, 3))\n\n\nplt.imshow(D)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:42.699686Z","iopub.execute_input":"2021-08-15T13:00:42.700003Z","iopub.status.idle":"2021-08-15T13:00:42.870227Z","shell.execute_reply.started":"2021-08-15T13:00:42.699973Z","shell.execute_reply":"2021-08-15T13:00:42.869221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading the Data","metadata":{}},{"cell_type":"code","source":"flag = 0\ntensor_list = []\nfor dirname, _, filenames in os.walk('../input/g2net-gravitational-wave-detection/train'):\n    for filename in filenames:\n        tensor_path = os.path.join(dirname, filename)\n        my_tensor = np.load(tensor_path, allow_pickle=True)\n        max_of_rows = my_tensor.max(axis=1)\n        my_tensor = my_tensor / max_of_rows[:, np.newaxis]\n        \n        d1 = my_tensor[0]\n        d2 = my_tensor[1]\n        d3 = my_tensor[2]\n\n        d1 = d1.reshape(64, 64, 1)\n        d2 = d2.reshape(64, 64, 1)\n        d3 = d3.reshape(64, 64, 1)\n\n        d = np.concatenate((d1,d2,d3), axis=2)\n        \n        tensor_list.append(d)\n        flag += 1\n    if flag > 1000:\n        break\n        ","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:42.871362Z","iopub.execute_input":"2021-08-15T13:00:42.871645Z","iopub.status.idle":"2021-08-15T13:00:44.358993Z","shell.execute_reply.started":"2021-08-15T13:00:42.871612Z","shell.execute_reply":"2021-08-15T13:00:44.357979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(tensor_list)\ny_train = training_labels.target.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:44.361550Z","iopub.execute_input":"2021-08-15T13:00:44.361899Z","iopub.status.idle":"2021-08-15T13:00:44.426864Z","shell.execute_reply.started":"2021-08-15T13:00:44.361845Z","shell.execute_reply":"2021-08-15T13:00:44.426092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:44.429089Z","iopub.execute_input":"2021-08-15T13:00:44.429709Z","iopub.status.idle":"2021-08-15T13:00:44.436048Z","shell.execute_reply.started":"2021-08-15T13:00:44.429634Z","shell.execute_reply":"2021-08-15T13:00:44.435140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exhibition of the Data","metadata":{}},{"cell_type":"code","source":"plt.imshow(X_train[0])","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:44.437181Z","iopub.execute_input":"2021-08-15T13:00:44.437611Z","iopub.status.idle":"2021-08-15T13:00:44.606213Z","shell.execute_reply.started":"2021-08-15T13:00:44.437568Z","shell.execute_reply":"2021-08-15T13:00:44.605442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = y_train[:len(X_train)]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:44.607327Z","iopub.execute_input":"2021-08-15T13:00:44.607758Z","iopub.status.idle":"2021-08-15T13:00:44.611276Z","shell.execute_reply.started":"2021-08-15T13:00:44.607713Z","shell.execute_reply":"2021-08-15T13:00:44.610567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:00:44.612427Z","iopub.execute_input":"2021-08-15T13:00:44.612903Z","iopub.status.idle":"2021-08-15T13:00:44.626466Z","shell.execute_reply.started":"2021-08-15T13:00:44.612836Z","shell.execute_reply":"2021-08-15T13:00:44.625803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:04:20.834194Z","iopub.execute_input":"2021-08-15T13:04:20.834582Z","iopub.status.idle":"2021-08-15T13:04:20.840976Z","shell.execute_reply.started":"2021-08-15T13:04:20.834552Z","shell.execute_reply":"2021-08-15T13:04:20.839973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building the Model","metadata":{}},{"cell_type":"code","source":"inputs = keras.Input(shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:04:20.842524Z","iopub.execute_input":"2021-08-15T13:04:20.842932Z","iopub.status.idle":"2021-08-15T13:04:20.855094Z","shell.execute_reply.started":"2021-08-15T13:04:20.842873Z","shell.execute_reply":"2021-08-15T13:04:20.853931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import ResNet50\n\nres_model = ResNet50(input_tensor=inputs, weights='imagenet')","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:04:20.856727Z","iopub.execute_input":"2021-08-15T13:04:20.857248Z","iopub.status.idle":"2021-08-15T13:04:22.598875Z","shell.execute_reply.started":"2021-08-15T13:04:20.857205Z","shell.execute_reply":"2021-08-15T13:04:22.597817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(res_model.layers)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:04:22.600536Z","iopub.execute_input":"2021-08-15T13:04:22.600909Z","iopub.status.idle":"2021-08-15T13:04:22.607240Z","shell.execute_reply.started":"2021-08-15T13:04:22.600864Z","shell.execute_reply":"2021-08-15T13:04:22.606218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in res_model.layers[:161]:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:04:22.608407Z","iopub.execute_input":"2021-08-15T13:04:22.608690Z","iopub.status.idle":"2021-08-15T13:04:22.624635Z","shell.execute_reply.started":"2021-08-15T13:04:22.608663Z","shell.execute_reply":"2021-08-15T13:04:22.623668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def scheduler(epoch, lr):\n    if epoch < 10:\n        return lr\n    else:\n        return lr * tf.math.exp(-0.1)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:04:22.625931Z","iopub.execute_input":"2021-08-15T13:04:22.626247Z","iopub.status.idle":"2021-08-15T13:04:22.635959Z","shell.execute_reply.started":"2021-08-15T13:04:22.626219Z","shell.execute_reply":"2021-08-15T13:04:22.635015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scheduler_callback =  tf.keras.callbacks.LearningRateScheduler(scheduler)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:04:22.638744Z","iopub.execute_input":"2021-08-15T13:04:22.639337Z","iopub.status.idle":"2021-08-15T13:04:22.648502Z","shell.execute_reply.started":"2021-08-15T13:04:22.639293Z","shell.execute_reply":"2021-08-15T13:04:22.647758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import callbacks\n\nearly_stopping = callbacks.EarlyStopping(\n    min_delta=0.1, # minimium amount of change to count as an improvement\n    patience=20, # how many epochs to wait before stopping\n    restore_best_weights=True,\n)\ncallbacks = [\n    keras.callbacks.TensorBoard(log_dir='./logs'),\n    early_stopping,\n    scheduler_callback\n]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:04:22.684653Z","iopub.execute_input":"2021-08-15T13:04:22.685238Z","iopub.status.idle":"2021-08-15T13:04:22.692310Z","shell.execute_reply.started":"2021-08-15T13:04:22.685196Z","shell.execute_reply":"2021-08-15T13:04:22.691480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers\nfrom tensorflow.keras.layers.experimental import preprocessing\n\ninputs = keras.Input(shape=(64, 64, 3))\npre = preprocessing.Resizing(int(224), int(224), interpolation='bilinear')(inputs)\nx = res_model(pre)\nx = layers.Flatten()(x)\nx = layers.Dense(100, activation=\"sigmoid\")(x)\noutputs = layers.Dense(2, activation=\"softmax\")(x)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:04:22.693478Z","iopub.execute_input":"2021-08-15T13:04:22.693965Z","iopub.status.idle":"2021-08-15T13:04:23.161402Z","shell.execute_reply.started":"2021-08-15T13:04:22.693934Z","shell.execute_reply":"2021-08-15T13:04:23.160501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers\n\ninputs = keras.Input(shape=(64, 64, 3))\nx = layers.Conv2D(filters=32, kernel_size=(3, 3), activation=\"relu\")(inputs)\n# x = layers.MaxPooling2D(pool_size=(2, 2))(x)\n# x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation=\"relu\")(x)\n# x = layers.MaxPooling2D(pool_size=(3, 3))(x)\n# x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation=\"relu\")(x)\n\n# Apply global average pooling to get flat feature vectors\nx = layers.GlobalAveragePooling2D()(x)\n\n# Add a dense classifier on top\nnum_classes = 2\noutputs = layers.Dense(num_classes, activation=\"softmax\")(x)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:07:59.332468Z","iopub.execute_input":"2021-08-15T13:07:59.332855Z","iopub.status.idle":"2021-08-15T13:07:59.365220Z","shell.execute_reply.started":"2021-08-15T13:07:59.332825Z","shell.execute_reply":"2021-08-15T13:07:59.364172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[:10]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:07:59.369158Z","iopub.execute_input":"2021-08-15T13:07:59.369515Z","iopub.status.idle":"2021-08-15T13:07:59.377174Z","shell.execute_reply.started":"2021-08-15T13:07:59.369484Z","shell.execute_reply":"2021-08-15T13:07:59.375826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Model(inputs=inputs, outputs=outputs)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:07:59.379302Z","iopub.execute_input":"2021-08-15T13:07:59.379695Z","iopub.status.idle":"2021-08-15T13:07:59.395681Z","shell.execute_reply.started":"2021-08-15T13:07:59.379657Z","shell.execute_reply":"2021-08-15T13:07:59.394521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:07:59.397727Z","iopub.execute_input":"2021-08-15T13:07:59.398185Z","iopub.status.idle":"2021-08-15T13:07:59.414858Z","shell.execute_reply.started":"2021-08-15T13:07:59.398134Z","shell.execute_reply":"2021-08-15T13:07:59.413275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:07:59.417914Z","iopub.execute_input":"2021-08-15T13:07:59.418392Z","iopub.status.idle":"2021-08-15T13:07:59.442685Z","shell.execute_reply.started":"2021-08-15T13:07:59.418349Z","shell.execute_reply":"2021-08-15T13:07:59.441423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = y_train[:len(X_train)]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:07:59.445039Z","iopub.execute_input":"2021-08-15T13:07:59.445535Z","iopub.status.idle":"2021-08-15T13:07:59.454757Z","shell.execute_reply.started":"2021-08-15T13:07:59.445490Z","shell.execute_reply":"2021-08-15T13:07:59.453700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=0.2, random_state=0)\n\nX_valid, X_test, y_valid, y_test = train_test_split(X_valid, y_valid, test_size=0.2, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:07:59.457990Z","iopub.execute_input":"2021-08-15T13:07:59.458289Z","iopub.status.idle":"2021-08-15T13:07:59.560032Z","shell.execute_reply.started":"2021-08-15T13:07:59.458260Z","shell.execute_reply":"2021-08-15T13:07:59.559015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[0].shape","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:07:59.561854Z","iopub.execute_input":"2021-08-15T13:07:59.562260Z","iopub.status.idle":"2021-08-15T13:07:59.569682Z","shell.execute_reply.started":"2021-08-15T13:07:59.562220Z","shell.execute_reply":"2021-08-15T13:07:59.568535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the Model","metadata":{}},{"cell_type":"code","source":"history = model.fit(X_train, y_train, batch_size=32, epochs=10, validation_data=(X_valid, y_valid),\n                   callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:08:08.930804Z","iopub.execute_input":"2021-08-15T13:08:08.931328Z","iopub.status.idle":"2021-08-15T13:08:16.769367Z","shell.execute_reply.started":"2021-08-15T13:08:08.931297Z","shell.execute_reply":"2021-08-15T13:08:16.768427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(num=0)\nplt.title('loss')\nplt.plot(history.history['loss'], label='train')\nplt.plot(history.history['val_loss'], label='validation')\nplt.legend()\n\nplt.figure(num=1)\nplt.title('accuracy')\nplt.plot(history.history['accuracy'], label='train')\nplt.plot(history.history['val_accuracy'], label='validation')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:08:16.771122Z","iopub.execute_input":"2021-08-15T13:08:16.771403Z","iopub.status.idle":"2021-08-15T13:08:17.135481Z","shell.execute_reply.started":"2021-08-15T13:08:16.771376Z","shell.execute_reply":"2021-08-15T13:08:17.134406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, acc = model.evaluate(X_test, y_test)  # returns loss and metrics\nprint(\"loss: %.2f\" % loss)\nprint(\"acc: %.2f\" % acc)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:08:17.137231Z","iopub.execute_input":"2021-08-15T13:08:17.137542Z","iopub.status.idle":"2021-08-15T13:08:17.207643Z","shell.execute_reply.started":"2021-08-15T13:08:17.137513Z","shell.execute_reply":"2021-08-15T13:08:17.206675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(X_test)\npredictions = np.argmax(predictions, axis=1)    ","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:08:17.209047Z","iopub.execute_input":"2021-08-15T13:08:17.209302Z","iopub.status.idle":"2021-08-15T13:08:17.267723Z","shell.execute_reply.started":"2021-08-15T13:08:17.209277Z","shell.execute_reply":"2021-08-15T13:08:17.266834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:08:17.268966Z","iopub.execute_input":"2021-08-15T13:08:17.269230Z","iopub.status.idle":"2021-08-15T13:08:17.274972Z","shell.execute_reply.started":"2021-08-15T13:08:17.269205Z","shell.execute_reply":"2021-08-15T13:08:17.273982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:08:17.276391Z","iopub.execute_input":"2021-08-15T13:08:17.277010Z","iopub.status.idle":"2021-08-15T13:08:17.287365Z","shell.execute_reply.started":"2021-08-15T13:08:17.276967Z","shell.execute_reply":"2021-08-15T13:08:17.286481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}