{"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":"<p p style = \"font-family: garamond; font-size:40px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">G2Net Gravitational Waves</p>\n\n![](https://www.g2net.eu/wp-content/uploads/2021/03/Acoustic_sound_wave_quantum_research-1170x600.jpg)\n","metadata":{}},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:40px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">What are we discussing today? </p>\n <p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#006699; border-radius: 10px 10px; text-align:center\"> Constant Q Transform<br>\n GPU Efficient Network <br>\n FastAI <br>\n Test Time Augmentation <br>\n Weights and Biases for Experiment Tracking","metadata":{}},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:35px; font-style: normal;background-color: #f6f5f5; color :#ff0066; border-radius: 10px 10px; text-align:center\">Upvote the kernel if you find it insightful!</p>","metadata":{}},{"cell_type":"markdown","source":"<center><img src=\"https://i.imgur.com/gb6B4ig.png\" width=\"400\" alt=\"Weights & Biases\" /></center><br>\n<p style = \"font-family: garamond; font-size: 20px; font-style: normal; border-radius: 10px 10px; text-align:center\">Wandb is a developer tool for companies turn deep learning research projects into deployed software by helping teams track their models, visualize model performance and easily automate training and improving models.\nWe will use their tools to log hyperparameters and output metrics from your runs, then visualize and compare results and quickly share findings with your colleagues.<br><br>We'll be using this to train our K Fold Cross Validation and gain better insights about our training. <br><br></p>\n\n![img](https://i.imgur.com/BGgfZj3.png)","metadata":{}},{"cell_type":"code","source":"!pip install -q --upgrade wandb ","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-07-30T14:07:07.097496Z","iopub.execute_input":"2021-07-30T14:07:07.097934Z","iopub.status.idle":"2021-07-30T14:07:18.518686Z","shell.execute_reply.started":"2021-07-30T14:07:07.097840Z","shell.execute_reply":"2021-07-30T14:07:18.517766Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Import GPU Efficient Network from Github</p>","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/idstcv/GPU-Efficient-Networks.git","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-30T14:07:19.812001Z","iopub.execute_input":"2021-07-30T14:07:19.812336Z","iopub.status.idle":"2021-07-30T14:07:21.284269Z","shell.execute_reply.started":"2021-07-30T14:07:19.812302Z","shell.execute_reply":"2021-07-30T14:07:21.283228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ./GPU-Efficient-Networks","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-30T14:07:21.285976Z","iopub.execute_input":"2021-07-30T14:07:21.286330Z","iopub.status.idle":"2021-07-30T14:07:21.294763Z","shell.execute_reply.started":"2021-07-30T14:07:21.286288Z","shell.execute_reply":"2021-07-30T14:07:21.293604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import GENet","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:21.297015Z","iopub.execute_input":"2021-07-30T14:07:21.297643Z","iopub.status.idle":"2021-07-30T14:07:22.965841Z","shell.execute_reply.started":"2021-07-30T14:07:21.297602Z","shell.execute_reply":"2021-07-30T14:07:22.964848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-30T14:07:22.969281Z","iopub.execute_input":"2021-07-30T14:07:22.969584Z","iopub.status.idle":"2021-07-30T14:07:22.978228Z","shell.execute_reply.started":"2021-07-30T14:07:22.969544Z","shell.execute_reply":"2021-07-30T14:07:22.977244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Import Libraries</p>","metadata":{}},{"cell_type":"code","source":"# Python library to interact with the file system.\nimport os\n\n#Visualization\nimport plotly.express as px\n\n\n# Software library written for data manipulation and analysis.\nimport pandas as pd\n\n# fastai library for computer vision tasks\nfrom fastai.vision.all import *\nfrom fastai.callback.wandb import *\n\n# Developing and training neural network based deep learning models.\nimport torch\nfrom torch import nn\n\n# Python library for image augmentation\nimport albumentations as A\n\n\nimport wandb\nwandb.login()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-07-30T14:07:22.981420Z","iopub.execute_input":"2021-07-30T14:07:22.981925Z","iopub.status.idle":"2021-07-30T14:07:36.824620Z","shell.execute_reply.started":"2021-07-30T14:07:22.981896Z","shell.execute_reply":"2021-07-30T14:07:36.823739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Define Seed for Reproducibility</p>","metadata":{}},{"cell_type":"code","source":"def set_seed(dls, x=42): \n    random.seed(x)\n    dls.rng.seed(x) \n    np.random.seed(x)\n    torch.manual_seed(x)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    if torch.cuda.is_available(): \n        torch.cuda.manual_seed_all(x)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:36.826018Z","iopub.execute_input":"2021-07-30T14:07:36.826494Z","iopub.status.idle":"2021-07-30T14:07:36.832974Z","shell.execute_reply.started":"2021-07-30T14:07:36.826449Z","shell.execute_reply":"2021-07-30T14:07:36.831805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Load Train and Test</p>","metadata":{}},{"cell_type":"code","source":"path = Path('../input/g2net-constant-q-transformed')","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:36.834459Z","iopub.execute_input":"2021-07-30T14:07:36.834853Z","iopub.status.idle":"2021-07-30T14:07:36.843190Z","shell.execute_reply.started":"2021-07-30T14:07:36.834817Z","shell.execute_reply":"2021-07-30T14:07:36.842291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(path/'training_labels.csv')\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:36.846275Z","iopub.execute_input":"2021-07-30T14:07:36.846825Z","iopub.status.idle":"2021-07-30T14:07:37.327210Z","shell.execute_reply.started":"2021-07-30T14:07:36.846786Z","shell.execute_reply":"2021-07-30T14:07:37.326392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['id'] = train_df['id'].map(lambda x : f'{path}/train_cqt/train_cqt/{x}.png' )\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:37.329208Z","iopub.execute_input":"2021-07-30T14:07:37.329574Z","iopub.status.idle":"2021-07-30T14:07:37.727213Z","shell.execute_reply.started":"2021-07-30T14:07:37.329535Z","shell.execute_reply":"2021-07-30T14:07:37.726213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_df = train_df.sample(frac=0.1, random_state=42)\ntrain_df = train_df.reset_index(drop=True)\nlen(train_df)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:37.728694Z","iopub.execute_input":"2021-07-30T14:07:37.729062Z","iopub.status.idle":"2021-07-30T14:07:37.763804Z","shell.execute_reply.started":"2021-07-30T14:07:37.729026Z","shell.execute_reply":"2021-07-30T14:07:37.762832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Minimal EDA</p>","metadata":{}},{"cell_type":"code","source":"dist = train_df.target.map({0:'Target 0', 1:'Target 1'})\ndist = dist.value_counts()\nfig = px.pie(dist,\n             values='target',\n             names=dist.index,\n             hole=.4,title=\"Target Distribution\")\nfig.update_traces(textinfo='percent+label', pull=0.05)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:37.765156Z","iopub.execute_input":"2021-07-30T14:07:37.765508Z","iopub.status.idle":"2021-07-30T14:07:38.833478Z","shell.execute_reply.started":"2021-07-30T14:07:37.765470Z","shell.execute_reply":"2021-07-30T14:07:38.831742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Create Dataloaders</p>","metadata":{}},{"cell_type":"code","source":"# obtain the input images.\ndef get_x(r):\n    return r['id']\n\n# obtain the targets.\ndef get_y(r):\n    return r['target']","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:38.835052Z","iopub.execute_input":"2021-07-30T14:07:38.835422Z","iopub.status.idle":"2021-07-30T14:07:38.840309Z","shell.execute_reply.started":"2021-07-30T14:07:38.835385Z","shell.execute_reply":"2021-07-30T14:07:38.839264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Albumentations with FastAI</p>\n\n<p style = \"font-family: garamond; font-size: 20px; font-style: normal; border-radius: 10px 10px; text-align:center\"> The Albumentation code has been borrowed from Fastai docs. It's very common to use different transforms on the training dataset versus the validation dataset. Lets see how!<br>\nAlbumentations is a Python library for image augmentation. Image augmentation is used in deep learning and computer vision tasks to increase the quality of trained models. The purpose of image augmentation is to create new training samples from the existing data.<br><br>\n📍 Albumentations supports all common computer vision tasks such as classification, semantic segmentation, instance segmentation, object detection, and pose estimation.<br>\n📍 The library provides a simple unified API to work with all data types: images (RBG-images, grayscale images, multispectral images), segmentation masks, bounding boxes, and keypoints.<br>\n📍 The library contains more than 70 different augmentations to generate new training samples from the existing data.<br>\n📍 Albumentations is fast.<br>\n","metadata":{}},{"cell_type":"code","source":"'''AlbumentationsTransform will perform different transforms over both\n   the training and validation datasets ''' \nclass AlbumentationsTransform(RandTransform):\n    \n    '''split_idx is None, which allows for us to say when we're setting our split_idx.\n       We set an order to 2 which means any resize operations are done first before our new transform. '''\n    split_idx, order = None, 2\n    \n    def __init__(self, train_aug, valid_aug): store_attr()\n    \n    # Inherit from RandTransform, allows for us to set that split_idx in our before_call.\n    def before_call(self, b, split_idx):\n        self.idx = split_idx\n    \n    # If split_idx is 0, run the trainining augmentation, otherwise run the validation augmentation. \n    def encodes(self, img: PILImage):\n        if self.idx == 0:\n            aug_img = self.train_aug(image=np.array(img))['image']\n        else:\n            aug_img = self.valid_aug(image=np.array(img))['image']\n        return PILImage.create(aug_img)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:38.841718Z","iopub.execute_input":"2021-07-30T14:07:38.842128Z","iopub.status.idle":"2021-07-30T14:07:38.851587Z","shell.execute_reply.started":"2021-07-30T14:07:38.842079Z","shell.execute_reply":"2021-07-30T14:07:38.850351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_aug(size): \n    \n    return A.Compose([\n            # allows to combine RandomCrop and RandomScale\n            A.RandomResizedCrop(size,size),\n        \n            # Randomly apply affine transforms: translate, scale and rotate the input.\n            A.ShiftScaleRotate(p=0.5),\n        \n            # Randomly change hue, saturation and value of the input image.\n            A.HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),\n        \n            # Randomly change brightness and contrast of the input image.\n            A.RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),\n        \n            # CoarseDropout of the rectangular regions in the image.\n            A.CoarseDropout(p=0.5),\n        \n            # Cutout of the square regions in the image.\n            A.Cutout(p=0.5) ])\n\ndef get_valid_aug(size): \n    \n    return A.Compose([\n    # Crop the central part of the input.   \n    A.CenterCrop(size, size, p=1.),\n    \n    # Resize the input to the given height and width.    \n    A.Resize(size,size)], p=1.)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:38.853105Z","iopub.execute_input":"2021-07-30T14:07:38.853726Z","iopub.status.idle":"2021-07-30T14:07:38.862517Z","shell.execute_reply.started":"2021-07-30T14:07:38.853679Z","shell.execute_reply":"2021-07-30T14:07:38.861557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''The first step item_tfms resizes all the images to the same size (this happens on the CPU) \n   and then batch_tfms happens on the GPU for the entire batch of images. '''\n# Transforms we need to do for each image in the dataset\nitem_tfms = [Resize(224), AlbumentationsTransform(get_train_aug(224), get_valid_aug(224))]\n\n# Transforms that can take place on a batch of images\nbatch_tfms = [Normalize.from_stats(*imagenet_stats)]","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-07-30T14:07:38.863759Z","iopub.execute_input":"2021-07-30T14:07:38.864326Z","iopub.status.idle":"2021-07-30T14:07:44.612020Z","shell.execute_reply.started":"2021-07-30T14:07:38.864285Z","shell.execute_reply":"2021-07-30T14:07:44.611002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Preprocess and Prepare Dataloader</p>","metadata":{}},{"cell_type":"code","source":"def get_data(bs=32, data_df=train_df):\n    dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                       splitter=RandomSplitter(seed=42), # split data into training and validation subsets.\n                       get_x=get_x, # obtain the input images.\n                       get_y=get_y, # obtain the targets.\n                       item_tfms = item_tfms,\n                       batch_tfms = batch_tfms)\n    return dblock.dataloaders(data_df,bs=bs)\n\ndls = get_data()\nset_seed(dls, 42)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:44.613523Z","iopub.execute_input":"2021-07-30T14:07:44.613930Z","iopub.status.idle":"2021-07-30T14:07:55.533227Z","shell.execute_reply.started":"2021-07-30T14:07:44.613890Z","shell.execute_reply":"2021-07-30T14:07:55.532179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Visualize Stacked CQT Images</p>","metadata":{}},{"cell_type":"code","source":"# We can call show_batch() to see what a sample of a batch looks like.\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:55.534735Z","iopub.execute_input":"2021-07-30T14:07:55.535125Z","iopub.status.idle":"2021-07-30T14:07:57.104433Z","shell.execute_reply.started":"2021-07-30T14:07:55.535087Z","shell.execute_reply":"2021-07-30T14:07:57.102531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">GPU Efficient Networks</p>\n\n![](https://raw.githubusercontent.com/idstcv/GPU-Efficient-Networks/master/misc/genet_acc_speed_curve.jpg)\n\n<p style = \"font-family: garamond; font-size: 20px; font-style: normal; border-radius: 10px 10px; text-align:center\"> The proposed design space is optimized for fast GPU inference. In this space, it uses a semi-automatic NAS to help us design GPU-Efficient Networks. GENets use full convolutions in low-level stages and depth-wise\nconvolution and/or bottleneck structure in high-level stages.<br>This design is inspired by the observation that convolutional kernels in the high-level stages are more likely to have low intrinsic rank and different types of convolutions have different kinds of efficiency on GPU.<br>","metadata":{}},{"cell_type":"code","source":"model = GENet.genet_large(pretrained=True, root='../input/genetparam/')","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:07:57.106131Z","iopub.execute_input":"2021-07-30T14:07:57.107068Z","iopub.status.idle":"2021-07-30T14:08:00.145973Z","shell.execute_reply.started":"2021-07-30T14:07:57.106998Z","shell.execute_reply":"2021-07-30T14:08:00.144915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.init(project='G2Net-Fastai', job_type='train', name = 'GPU Efficient Network Large', config = {'competetion': 'G2Net','_wandb_kernel':'tang'})","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:08:00.147583Z","iopub.execute_input":"2021-07-30T14:08:00.147973Z","iopub.status.idle":"2021-07-30T14:08:06.921152Z","shell.execute_reply.started":"2021-07-30T14:08:00.147916Z","shell.execute_reply":"2021-07-30T14:08:06.920184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Group together some dls, a model, and metrics to handle training\nlearn = Learner(dls, model, metrics = RocAucBinary(), cbs=[WandbCallback(log='all'), SaveModelCallback()]) ","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:08:06.924842Z","iopub.execute_input":"2021-07-30T14:08:06.926609Z","iopub.status.idle":"2021-07-30T14:08:06.936749Z","shell.execute_reply.started":"2021-07-30T14:08:06.925304Z","shell.execute_reply":"2021-07-30T14:08:06.935737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Choosing a good learning rate\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:08:06.939289Z","iopub.execute_input":"2021-07-30T14:08:06.940352Z","iopub.status.idle":"2021-07-30T14:08:58.948756Z","shell.execute_reply.started":"2021-07-30T14:08:06.940311Z","shell.execute_reply":"2021-07-30T14:08:58.947957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We can use the fine_tune function to train a model with this given learning rate\nlearn.fine_tune(4, base_lr=0.0010000000474974513)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T14:09:07.942087Z","iopub.execute_input":"2021-07-30T14:09:07.942426Z","iopub.status.idle":"2021-07-30T15:06:41.386135Z","shell.execute_reply.started":"2021-07-30T14:09:07.942393Z","shell.execute_reply":"2021-07-30T15:06:41.385212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\"><a href = 'https://wandb.ai/tanishqgautam/G2Net-Fastai'>Check out the Weights and Biases Dashboard here $\\rightarrow$ </a></p>","metadata":{}},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Final Metrics</p><br>\n\n<center><img src=\"https://i.imgur.com/HBzvpxZ.png\" width=\"1500\" alt=\"metrics\" /></center>","metadata":{}},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Prediction Samples</p><br>\n\n<center><img src=\"https://i.imgur.com/qoFuEyk.png\" width=\"1500\" alt=\"metrics\" /></center>","metadata":{}},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Evaluation Metrics</p><br>\n\n<center><img src=\"https://i.imgur.com/wy8lpLe.png\" width=\"1500\" alt=\"metrics\" /></center>","metadata":{}},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Submission</p><br>","metadata":{}},{"cell_type":"code","source":"test_path = Path('../input/g2net-gravitational-wave-detection')\nsample = pd.read_csv(test_path/'sample_submission.csv')\nsample","metadata":{"execution":{"iopub.status.busy":"2021-07-30T15:09:44.134698Z","iopub.execute_input":"2021-07-30T15:09:44.135098Z","iopub.status.idle":"2021-07-30T15:09:44.336995Z","shell.execute_reply.started":"2021-07-30T15:09:44.135063Z","shell.execute_reply":"2021-07-30T15:09:44.335668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_sample = sample.copy()\n_sample['id'] = _sample['id'].map(lambda x: f'{path}/test_cqt/test_cqt/{x}.png')\ntest_dl = dls.test_dl(_sample)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T15:09:49.771437Z","iopub.execute_input":"2021-07-30T15:09:49.771756Z","iopub.status.idle":"2021-07-30T15:09:49.942079Z","shell.execute_reply.started":"2021-07-30T15:09:49.771728Z","shell.execute_reply":"2021-07-30T15:09:49.941223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_sample.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-30T15:09:51.649215Z","iopub.execute_input":"2021-07-30T15:09:51.649531Z","iopub.status.idle":"2021-07-30T15:09:51.663979Z","shell.execute_reply.started":"2021-07-30T15:09:51.649501Z","shell.execute_reply":"2021-07-30T15:09:51.662845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-07-30T15:09:54.482279Z","iopub.execute_input":"2021-07-30T15:09:54.482598Z","iopub.status.idle":"2021-07-30T15:09:55.915945Z","shell.execute_reply.started":"2021-07-30T15:09:54.482568Z","shell.execute_reply":"2021-07-30T15:09:55.914498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p p style = \"font-family: garamond; font-size:30px; font-style: normal;background-color: #f6f5f5; color :#6666ff; border-radius: 10px 10px; text-align:center\">Test Time Augmentation (TTA)</p>\n\n<p style = \"font-family: garamond; font-size: 20px; font-style: normal; border-radius: 10px 10px; text-align:center\"> Similar to what Data Augmentation is doing to the training set, the purpose of Test Time Augmentation is to perform random modifications to the test images. Thus, instead of showing the regular, “clean” images, only once to the trained model, we will show it the augmented images several times. We will then average the predictions of each corresponding image and take that as our final guess. <br><br>\nThe reason why it works is that, by averaging our predictions, on randomly modified images, we are also averaging the errors. The error can be big in a single vector, leading to a wrong answer, but when averaged, only the correct answer stand out.</p>","metadata":{}},{"cell_type":"code","source":"a, _ = learn.tta(dl=test_dl, n=1)\npred = a.argmax(dim=1).numpy()\nsample['target'] = pred","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-07-30T15:11:55.366869Z","iopub.execute_input":"2021-07-30T15:11:55.367259Z","iopub.status.idle":"2021-07-30T15:11:55.395455Z","shell.execute_reply.started":"2021-07-30T15:11:55.367230Z","shell.execute_reply":"2021-07-30T15:11:55.394174Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T15:12:10.308624Z","iopub.execute_input":"2021-07-30T15:12:10.308998Z","iopub.status.idle":"2021-07-30T15:12:10.837203Z","shell.execute_reply.started":"2021-07-30T15:12:10.308961Z","shell.execute_reply":"2021-07-30T15:12:10.836192Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]}]}