{"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":"# G2Net - TF Baseline\n\nStart with a simple baseline similar to [Basic spectrogram image classification](https://www.kaggle.com/code/junkoda/basic-spectrogram-image-classification). We effectively use a the time-averaged power spectrum as input images and train a simple EfficientNetB1. \n\nThe TF records have been prepared in a separate kernel [G2Net - Record Generation](https://www.kaggle.com/code/morodertobias/g2net-record-generation).\n\n## References\n- [Basic spectrogram image classification](https://www.kaggle.com/code/junkoda/basic-spectrogram-image-classification)\n- [G2Net TF-Keras Train-Test With TPU](https://www.kaggle.com/code/itsuki9180/g2net-tf-keras-train-test-with-tpu)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"#### Library used in notebook\n##### Author\n\n> #### WaveTF is developed by\n\n> #### Francesco Versaci, CRS4 francesco.versaci@gmail.com\n##### License\n\nWaveTF is licensed under the Apache License, Version 2.0. See LICENSE for further details.\n\n##### Author description\nWavelet transforms are a family of signal transformations They produce a mix of time/spatial and frequency data Countless applications, e.g., image compression, medical\nimaging, finance, geophysics, and astronomy\nThere is a growing number of applications in machine learning But there were no efficient 2D wavelet libraries available for\nKeras","metadata":{}},{"cell_type":"markdown","source":"> Understanding the wavelet transform is straightforward once you have a solid grasp on how the Fourier transform works. \n\n### Disclaimer \nIn this notebook the wavelet transform is not applied to signal : it is applied to the image or to an image as an array. \n\n\nThe Haar transform is the simplest orthogonal wavelet transform. It is computed by iterating difference and averaging between odd and even samples of the signal. Since we are in 2-D, we need to compute the average and difference in the horizontal and then in the vertical direction (or in the reverse order, it does not mind).\nmore on the subject here:\nhttps://www.numerical-tours.com/matlab/wavelet_2_haar2d/\n\n\nIn mathematics, the Haar wavelet is a sequence of rescaled \"square-shaped\" functions which together form a wavelet family or basis. Wavelet analysis is similar to Fourier analysis in that it allows a target function over an interval to be represented in terms of an orthonormal basis. The Haar sequence is now recognised as the first known wavelet basis and extensively used as a teaching example.\n\n<img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Haar_wavelet.svg/800px-Haar_wavelet.svg.png\" width=\"300\"> wikipedia: Haar wavelet\n\nApplication in simmilar cases( Image processing):\n* Image compression:\nhttps://www.idosi.org/mejsr/mejsr2(2)/7.pdf\n*  2-D multiresolution analysis with the Haar transform. It was introduced in 1910 by Haar:\nhttps://www.numerical-tours.com/matlab/wavelet_2_haar2d/\n* edge detection: Edge Detection In Images Using Haar Wavelets, Sobel, Gabor And Laplacian Filters:\nhttps://www.researchgate.net/publication/324911043_Edge_Detection_In_Images_Using_Haar_Wavelets_Sobel_Gabor_And_Laplacian_Filters\n","metadata":{}},{"cell_type":"code","source":"import os\ntarget = '/kaggle/working/smalltrain/'\n\nif not os.path.exists(target):\n    os.mkdir(target)\n# if not os.path.exists(target):\n#     os.mkdir(target)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-22T13:17:07.988331Z","iopub.execute_input":"2022-12-22T13:17:07.988858Z","iopub.status.idle":"2022-12-22T13:17:08.014121Z","shell.execute_reply.started":"2022-12-22T13:17:07.988755Z","shell.execute_reply":"2022-12-22T13:17:08.013039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\nimport re\nimport pathlib\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nprint(tf.__version__)\nfrom pydantic import BaseModel\nfrom sklearn.model_selection import train_test_split\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-22T13:17:08.015891Z","iopub.execute_input":"2022-12-22T13:17:08.016236Z","iopub.status.idle":"2022-12-22T13:17:14.186071Z","shell.execute_reply.started":"2022-12-22T13:17:08.016205Z","shell.execute_reply":"2022-12-22T13:17:14.184807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted(tf.config.list_logical_devices())","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-22T13:17:14.188192Z","iopub.execute_input":"2022-12-22T13:17:14.189277Z","iopub.status.idle":"2022-12-22T13:17:14.216321Z","shell.execute_reply.started":"2022-12-22T13:17:14.189241Z","shell.execute_reply":"2022-12-22T13:17:14.214575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Device:', tpu.master())\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.TPUStrategy(tpu)\n    print(\"TPU in use!\")\nexcept:\n    strategy = tf.distribute.get_strategy()\nprint(\"Strategy:\", strategy)\nprint(\"Number of replicas:\", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:17:14.218209Z","iopub.execute_input":"2022-12-22T13:17:14.21919Z","iopub.status.idle":"2022-12-22T13:17:14.237854Z","shell.execute_reply.started":"2022-12-22T13:17:14.218771Z","shell.execute_reply":"2022-12-22T13:17:14.236395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# INPUT_DIR_train = '/kaggle/input/360x360g2netpng/width128/train/'\n\n# INPUT_DIR_test = '/kaggle/input/360x360g2netpng/width128/test/'\n# # #360x360\n\n# INPUT_DIR_train = '/kaggle/input/360x360g2netpng/train/'\n\n# INPUT_DIR_test = '/kaggle/input/360x360g2netpng/test/'\n\nINPUT_DIR_train = '/kaggle/input/360x360g2netpng/width360/train/'\n\nINPUT_DIR_test = '/kaggle/input/360x360g2netpng/width360/test/'\n\n#right now this training set is garbage :0.457 in LB - the third channele is a.real**2\n\n# INPUT_DIR_train = '/kaggle/input/covariancedatasetg2net/width360/train/'\n\n# INPUT_DIR_test = '/kaggle/input/covariancedatasetg2net/width360/width360/test/'","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:17:14.241372Z","iopub.execute_input":"2022-12-22T13:17:14.242395Z","iopub.status.idle":"2022-12-22T13:17:14.250146Z","shell.execute_reply.started":"2022-12-22T13:17:14.24236Z","shell.execute_reply":"2022-12-22T13:17:14.247599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Config","metadata":{}},{"cell_type":"code","source":"class Config(BaseModel):\n    seed = 887\n    model_name = \"enetb1_v1\"\n    model_dir = \"/kaggle/working/model\"\n    # data\n \n    path_submission = \"/kaggle/input/g2net-detecting-continuous-gravitational-waves/sample_submission.csv\"\n\n    img_size = (360, 360)\n    channels = 3\n    img_shape = (*img_size, channels)\n    # model\n    base_model_weights = \"imagenet\"\n    dropout = 0.3\n    # training\n    shuffle_size = 128\n    epochs = 150\n#     batch_size = 16 * strategy.num_replicas_in_sync\n    batch_size = 32 * strategy.num_replicas_in_sync\n    test_batch_size = 64\n    lr = 2e-5\n    patience = 12\n    \ncfg = Config()\ncfg.dict()","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:20.798632Z","iopub.execute_input":"2022-12-22T13:23:20.799207Z","iopub.status.idle":"2022-12-22T13:23:20.813834Z","shell.execute_reply.started":"2022-12-22T13:23:20.799166Z","shell.execute_reply":"2022-12-22T13:23:20.812684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare data\n\nPrepare training, validation and test datasets.","metadata":{}},{"cell_type":"markdown","source":"## Data handling\n\nThe tensorflow records have the stacked image encoded as tensor; so in particular not normalized to fit the default image ranges. A target is present in the training data, an id in the test data.\n","metadata":{}},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:17:14.275361Z","iopub.execute_input":"2022-12-22T13:17:14.278451Z","iopub.status.idle":"2022-12-22T13:17:14.286433Z","shell.execute_reply.started":"2022-12-22T13:17:14.278394Z","shell.execute_reply":"2022-12-22T13:17:14.285048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ","metadata":{}},{"cell_type":"markdown","source":"#### Creating a dataset from directory\nCalling image_dataset_from_directory(main_directory, labels='inferred') will return a tf.data.Dataset that yields batches of images from the subdirectories class_a and class_b (signal or noise in our case), together with labels 0 and 1 (0 corresponding to class_a and 1 corresponding to class_b).\n\nSupported image formats: jpeg, png, bmp, gif. Animated gifs are truncated to the first frame.","metadata":{}},{"cell_type":"code","source":"image_size = (360, 360)\nbatch_size = 32\n\ntrain_ds = tf.keras.preprocessing.image_dataset_from_directory(\n    INPUT_DIR_train,\n    validation_split=0.2,\n    subset=\"training\",\n    seed=1337,\n    shuffle=True,\n    image_size=image_size,\n    batch_size=batch_size,\n)\nval_ds = tf.keras.preprocessing.image_dataset_from_directory(\n    INPUT_DIR_train,\n    validation_split=0.2,\n    subset=\"validation\",\n    seed=1337,\n    shuffle=True,\n    image_size=image_size,\n    batch_size=batch_size,\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:17:14.287862Z","iopub.execute_input":"2022-12-22T13:17:14.288482Z","iopub.status.idle":"2022-12-22T13:17:18.515247Z","shell.execute_reply.started":"2022-12-22T13:17:14.288434Z","shell.execute_reply":"2022-12-22T13:17:18.514114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test csv is just an input of id's of test files. why is in that data set? laziness:)","metadata":{}},{"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/360x360g2netpng/test_files.csv')\n#test=test.rename(columns={0: \"calea\", 1: \"id\"})\ntest = test.loc[:, ~test.columns.str.contains('^Unnamed')]\ntest=test.rename(columns={'0': \"calea\", '1': \"id\"}, errors=\"raise\")\n# test.calea=test.calea.str.replace('/kaggle/working/test/',\"/kaggle/input/360x360g2netpng/test/\")\ntest.calea=test.calea.str.replace('/kaggle/working/test/',INPUT_DIR_test)\n# /kaggle/input/360x360g2netpng/width128/train/\n\n\n#test.calea=test.calea.str.replace('.jpg',\".png\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:17:18.516904Z","iopub.execute_input":"2022-12-22T13:17:18.51762Z","iopub.status.idle":"2022-12-22T13:17:18.586873Z","shell.execute_reply.started":"2022-12-22T13:17:18.517586Z","shell.execute_reply":"2022-12-22T13:17:18.585779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Verify the content of test dataframe","metadata":{}},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:17:18.588341Z","iopub.execute_input":"2022-12-22T13:17:18.588709Z","iopub.status.idle":"2022-12-22T13:17:18.60555Z","shell.execute_reply.started":"2022-12-22T13:17:18.588678Z","shell.execute_reply":"2022-12-22T13:17:18.604475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check for the file format. This instance of machine has a tensorflow-png conflict. a set of warning is issed- cand be solved upgrading tensorflow. ","metadata":{}},{"cell_type":"code","source":"test.calea[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:17:18.61039Z","iopub.execute_input":"2022-12-22T13:17:18.61075Z","iopub.status.idle":"2022-12-22T13:17:18.618081Z","shell.execute_reply.started":"2022-12-22T13:17:18.610718Z","shell.execute_reply":"2022-12-22T13:17:18.616908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras_preprocessing.image import ImageDataGenerator\n# # image_size = (360, 360)\n# # batch_size = 16\n# # test_ds = tf.keras.preprocessing.image_dataset_from_directory(\n# #     \"/kaggle/input/360x360g2netpng/test/\",\n# #     shuffle=False,\n# #     validation_split=None,\n# #     class_names=None,\n# #     image_size=image_size,\n# #     batch_size=batch_size,\n# # )\n# test_datagen=ImageDataGenerator(rescale=1./255.)\n# test_generator=test_datagen.flow_from_dataframe(\n# dataframe=test,\n\n# x_col=\"calea\",\n# y_col=None,\n# batch_size=32,\n# seed=42,\n# shuffle=False,\n# class_mode=None,\n# target_size=(360,360))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-22T13:17:18.619629Z","iopub.execute_input":"2022-12-22T13:17:18.620489Z","iopub.status.idle":"2022-12-22T13:17:18.627851Z","shell.execute_reply.started":"2022-12-22T13:17:18.620446Z","shell.execute_reply":"2022-12-22T13:17:18.626647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Check dataset","metadata":{}},{"cell_type":"markdown","source":"WaveTF","metadata":{}},{"cell_type":"markdown","source":"# Neural network\n","metadata":{}},{"cell_type":"code","source":"# from tensorflow.keras.applications.efficientnet import EfficientNetB1 as BaseModel\n# from tensorflow.keras.applications.efficientnet import preprocess_input\nfrom tensorflow.keras import layers, Model, Input, losses, metrics, optimizers, callbacks","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-22T13:23:33.060929Z","iopub.execute_input":"2022-12-22T13:23:33.061348Z","iopub.status.idle":"2022-12-22T13:23:33.06673Z","shell.execute_reply.started":"2022-12-22T13:23:33.061315Z","shell.execute_reply":"2022-12-22T13:23:33.065913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def unfreeze_model(model):\n#     for layer in model.layers:\n#         if not isinstance(layer, tf.keras.layers.BatchNormalization):\n#             layer.trainable = True\n#         else:\n#             layer.trainable = False\n\n\n# def create_model():       \n#     inputs = Input(shape=cfg.img_shape, dtype=tf.float32)\n#     x = layers.Conv2D(3, 1, name='adjustment')(inputs)\n#     x = layers.Lambda(preprocess_input, name=\"preprocess_input\")(x)\n#     base_model = BaseModel(include_top=False, weights=cfg.base_model_weights, pooling=\"avg\")\n#     base_model.trainable = False\n#     # unfreeze_model(base_model)\n#     x = base_model(x)\n#     x = layers.BatchNormalization()(x)\n#     x = layers.Dropout(cfg.dropout, name=\"top_dropout\")(x)\n#     outputs = layers.Dense(1, activation=\"sigmoid\", name=\"logits\")(x)\n#     model = Model(inputs=inputs, outputs=outputs, name=cfg.model_name)\n#     model.compile(\n#         optimizer=optimizers.Adam(learning_rate=cfg.lr),\n#         loss=losses.BinaryCrossentropy(), \n#         metrics=['acc']\n#     )\n#     return model","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-22T13:23:33.335412Z","iopub.execute_input":"2022-12-22T13:23:33.335864Z","iopub.status.idle":"2022-12-22T13:23:33.34271Z","shell.execute_reply.started":"2022-12-22T13:23:33.335827Z","shell.execute_reply":"2022-12-22T13:23:33.341236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Demo of WaveTF layer","metadata":{"execution":{"iopub.status.busy":"2022-11-19T09:49:48.999348Z","iopub.execute_input":"2022-11-19T09:49:48.999677Z","iopub.status.idle":"2022-11-19T09:49:52.017972Z","shell.execute_reply.started":"2022-11-19T09:49:48.999641Z","shell.execute_reply":"2022-11-19T09:49:52.016952Z"}}},{"cell_type":"markdown","source":"### install the wavetf package from kaggle ( see the datasets) \nProceed like in any other case for packages installed from dataset in kaggle notebooks.","metadata":{}},{"cell_type":"code","source":"package='/kaggle/input/wavetf/WaveTF-master/* ./'\n#!cp -r ../input/kagglepet/kagglePet/* ./\n!cp -r /kaggle/input/wavetf/WaveTF-master/* ./\n!cp -r /kaggle/input/wavetf/WaveTF-master/wavetf/* ./","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:34.451221Z","iopub.execute_input":"2022-12-22T13:23:34.452288Z","iopub.status.idle":"2022-12-22T13:23:36.718257Z","shell.execute_reply.started":"2022-12-22T13:23:34.452239Z","shell.execute_reply":"2022-12-22T13:23:36.716873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"import the package wavetf","metadata":{}},{"cell_type":"code","source":"import wavetf\nfrom wavetf import WaveTFFactory","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:36.720424Z","iopub.execute_input":"2022-12-22T13:23:36.72153Z","iopub.status.idle":"2022-12-22T13:23:36.727378Z","shell.execute_reply.started":"2022-12-22T13:23:36.721469Z","shell.execute_reply":"2022-12-22T13:23:36.726138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Demo wave TF","metadata":{}},{"cell_type":"markdown","source":"Reference demo from original paper of Francesco Versaci with some minor modifications: dtype and size\nBasic a random tensor is generated, a waveTF layer is called and after thet it. is applied the inveser function. \n","metadata":{}},{"cell_type":"code","source":"# input tensor\nt0 = tf. random.uniform([32, 360, 360, 3], dtype=tf.float32 )\n# transform\nw = WaveTFFactory().build('db2', dim=2)\nt1 = w.call(t0)\n# anti−transform\nw_i = WaveTFFactory().build('db2', dim=2, inverse=True)\nt2 = w_i.call(t1)\n# compute difference\n# delta = abs(t2−t0)\n# print ( f ' Precision error : { tf .math.reduce_max(delta)}\ndelta=abs(t2-t0)\n\nprint(f\"Precision error :{tf .math.reduce_max(delta)}.\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:36.729374Z","iopub.execute_input":"2022-12-22T13:23:36.730045Z","iopub.status.idle":"2022-12-22T13:23:38.598629Z","shell.execute_reply.started":"2022-12-22T13:23:36.729998Z","shell.execute_reply":"2022-12-22T13:23:38.597751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load an image from dataset","metadata":{}},{"cell_type":"code","source":"from PIL import Image\n# from IPython.display import  Image, display\n# from PIL import ImageShow\nimage = Image.open('/kaggle/input/covariancedatasetg2net/width360/train/signal/sintetic_train1005.jpg')\n \n# summarize some details about the image\nprint(image.format)\nprint(image.size)\nprint(image.mode)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:38.600348Z","iopub.execute_input":"2022-12-22T13:23:38.600691Z","iopub.status.idle":"2022-12-22T13:23:38.610622Z","shell.execute_reply.started":"2022-12-22T13:23:38.600661Z","shell.execute_reply":"2022-12-22T13:23:38.609236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load an image from dataset and plot that image","metadata":{}},{"cell_type":"code","source":"from IPython.display import display\nfrom PIL import Image\n\n\npath=\"/kaggle/input/covariancedatasetg2net/width360/train/signal/sintetic_train1005.jpg\"\nimage = Image.open(path)\n \n# summarize some details about the image\nprint(image.format)\nprint(image.size)\nprint(image.mode)\ndisplay(Image.open(path))","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:38.612144Z","iopub.execute_input":"2022-12-22T13:23:38.61478Z","iopub.status.idle":"2022-12-22T13:23:38.670369Z","shell.execute_reply.started":"2022-12-22T13:23:38.61474Z","shell.execute_reply":"2022-12-22T13:23:38.669029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### from image to tensor\nthis is just for fun","metadata":{}},{"cell_type":"markdown","source":"##### Plotting Images from first batch of train dataset","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfont = {'family' : 'normal',\n        'weight' : 'bold',\n        'size'   : 7}\nplt.rc('font', **font)\nplt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n      for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        image_path='/kaggle/working/smalltrain/'\n        filename=image_path+\"imagine\"+str(i)+\".jpg\"\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(labels[i].numpy())\n#         £print(labels[i].numpy())\n        plt.axis(\"off\")\n        plt.savefig(filename)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:38.672029Z","iopub.execute_input":"2022-12-22T13:23:38.679308Z","iopub.status.idle":"2022-12-22T13:23:44.416147Z","shell.execute_reply.started":"2022-12-22T13:23:38.679255Z","shell.execute_reply":"2022-12-22T13:23:44.415218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### tensor to numpy \ntensor to numpy it may be usefull","metadata":{}},{"cell_type":"code","source":"for images, labels in train_ds.take(1):  # only take first element of dataset\n    numpy_images = images.numpy()\n    numpy_labels = labels.numpy()","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:44.417594Z","iopub.execute_input":"2022-12-22T13:23:44.418611Z","iopub.status.idle":"2022-12-22T13:23:45.691372Z","shell.execute_reply.started":"2022-12-22T13:23:44.418569Z","shell.execute_reply":"2022-12-22T13:23:45.690232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"check the shape","metadata":{}},{"cell_type":"code","source":"numpy_images[1].shape","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:45.69671Z","iopub.execute_input":"2022-12-22T13:23:45.697094Z","iopub.status.idle":"2022-12-22T13:23:45.704326Z","shell.execute_reply.started":"2022-12-22T13:23:45.697059Z","shell.execute_reply":"2022-12-22T13:23:45.7033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"check the shapes of the dataset","metadata":{}},{"cell_type":"code","source":"for image_batch, labels_batch in train_ds:\n              print(image_batch.shape)\n              print(labels_batch.shape)\n              break","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:45.705762Z","iopub.execute_input":"2022-12-22T13:23:45.706101Z","iopub.status.idle":"2022-12-22T13:23:46.982456Z","shell.execute_reply.started":"2022-12-22T13:23:45.706069Z","shell.execute_reply":"2022-12-22T13:23:46.981378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load an image","metadata":{}},{"cell_type":"code","source":"path=\"/kaggle/input/covariancedatasetg2net/width360/train/signal/sintetic_train1005.jpg\"\nimage = Image.open(path)\nimage=np.asarray(image).astype(float)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:46.983924Z","iopub.execute_input":"2022-12-22T13:23:46.984706Z","iopub.status.idle":"2022-12-22T13:23:46.998218Z","shell.execute_reply.started":"2022-12-22T13:23:46.984667Z","shell.execute_reply":"2022-12-22T13:23:46.996934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### transform image to float64 tensor","metadata":{}},{"cell_type":"code","source":"img_to_tensor = tf.convert_to_tensor(image,dtype=tf.float64)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:46.999703Z","iopub.execute_input":"2022-12-22T13:23:47.000045Z","iopub.status.idle":"2022-12-22T13:23:47.006301Z","shell.execute_reply.started":"2022-12-22T13:23:47.000013Z","shell.execute_reply":"2022-12-22T13:23:47.005111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The layer take batches of images. So for a single image add batch axis:\nAdding batch size to one image\"\nhttps://www.tensorflow.org/api_docs/python/tf/image/resize","metadata":{}},{"cell_type":"markdown","source":"check the shape a of the image with batch dimension added:","metadata":{}},{"cell_type":"code","source":"image = img_to_tensor[tf.newaxis, ...,]\nimage.shape.as_list()  # [batch, height, width, channels]","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:47.007722Z","iopub.execute_input":"2022-12-22T13:23:47.008051Z","iopub.status.idle":"2022-12-22T13:23:47.019565Z","shell.execute_reply.started":"2022-12-22T13:23:47.00802Z","shell.execute_reply":"2022-12-22T13:23:47.018434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Initialize a layer","metadata":{}},{"cell_type":"code","source":"w = WaveTFFactory().build('haar', dim=2)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:47.020943Z","iopub.execute_input":"2022-12-22T13:23:47.021279Z","iopub.status.idle":"2022-12-22T13:23:47.032302Z","shell.execute_reply.started":"2022-12-22T13:23:47.021248Z","shell.execute_reply":"2022-12-22T13:23:47.031161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Apply  wave transform calling the keras layer","metadata":{}},{"cell_type":"code","source":"t1 = w.call(image)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:38:54.632312Z","iopub.execute_input":"2022-12-22T13:38:54.633206Z","iopub.status.idle":"2022-12-22T13:38:54.667422Z","shell.execute_reply.started":"2022-12-22T13:38:54.633169Z","shell.execute_reply":"2022-12-22T13:38:54.666498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check the shape of the resulted layer:","metadata":{}},{"cell_type":"code","source":"t1.shape[3]","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:38:56.749828Z","iopub.execute_input":"2022-12-22T13:38:56.750437Z","iopub.status.idle":"2022-12-22T13:38:56.755963Z","shell.execute_reply.started":"2022-12-22T13:38:56.750403Z","shell.execute_reply":"2022-12-22T13:38:56.755111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t1.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:38:57.407727Z","iopub.execute_input":"2022-12-22T13:38:57.408448Z","iopub.status.idle":"2022-12-22T13:38:57.415248Z","shell.execute_reply.started":"2022-12-22T13:38:57.40841Z","shell.execute_reply":"2022-12-22T13:38:57.414228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Plot one output","metadata":{}},{"cell_type":"code","source":"t1","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:39:10.729025Z","iopub.execute_input":"2022-12-22T13:39:10.72943Z","iopub.status.idle":"2022-12-22T13:39:10.740249Z","shell.execute_reply.started":"2022-12-22T13:39:10.729395Z","shell.execute_reply":"2022-12-22T13:39:10.738897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams[\"figure.figsize\"] = (7,7)\nplt.imshow(t1[0,:,:,1].numpy().astype(\"uint8\"))\n","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:53:53.628303Z","iopub.execute_input":"2022-12-22T13:53:53.628743Z","iopub.status.idle":"2022-12-22T13:53:54.025335Z","shell.execute_reply.started":"2022-12-22T13:53:53.628704Z","shell.execute_reply":"2022-12-22T13:53:54.024118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Plotting the output of WaveTF layer \nthe resulted images are 180x180 \nefficient and hardware-friendly Haar-wavelet transform, known for its effectiveness in image compression ","metadata":{}},{"cell_type":"markdown","source":"https://github.com/ramanpreetSinghKhinda/CSE_573_Wavelet_Edge_Detection/blob/41a15a8462273ada8a7783ca8f71a8cefad7016b/Wavelet_Edge_Detection_Project.py#L256","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor i in range(t1.shape[3]):\n    ax = plt.subplot(4, 3, i + 1)\n    image_path='/kaggle/working/smalltrain/'\n    plt.imshow(t1[0,:,:,i].numpy().astype(\"uint8\"))\n    plt.title(i)\n    #         £print(labels[i].numpy())\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:23:47.414207Z","iopub.execute_input":"2022-12-22T13:23:47.414959Z","iopub.status.idle":"2022-12-22T13:23:48.741833Z","shell.execute_reply.started":"2022-12-22T13:23:47.414926Z","shell.execute_reply":"2022-12-22T13:23:48.740883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"the error between initial image and inverse transfor is not like in the paper \nit is orders of magnitude: bigger","metadata":{}},{"cell_type":"markdown","source":"Thresholding","metadata":{}},{"cell_type":"code","source":"t1","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:56:03.886999Z","iopub.execute_input":"2022-12-22T13:56:03.88743Z","iopub.status.idle":"2022-12-22T13:56:03.898609Z","shell.execute_reply.started":"2022-12-22T13:56:03.887391Z","shell.execute_reply":"2022-12-22T13:56:03.89734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = tf.where(t1<220, 0.0,t1)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:59:17.122003Z","iopub.execute_input":"2022-12-22T13:59:17.122391Z","iopub.status.idle":"2022-12-22T13:59:17.129963Z","shell.execute_reply.started":"2022-12-22T13:59:17.122359Z","shell.execute_reply":"2022-12-22T13:59:17.128741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = tf.where(result<400, 255,t1)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:59:17.269157Z","iopub.execute_input":"2022-12-22T13:59:17.26957Z","iopub.status.idle":"2022-12-22T13:59:17.276567Z","shell.execute_reply.started":"2022-12-22T13:59:17.269527Z","shell.execute_reply":"2022-12-22T13:59:17.275175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:59:17.556453Z","iopub.execute_input":"2022-12-22T13:59:17.556849Z","iopub.status.idle":"2022-12-22T13:59:17.567206Z","shell.execute_reply.started":"2022-12-22T13:59:17.556814Z","shell.execute_reply":"2022-12-22T13:59:17.565805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w_i = WaveTFFactory().build('haar', dim=2, inverse=True)\nt3 = w_i.call(result)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:59:17.754586Z","iopub.execute_input":"2022-12-22T13:59:17.754976Z","iopub.status.idle":"2022-12-22T13:59:17.793573Z","shell.execute_reply.started":"2022-12-22T13:59:17.754945Z","shell.execute_reply":"2022-12-22T13:59:17.792545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams[\"figure.figsize\"] = (7,7)\nplt.imshow(t3[0].numpy().astype(\"uint8\"))","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:59:18.41402Z","iopub.execute_input":"2022-12-22T13:59:18.414427Z","iopub.status.idle":"2022-12-22T13:59:18.826658Z","shell.execute_reply.started":"2022-12-22T13:59:18.41438Z","shell.execute_reply":"2022-12-22T13:59:18.825203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w_i = WaveTFFactory().build('haar', dim=2, inverse=True)\nt2 = w_i.call(t1)\n# compute difference\n# delta = abs(t2−t0)\n# print ( f ' Precision error : { tf .math.reduce_max(delta)}\n#delta=abs(t2-t0)\n\n#print(f\"Precision error :{tf .math.reduce_max(delta)}.\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:59:19.423337Z","iopub.execute_input":"2022-12-22T13:59:19.423751Z","iopub.status.idle":"2022-12-22T13:59:19.464254Z","shell.execute_reply.started":"2022-12-22T13:59:19.423717Z","shell.execute_reply":"2022-12-22T13:59:19.46323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Inverse transform of the output layer","metadata":{}},{"cell_type":"code","source":" plt.imshow(t2[0].numpy().astype(\"uint8\"))","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:19.569762Z","iopub.execute_input":"2022-12-17T11:07:19.57047Z","iopub.status.idle":"2022-12-17T11:07:19.83841Z","shell.execute_reply.started":"2022-12-17T11:07:19.570437Z","shell.execute_reply":"2022-12-17T11:07:19.837479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Original image for comapration. ","metadata":{}},{"cell_type":"code","source":"display(Image.open(path))","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:19.840198Z","iopub.execute_input":"2022-12-17T11:07:19.840589Z","iopub.status.idle":"2022-12-17T11:07:19.881992Z","shell.execute_reply.started":"2022-12-17T11:07:19.840538Z","shell.execute_reply":"2022-12-17T11:07:19.881265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Comparing the two images :it is worth testing WaveTF at least for preprocessing or as ca convolution layer(to be tested). ","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# def mse(img1, img2):\n#             h, w ,c = img1.shape\n#             red1=img1[:,:,0]\n#             red2=img2[:,:,0]\n# #             diff = cv2.subtract(img1, img2)\n#             diff = cv2.subtract(red1, red2)\n#             err = np.sum(diff**2)\n#             mse = err/(float(h*w))\n#             return mse","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:19.883169Z","iopub.execute_input":"2022-12-17T11:07:19.883632Z","iopub.status.idle":"2022-12-17T11:07:19.887741Z","shell.execute_reply.started":"2022-12-17T11:07:19.883602Z","shell.execute_reply":"2022-12-17T11:07:19.886777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import cv2\n# img1=t2[0].numpy().astype(\"uint8\")\n# print(img1.shape)\n# img2 = cv2.imread(path)\n# # img2=Image.open(path)\n# print(img2.shape)\n# error = mse(img1, img2)\n# print(error)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:19.889297Z","iopub.execute_input":"2022-12-17T11:07:19.890066Z","iopub.status.idle":"2022-12-17T11:07:19.898504Z","shell.execute_reply.started":"2022-12-17T11:07:19.890027Z","shell.execute_reply":"2022-12-17T11:07:19.897516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n    \n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import h5py\n# from path import Path\n# def read_data_c(file):\n#     file = Path(file)\n#     with h5py.File(file, \"r\") as f:\n#         filename = file.stem\n#         f = f[filename]\n#         h1 = f[\"H1\"]\n#         l1 = f[\"L1\"]\n#         freq_hz = f[\"frequency_Hz\"]\n        \n#         h1_stft = h1[\"SFTs\"][()]\n#         h1_timestamp = h1[\"timestamps_GPS\"][()]\n#         # H2 data\n#         l1_stft = l1[\"SFTs\"][()]\n#         l1_timestamp = l1[\"timestamps_GPS\"][()]\n        \n#         return [h1_stft, h1_timestamp],            [l1_stft, l1_timestamp], np.array(freq_hz)\n# [h1_stft, h1_timestamp], [l1_stft, l1_timestamp], freq_hz=read_data_c('/kaggle/input/g2net-detecting-continuous-gravitational-waves/train/0367dc82c.hdf5')\n#What if?\n# def get_back_image(h1_stft):\n#     img=np.zeros([360,4320,3])\n#     a=h1_stft[:,:4320]\n#     r=np.absolute(a)\n#     g=a.real\n#     b=a.imag\n#     img[:,:,0]=r\n#     img[:,:,1]=b\n#     img[:,:,2]=g\n#     return img\n# img=get_back_image(h1_stft)\n# img_to_tensor = tf.convert_to_tensor(img,dtype=tf.float64)\n# imagine = img_to_tensor[tf.newaxis, ...,]\n# imagine.shape.as_list()  # [batch, height, width, channels]\n# t1x = w.call(imagine)\n# t1x = w.call(t1x)\n# t1x = w.call(t1x)\n# t1x = w.call(t1x)\n# plt.figure(figsize=(15, 10))\n# plt.imshow(t1x[0,:,:,0].numpy().astype(\"uint8\"))\n# plt.figure(figsize=(30, 46))\n# for i in range(t1.shape[3]):\n#     ax = plt.subplot(20,1, i + 1)\n#     image_path='/kaggle/working/smalltrain/'\n#     plt.imshow(t1x[0,:,:,i].numpy().astype(\"uint8\"))\n#     plt.title(i)\n#     #         £print(labels[i].numpy())\n#     plt.axis(\"off\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-17T11:07:19.900286Z","iopub.execute_input":"2022-12-17T11:07:19.900937Z","iopub.status.idle":"2022-12-17T11:07:19.910687Z","shell.execute_reply.started":"2022-12-17T11:07:19.900904Z","shell.execute_reply":"2022-12-17T11:07:19.909626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Reference Model  of WaveTF ","metadata":{}},{"cell_type":"code","source":"# Copyright 2020 CRS4 (http://www.crs4.it/)\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport math\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras import regularizers\nfrom tensorflow.keras.layers import Input, Conv2D, Concatenate, Dense, Lambda, BatchNormalization, GlobalAveragePooling2D, Activation,MaxPooling2D\nfrom tensorflow.keras.models import Model\nfrom wavetf import WaveTFFactory\n\n# def wavelet_cnn(input_shape, ks=3, baselev=4, wavelet=True,\n#                 wave_kern='db2', hsv=True, convrep=2, num_classes=2):\n#def wavelet_cnn(input_shape, ks=3, baselev=12, wavelet=True,\ndef wavelet_cnn(input_shape, ks=3, baselev=4, wavelet=True,\n                wave_kern='haar', hsv=True, convrep=4, num_classes=2):\n    inputs = Input(input_shape)\n    chans = input_shape[2] # number of channels, e.g., 3 if RGB\n    bl = baselev\n\n    # wavelet computation\n    if (wavelet) :\n        # convert RGB to HSV?\n        if (hsv):\n            wave0 = Lambda(lambda x: tf.image.rgb_to_hsv(x))(inputs)\n        else:\n            wave0 = inputs\n        # compute 4 level of wavelet\n        wave1 = WaveTFFactory.build(wave_kern)(wave0)\n        # compute new wavelet features from LL componenents\n        wave2 = WaveTFFactory.build(wave_kern)(wave1[:,:,:,:chans]) \n        wave3 = WaveTFFactory.build(wave_kern)(wave2[:,:,:,:chans])\n        wave4 = WaveTFFactory.build(wave_kern)(wave3[:,:,:,:chans])\n        # normalize\n        waves = [wave1, wave2, wave3, wave4]\n        for l in waves :\n            l = BatchNormalization()(l)\n    else :\n        wave1 = wave2 = wave3 = wave4 = None\n\n    kinit ='glorot_normal' # 'he_normal'\n\n    def rep_conv(cnn, scale = 1) :\n        for i in range(convrep) :\n            cnn = Conv2D(scale * bl, ks, activation = 'relu', padding = 'same',\n                         kernel_initializer = kinit)(cnn)\n#             cnn= MaxPooling2D(3, strides=2, padding=\"same\")(cnn)\n        return cnn\n\n    def pool_down(cnn, mul):\n        cnn = Conv2D(mul * bl, ks, activation = 'relu', padding = 'same',\n                     kernel_initializer = kinit, strides=(2, 2))(cnn)\n        return (cnn)\n    def max_poolingX(cnn, scale = 1):\n        cnn=cnn, MaxPooling2D(2, strides=2, padding=\"same\")(cnn)\n        return cnn\n    cnn = inputs\n    cnn = rep_conv(cnn, 1)\n#     cnn = max_poolingX(cnn, 1)\n    #cnn= MaxPooling2D(3, strides=2, padding=\"same\")(cnn)\n    for l in range(4) :\n        cnn = pool_down(cnn, 2**(l+1))\n        cnn = rep_conv(cnn, 2**(l+1))\n        if (wavelet):\n             cnn = Concatenate(axis=3)([cnn, waves[l]])\n            #cnn = Concatenate(axis=3)([waves[l],cnn ])\n\n    # output\n    cnn = Conv2D(2048, ks)(cnn)\n    cnn = Activation('relu')(cnn)\n    cnn = GlobalAveragePooling2D()(cnn)\n#     outputs = Dense(num_classes, activation='softmax')(cnn)\n    outputs = Dense(1, activation='sigmoid')(cnn)\n#     outputs = Dense(1, activation='tanh')(cnn)\n    model = Model(inputs = inputs, outputs = outputs)\n    return model\n\n#wavelet_cnn([128,128,3])\n#wavelet_cnn([128,128,3], wave_kern='haar')","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:19.912288Z","iopub.execute_input":"2022-12-17T11:07:19.912632Z","iopub.status.idle":"2022-12-17T11:07:19.933648Z","shell.execute_reply.started":"2022-12-17T11:07:19.912599Z","shell.execute_reply":"2022-12-17T11:07:19.93267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session()\nwith strategy.scope():\n     model1=wavelet_cnn([360,360,3], wave_kern='haar')\n#    model1=wavelet_cnn([360,360,3])\nmodel1.summary()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:19.935226Z","iopub.execute_input":"2022-12-17T11:07:19.935801Z","iopub.status.idle":"2022-12-17T11:07:20.91885Z","shell.execute_reply.started":"2022-12-17T11:07:19.935767Z","shell.execute_reply":"2022-12-17T11:07:20.917766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model1, to_file=\"my_model.png\", show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:20.920663Z","iopub.execute_input":"2022-12-17T11:07:20.92128Z","iopub.status.idle":"2022-12-17T11:07:22.324646Z","shell.execute_reply.started":"2022-12-17T11:07:20.921223Z","shell.execute_reply":"2022-12-17T11:07:22.323509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(model1.summary())","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:22.326714Z","iopub.execute_input":"2022-12-17T11:07:22.327596Z","iopub.status.idle":"2022-12-17T11:07:22.333585Z","shell.execute_reply.started":"2022-12-17T11:07:22.327529Z","shell.execute_reply":"2022-12-17T11:07:22.331291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"def get_callbacks():\n    cbs = [\n        callbacks.EarlyStopping(\n            monitor=\"val_loss\",\n            verbose=1,\n            patience=cfg.patience,\n            restore_best_weights=True,\n        ),\n    ]\n    return cbs\n\n\ndef show_history(history):\n    history_frame = pd.DataFrame(history.history)\n    history_frame.index = pd.RangeIndex(1, len(history_frame) + 1, name=\"epoch\")\n    display(history_frame.style\\\n        .highlight_min(color='lightgreen', subset=['val_loss'])\\\n        .highlight_max(color='lightgreen', subset=['val_acc'])\n    )\n    fig, ax = plt.subplots(1, 2, figsize=(16, 6))\n    history_frame.loc[:, ['loss', 'val_loss']].plot(ax=ax[0], title='loss')\n    history_frame.loc[:, ['acc', 'val_acc']].plot(ax=ax[1], title='acc')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:22.335016Z","iopub.execute_input":"2022-12-17T11:07:22.335568Z","iopub.status.idle":"2022-12-17T11:07:22.350965Z","shell.execute_reply.started":"2022-12-17T11:07:22.335527Z","shell.execute_reply":"2022-12-17T11:07:22.349923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_steps = count_data_items(train_files) // cfg.batch_size\n# print(\"steps per epoch:\", train_steps)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:22.352621Z","iopub.execute_input":"2022-12-17T11:07:22.353017Z","iopub.status.idle":"2022-12-17T11:07:22.366038Z","shell.execute_reply.started":"2022-12-17T11:07:22.352984Z","shell.execute_reply":"2022-12-17T11:07:22.365146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from IPython.display import clear_output\n\n# !pip install -q tensorflow==2.4.1\n\n# clear_output()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:22.369497Z","iopub.execute_input":"2022-12-17T11:07:22.369817Z","iopub.status.idle":"2022-12-17T11:07:22.377179Z","shell.execute_reply.started":"2022-12-17T11:07:22.369791Z","shell.execute_reply":"2022-12-17T11:07:22.376253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.compile(\n        optimizer=optimizers.SGD(learning_rate=cfg.lr),\n#         loss=losses.SquaredHinge(), \n        loss=losses.BinaryCrossentropy(), \n#         loss=losses.CategoricalCrossentropy(),     \n        metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2022-12-17T11:07:22.379012Z","iopub.execute_input":"2022-12-17T11:07:22.379471Z","iopub.status.idle":"2022-12-17T11:07:22.399392Z","shell.execute_reply.started":"2022-12-17T11:07:22.37943Z","shell.execute_reply":"2022-12-17T11:07:22.398485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nhistory = model1.fit(\n    train_ds, \n    epochs=cfg.epochs,\n    validation_data=val_ds,\n    callbacks=get_callbacks(),\n    verbose=2\n)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-17T11:07:22.402237Z","iopub.execute_input":"2022-12-17T11:07:22.403172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_history(history)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.save(cfg.model_dir)\nos.listdir(cfg.model_dir)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test\n\nPredict on test dataset.","metadata":{}},{"cell_type":"code","source":"INPUT_DIR_submission='../input/g2net-detecting-continuous-gravitational-waves/sample_submission.csv'\nsample_submission=pd.read_csv(INPUT_DIR_submission)\nsample_submission.tail()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" test.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" test.calea[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idg = ImageDataGenerator()\ntest_generator  = idg.flow_from_dataframe(\n    test,\n    INPUT_DIR_test,\n    x_col = \"calea\",\n    y_col =None,\n    class_mode=None,\n    shuffle = False,\n    target_size=(360, 360),\n    batch_size=64)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# val_ds = val_ds.prefetch(tf.data.AUTOTUNE)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model1.predict(test_generator, verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['probability'] = preds","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['probability']=sample_submission['id'].map(test.set_index('id')['probability'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['target']=sample_submission['probability']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" sample_submission.describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample_submission[sample_submission.id=='bf06b17c8']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data=sample_submission, x=\"target\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = sample_submission.drop(columns=['probability'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Check submission","metadata":{"execution":{"iopub.status.busy":"2022-11-19T12:46:13.250228Z","iopub.execute_input":"2022-11-19T12:46:13.250498Z","iopub.status.idle":"2022-11-19T12:46:13.27234Z","shell.execute_reply.started":"2022-11-19T12:46:13.250469Z","shell.execute_reply":"2022-11-19T12:46:13.271668Z"}}},{"cell_type":"code","source":"sample_submission[\"target\"].describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Write submission","metadata":{}},{"cell_type":"code","source":"sample_submission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head submission.csv","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}