{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":37077,"databundleVersionId":4333111,"sourceType":"competition"},{"sourceId":4636143,"sourceType":"datasetVersion","datasetId":2696714}],"dockerImageVersionId":30301,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from google.colab import drive\n\ndrive.mount('/content/drive')","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# !pip install torchinfo\n\n# Use timm pretrained image model\n!pip install timm\n!pip install nexusformat\n!pip install git+https://github.com/PyFstat/PyFstat@python37","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:12:03.060921Z","iopub.execute_input":"2022-12-02T09:12:03.061348Z","iopub.status.idle":"2022-12-02T09:13:04.020811Z","shell.execute_reply.started":"2022-12-02T09:12:03.061262Z","shell.execute_reply":"2022-12-02T09:13:04.019169Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install torchinfo","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:13:04.02465Z","iopub.execute_input":"2022-12-02T09:13:04.025053Z","iopub.status.idle":"2022-12-02T09:13:14.650979Z","shell.execute_reply.started":"2022-12-02T09:13:04.025018Z","shell.execute_reply":"2022-12-02T09:13:14.649805Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! pip uninstall torchmetrics --y","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:13:14.652843Z","iopub.execute_input":"2022-12-02T09:13:14.653606Z","iopub.status.idle":"2022-12-02T09:13:16.970038Z","shell.execute_reply.started":"2022-12-02T09:13:14.653564Z","shell.execute_reply":"2022-12-02T09:13:16.968822Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! pip install torchmetrics","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:13:16.973559Z","iopub.execute_input":"2022-12-02T09:13:16.974393Z","iopub.status.idle":"2022-12-02T09:13:27.867728Z","shell.execute_reply.started":"2022-12-02T09:13:16.97435Z","shell.execute_reply":"2022-12-02T09:13:27.866468Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from pathlib import Path\nfrom glob import glob\n# from tqdm import tqdm\nimport time\nfrom datetime import timedelta\nfrom PIL import Image, ImageFilter\nimport cv2\nimport scipy.ndimage\nimport os\nfrom torchmetrics.classification import BinaryAUROC\n\n\nfrom ipywidgets import interact, fixed\nfrom torchvision import transforms\nfrom torch.utils.data import Dataset, DataLoader, Subset\nimport torchvision\nfrom torchinfo import summary\nfrom torchvision.models.feature_extraction import create_feature_extractor\nfrom torchvision.transforms.functional import InterpolationMode\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport time\nimport h5py\n# import timm\nimport torch\nimport torch.nn as nn\nimport random\n\nrandom.seed(42)\ntorch.manual_seed(42)\nnp.random.seed(42)\n\n# import pyfstat\n# from pyfstat.utils import get_sft_as_arrays\n\nfrom tqdm.auto import tqdm\n# from sklearn.model_selection import KFold\n# from sklearn.metrics import roc_auc_score\n# from timm.scheduler import CosineLRScheduler","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:13:27.869619Z","iopub.execute_input":"2022-12-02T09:13:27.87036Z","iopub.status.idle":"2022-12-02T09:13:32.226461Z","shell.execute_reply.started":"2022-12-02T09:13:27.870305Z","shell.execute_reply":"2022-12-02T09:13:32.225305Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:13:32.228257Z","iopub.execute_input":"2022-12-02T09:13:32.229283Z","iopub.status.idle":"2022-12-02T09:13:32.345458Z","shell.execute_reply.started":"2022-12-02T09:13:32.229241Z","shell.execute_reply":"2022-12-02T09:13:32.344461Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"CFG = {}\nCFG['MODEL_WEIGHTS_PATH'] = \"C:/Users/semen/Desktop/job_folder/G2Net/Colab_stuff/model_weights\"\nCFG['DATASET']            = \"/kaggle/input/g2net-detecting-continuous-gravitational-waves\"\n# CFG['DATASET']            = \"C:/Users/semen/Desktop/job_folder/G2Net/data\"\nCFG['TRAIN_PATH']         = \"C:/Users/semen/Desktop/job_folder/G2Net/data/train\"\n\nCFG['CASTOM_DF_PATH']     = \"C:/Users/semen/Desktop/job_folder/G2Net/Colab_stuff/castom_df\" \nCFG['XY_PATH']            = \"C:/Users/semen/Desktop/job_folder/G2Net/Colab_stuff/xy.csv\"\n# CFG['TRAIN CSV']        = \"\"\n\nCFG[\"TRAIN_TO_VAL_RATIO\"] = 0.8\nCFG[\"INPUT_SHAPE\"]        = (1,256,256)     \nCFG[\"BATCH_SIZE\"]         = 32\nCFG[\"SIZE\"]               = (360, 1500)\nCFG[\"LINE_LVL\"]           = 50\nCFG[\"LINE_DISP\"]          = 5\nCFG[\"DF_SIZE\"]            = 1000\nCFG[\"SIGNAL_LVL\"]         = [0.1, 0.4]\n\nCFG['NUM_EPOCHS']         = 300\nCFG['NOISE_LVL']          = 0.7\nCFG['WEIGHTS_NAME']       = f\"_02_12_2022___epochs_{CFG['NUM_EPOCHS']}___signal_lvl_0.1-0.4____L1\"","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:13:38.063408Z","iopub.execute_input":"2022-12-02T09:13:38.064025Z","iopub.status.idle":"2022-12-02T09:13:38.071679Z","shell.execute_reply.started":"2022-12-02T09:13:38.063981Z","shell.execute_reply":"2022-12-02T09:13:38.070233Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class G2Sample():\n    def __init__(    self, \n                     file_idx=None, \n                     hdf=None, \n                     idx=None, \n                     label=None, \n                     h1=None, \n                     l1=None, \n                     loaded=False, \n                     valid=False,\n                ):\n        \n        self.hdf       = hdf\n        self.file_idx  = file_idx\n        self.idx       = idx\n        self.label     = label\n        self.h1        = h1 \n        self.l1        = l1\n        self.valid     = valid\n        self.loaded    = loaded\n        \n\n    def preload(self):\n        file = self.hdf\n        with h5py.File(file, \"r\") as f:\n            f = f[self.file_idx]\n            h1 = f[\"H1\"]\n            l1 = f[\"L1\"]\n        \n            h1_stft = h1[\"SFTs\"][()] * 1e22\n            h1_img = self.transform_img_part(h1_stft)\n            h1_img /= np.mean(h1_img)                    #normalize  -> mean = 1\n            self.h1 = h1_img\n\n#             l1_stft = l1[\"SFTs\"][()] * 1e22\n#             l1_img = self.transform_img_part(l1_stft)\n#             l1_img /= np.mean(l1_img)                    #normalize  -> mean = 1\n#             self.l1 = l1_img\n\n            self.loaded = True\n    \n    \n    def unload(self):\n        self.h1 = None \n        self.l1 = None\n        self.loaded = False\n        \n        \n    def transform_img_part(self, img_original):\n        return img_original.real**2 + img_original.imag**2\n    \n    \n\nclass G2Dataset(torch.utils.data.Dataset):\n    def __init__(self, root_dir, preload=True, fake_train_numb=3200, segmentation_mode=False):\n        self.root_dir = root_dir\n        self.fake_train_numb = fake_train_numb\n        self.segmentation_mode = segmentation_mode\n        self.__samples = []\n        self.train = True\n        self.preload = preload\n        if preload:\n            self.__preload()\n        else:\n            self.__load()\n        self.train_idx = self.__get_train_idx(mode=\"Train\")     \n        self.valid_idx = self.__get_train_idx(mode=\"Valid\")     \n\n        \n        \n    def __get_train_idx(self, mode):\n        labels = []\n        for i in range(len(self.__samples)):\n            if mode == \"Train\":\n                valid = self.__samples[i].valid\n                idx = self.__samples[i].idx\n                if not valid and idx:\n                    labels.append(i)\n            if mode == \"Valid\":\n                valid = self.__samples[i].valid\n                if valid:\n                    labels.append(i)\n        return labels\n        \n        \n    def __preload(self):\n        file_name_list = os.listdir(f\"{CFG['DATASET']}/test/\")\n        for i, file_name in tqdm(enumerate(file_name_list)):\n            file_idx = file_name.split('.')[0]\n            sample = G2Sample(idx=i, \n                              hdf=f\"{CFG['DATASET']}/test/{file_name}\",\n                              file_idx=file_idx)\n            self.__samples.append(sample)\n    \n    \n    def __load(self):\n        #load valid samples\n        df = pd.read_csv(self.root_dir + '/train_labels.csv')\n        df = df[df.target >= 0]  # Remove 3 unknowns (target = -1)\n        for i, row in tqdm(df.iterrows()):\n            idx = row[\"id\"]\n            target = row[\"target\"]\n            self.__read_data(idx, target, file=f\"{self.root_dir}/valid/{idx}.hdf5\", valid=True)\n#             if i == 25:\n#                 break\n        print(f\"Successfully loaded {i} validation samples\")\n        \n        \n        #load train samples\n        arr = os.listdir(CFG['TRAIN_PATH'])\n        for file_name in tqdm(arr):\n            idx = file_name.split('.')[0]\n            target = None \n            self.__read_data(idx, target, file=f\"{self.root_dir}/train/{idx}.hdf5\", valid=False)\n        print(f\"Successfully loaded {len(arr)} train samples\")\n        \n        \n        #load fake train samples\n        for file_name in tqdm(range(self.fake_train_numb)):\n            sample = G2Sample(idx=False, \n                              label=None, \n                              h1=False, \n                              l1=False,\n                              loaded = True,\n                              valid = False)\n            self.__samples.append(sample)\n        print(f\"Successfully loaded {self.fake_train_numb} Fake train samples\")\n\n\n        \n    def __read_data(self, idx, label, file, valid):\n        with h5py.File(file, \"r\") as f:\n            f = f[idx]\n            h1 = f[\"H1\"]\n            l1 = f[\"L1\"]\n#             h1_stft = h1[\"SFTs\"][()].astype('complex128') * 1e22\n\n            h1_img=None\n#             h1_stft = h1[\"SFTs\"][()] * 1e22\n#             h1_img = self.__transform_img_part(h1_stft)\n#             h1_img /= np.mean(h1_img)                    #normalize  -> mean = 1\n\n#             l1_img = None\n            l1_stft = l1[\"SFTs\"][()] * 1e22\n            l1_img = self.__transform_img_part(l1_stft)\n            l1_img /= np.mean(l1_img)                      #normalize  -> mean = 1\n            \n            sample = G2Sample(idx=idx, \n                              label=label, \n                              h1=h1_img, \n                              l1=l1_img,\n                              loaded = True,\n                              valid = valid)\n            self.__samples.append(sample)\n\n\n    def __len__(self):\n        return len(self.__samples)\n    \n    \n    def at(self, idx):\n        sample = self.__samples[idx]\n        return sample\n        \n        \n    def __getitem__(self, idx):\n        sample = self.__samples[idx]\n#         h1_img= sample.h1\n#         h1_img= sample.l1                   #  change detector to L1\n#         idx_ = self.__samples[idx].idx\n\n        if self.preload:\n            if not sample.loaded:\n                sample.preload()\n            h1_img= sample.h1             \n#             h1_img= sample.l1               #  change detector to L1\n            h1_img = self.__reshape(h1_img, 256)\n            h1_img = self.__preprocess(h1_img)\n            sample.unload()\n            return h1_img, idx\n        \n        else:\n            if self.train:\n                sin=False\n                if not idx_:\n                    h1_img_idx = np.random.choice(self.train_idx)\n                    sample = self.__samples[h1_img_idx]\n                    h1_img= sample.l1 \n                if random.randrange(0, 100) < 50:\n                    label = 0\n                    h1_img = self.__gen_noise_by_shuffle(h1_img)\n                    h1_img = np.expand_dims(h1_img, axis=0)\n                else: \n                    label = 1\n                    sin = True\n                    if self.segmentation_mode:\n                        h1_img, sin_img = self.__generate_sin_and_noise(h1_img, segmentation_mode=True)\n                    else:\n                        h1_img = self.__generate_sin_and_noise(h1_img, segmentation_mode=False)\n                    \n            else:\n                label = sample.label\n            h1_img = self.__reshape(h1_img, 256)\n            h1_img = self.__preprocess(h1_img)\n            \n            if self.segmentation_mode and self.train and sin:\n                return h1_img, sin_img, label, idx\n            else:\n                return h1_img, label, idx\n\n        \n    def __transform_img_part(self, img_original):\n        return img_original.real**2 + img_original.imag**2\n    \n    \n    def __preprocess(self, imgs): \n        F = 0.7\n        x = torch.tensor(imgs).to(device)\n        x = x.unsqueeze(dim=0)\n        x = torch.cat([x, x, x], dim=0)\n        if self.train:\n            normalize = transforms.Compose([    \n                transforms.Lambda(lambda x: (x - x.min())/((x.max() - x.min()))),\n#                 transforms.Resize(size=(CFG[\"INPUT_SHAPE\"][1], CFG[\"INPUT_SHAPE\"][2]), interpolation=InterpolationMode.BILINEAR)\n#                 transforms.Lambda(lambda x: (x - x.min())/((x.max() - x.min()))),\n#                 transforms.RandomAffine(degrees=(-20,20), translate=(0.2,0.2), scale=(0.7,1.0), interpolation = transforms.InterpolationMode.BILINEAR),\n#                 transforms.RandomHorizontalFlip(),\n#                 transforms.RandomVerticalFlip(),\n#                 transforms.RandomPerspective(0.2),\n#                 transforms.Normalize(mean=[0.485*F, 0.456*F, 0.406*F], std=[0.229*F, 0.224*F, 0.225*F]),\n            ])\n        else:\n            normalize = transforms.Compose([\n#                 transforms.Resize(size=(CFG[\"INPUT_SHAPE\"][1], CFG[\"INPUT_SHAPE\"][2])),\n                transforms.Lambda(lambda x: (x - x.min())/((x.max() - x.min())))\n                ])\n        x = normalize(x)\n        img_transformed = x.cpu().numpy().astype(np.float32)\n        return img_transformed\n    \n    \n    def __reshape(self, img, final_width):\n        img = img.squeeze()\n        img = img[:, :4096]\n        time_mean = int(4096 / final_width)\n        img = np.mean(img.reshape(360, int(img.shape[1]/time_mean), time_mean), axis=2)\n#         img = np.expand_dims(img, axis=0)\n        return img\n    \n    \n    def __gen_noise_by_shuffle(self, img):\n        h, w = img.shape[0], img.shape[1]\n        np.random.shuffle(img.ravel())\n        img = np.reshape(img, (h, w))\n        return img\n\n    \n    def __generate_sin(self, x_max=125 ):\n        x = np.linspace(0, x_max, x_max*10)\n        mult = np.random.choice(range(2, 10))/10\n        shift = np.random.choice(range(x_max*6))\n        def_freq = ((x*mult)+shift)/x_max\n        y = np.sin(def_freq)*180+180\n        #shift on y axis\n        shift_space = y.min() if y.min() > 360 - y.max() else 360 - y.max()\n        shift_space = np.random.choice(range(int(shift_space)))\n        if y.min() > 360 - y.max():\n            y = y - shift_space\n        else:\n            y = y + shift_space\n        return([x, y])    \n\n    \n    \n    def __generate_sin_and_noise(self, img, segmentation_mode):\n        img_noise = self.__gen_noise_by_shuffle(img)\n        x, y = self.__generate_sin(x_max = img_noise.shape[1])\n        coords = np.column_stack((x, y))\n        noise_sig =round(random.uniform(CFG[\"SIGNAL_LVL\"][0], CFG[\"SIGNAL_LVL\"][1]), 10)\n        img = np.zeros(img_noise.shape)\n        sin_img = cv2.polylines(img, np.int32([coords]), False, color=255, thickness=1, lineType=cv2.LINE_AA)/255.\n        img = sin_img*sig_lvl + img_noise\n        img = np.expand_dims(img, axis=0)\n        if segmentation_mode:\n            return(img, sin_img)\n        else:\n            return(img)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:46:20.76525Z","iopub.execute_input":"2022-12-02T09:46:20.768182Z","iopub.status.idle":"2022-12-02T09:46:20.821334Z","shell.execute_reply.started":"2022-12-02T09:46:20.768123Z","shell.execute_reply":"2022-12-02T09:46:20.82026Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset = G2Dataset(CFG['DATASET'], preload=True, segmentation_mode=False)\nlen(dataset)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:46:27.595476Z","iopub.execute_input":"2022-12-02T09:46:27.596002Z","iopub.status.idle":"2022-12-02T09:46:27.666143Z","shell.execute_reply.started":"2022-12-02T09:46:27.595956Z","shell.execute_reply":"2022-12-02T09:46:27.665079Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\nimport os\nfrom pathlib import Path\n\ntrain_path = \"/kaggle/input/g2net-detecting-continuous-gravitational-waves/test\"\nsave_train_path = \"/kaggle/working/my_train_30\"\nzip_name = \"/kaggle/working/train_zip_10\"\n\nPath(save_train_path).mkdir(parents=True, exist_ok=True)\narr = os.listdir(train_path)\n\nfor i in range(10):\n    shutil.copy(f\"{train_path}/{arr[i]}\", f\"{save_train_path}/{arr[i]}\")\nprint(\"Files copied\")\n\nshutil.make_archive(zip_name, 'zip', save_train_path)\nprint(\"Archive created\")","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%matplotlib inline\n\n@interact\ndef show(s=(0, len(dataset)-1)):\n    dataset.train=True\n    try:\n        img, label, idx = dataset[s]\n        print(\"Label:\", label)\n    except:\n        img, idx = dataset[s]\n    img = img.squeeze().transpose(1,2,0)\n    print(img.shape)\n    print(\"i:\", s)\n    \n    print(img.min(), img.max(), img.std())\n    plt.figure(figsize=(10,10))\n    plt.imshow(img)\n    #536","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:46:28.068512Z","iopub.execute_input":"2022-12-02T09:46:28.070054Z","iopub.status.idle":"2022-12-02T09:46:28.795944Z","shell.execute_reply.started":"2022-12-02T09:46:28.070012Z","shell.execute_reply":"2022-12-02T09:46:28.794772Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Split data to train and validation","metadata":{}},{"cell_type":"markdown","source":"### New split by validation folder","metadata":{}},{"cell_type":"code","source":"valid_labels = []\ntrain_labels = []\n\nfor i in range(len(dataset)):\n    valid = dataset.at(i).valid\n    valid_labels.append(i) if valid else train_labels.append(i)\n        \ndata_trn = Subset(dataset, train_labels)\ndata_val = Subset(dataset, valid_labels)\n\nloader_trn = DataLoader(data_trn, batch_size=CFG[\"BATCH_SIZE\"], shuffle=True,  pin_memory=True)    # num_workers=CFG[\"num_workers\"]\nloader_val = DataLoader(data_val, batch_size=CFG[\"BATCH_SIZE\"], shuffle=False, pin_memory=True)\n        \nprint(f'Train set: {len(data_trn)}')\n#dataset_stat(data_trn)\n\nprint(f'Valid set: {len(data_val)}')\n#dataset_stat(data_val)","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Models","metadata":{}},{"cell_type":"markdown","source":"## EffNet B0","metadata":{}},{"cell_type":"code","source":"torch.cuda.empty_cache()\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\ndevice","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:16:04.400003Z","iopub.execute_input":"2022-12-02T09:16:04.400386Z","iopub.status.idle":"2022-12-02T09:16:04.40934Z","shell.execute_reply.started":"2022-12-02T09:16:04.400354Z","shell.execute_reply":"2022-12-02T09:16:04.408321Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class EfficientNet_B0(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n        #self.conv = nn.Conv2d(1, 3, kernel_size=(1, 1), padding='same')        \n        effnet = torchvision.models.efficientnet_b0(pretrained=True).to(device)\n        self.model = create_feature_extractor(effnet, ['flatten'])\n        freeze = False\n        if freeze:\n            for param in self.model.parameters():\n                param.requires_grad = False\n        self.linear = torch.nn.Linear(1280, 1)\n\n    def forward(self, x):\n        #out = self.conv(x)                                  \n#         out = torch.cat([x,x,x],dim=1)\n#         out = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(out)\n#         print(out.min(), out.max(), out.std())\n        out = self.model(x)['flatten']\n        out = self.linear(out)\n        return out\n\n    def predict(self, x):\n        out = self.forward(x)\n        out = torch.sigmoid(out)\n        return out","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:16:06.365355Z","iopub.execute_input":"2022-12-02T09:16:06.366126Z","iopub.status.idle":"2022-12-02T09:16:06.374213Z","shell.execute_reply.started":"2022-12-02T09:16:06.366086Z","shell.execute_reply":"2022-12-02T09:16:06.372985Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"CFG['LR']           = 1e-4            # learning rate\nCFG['L2']           = 0               # L2 regularization 1e-3\nCFG['NUM_STEPS']    = 500             # steps per epoch\nCFG['DROPOUT']      = 0.2             # dropout\nCFG['WEIGHTS_PATH'] = '/kaggle/input/weights-for-new-submmit/epochs_300_signal_lvl_0.1-0.4___L1.pth'\n# CFG['WEIGHTS']    = torchvision.EfficientNet_B0_Weights.DEFAULT  # weights None 'IMAGENET1K_V1' 'IMAGENET1K_V2'\n\n\nmodel = EfficientNet_B0()\nmodel.to(device)\n\n# model.load_state_dict(torch.load(CFG['WEIGHTS_PATH'], map_location=torch.device('cpu')))\nmodel.load_state_dict(torch.load(CFG['WEIGHTS_PATH']))\n\n# loss function\n# loss_fn = nn.CrossEntropyLoss()\n# loss_fn = nn.functional.binary_cross_entropy_with_logits()\n\n#AUC\nauc_fn = BinaryAUROC()\n\n# ADAM Optimizer\noptimizer = torch.optim.Adam(model.parameters(), lr=CFG['LR'], weight_decay=CFG['L2'])   \n\ntry:\n    img, label, idx = dataset[0]\nexcept:\n    img, idx = dataset[0]\n# Model statistics\nsummary(model, input_size=(CFG[\"BATCH_SIZE\"], 3, img.shape[1], img.shape[2]), col_names=[\"input_size\", \"output_size\",\"num_params\"])","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:16:18.718277Z","iopub.execute_input":"2022-12-02T09:16:18.718682Z","iopub.status.idle":"2022-12-02T09:16:29.345457Z","shell.execute_reply.started":"2022-12-02T09:16:18.718648Z","shell.execute_reply":"2022-12-02T09:16:29.344439Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluate and create submit file","metadata":{}},{"cell_type":"code","source":"predicted_label = [] \n\ndataset = G2Dataset(CFG['DATASET'], preload=True)\ndata_val = Subset(dataset, range(len(dataset)))\nloader_val = DataLoader(data_val, batch_size=CFG[\"BATCH_SIZE\"], shuffle=False, pin_memory=True)\n\n\n# Put model in eval mode\nmodel.eval() \ndataset.train=False\n\nwith torch.inference_mode():\n    for batch, (X, idx) in tqdm(enumerate(loader_val)):\n        X = X.float()\n        X = X.to(device)\n        # 1. Forward pass\n        test_pred_logits = model(X)\n        test_pred_labels = test_pred_logits.sigmoid().squeeze()\n        predicted_label.extend(test_pred_labels.cpu().detach().numpy())\n        ","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:46:47.626146Z","iopub.execute_input":"2022-12-02T09:46:47.626614Z","iopub.status.idle":"2022-12-02T10:04:24.470726Z","shell.execute_reply.started":"2022-12-02T09:46:47.626571Z","shell.execute_reply":"2022-12-02T10:04:24.469588Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_name_list = os.listdir(f\"{CFG['DATASET']}/test/\")\nnew_file_name_list = [file_name.split('.')[0] for file_name in file_name_list]\n\n# predicted_label\ndf = pd.DataFrame({\"id\":new_file_name_list,\n                   \"target\": predicted_label})\ndf.to_csv(\"./submit_file_7.csv\", index=False)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:09:13.263869Z","iopub.execute_input":"2022-12-02T10:09:13.264227Z","iopub.status.idle":"2022-12-02T10:09:13.300537Z","shell.execute_reply.started":"2022-12-02T10:09:13.264196Z","shell.execute_reply":"2022-12-02T10:09:13.299565Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### avarage pooling","metadata":{}},{"cell_type":"code","source":"df_1 = pd.read_csv(\"/kaggle/working/submit_file_6.csv\")\ntarget_1 = df_1[\"target\"].to_numpy()\ndf_2 = pd.read_csv(\"/kaggle/working/submit_file_7.csv\")\ntarget_2 = df_2[\"target\"].to_numpy()\n\ntarget_3 = (target_1 + target_2)/2\n\ndf_1[\"target\"] = target_3\ndf_1.to_csv(\"./submit_file_9.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:09:33.018228Z","iopub.execute_input":"2022-12-02T10:09:33.018682Z","iopub.status.idle":"2022-12-02T10:09:33.055199Z","shell.execute_reply.started":"2022-12-02T10:09:33.018646Z","shell.execute_reply":"2022-12-02T10:09:33.054273Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### MAX pooling","metadata":{}},{"cell_type":"code","source":"df_1 = pd.read_csv(\"C:/Users/semen/Desktop/job_folder/G2Net/submit_file_6.csv\")\ntarget_5 = df_1[\"target\"].to_numpy()\ndf_2 = pd.read_csv(\"C:/Users/semen/Desktop/job_folder/G2Net/submit_file_7.csv\")\ntarget_4 = df_2[\"target\"].to_numpy()\n\nconfident_arr = []\nfor i in range(len(target_5)):\n    r1 = np.abs(0.5 - target_5[i])\n    r2 = np.abs(0.5 - target_4[i])\n    if r1 > r2:\n        confident_arr.append(target_5[i])\n    else:\n        confident_arr.append(target_4[i])\n        \ndf_0 = df_1\ndf_0[\"target\"] = confident_arr\ndf_0.to_csv(\"C:/Users/semen/Desktop/job_folder/G2Net/submit_file_merge_6-7_CONFIDENT.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:06:10.35108Z","iopub.execute_input":"2022-12-02T10:06:10.351432Z","iopub.status.idle":"2022-12-02T10:06:10.3573Z","shell.execute_reply.started":"2022-12-02T10:06:10.351402Z","shell.execute_reply":"2022-12-02T10:06:10.355908Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:06:12.127119Z","iopub.execute_input":"2022-12-02T10:06:12.127463Z","iopub.status.idle":"2022-12-02T10:06:12.143335Z","shell.execute_reply.started":"2022-12-02T10:06:12.127434Z","shell.execute_reply":"2022-12-02T10:06:12.141285Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:06:27.029578Z","iopub.execute_input":"2022-12-02T10:06:27.029978Z","iopub.status.idle":"2022-12-02T10:06:27.052087Z","shell.execute_reply.started":"2022-12-02T10:06:27.029948Z","shell.execute_reply":"2022-12-02T10:06:27.0511Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}