{"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":"# **Many thanks to Adriano Passos for the idea of using RGB images in this competition. In this code, I have minimally modified its code, adding resize 50 by 50 when forming the dataset. To save time, I also used the Adriano Passos dataset for a test sample. In this competition, I used the CNN-LSTM neural network.**\nTEST DATASET: https://www.kaggle.com/coldfir3/g2net-cqt-dataset-test-jpgrgb","metadata":{}},{"cell_type":"code","source":"fast_sub = True # set this to False to generate the whole dataset\ntrain = True # set this to True to generate the train set\ntest = True # set this to True to generate the test set","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-29T19:08:27.462779Z","iopub.execute_input":"2021-09-29T19:08:27.463086Z","iopub.status.idle":"2021-09-29T19:08:27.557432Z","shell.execute_reply.started":"2021-09-29T19:08:27.463006Z","shell.execute_reply":"2021-09-29T19:08:27.556736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!python -m pip install gwpy\n!pip install astropy==4.2.1","metadata":{"execution":{"iopub.status.busy":"2021-09-29T19:08:27.560724Z","iopub.execute_input":"2021-09-29T19:08:27.56247Z","iopub.status.idle":"2021-09-29T19:08:55.180559Z","shell.execute_reply.started":"2021-09-29T19:08:27.561272Z","shell.execute_reply":"2021-09-29T19:08:55.179625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from gwpy.timeseries import TimeSeries\nfrom gwpy.plot import Plot\nimport numpy as np\nfrom scipy import signal\nfrom PIL import Image\n\nimport os\nimport pandas as pd\nimport numpy as np\nfrom tqdm.auto import tqdm\nfrom joblib import Parallel, delayed\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2021-09-29T19:08:55.181966Z","iopub.execute_input":"2021-09-29T19:08:55.182246Z","iopub.status.idle":"2021-09-29T19:08:58.139361Z","shell.execute_reply.started":"2021-09-29T19:08:55.182211Z","shell.execute_reply":"2021-09-29T19:08:58.138621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\ndef sig2rgb(fname, whiten = True, window=0.2, bandpass=False, f_range = (30,400), q_range = (16,32), q_max = 10):\n    \n    # Load the file \n    data = np.load(fname)\n    # Split each chanel and convert to TimeSeries\n    data = map(lambda x: TimeSeries(x, sample_rate=2048), data)\n    # Whiten the signal and apply a tukey window\n    data = map(lambda x: x.whiten(window=(\"tukey\", window)), data)\n    # (optional) bandpass filter\n    if bandpass:\n        data = map(lambda x: x.bandpass(*f_range), data)\n    # Q-transform\n    data = map(lambda x: x.q_transform(qrange=q_range, frange=f_range, logf=True, whiten=False), data)\n    # Convert to RGB image\n    img = np.stack(list(data), axis = -1)\n    img = np.clip(img, 0, q_max)/q_max * 255\n    img = img.astype(np.uint8)\n    img = Image.fromarray(img).rotate(90, expand=1)\n    img = img.resize((50, 50))\n#resized_image.save('resized.png')\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-09-29T19:08:58.141429Z","iopub.execute_input":"2021-09-29T19:08:58.14169Z","iopub.status.idle":"2021-09-29T19:08:58.151446Z","shell.execute_reply.started":"2021-09-29T19:08:58.141657Z","shell.execute_reply":"2021-09-29T19:08:58.149986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sig2rgb('../input/g2net-gravitational-wave-detection/train/0/0/0/00000e74ad.npy')\n","metadata":{"execution":{"iopub.status.busy":"2021-09-29T19:08:58.152908Z","iopub.execute_input":"2021-09-29T19:08:58.153239Z","iopub.status.idle":"2021-09-29T19:08:58.618471Z","shell.execute_reply.started":"2021-09-29T19:08:58.153205Z","shell.execute_reply":"2021-09-29T19:08:58.61764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n\n\ndef save_img(x, folder_out, **kwargs):\n    fname = Path('../input/g2net-gravitational-wave-detection/' + folder_out + '/' + '/'.join([x[0], x[1], x[2], x]) + '.npy')\n    file_out = folder_out + '/'+ fname.with_suffix('.jpg').name\n    x = sig2rgb(fname, **kwargs)\n    x.save(file_out)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T19:08:58.620022Z","iopub.execute_input":"2021-09-29T19:08:58.620352Z","iopub.status.idle":"2021-09-29T19:08:58.625938Z","shell.execute_reply.started":"2021-09-29T19:08:58.620316Z","shell.execute_reply":"2021-09-29T19:08:58.625319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport shutil\nimport os\n\nos.makedirs('./kek', exist_ok = True)\n\nsrc_dir = \"../input/g2net-cqt-dataset-test-jpgrgb\"\ndst_dir = \"./kek\"\nfor jpgfile in glob.iglob(os.path.join(src_dir, \"*.jpg\")):\n    shutil.copy(jpgfile, dst_dir)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-29T19:08:58.627193Z","iopub.execute_input":"2021-09-29T19:08:58.627637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = r'./kek'\nfor file in os.listdir(f):\n    f_img = f+\"/\"+file\n    img = Image.open(f_img)\n    img = img.resize((50,50))\n    img.save(f_img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"from PIL import Image\nimport os, sys\npath = \"./kek/\"\ndirs = os.listdir( path )\n\n\ndef resize():\n    for item in dirs:\n        if os.path.isfile(path+item):\n            im = Image.open(path+item)\n            f, e = os.path.splitext(path+item)\n            imResize = im.resize((75,75), Image.ANTIALIAS)\n            imResize.save(f + '*.jpg', 'JPEG')\n\nresize()           \n","metadata":{"execution":{"iopub.status.busy":"2021-09-28T17:12:36.768512Z","iopub.execute_input":"2021-09-28T17:12:36.768825Z","iopub.status.idle":"2021-09-28T17:12:36.957239Z","shell.execute_reply.started":"2021-09-28T17:12:36.768793Z","shell.execute_reply":"2021-09-28T17:12:36.956197Z"}}},{"cell_type":"code","source":"train_df = pd.read_csv('../input/g2net-gravitational-wave-detection/training_labels.csv')\nif fast_sub: train_ids = train_df['id'][0:10000]\nelse: train_ids = train_df['id']\nif train:\n    os.makedirs('train', exist_ok = True)\n    o = Parallel(n_jobs=4)(delayed(save_img)(x, 'train') for x in tqdm(train_ids))\n    #zip_folder('train')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_t = './train/*.jpg'\nadres = glob.glob(path_t)\nlen(adres)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\nfilenames = [img for img in glob.glob(\"./train/*.jpg\")]\n\nfilenames.sort() # ADD THIS LINE\n\nimages = []\nfor img in filenames:\n    n= cv2.imread(img, cv2.IMREAD_UNCHANGED)\n    images.append(n)\n    print (img)\nplt.imshow(images[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('./train/00000e74ad.jpg', cv2.IMREAD_UNCHANGED)\narr = np.array(img)\narr\nplt.imshow(img)\nsig2rgb('../input/g2net-gravitational-wave-detection/train/0/0/0/00000e74ad.npy')\nimg.shape\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(images)\n#images = images.astype('float32')\nimages = images.reshape(images.shape[0],1, 50, 50, 3)\n\n\ntrain_df = train_df[0:10000]\ntrain_df\ntrain_df['target'].hist()\nimages.shape\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_df['target']\n\ny","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --upgrade tensorflow\nfrom tensorflow import keras \nfrom tensorflow.keras import backend as K\n\n\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.layers import Activation\nfrom tensorflow.keras.layers import LSTM\nfrom tensorflow.keras.layers import Bidirectional\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import ConvLSTM2D\nfrom tensorflow.keras.layers import TimeDistributed\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nnp.random.seed(500)\n\n\n\n#  Обучение нейронной сети \n\n# Creating a model\nmodel = Sequential()\nmodel.add(TimeDistributed(Conv2D(filters=300, kernel_size=(5,5), padding = \"same\", activation='relu'), input_shape=(1, 50, 50, 3)))\nmodel.add(Dropout(0.20))\nmodel.add(TimeDistributed(MaxPooling2D(pool_size=(5,5))))\nmodel.add(TimeDistributed(Conv2D(filters=150, kernel_size=(5,5), padding = \"same\", activation='relu'), input_shape=(1, 50, 50, 3)))\nmodel.add(Dropout(0.20))\nmodel.add(TimeDistributed(MaxPooling2D(pool_size=(5,5))))\nmodel.add(TimeDistributed(Conv2D(filters=75, kernel_size=(5,5), padding = \"same\", activation='relu'), input_shape=(1, 50, 50, 3)))\nmodel.add(Dropout(0.20))\nmodel.add(TimeDistributed(Flatten()))\nmodel.add(LSTM(150, activation='relu', return_sequences=True))\nmodel.add(Dropout(0.20))\nmodel.add(LSTM(50, activation='relu', return_sequences=True))\nmodel.add(Dropout(0.20))\nmodel.add(Dense(1))\nmodel.add(Activation('sigmoid'))\n\n\n# Compiling model\nmodel.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer='adam', \n              metrics=[\"accuracy\"])\n\ncallback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=10)\n\n# Training a model\n\nnet_res_1 = model.fit(images, y,\n                    epochs=100,\n                    verbose = 1, batch_size=200, callbacks=[callback])#, validation_data = (X_test, y_test))\n\n\n\n#98","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(images)\ny_pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/g2net-gravitational-wave-detection/sample_submission.csv')\ntest_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = [img for img in glob.glob(\"./kek/*.jpg\")]\n\nfilenames.sort() # ADD THIS LINE\n\nimages = []\nfor img in filenames:\n    n= cv2.imread(img, cv2.IMREAD_UNCHANGED)\n    images.append(n)\n    print (img)\nplt.imshow(images[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(images)\n#images = images.astype('float32')\nimages = images.reshape(images.shape[0],1, 50, 50, 3)\n\nsubm_predict = model.predict(images)\nsubm_predict\n\nimport gc\ndel images\n\nmysor = gc.collect()\n\n# делаем сабмит\ntest_df = test_df.drop(['target'], axis=1)\ntest_df['target'] = subm_predict.reshape(len(subm_predict)).tolist()\ntest_df.to_csv('submission.csv',index = False)\ntest_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}