{"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":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nInfKernel\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\nimport os\nimport gc\nimport random\nimport math\nfrom collections import defaultdict\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport torch\nfrom torch import nn\nfrom torchvision import transforms\nimport torchvision.transforms as T\nimport torchvision.transforms.functional as TTF\nfrom torch.utils.data import Dataset, DataLoader\n\nfrom PIL import Image\nfrom tqdm.notebook import tqdm\nfrom transformers import get_cosine_schedule_with_warmup\nfrom tqdm.auto import tqdm\n\nimport warnings\nwarnings.simplefilter('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-09T07:48:21.200629Z","iopub.execute_input":"2023-08-09T07:48:21.201095Z","iopub.status.idle":"2023-08-09T07:48:32.98205Z","shell.execute_reply.started":"2023-08-09T07:48:21.201023Z","shell.execute_reply":"2023-08-09T07:48:32.980998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append(\"../input/pretrained-models-pytorch\")\nsys.path.append(\"../input/efficientnet-pytorch\")\nsys.path.append(\"/kaggle/input/smp-github/segmentation_models.pytorch-master\")\nimport segmentation_models_pytorch as smp\n\nprint(f\"Segmentation Models version: {smp.__version__}\")","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:32.987867Z","iopub.execute_input":"2023-08-09T07:48:32.990966Z","iopub.status.idle":"2023-08-09T07:48:36.161757Z","shell.execute_reply.started":"2023-08-09T07:48:32.990928Z","shell.execute_reply":"2023-08-09T07:48:36.16081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed=1234):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    \n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = False\n    torch.backends.cudnn.benchmark = True\nset_seed(42)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.163414Z","iopub.execute_input":"2023-08-09T07:48:36.163811Z","iopub.status.idle":"2023-08-09T07:48:36.174113Z","shell.execute_reply.started":"2023-08-09T07:48:36.16376Z","shell.execute_reply":"2023-08-09T07:48:36.173117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preparation","metadata":{}},{"cell_type":"code","source":"class Paths:\n    data = '/kaggle/input/google-research-identify-contrails-reduce-global-warming'\n    data_root = '/kaggle/input/google-research-identify-contrails-reduce-global-warming/test/'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.178909Z","iopub.execute_input":"2023-08-09T07:48:36.179333Z","iopub.status.idle":"2023-08-09T07:48:36.185166Z","shell.execute_reply.started":"2023-08-09T07:48:36.179305Z","shell.execute_reply":"2023-08-09T07:48:36.184136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(Paths.data_root)\ntest_df = pd.DataFrame(filenames, columns=['record_id'])\n\ntest_df['path'] = Paths.data_root + test_df['record_id'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.186553Z","iopub.execute_input":"2023-08-09T07:48:36.187099Z","iopub.status.idle":"2023-08-09T07:48:36.206131Z","shell.execute_reply.started":"2023-08-09T07:48:36.187046Z","shell.execute_reply":"2023-08-09T07:48:36.20519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.208311Z","iopub.execute_input":"2023-08-09T07:48:36.208937Z","iopub.status.idle":"2023-08-09T07:48:36.223048Z","shell.execute_reply.started":"2023-08-09T07:48:36.208905Z","shell.execute_reply":"2023-08-09T07:48:36.222106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ContrailsDataset(torch.utils.data.Dataset):\n    def __init__(self, df, train=True, image_size=256, use_normalize_image=True, use_preprocess_p1=False):\n        \n        self.df = df\n        self.trn = train\n        self.normalize_image = T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))\n        self.image_size = Config.image_size\n        self.use_normalize_image = use_normalize_image\n        self.use_preprocess_p1 = use_preprocess_p1\n        if self.image_size != 256:\n            self.resize_image = T.transforms.Resize(image_size)\n    \n    def read_record(self, directory):\n        record_data = {}\n        for x in [\n            \"band_11\", \n            \"band_14\", \n            \"band_15\"\n        ]:\n\n            record_data[x] = np.load(os.path.join(directory, x + \".npy\"))\n\n        return record_data\n\n    def normalize_range(self, data, bounds):\n        \"\"\"Maps data to the range [0, 1].\"\"\"\n        return (data - bounds[0]) / (bounds[1] - bounds[0])\n    \n    def get_false_color(self, record_data):\n        _T11_BOUNDS = (243, 303)\n        _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n        _TDIFF_BOUNDS = (-4, 2)\n        \n        N_TIMES_BEFORE = 4\n\n        r = self.normalize_range(record_data[\"band_15\"] - record_data[\"band_14\"], _TDIFF_BOUNDS)\n        g = self.normalize_range(record_data[\"band_14\"] - record_data[\"band_11\"], _CLOUD_TOP_TDIFF_BOUNDS)\n        b = self.normalize_range(record_data[\"band_14\"], _T11_BOUNDS)\n        false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n        img = false_color[..., N_TIMES_BEFORE]\n\n        return img\n    \n    def __getitem__(self, index):\n        row = self.df.iloc[index]\n        con_path = row.path\n        data = self.read_record(con_path)    \n        \n        img = self.get_false_color(data)\n        \n        img = torch.tensor(img)\n        img = img.permute(2, 0, 1)\n        \n        # ---------------------------------------------------------- #\n        # DA, Preprocess\n        # ---------------------------------------------------------- #\n        \"\"\" use_normalize_image \"\"\"\n        if self.use_normalize_image:\n            img = self.normalize_image(img)\n        \"\"\" image_size \"\"\"\n        if self.image_size != 256:\n            img = self.resize_image(img)\n        \"\"\" use_preprocess_p1 \"\"\"\n        if self.use_preprocess_p1:\n            img = TTF.to_pil_image(img)\n            img = TTF.adjust_contrast(img, 0.8)\n            img = TTF.adjust_sharpness(img, 15)\n            img = TTF.adjust_saturation(img, 1)\n            img = TTF.to_tensor(img)\n            \n        return img.float()\n    \n    def __len__(self):\n        return len(self.df)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.225045Z","iopub.execute_input":"2023-08-09T07:48:36.225565Z","iopub.status.idle":"2023-08-09T07:48:36.244212Z","shell.execute_reply.started":"2023-08-09T07:48:36.225515Z","shell.execute_reply":"2023-08-09T07:48:36.243267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nModels\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"class Config:\n    batch_size = 8\n    image_size = 512\n\ntest_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size = Config.image_size\n    )\n \ntest_dl = DataLoader(test_ds, batch_size=Config.batch_size, num_workers = 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.245606Z","iopub.execute_input":"2023-08-09T07:48:36.246327Z","iopub.status.idle":"2023-08-09T07:48:36.258021Z","shell.execute_reply.started":"2023-08-09T07:48:36.246292Z","shell.execute_reply":"2023-08-09T07:48:36.256908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNet(nn.Module):\n    def __init__(self, cfg):\n        super(UNet, self).__init__()\n        \n        self.cfg = cfg\n        self.training = True\n        \n        self.model = smp.Unet(\n            encoder_name=cfg.encoder, \n            encoder_weights=cfg.weights, \n            decoder_use_batchnorm=True,\n            classes=len(cfg.classes), \n            activation=cfg.activation,\n        )\n        \n        self.loss_fn = smp.losses.DiceLoss(mode='binary')\n    \n    def forward(self, imgs):\n        \n        x = imgs\n        logits = self.model(x)\n        if Config.image_size != 256:\n            logits = torch.nn.functional.interpolate(logits, size=256, mode='bilinear')\n        \n        return {\"logits\": logits.sigmoid()}","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.261266Z","iopub.execute_input":"2023-08-09T07:48:36.261665Z","iopub.status.idle":"2023-08-09T07:48:36.270574Z","shell.execute_reply.started":"2023-08-09T07:48:36.261633Z","shell.execute_reply":"2023-08-09T07:48:36.269254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNetPlusPlus(nn.Module):\n    def __init__(self, cfg):\n        super(UNetPlusPlus, self).__init__()\n        \n        self.cfg = cfg\n        self.training = True\n        \n        self.model = smp.UnetPlusPlus(\n            encoder_name=cfg.encoder, \n            encoder_weights=cfg.weights, \n            decoder_use_batchnorm=True,\n            classes=len(cfg.classes), \n            activation=cfg.activation,\n        )\n        \n        self.loss_fn = smp.losses.DiceLoss(mode='binary')\n    \n    def forward(self, imgs):\n        \n        x = imgs\n        logits = self.model(x)\n        if Config.image_size != 256:\n            logits = torch.nn.functional.interpolate(logits, size=256, mode='bilinear')\n        \n        return {\"logits\": logits.sigmoid()}","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.276017Z","iopub.execute_input":"2023-08-09T07:48:36.276376Z","iopub.status.idle":"2023-08-09T07:48:36.284525Z","shell.execute_reply.started":"2023-08-09T07:48:36.27635Z","shell.execute_reply":"2023-08-09T07:48:36.28328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomLoss(nn.Module):\n    def __init__(self):\n        super(CustomLoss,self).__init__()\n        \n        self.diceloss = smp.losses.DiceLoss(mode='binary')\n        self.binloss = smp.losses.SoftBCEWithLogitsLoss(reduction = 'mean' , smooth_factor = 0.2)\n\n    def forward(self, output, mask):\n        output = torch.squeeze(output)\n        mask = torch.squeeze(mask)\n        \n        dice = self.diceloss(output , mask)\n        bce = self.binloss(output , mask)\n        \n        loss = dice * 0.3 + bce * 0.7\n        return loss","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.286205Z","iopub.execute_input":"2023-08-09T07:48:36.286825Z","iopub.status.idle":"2023-08-09T07:48:36.300088Z","shell.execute_reply.started":"2023-08-09T07:48:36.286792Z","shell.execute_reply":"2023-08-09T07:48:36.29874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNetPlusPlusAtt2048(nn.Module):\n    def __init__(self, cfg):\n        super(UNetPlusPlusAtt2048, self).__init__()\n        \n        self.cfg = cfg\n        self.training = True\n        \n        self.model = smp.UnetPlusPlus(\n            encoder_name=cfg.encoder, \n            encoder_weights=cfg.weights, \n            decoder_use_batchnorm=True,\n            classes=len(cfg.classes), \n            activation=cfg.activation,\n            decoder_attention_type ='scse', # Use Attention Base\n            encoder_depth=5,                # --->Changed Use Add Depth\n            decoder_channels=[2048, 1024, 512, 256, 128],# --->Changed Use Add Depth\n        )\n        \n        self.loss_fn = CustomLoss()\n    \n    def forward(self, imgs):\n        \n        x = imgs\n        logits = self.model(x)\n        if Config.image_size != 256:\n            logits = torch.nn.functional.interpolate(logits, size=256, mode='bilinear')\n        \n        return {\"logits\": logits.sigmoid()}","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.301478Z","iopub.execute_input":"2023-08-09T07:48:36.302058Z","iopub.status.idle":"2023-08-09T07:48:36.31309Z","shell.execute_reply.started":"2023-08-09T07:48:36.302023Z","shell.execute_reply":"2023-08-09T07:48:36.312124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNetPlusPlusAtt1024(nn.Module):\n    def __init__(self, cfg):\n        super(UNetPlusPlusAtt1024, self).__init__()\n        \n        self.cfg = cfg\n        self.training = True\n        \n        self.model = smp.UnetPlusPlus(\n            encoder_name=cfg.encoder, \n            encoder_weights=cfg.weights, \n            decoder_use_batchnorm=True,\n            classes=len(cfg.classes), \n            activation=cfg.activation,\n            decoder_attention_type ='scse', # Use Attention Base\n            encoder_depth=5,                # --->Changed Use Add Depth\n            decoder_channels=[1024, 512, 256, 128, 64],# --->Changed Use Add Depth\n        )\n        \n        self.loss_fn = CustomLoss()\n    \n    def forward(self, imgs):\n        \n        x = imgs\n        logits = self.model(x)\n        if Config.image_size != 256:\n            logits = torch.nn.functional.interpolate(logits, size=256, mode='bilinear')\n        \n        return {\"logits\": logits.sigmoid()}","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.314661Z","iopub.execute_input":"2023-08-09T07:48:36.316134Z","iopub.status.idle":"2023-08-09T07:48:36.327523Z","shell.execute_reply.started":"2023-08-09T07:48:36.316099Z","shell.execute_reply":"2023-08-09T07:48:36.32664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomLoss_bce(nn.Module):\n    def __init__(self):\n        super(CustomLoss_bce,self).__init__()\n        \n        self.diceloss = smp.losses.DiceLoss(mode='binary')\n        self.binloss = smp.losses.SoftBCEWithLogitsLoss(reduction = 'mean' , smooth_factor = 0.2)\n\n    def forward(self, output, mask):\n        output = torch.squeeze(output)\n        mask = torch.squeeze(mask)\n        \n        dice = self.diceloss(output , mask)\n        bce = self.binloss(output , mask)\n        \n        loss = bce\n        return loss","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.328969Z","iopub.execute_input":"2023-08-09T07:48:36.329499Z","iopub.status.idle":"2023-08-09T07:48:36.339975Z","shell.execute_reply.started":"2023-08-09T07:48:36.329467Z","shell.execute_reply":"2023-08-09T07:48:36.339127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNetPlusPlusAtt512(nn.Module):\n    def __init__(self, cfg):\n        super(UNetPlusPlusAtt512, self).__init__()\n        \n        self.cfg = cfg\n        self.training = True\n        \n        self.model = smp.UnetPlusPlus(\n            encoder_name=cfg.encoder, \n            encoder_weights=cfg.weights, \n            decoder_use_batchnorm=True,\n            classes=len(cfg.classes), \n            activation=cfg.activation,\n            decoder_attention_type ='scse', # Use Attention Base\n            encoder_depth=5,                # --->Changed Use Add Depth\n            decoder_channels=[512, 256, 128, 64, 32],# --->Changed Use Add Depth\n        )\n        \n        self.loss_fn = CustomLoss_bce()\n    \n    def forward(self, imgs):\n        \n        x = imgs\n        logits = self.model(x)\n        if Config.image_size != 256:\n            logits = torch.nn.functional.interpolate(logits, size=256, mode='bilinear')\n        \n        return {\"logits\": logits.sigmoid()}","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.34136Z","iopub.execute_input":"2023-08-09T07:48:36.341828Z","iopub.status.idle":"2023-08-09T07:48:36.353916Z","shell.execute_reply.started":"2023-08-09T07:48:36.341796Z","shell.execute_reply":"2023-08-09T07:48:36.352903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomLoss0307(nn.Module):\n    def __init__(self):\n        super(CustomLoss0307,self).__init__()\n        \n        self.diceloss = smp.losses.DiceLoss(mode='binary')\n        self.binloss = smp.losses.SoftBCEWithLogitsLoss(reduction = 'mean' , smooth_factor = 0.2)\n\n    def forward(self, output, mask):\n        output = torch.squeeze(output)\n        mask = torch.squeeze(mask)\n        \n        dice = self.diceloss(output , mask)\n        bce = self.binloss(output , mask)\n        \n        loss = dice * 0.3 + bce * 0.7\n        return loss\n    \nclass UNetPlusPlusAtt512Loss0307(nn.Module):\n    def __init__(self, cfg):\n        super(UNetPlusPlusAtt512Loss0307, self).__init__()\n        \n        self.cfg = cfg\n        self.training = True\n        \n        self.model = smp.UnetPlusPlus(\n            encoder_name=cfg.encoder, \n            encoder_weights=cfg.weights, \n            decoder_use_batchnorm=True,\n            classes=len(cfg.classes), \n            activation=cfg.activation,\n            decoder_attention_type ='scse', # Use Attention Base\n            encoder_depth=5,                # --->Changed Use Add Depth\n            decoder_channels=[512, 256, 128, 64, 32],# --->Changed Use Add Depth\n        )\n        \n        self.loss_fn = CustomLoss0307()\n    \n    def forward(self, imgs):\n        \n        x = imgs\n        logits = self.model(x)\n        if Config.image_size != 256:\n            logits = torch.nn.functional.interpolate(logits, size=256, mode='bilinear')\n        \n        return {\"logits\": logits.sigmoid()}","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.355495Z","iopub.execute_input":"2023-08-09T07:48:36.355956Z","iopub.status.idle":"2023-08-09T07:48:36.368897Z","shell.execute_reply.started":"2023-08-09T07:48:36.355925Z","shell.execute_reply":"2023-08-09T07:48:36.367721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNetPlusPlusAtt512d4Loss0307(nn.Module):\n    def __init__(self, cfg):\n        super(UNetPlusPlusAtt512d4Loss0307, self).__init__()\n        \n        self.cfg = cfg\n        self.training = True\n        \n        self.model = smp.UnetPlusPlus(\n            encoder_name=cfg.encoder, \n            encoder_weights=cfg.weights, \n            decoder_use_batchnorm=True,\n            classes=len(cfg.classes), \n            activation=cfg.activation,\n            decoder_attention_type ='scse', # Use Attention Base\n            encoder_depth=4,                # --->Changed Use Add Depth\n            decoder_channels=[512, 256, 128, 64],# --->Changed Use Add Depth\n        )\n        \n        self.loss_fn = CustomLoss0307()\n    \n    def forward(self, imgs):\n        \n        x = imgs\n        logits = self.model(x)\n        if Config.image_size != 256:\n            logits = torch.nn.functional.interpolate(logits, size=256, mode='bilinear')\n        \n        return {\"logits\": logits.sigmoid()}","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:52.56691Z","iopub.execute_input":"2023-08-09T07:58:52.567976Z","iopub.status.idle":"2023-08-09T07:58:52.577321Z","shell.execute_reply.started":"2023-08-09T07:58:52.56794Z","shell.execute_reply":"2023-08-09T07:58:52.576039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNetAtt(nn.Module):\n    def __init__(self, cfg):\n        super(UNetAtt, self).__init__()\n        \n        self.cfg = cfg\n        self.training = True\n        \n        self.model = smp.UnetPlusPlus(\n            encoder_name=cfg.encoder, \n            encoder_weights=cfg.weights, \n            decoder_use_batchnorm=True,\n            classes=len(cfg.classes), \n            activation=cfg.activation,\n            decoder_attention_type ='scse' # Use Attention Base\n        )\n        \n        self.loss_fn = CustomLoss()\n    \n    def forward(self, imgs):\n        \n        x = imgs\n        logits = self.model(x)\n        if Config.image_size != 256:\n            logits = torch.nn.functional.interpolate(logits, size=256, mode='bilinear')\n        \n        return {\"logits\": logits.sigmoid()}","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.370618Z","iopub.execute_input":"2023-08-09T07:48:36.37125Z","iopub.status.idle":"2023-08-09T07:48:36.382393Z","shell.execute_reply.started":"2023-08-09T07:48:36.371215Z","shell.execute_reply":"2023-08-09T07:48:36.381522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_pred_val(model_path, cfg, test_dl, arc=\"UNet++\"):\n    if arc == \"UNet\":\n        model = UNet(cfg).to(cfg.device)\n    elif arc == \"UnetAtt\":\n        model = UNetAtt(cfg).to(cfg.device)\n    elif arc == \"UNetPlusPlusAtt1024\":\n        model = UNetPlusPlusAtt1024(cfg).to(cfg.device)\n    elif arc == \"UNetPlusPlusAtt2048\":\n        model = UNetPlusPlusAtt2048(cfg).to(cfg.device)\n    else:\n        model = UNetPlusPlus(cfg).to(cfg.device)\n    model.load_state_dict(torch.load(model_path, map_location=torch.device('cuda')))\n    model.eval()\n    torch.set_grad_enabled(False)\n\n    val_data = defaultdict(list)\n    pbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\n    for step, X in pbar: \n        X = X.to(cfg.device)\n\n        output = model(X)\n        for key, val in output.items():\n            val_data[key] += [output[key]]\n\n    for key, val in output.items():\n        value = val_data[key]\n        if len(value[0].shape) == 0:\n            val_data[key] = torch.stack(value)\n        else:\n            val_data[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n    del model\n    gc.collect()\n    torch.cuda.empty_cache()\n    return val_data","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.385884Z","iopub.execute_input":"2023-08-09T07:48:36.386193Z","iopub.status.idle":"2023-08-09T07:48:36.398418Z","shell.execute_reply.started":"2023-08-09T07:48:36.386169Z","shell.execute_reply":"2023-08-09T07:48:36.397326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nBlending Init\n</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\n[IMPORTANT]Consolidate into a single mask to prevent OOM\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"all_pred_mask_sum = None\nall_pred_mask_num = None\nall_pred_mask_w   = None\ndef pred_mask_sum(pred, pred_weight=1.0):\n    global all_pred_mask_sum\n    global all_pred_mask_num\n    global all_pred_mask_w\n    \n    \"\"\" First Call Initialize \"\"\"\n    if all_pred_mask_sum == None:\n        print(\"[INFO]Init all Pred\")\n        all_pred_mask_sum = pred\n        all_pred_mask_num = 1\n        all_pred_mask_w = pred_weight\n    else:\n        \"\"\" After Call Sum \"\"\"\n        print(\"[INFO]mask sum\")\n        all_pred_mask_num += 1\n        all_pred_mask_w += pred_weight\n        for i, (_all_pred, _pred) in enumerate(zip(all_pred_mask_sum['logits'], pred['logits'])):\n            all_pred_mask_sum['logits'][i][0] += pred['logits'][i][0]*pred_weight","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.401687Z","iopub.execute_input":"2023-08-09T07:48:36.401982Z","iopub.status.idle":"2023-08-09T07:48:36.411414Z","shell.execute_reply.started":"2023-08-09T07:48:36.401959Z","shell.execute_reply":"2023-08-09T07:48:36.410408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nModel TTS\n</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config:\n    batch_size = 32\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-eca_nfnet_l1'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 11\tThreshold : 0.05\tDice : 0.6498515972880622\n    model_ckpt = '/kaggle/input/contrails-train-aa-530-20230623094438/epoch-11.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.412938Z","iopub.execute_input":"2023-08-09T07:48:36.414158Z","iopub.status.idle":"2023-08-09T07:48:36.450661Z","shell.execute_reply.started":"2023-08-09T07:48:36.414114Z","shell.execute_reply":"2023-08-09T07:48:36.449654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNetPlusPlus(Config).to(Config.device)\nmodel.load_state_dict(torch.load(Config.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:36.452224Z","iopub.execute_input":"2023-08-09T07:48:36.452554Z","iopub.status.idle":"2023-08-09T07:48:45.991729Z","shell.execute_reply.started":"2023-08-09T07:48:36.452524Z","shell.execute_reply":"2023-08-09T07:48:45.989685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntorch.set_grad_enabled(False)\n\nval_data = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config.device)\n\n    output = model(X)\n    for key, val in output.items():\n        val_data[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data[key]\n    if len(value[0].shape) == 0:\n        val_data[key] = torch.stack(value)\n    else:\n        val_data[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data)\n\"\"\" Clear Cache \"\"\"\ndel model, val_data\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:45.997183Z","iopub.execute_input":"2023-08-09T07:48:45.997619Z","iopub.status.idle":"2023-08-09T07:48:57.461883Z","shell.execute_reply.started":"2023-08-09T07:48:45.99757Z","shell.execute_reply":"2023-08-09T07:48:57.460692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel0\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config0:\n    batch_size = 32\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-eca_nfnet_l2'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 4\tThreshold : 0.65\tDice : 0.6515608388022919\n    model_ckpt = '/kaggle/input/contrails-train-bbb-a124-20230708231518/epoch-04.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:57.466514Z","iopub.execute_input":"2023-08-09T07:48:57.467155Z","iopub.status.idle":"2023-08-09T07:48:57.475657Z","shell.execute_reply.started":"2023-08-09T07:48:57.467115Z","shell.execute_reply":"2023-08-09T07:48:57.47439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model0 = UNetPlusPlusAtt2048(Config0).to(Config0.device)\nmodel0.load_state_dict(torch.load(Config0.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:48:57.477669Z","iopub.execute_input":"2023-08-09T07:48:57.478652Z","iopub.status.idle":"2023-08-09T07:49:11.573973Z","shell.execute_reply.started":"2023-08-09T07:48:57.478619Z","shell.execute_reply":"2023-08-09T07:49:11.573117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model0.eval()\ntorch.set_grad_enabled(False)\n\nval_data0 = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config0.device)\n\n    output = model0(X)\n    for key, val in output.items():\n        val_data0[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data0[key]\n    if len(value[0].shape) == 0:\n        val_data0[key] = torch.stack(value)\n    else:\n        val_data0[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data0)\n\"\"\" Clear Cache \"\"\"\ndel model0, val_data0\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:11.575231Z","iopub.execute_input":"2023-08-09T07:49:11.575572Z","iopub.status.idle":"2023-08-09T07:49:21.367695Z","shell.execute_reply.started":"2023-08-09T07:49:11.57554Z","shell.execute_reply":"2023-08-09T07:49:21.366655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nBBB-A1245-TRAIN-TTS-UnetPPAtt2048-eca_nfnet_l1-e20-x512-CustomLoss\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config0_a1245:\n    batch_size = 32\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-eca_nfnet_l1'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 4\tThreshold : 0.65\tDice : 0.6515608388022919\n    model_ckpt = '/kaggle/input/contrails-train-bbb-a1245-20230713135807/epoch-10.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:21.369716Z","iopub.execute_input":"2023-08-09T07:49:21.37013Z","iopub.status.idle":"2023-08-09T07:49:21.376818Z","shell.execute_reply.started":"2023-08-09T07:49:21.370094Z","shell.execute_reply":"2023-08-09T07:49:21.375757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model0_a1245 = UNetPlusPlusAtt2048(Config0_a1245).to(Config0_a1245.device)\nmodel0_a1245.load_state_dict(torch.load(Config0_a1245.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:21.378682Z","iopub.execute_input":"2023-08-09T07:49:21.379063Z","iopub.status.idle":"2023-08-09T07:49:34.241137Z","shell.execute_reply.started":"2023-08-09T07:49:21.379031Z","shell.execute_reply":"2023-08-09T07:49:34.240079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model0_a1245.eval()\ntorch.set_grad_enabled(False)\n\nval_data0_a1245 = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config0_a1245.device)\n\n    output = model0_a1245(X)\n    for key, val in output.items():\n        val_data0_a1245[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data0_a1245[key]\n    if len(value[0].shape) == 0:\n        val_data0_a1245[key] = torch.stack(value)\n    else:\n        val_data0_a1245[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data0_a1245)\n\"\"\" Clear Cache \"\"\"\ndel model0_a1245, val_data0_a1245\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:34.249931Z","iopub.execute_input":"2023-08-09T07:49:34.25026Z","iopub.status.idle":"2023-08-09T07:49:35.299357Z","shell.execute_reply.started":"2023-08-09T07:49:34.250233Z","shell.execute_reply":"2023-08-09T07:49:35.298301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nBBB-A1244-TRAIN-TTS-UnetPPAtt2048-efficientnet-b7-e20-x512-CustomLoss\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config_a1244:\n    batch_size = 32\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'timm-efficientnet-b7'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 13\tThreshold : 0.50\tDice : 0.6486211196660513\n    model_ckpt = '/kaggle/input/contrails-train-bbb-a1244-20230714054923/epoch-13.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:35.301372Z","iopub.execute_input":"2023-08-09T07:49:35.301775Z","iopub.status.idle":"2023-08-09T07:49:35.308192Z","shell.execute_reply.started":"2023-08-09T07:49:35.301738Z","shell.execute_reply":"2023-08-09T07:49:35.30709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model0_a1244 = UNetPlusPlusAtt2048(Config_a1244).to(Config_a1244.device)\nmodel0_a1244.load_state_dict(torch.load(Config_a1244.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:35.309888Z","iopub.execute_input":"2023-08-09T07:49:35.310615Z","iopub.status.idle":"2023-08-09T07:49:44.416416Z","shell.execute_reply.started":"2023-08-09T07:49:35.310572Z","shell.execute_reply":"2023-08-09T07:49:44.415487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model0_a1244.eval()\ntorch.set_grad_enabled(False)\n\nval_data0_a1244 = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config_a1244.device)\n\n    output = model0_a1244(X)\n    for key, val in output.items():\n        val_data0_a1244[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data0_a1244[key]\n    if len(value[0].shape) == 0:\n        val_data0_a1244[key] = torch.stack(value)\n    else:\n        val_data0_a1244[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data0_a1244)\n\"\"\" Clear Cache \"\"\"\ndel model0_a1244, val_data0_a1244\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:44.417762Z","iopub.execute_input":"2023-08-09T07:49:44.418409Z","iopub.status.idle":"2023-08-09T07:49:50.261514Z","shell.execute_reply.started":"2023-08-09T07:49:44.418379Z","shell.execute_reply":"2023-08-09T07:49:50.260456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel1\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config1:\n    batch_size = 32\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-seresnextaa101d_32x8d'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 15\tThreshold : 0.05\tDice : 0.6507990340637085\n    model_ckpt = '/kaggle/input/contrails-train-aa-540-20230624172035/epoch-16.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:50.263306Z","iopub.execute_input":"2023-08-09T07:49:50.263659Z","iopub.status.idle":"2023-08-09T07:49:50.270179Z","shell.execute_reply.started":"2023-08-09T07:49:50.263623Z","shell.execute_reply":"2023-08-09T07:49:50.269183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = UNetPlusPlus(Config1).to(Config1.device)\nmodel1.load_state_dict(torch.load(Config1.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:50.271797Z","iopub.execute_input":"2023-08-09T07:49:50.272377Z","iopub.status.idle":"2023-08-09T07:49:57.316011Z","shell.execute_reply.started":"2023-08-09T07:49:50.272344Z","shell.execute_reply":"2023-08-09T07:49:57.315099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.eval()\ntorch.set_grad_enabled(False)\n\nval_data1 = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config.device)\n\n    output = model1(X)\n    for key, val in output.items():\n        val_data1[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data1[key]\n    if len(value[0].shape) == 0:\n        val_data1[key] = torch.stack(value)\n    else:\n        val_data1[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data1)\n\"\"\" Clear Cache \"\"\"\ndel model1, val_data1\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:49:57.317961Z","iopub.execute_input":"2023-08-09T07:49:57.318351Z","iopub.status.idle":"2023-08-09T07:50:00.40417Z","shell.execute_reply.started":"2023-08-09T07:49:57.318319Z","shell.execute_reply":"2023-08-09T07:50:00.403049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel2\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config2:\n    batch_size = 32\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-eca_nfnet_l2'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 13\tThreshold : 0.05\tDice : 0.6501585320903378\n    model_ckpt = '/kaggle/input/contrails-train-aa-560-20230624172416/epoch-13.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:50:00.406399Z","iopub.execute_input":"2023-08-09T07:50:00.407254Z","iopub.status.idle":"2023-08-09T07:50:00.414172Z","shell.execute_reply.started":"2023-08-09T07:50:00.407212Z","shell.execute_reply":"2023-08-09T07:50:00.412982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = UNetPlusPlus(Config2).to(Config2.device)\nmodel2.load_state_dict(torch.load(Config2.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:50:00.415828Z","iopub.execute_input":"2023-08-09T07:50:00.416262Z","iopub.status.idle":"2023-08-09T07:50:05.853232Z","shell.execute_reply.started":"2023-08-09T07:50:00.416192Z","shell.execute_reply":"2023-08-09T07:50:05.852268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.eval()\ntorch.set_grad_enabled(False)\n\nval_data2 = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config.device)\n\n    output = model2(X)\n    for key, val in output.items():\n        val_data2[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data2[key]\n    if len(value[0].shape) == 0:\n        val_data2[key] = torch.stack(value)\n    else:\n        val_data2[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data2)\n\"\"\" Clear Cache \"\"\"\ndel model2, val_data2\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:50:05.854572Z","iopub.execute_input":"2023-08-09T07:50:05.855187Z","iopub.status.idle":"2023-08-09T07:50:06.633812Z","shell.execute_reply.started":"2023-08-09T07:50:05.855153Z","shell.execute_reply":"2023-08-09T07:50:06.63271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel8\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config8:\n    batch_size = 32\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'timm-efficientnet-b7'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 16\tThreshold : 0.05\tDice : 0.6407275630749453\n    model_ckpt = '/kaggle/input/contrails-train-aa-580-20230630092848/epoch-16.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:50:06.635496Z","iopub.execute_input":"2023-08-09T07:50:06.636161Z","iopub.status.idle":"2023-08-09T07:50:06.643314Z","shell.execute_reply.started":"2023-08-09T07:50:06.636114Z","shell.execute_reply":"2023-08-09T07:50:06.642199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model8 = UNetPlusPlus(Config8).to(Config8.device)\nmodel8.load_state_dict(torch.load(Config8.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:50:06.64515Z","iopub.execute_input":"2023-08-09T07:50:06.645477Z","iopub.status.idle":"2023-08-09T07:50:11.255299Z","shell.execute_reply.started":"2023-08-09T07:50:06.645447Z","shell.execute_reply":"2023-08-09T07:50:11.254287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model8.eval()\ntorch.set_grad_enabled(False)\n\nval_data8 = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config.device)\n\n    output = model8(X)\n    for key, val in output.items():\n        val_data8[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data8[key]\n    if len(value[0].shape) == 0:\n        val_data8[key] = torch.stack(value)\n    else:\n        val_data8[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data8)\n\"\"\" Clear Cache \"\"\"\ndel model8, val_data8\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:50:11.256871Z","iopub.execute_input":"2023-08-09T07:50:11.25727Z","iopub.status.idle":"2023-08-09T07:50:12.672153Z","shell.execute_reply.started":"2023-08-09T07:50:11.257236Z","shell.execute_reply":"2023-08-09T07:50:12.671084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nModel.CV\n</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel3\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config3:\n    batch_size = 4\n    seed = 42\n    \n    encoder = 'tu-eca_nfnet_l1'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 13\tThreshold : 0.05\tDice : 0.6501585320903378\n    model_ckpts = [\n        f\"/kaggle/input/contrails-train-aaa-t201-20230625113600/model_best_dice_f{fold}.pth\" for fold in range(n_folds)\n    ]\n\nval_data_list = []\nfor fold in range(Config3.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config3.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config3.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config3.model_ckpts[fold], Config3, test_dl))\n    print(f\"finish fold{fold}.\")\n\nval_data3 = defaultdict(list)\n\nval_data3[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data3[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data3[\"logits\"] = val_data3[\"logits\"] / Config3.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data3)\ndel val_data3\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:50:12.674106Z","iopub.execute_input":"2023-08-09T07:50:12.674516Z","iopub.status.idle":"2023-08-09T07:50:37.488058Z","shell.execute_reply.started":"2023-08-09T07:50:12.674478Z","shell.execute_reply":"2023-08-09T07:50:37.48706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel4\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config4:\n    batch_size = 4\n    seed = 42\n    \n    encoder = 'tu-eca_nfnet_l2'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 13\tThreshold : 0.05\tDice : 0.6501585320903378\n    model_ckpts = [\n        f\"/kaggle/input/contrails-train-aaa-t202-20230626070636/model_best_dice_f{fold}.pth\" for fold in range(n_folds)\n    ]\n\n\nval_data_list = []\nfor fold in range(Config4.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config4.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config4.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config4.model_ckpts[fold], Config4, test_dl))\n    print(f\"finish fold{fold}.\")\n\nval_data4 = defaultdict(list)\n\nval_data4[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data4[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data4[\"logits\"] = val_data4[\"logits\"] / Config4.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data4)\ndel val_data4\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:50:37.489755Z","iopub.execute_input":"2023-08-09T07:50:37.490358Z","iopub.status.idle":"2023-08-09T07:51:07.237167Z","shell.execute_reply.started":"2023-08-09T07:50:37.490321Z","shell.execute_reply":"2023-08-09T07:51:07.236124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel5\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config5:\n    batch_size = 4\n    seed = 42\n    \n    encoder = 'tu-seresnext101d_32x8d'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 13\tThreshold : 0.05\tDice : 0.6501585320903378\n    model_ckpts = [\n        f\"/kaggle/input/contrails-train-aaa-t203-20230627113652/model_best_dice_f{fold}.pth\" for fold in range(n_folds)\n    ]\n\n\nval_data_list = []\nfor fold in range(Config5.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config5.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config5.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config5.model_ckpts[fold], Config5, test_dl))\n    print(f\"finish fold{fold}.\")\n\nval_data5 = defaultdict(list)\n\nval_data5[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data5[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data5[\"logits\"] = val_data5[\"logits\"] / Config5.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data5)\ndel val_data5\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:51:07.238907Z","iopub.execute_input":"2023-08-09T07:51:07.239302Z","iopub.status.idle":"2023-08-09T07:51:51.834145Z","shell.execute_reply.started":"2023-08-09T07:51:07.239267Z","shell.execute_reply":"2023-08-09T07:51:51.833144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel6\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config6:\n    batch_size = 4\n    seed = 42\n    \n    encoder = 'tu-rexnetr_300'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 13\tThreshold : 0.05\tDice : 0.6501585320903378\n    model_ckpts = [\n        f\"/kaggle/input/contrails-train-aaa-t204-20230627112930/model_best_dice_f{fold}.pth\" for fold in range(n_folds)\n    ]\n\n\nval_data_list = []\nfor fold in range(Config6.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config6.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config6.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config6.model_ckpts[fold], Config6, test_dl))\n    print(f\"finish fold{fold}.\")\n\nval_data6 = defaultdict(list)\n\nval_data6[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data6[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data6[\"logits\"] = val_data6[\"logits\"] / Config6.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data6,pred_weight=0.6)\ndel val_data6\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:51:51.835842Z","iopub.execute_input":"2023-08-09T07:51:51.836452Z","iopub.status.idle":"2023-08-09T07:52:15.461929Z","shell.execute_reply.started":"2023-08-09T07:51:51.836416Z","shell.execute_reply":"2023-08-09T07:52:15.460891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel7\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config7:\n    batch_size = 4\n    seed = 42\n    \n    encoder = 'tu-seresnextaa101d_32x8d'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 13\tThreshold : 0.05\tDice : 0.6501585320903378\n    model_ckpts = [\n        f\"/kaggle/input/contrails-train-aaa-t205-20230628083051/model_best_dice_f{fold}.pth\" for fold in range(n_folds)\n    ]\n\n\nval_data_list = []\nfor fold in range(Config7.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config7.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config7.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config7.model_ckpts[fold], Config7, test_dl))\n    print(f\"finish fold{fold}.\")\n\nval_data7 = defaultdict(list)\n\nval_data7[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data7[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data7[\"logits\"] = val_data7[\"logits\"] / Config7.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data7)\ndel val_data7\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:52:15.46371Z","iopub.execute_input":"2023-08-09T07:52:15.46433Z","iopub.status.idle":"2023-08-09T07:52:53.223976Z","shell.execute_reply.started":"2023-08-09T07:52:15.464293Z","shell.execute_reply":"2023-08-09T07:52:53.222981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel9\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config9:\n    batch_size = 4\n    seed = 42\n    \n    encoder = 'tu-tf_efficientnetv2_l'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # OOF:0.6767830992\n    model_ckpts = [\n        \"/kaggle/input/contrails-train-bbb-a110-20230702143049/model_best_dice_fold00_epoch12.pth\",\n        \"/kaggle/input/contrails-train-bbb-a110-20230702143049/model_best_dice_fold01_epoch16.pth\",\n        \"/kaggle/input/contrails-train-bbb-a110-20230702143049/model_best_dice_fold02_epoch12.pth\",\n        \"/kaggle/input/contrails-train-bbb-a110-20230702143049/model_best_dice_fold03_epoch11.pth\",\n        \"/kaggle/input/contrails-train-bbb-a110-20230702143049/model_best_dice_fold04_epoch18.pth\"\n    ]\n\n\nval_data_list = []\nfor fold in range(Config9.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config9.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config9.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config9.model_ckpts[fold], Config9, test_dl))\n    print(f\"finish fold{fold}.\")\n\nval_data9 = defaultdict(list)\n\nval_data9[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data9[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data9[\"logits\"] = val_data9[\"logits\"] / Config9.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data9)\ndel val_data9\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:52:53.225637Z","iopub.execute_input":"2023-08-09T07:52:53.225985Z","iopub.status.idle":"2023-08-09T07:53:37.245944Z","shell.execute_reply.started":"2023-08-09T07:52:53.225953Z","shell.execute_reply":"2023-08-09T07:53:37.244974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nModel10\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config10:\n    batch_size = 4\n    seed = 42\n    \n    encoder = 'tu-eca_nfnet_l1'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # OOF:0.6806609338\n    model_ckpts = [\n        \"/kaggle/input/contrails-train-als-z102-20230704220527/model_best_dice_f0.pth\",\n        \"/kaggle/input/contrails-train-als-z102-20230704220527/model_best_dice_f1.pth\",\n        \"/kaggle/input/contrails-train-als-z102-20230704220527/model_best_dice_f2.pth\",\n        \"/kaggle/input/contrails-train-als-z102-20230704220527/model_best_dice_f3.pth\",\n        \"/kaggle/input/contrails-train-als-z102-20230704220527/model_best_dice_f4.pth\"\n    ]\n\n\nval_data_list = []\nfor fold in range(Config10.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config10.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config10.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config10.model_ckpts[fold], Config10, test_dl))\n    print(f\"finish fold{fold}.\")\n\nval_data10 = defaultdict(list)\n\nval_data10[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data10[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data10[\"logits\"] = val_data10[\"logits\"] / Config10.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data10)\ndel val_data10\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:53:37.247732Z","iopub.execute_input":"2023-08-09T07:53:37.248125Z","iopub.status.idle":"2023-08-09T07:54:01.624704Z","shell.execute_reply.started":"2023-08-09T07:53:37.248088Z","shell.execute_reply":"2023-08-09T07:54:01.62343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nInference x1024\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:54:01.62654Z","iopub.execute_input":"2023-08-09T07:54:01.626952Z","iopub.status.idle":"2023-08-09T07:54:01.927362Z","shell.execute_reply.started":"2023-08-09T07:54:01.626914Z","shell.execute_reply":"2023-08-09T07:54:01.925947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    batch_size = 8\n    image_size = 1024\n\ntest_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size = Config.image_size\n    )\n \ntest_dl = DataLoader(test_ds, batch_size=Config.batch_size, num_workers = 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:54:01.928994Z","iopub.execute_input":"2023-08-09T07:54:01.929699Z","iopub.status.idle":"2023-08-09T07:54:01.940243Z","shell.execute_reply.started":"2023-08-09T07:54:01.92966Z","shell.execute_reply":"2023-08-09T07:54:01.939141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nx1024 Model 1\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config_0:\n    batch_size = 4\n    seed = 42\n    \n    encoder = 'tu-tf_efficientnet_b4'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 1024\n    # OOF:0.6753651312\n    model_ckpts = [\n        \"/kaggle/input/contrails-train-bbb-a114-20230705222909/model_best_dice_fold00_epoch09.pth\",\n        \"/kaggle/input/contrails-train-bbb-a114-20230705222909/model_best_dice_fold01_epoch04.pth\",\n        \"/kaggle/input/contrails-train-bbb-a114-20230705222909/model_best_dice_fold02_epoch07.pth\",\n        \"/kaggle/input/contrails-train-bbb-a114-20230705222909/model_best_dice_fold03_epoch08.pth\",\n        \"/kaggle/input/contrails-train-bbb-a114-20230705222909/model_best_dice_fold04_epoch06.pth\",\n    ]\n\n\nval_data_list = []\nfor fold in range(Config_0.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config_0.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config_0.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config_0.model_ckpts[fold], Config_0, test_dl))\n    print(f\"finish fold{fold}.\")\n\nval_data_0 = defaultdict(list)\n\nval_data_0[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data_0[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data_0[\"logits\"] = val_data_0[\"logits\"] / Config_0.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data_0,pred_weight=1.1)\ndel val_data_0\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:54:01.942273Z","iopub.execute_input":"2023-08-09T07:54:01.943014Z","iopub.status.idle":"2023-08-09T07:54:19.205399Z","shell.execute_reply.started":"2023-08-09T07:54:01.942973Z","shell.execute_reply":"2023-08-09T07:54:19.204236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nx1024 Model 2\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:54:19.207043Z","iopub.execute_input":"2023-08-09T07:54:19.207731Z","iopub.status.idle":"2023-08-09T07:54:19.515414Z","shell.execute_reply.started":"2023-08-09T07:54:19.207694Z","shell.execute_reply":"2023-08-09T07:54:19.514144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config_1:\n    batch_size = 2\n    seed = 42\n    \n    encoder = 'tu-eca_nfnet_l2'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 1024\n    # OOF:0.6829754475\n    model_ckpts = [\n        \"/kaggle/input/contrails-train-bbb-a120-20230707231158/model_best_dice_fold00_epoch10.pth\",\n        \"/kaggle/input/contrails-train-bbb-a120-20230707231158/model_best_dice_fold01_epoch05.pth\",\n        \"/kaggle/input/contrails-train-bbb-a120-20230707231158/model_best_dice_fold02_epoch08.pth\",\n        \"/kaggle/input/contrails-train-bbb-a120-20230707231158/model_best_dice_fold03_epoch04.pth\",\n        \"/kaggle/input/contrails-train-bbb-a120-20230707231158/model_best_dice_fold04_epoch06.pth\",\n    ]\n\n\nval_data_list = []\nfor fold in range(Config_1.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config_1.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config_1.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config_1.model_ckpts[fold], Config_1, test_dl, arc=\"UnetAtt\"))\n    print(f\"finish fold{fold}.\")\n\nval_data_1 = defaultdict(list)\n\nval_data_1[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data_1[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data_1[\"logits\"] = val_data_1[\"logits\"] / Config_1.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data_1,pred_weight=1.1)\ndel val_data_1\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:54:19.517188Z","iopub.execute_input":"2023-08-09T07:54:19.518825Z","iopub.status.idle":"2023-08-09T07:55:03.795997Z","shell.execute_reply.started":"2023-08-09T07:54:19.518798Z","shell.execute_reply":"2023-08-09T07:55:03.795059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nInference Preprocess Dataset\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:55:03.800241Z","iopub.execute_input":"2023-08-09T07:55:03.802746Z","iopub.status.idle":"2023-08-09T07:55:04.12166Z","shell.execute_reply.started":"2023-08-09T07:55:03.802711Z","shell.execute_reply":"2023-08-09T07:55:04.120401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nALS-Z170-TRAIN-TTS-UnetPPAtt2048-tu-maxvit_base_tf_512-e20-x512-CustomLoss\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config:\n    batch_size = 2\n    image_size = 512\n\ntest_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size = Config.image_size,\n    )\n \ntest_dl = DataLoader(test_ds, batch_size=Config.batch_size, num_workers = 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:55:04.122976Z","iopub.execute_input":"2023-08-09T07:55:04.123604Z","iopub.status.idle":"2023-08-09T07:55:04.1368Z","shell.execute_reply.started":"2023-08-09T07:55:04.123571Z","shell.execute_reply":"2023-08-09T07:55:04.135647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config_z170:\n    batch_size = 32\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-maxvit_base_tf_512'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 3\tThreshold : 0.65\tDice : 0.6525253632003164\n    model_ckpt = '/kaggle/input/contrails-train-als-z170-20230718125250/epoch-05.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:55:04.138444Z","iopub.execute_input":"2023-08-09T07:55:04.138843Z","iopub.status.idle":"2023-08-09T07:55:04.147959Z","shell.execute_reply.started":"2023-08-09T07:55:04.138812Z","shell.execute_reply":"2023-08-09T07:55:04.146889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model0_z170 = UNetPlusPlusAtt2048(Config_z170).to(Config_z170.device)\nmodel0_z170.load_state_dict(torch.load(Config_z170.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:55:04.149419Z","iopub.execute_input":"2023-08-09T07:55:04.149816Z","iopub.status.idle":"2023-08-09T07:55:17.202098Z","shell.execute_reply.started":"2023-08-09T07:55:04.149745Z","shell.execute_reply":"2023-08-09T07:55:17.20108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model0_z170.eval()\ntorch.set_grad_enabled(False)\n\nval_data0_z170 = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config_z170.device)\n\n    output = model0_z170(X)\n    for key, val in output.items():\n        val_data0_z170[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data0_z170[key]\n    if len(value[0].shape) == 0:\n        val_data0_z170[key] = torch.stack(value)\n    else:\n        val_data0_z170[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data0_z170,pred_weight=1.1)\n\"\"\" Clear Cache \"\"\"\ndel model0_z170, val_data0_z170\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:55:17.203746Z","iopub.execute_input":"2023-08-09T07:55:17.204131Z","iopub.status.idle":"2023-08-09T07:55:23.951475Z","shell.execute_reply.started":"2023-08-09T07:55:17.204095Z","shell.execute_reply":"2023-08-09T07:55:23.950335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nALS-Z170-TRAIN-SKF5-UnetPPAtt2048-tu-maxvit_base_tf_512-e20-x512-CustomLoss-f\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:55:23.95327Z","iopub.execute_input":"2023-08-09T07:55:23.954174Z","iopub.status.idle":"2023-08-09T07:55:24.257949Z","shell.execute_reply.started":"2023-08-09T07:55:23.954135Z","shell.execute_reply":"2023-08-09T07:55:24.256388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config_1:\n    batch_size = 2\n    seed = 42\n    \n    encoder = 'tu-maxvit_base_tf_512'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # About OOF:0.680 ※Notebook一部セッション切れの為確認できず\n    model_ckpts = [\n        \"/kaggle/input/contrails-train-als-z170-20230723231703/model_best_dice_fold00_epoch10.pth\",\n        \"/kaggle/input/contrails-train-als-z170-20230723231703/model_best_dice_fold01_epoch05.pth\",\n        \"/kaggle/input/contrails-train-als-z170-20230723231703/model_best_dice_fold02_epoch06.pth\",\n        \"/kaggle/input/contrails-train-als-z170-20230723231703/model_best_dice_fold03_epoch08.pth\",\n        \"/kaggle/input/contrails-train-als-z170-20230723231703/model_best_dice_fold04_epoch10.pth\",\n    ]\n\n\nval_data_list = []\nfor fold in range(Config_1.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config_1.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config_1.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config_1.model_ckpts[fold], Config_1, test_dl, arc=\"UNetPlusPlusAtt2048\"))\n    print(f\"finish fold{fold}.\")\n\nval_data_1 = defaultdict(list)\n\nval_data_1[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data_1[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data_1[\"logits\"] = val_data_1[\"logits\"] / Config_1.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data_1,pred_weight=1.1)\ndel val_data_1\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:55:24.259684Z","iopub.execute_input":"2023-08-09T07:55:24.260054Z","iopub.status.idle":"2023-08-09T07:56:35.029441Z","shell.execute_reply.started":"2023-08-09T07:55:24.26002Z","shell.execute_reply":"2023-08-09T07:56:35.028007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\ntrain using human_individual_masks\n</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nBBB-A1245-TRAIN-TTS-UnetPPAtt2048-eca_nfnet_l1-e20-x512-CustomLoss-using-human_individual_masks\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config:\n    batch_size = 4\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-eca_nfnet_l1'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 13\tThreshold : 0.75\tDice : 0.6670477173042683\n    model_ckpt = '/kaggle/input/contrails-train-bbb-ta1245-eca-nfnet-l1-him/model_best_dice_e13.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:56:35.031245Z","iopub.execute_input":"2023-08-09T07:56:35.031627Z","iopub.status.idle":"2023-08-09T07:56:35.038477Z","shell.execute_reply.started":"2023-08-09T07:56:35.031583Z","shell.execute_reply":"2023-08-09T07:56:35.037087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNetPlusPlusAtt2048(Config).to(Config.device)\nmodel.load_state_dict(torch.load(Config.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:56:35.040192Z","iopub.execute_input":"2023-08-09T07:56:35.040633Z","iopub.status.idle":"2023-08-09T07:56:48.695913Z","shell.execute_reply.started":"2023-08-09T07:56:35.040602Z","shell.execute_reply":"2023-08-09T07:56:48.695009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntorch.set_grad_enabled(False)\n\nval_data = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config_a1244.device)\n\n    output = model(X)\n    for key, val in output.items():\n        val_data[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data[key]\n    if len(value[0].shape) == 0:\n        val_data[key] = torch.stack(value)\n    else:\n        val_data[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data, pred_weight=1.0)\n\"\"\" Clear Cache \"\"\"\ndel model, val_data\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:56:48.697469Z","iopub.execute_input":"2023-08-09T07:56:48.697892Z","iopub.status.idle":"2023-08-09T07:56:49.785553Z","shell.execute_reply.started":"2023-08-09T07:56:48.69786Z","shell.execute_reply":"2023-08-09T07:56:49.784494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nx384\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:56:49.787435Z","iopub.execute_input":"2023-08-09T07:56:49.787822Z","iopub.status.idle":"2023-08-09T07:56:50.091511Z","shell.execute_reply.started":"2023-08-09T07:56:49.787786Z","shell.execute_reply":"2023-08-09T07:56:50.090459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nALS-Z180-TRAIN-SKF5-UnetPPAtt2048-tu-maxxvitv2_rmlp_base_rw_384-e20-x384-CustomLoss\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"class Config:\n    batch_size = 2\n    image_size = 384\n\ntest_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size = Config.image_size,\n    )\n \ntest_dl = DataLoader(test_ds, batch_size=Config.batch_size, num_workers = 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:56:50.093273Z","iopub.execute_input":"2023-08-09T07:56:50.094517Z","iopub.status.idle":"2023-08-09T07:56:50.102987Z","shell.execute_reply.started":"2023-08-09T07:56:50.094479Z","shell.execute_reply":"2023-08-09T07:56:50.101922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config_1:\n    batch_size = 2\n    seed = 42\n    \n    encoder = 'tu-maxxvitv2_rmlp_base_rw_384'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    n_folds = 5\n    \n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 384\n    # About OOF:0.6775336912\n    model_ckpts = [\n        \"/kaggle/input/contrails-train-als-z180-20230729162357/model_best_dice_fold00_epoch03.pth\",\n        \"/kaggle/input/contrails-train-als-z180-20230729162357/model_best_dice_fold01_epoch04.pth\",\n        \"/kaggle/input/contrails-train-als-z180-20230729162357/model_best_dice_fold02_epoch09.pth\",\n        \"/kaggle/input/contrails-train-als-z180-20230729162357/model_best_dice_fold03_epoch04.pth\",\n        \"/kaggle/input/contrails-train-als-z180-20230729162357/model_best_dice_fold04_epoch04.pth\",\n    ]\n\n\nval_data_list = []\nfor fold in range(Config_1.n_folds):\n    test_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size=Config_1.image_size\n    )\n \n    test_dl = DataLoader(test_ds, batch_size=Config_1.batch_size, num_workers = 2)\n    \n    val_data_list.append(get_pred_val(Config_1.model_ckpts[fold], Config_1, test_dl, arc=\"UNetPlusPlusAtt2048\"))\n    print(f\"finish fold{fold}.\")\n\nval_data_1 = defaultdict(list)\n\nval_data_1[\"logits\"] = val_data_list[0][\"logits\"]\nfor vdata in val_data_list[1:]:\n    val_data_1[\"logits\"] += vdata[\"logits\"]\ndel val_data_list\ngc.collect()\n\nval_data_1[\"logits\"] = val_data_1[\"logits\"] / Config_1.n_folds\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data_1,pred_weight=1.0)\ndel val_data_1\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:56:50.104611Z","iopub.execute_input":"2023-08-09T07:56:50.10501Z","iopub.status.idle":"2023-08-09T07:58:18.194707Z","shell.execute_reply.started":"2023-08-09T07:56:50.104976Z","shell.execute_reply":"2023-08-09T07:58:18.19353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nx256 PL\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:18.196241Z","iopub.execute_input":"2023-08-09T07:58:18.196934Z","iopub.status.idle":"2023-08-09T07:58:18.501865Z","shell.execute_reply.started":"2023-08-09T07:58:18.196894Z","shell.execute_reply":"2023-08-09T07:58:18.500778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    batch_size = 2\n    image_size = 256\n\ntest_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size = Config.image_size,\n    )\n \ntest_dl = DataLoader(test_ds, batch_size=Config.batch_size, num_workers = 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:18.503362Z","iopub.execute_input":"2023-08-09T07:58:18.504418Z","iopub.status.idle":"2023-08-09T07:58:18.51218Z","shell.execute_reply.started":"2023-08-09T07:58:18.504353Z","shell.execute_reply":"2023-08-09T07:58:18.510793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    batch_size = 4\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-eca_nfnet_l1'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 256\n    # --[AllEpochBestVal]Epoch : 5\tThreshold : 0.70\tDice : 0.6593088048335969\n    model_ckpt = '/kaggle/input/contrails-train-bbb-z301-20230801105414/epoch-05.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:18.513592Z","iopub.execute_input":"2023-08-09T07:58:18.514377Z","iopub.status.idle":"2023-08-09T07:58:18.527179Z","shell.execute_reply.started":"2023-08-09T07:58:18.514342Z","shell.execute_reply":"2023-08-09T07:58:18.525905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNetPlusPlusAtt512Loss0307(Config).to(Config.device)\nmodel.load_state_dict(torch.load(Config.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:18.529489Z","iopub.execute_input":"2023-08-09T07:58:18.529882Z","iopub.status.idle":"2023-08-09T07:58:23.759206Z","shell.execute_reply.started":"2023-08-09T07:58:18.529847Z","shell.execute_reply":"2023-08-09T07:58:23.758276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntorch.set_grad_enabled(False)\n\nval_data = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config_a1244.device)\n\n    output = model(X)\n    for key, val in output.items():\n        val_data[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data[key]\n    if len(value[0].shape) == 0:\n        val_data[key] = torch.stack(value)\n    else:\n        val_data[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data, pred_weight=1.0)\n\"\"\" Clear Cache \"\"\"\ndel model, val_data\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:23.760796Z","iopub.execute_input":"2023-08-09T07:58:23.761165Z","iopub.status.idle":"2023-08-09T07:58:28.051634Z","shell.execute_reply.started":"2023-08-09T07:58:23.761132Z","shell.execute_reply":"2023-08-09T07:58:28.050599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nx512 PLv4\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:28.053126Z","iopub.execute_input":"2023-08-09T07:58:28.053978Z","iopub.status.idle":"2023-08-09T07:58:28.358877Z","shell.execute_reply.started":"2023-08-09T07:58:28.05394Z","shell.execute_reply":"2023-08-09T07:58:28.357381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    batch_size = 2\n    image_size = 512\n\ntest_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size = Config.image_size,\n    )\n \ntest_dl = DataLoader(test_ds, batch_size=Config.batch_size, num_workers = 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:28.360448Z","iopub.execute_input":"2023-08-09T07:58:28.36094Z","iopub.status.idle":"2023-08-09T07:58:28.37241Z","shell.execute_reply.started":"2023-08-09T07:58:28.360907Z","shell.execute_reply":"2023-08-09T07:58:28.371435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    batch_size = 2\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-maxvit_base_tf_512'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 512\n    # --[AllEpochBestVal]Epoch : 12\tThreshold : 0.75\tDice : 0.6641383083462168\n    model_ckpt = '/kaggle/input/contrails-train-als-z402-20230808135007/epoch-12.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:28.375902Z","iopub.execute_input":"2023-08-09T07:58:28.376204Z","iopub.status.idle":"2023-08-09T07:58:28.384928Z","shell.execute_reply.started":"2023-08-09T07:58:28.37618Z","shell.execute_reply":"2023-08-09T07:58:28.383733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNetPlusPlusAtt512Loss0307(Config).to(Config.device)\nmodel.load_state_dict(torch.load(Config.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:28.386095Z","iopub.execute_input":"2023-08-09T07:58:28.386421Z","iopub.status.idle":"2023-08-09T07:58:37.932031Z","shell.execute_reply.started":"2023-08-09T07:58:28.386393Z","shell.execute_reply":"2023-08-09T07:58:37.931023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntorch.set_grad_enabled(False)\n\nval_data = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config_a1244.device)\n\n    output = model(X)\n    for key, val in output.items():\n        val_data[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data[key]\n    if len(value[0].shape) == 0:\n        val_data[key] = torch.stack(value)\n    else:\n        val_data[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data, pred_weight=1.0)\n\"\"\" Clear Cache \"\"\"\ndel model, val_data\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:37.933769Z","iopub.execute_input":"2023-08-09T07:58:37.934154Z","iopub.status.idle":"2023-08-09T07:58:39.619483Z","shell.execute_reply.started":"2023-08-09T07:58:37.93412Z","shell.execute_reply":"2023-08-09T07:58:39.618405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nx224 PLv4CC2x2\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:39.621171Z","iopub.execute_input":"2023-08-09T07:58:39.621753Z","iopub.status.idle":"2023-08-09T07:58:39.924838Z","shell.execute_reply.started":"2023-08-09T07:58:39.621715Z","shell.execute_reply":"2023-08-09T07:58:39.923611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    batch_size = 4\n    image_size = 224\n\ntest_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size = Config.image_size,\n    )\n \ntest_dl = DataLoader(test_ds, batch_size=Config.batch_size, num_workers = 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:39.926666Z","iopub.execute_input":"2023-08-09T07:58:39.927056Z","iopub.status.idle":"2023-08-09T07:58:39.93658Z","shell.execute_reply.started":"2023-08-09T07:58:39.927022Z","shell.execute_reply":"2023-08-09T07:58:39.93573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    batch_size = 4\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-maxvit_rmlp_base_rw_224'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 224\n    # --[AllEpochBestVal]Epoch : 8\tThreshold : 0.75\tDice : 0.6530727939311428\n    model_ckpt = '/kaggle/input/contrails-train-als-z521-20230809025118/epoch-08.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:39.937602Z","iopub.execute_input":"2023-08-09T07:58:39.937851Z","iopub.status.idle":"2023-08-09T07:58:39.947685Z","shell.execute_reply.started":"2023-08-09T07:58:39.937829Z","shell.execute_reply":"2023-08-09T07:58:39.946749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNetPlusPlusAtt512Loss0307(Config).to(Config.device)\nmodel.load_state_dict(torch.load(Config.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:39.950843Z","iopub.execute_input":"2023-08-09T07:58:39.951137Z","iopub.status.idle":"2023-08-09T07:58:49.407403Z","shell.execute_reply.started":"2023-08-09T07:58:39.951113Z","shell.execute_reply":"2023-08-09T07:58:49.406306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntorch.set_grad_enabled(False)\n\nval_data = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config_a1244.device)\n\n    output = model(X)\n    for key, val in output.items():\n        val_data[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data[key]\n    if len(value[0].shape) == 0:\n        val_data[key] = torch.stack(value)\n    else:\n        val_data[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data, pred_weight=1.0)\n\"\"\" Clear Cache \"\"\"\ndel model, val_data\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:49.409888Z","iopub.execute_input":"2023-08-09T07:58:49.411029Z","iopub.status.idle":"2023-08-09T07:58:52.16498Z","shell.execute_reply.started":"2023-08-09T07:58:49.410992Z","shell.execute_reply":"2023-08-09T07:58:52.163962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nx224 PLv3CC2x2\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:58:52.578685Z","iopub.execute_input":"2023-08-09T07:58:52.57949Z","iopub.status.idle":"2023-08-09T07:58:53.496116Z","shell.execute_reply.started":"2023-08-09T07:58:52.579451Z","shell.execute_reply":"2023-08-09T07:58:53.494562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    batch_size = 4\n    image_size = 224\n\ntest_ds = ContrailsDataset(\n        test_df,\n        train = False,\n        image_size = Config.image_size,\n    )\n \ntest_dl = DataLoader(test_ds, batch_size=Config.batch_size, num_workers = 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:59:00.904174Z","iopub.execute_input":"2023-08-09T07:59:00.904556Z","iopub.status.idle":"2023-08-09T07:59:00.911166Z","shell.execute_reply.started":"2023-08-09T07:59:00.904528Z","shell.execute_reply":"2023-08-09T07:59:00.91018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    batch_size = 4\n    seed = 42\n    thr = 0.45\n    \n    encoder = 'tu-coatnet_rmlp_2_rw_224'\n    pretrained = False\n    weights = None\n    classes = ['contrail']\n    activation = None\n    in_chans = 3\n    \n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    image_size = 224\n    # --[AllEpochBestVal]Epoch : 5\tThreshold : 0.65\tDice : 0.6599159944332863\n    model_ckpt = '/kaggle/input/contrails-train-bbb-z415-20230809074415/epoch-05.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:59:58.569627Z","iopub.execute_input":"2023-08-09T07:59:58.570005Z","iopub.status.idle":"2023-08-09T07:59:58.576424Z","shell.execute_reply.started":"2023-08-09T07:59:58.569976Z","shell.execute_reply":"2023-08-09T07:59:58.575236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNetPlusPlusAtt512d4Loss0307(Config).to(Config.device)\nmodel.load_state_dict(torch.load(Config.model_ckpt, map_location=torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-08-09T08:00:00.374843Z","iopub.execute_input":"2023-08-09T08:00:00.375251Z","iopub.status.idle":"2023-08-09T08:00:05.67904Z","shell.execute_reply.started":"2023-08-09T08:00:00.37522Z","shell.execute_reply":"2023-08-09T08:00:05.678112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntorch.set_grad_enabled(False)\n\nval_data = defaultdict(list)\npbar = tqdm(enumerate(test_dl), total=len(test_dl), desc='Test')\nfor step, X in pbar: \n    X = X.to(Config_a1244.device)\n\n    output = model(X)\n    for key, val in output.items():\n        val_data[key] += [output[key]]\n\nfor key, val in output.items():\n    value = val_data[key]\n    if len(value[0].shape) == 0:\n        val_data[key] = torch.stack(value)\n    else:\n        val_data[key] = torch.cat(value, dim=0).cpu().detach().numpy()\n\n\"\"\" pred sum \"\"\"        \npred_mask_sum(val_data, pred_weight=1.0)\n\"\"\" Clear Cache \"\"\"\ndel model, val_data\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T08:00:06.951972Z","iopub.execute_input":"2023-08-09T08:00:06.952401Z","iopub.status.idle":"2023-08-09T08:00:10.507967Z","shell.execute_reply.started":"2023-08-09T08:00:06.952369Z","shell.execute_reply":"2023-08-09T08:00:10.506879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\n《《《《《《Submittion Create》》》》》》\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"del test_dl\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T08:00:12.034893Z","iopub.execute_input":"2023-08-09T08:00:12.035504Z","iopub.status.idle":"2023-08-09T08:00:12.363106Z","shell.execute_reply.started":"2023-08-09T08:00:12.035463Z","shell.execute_reply":"2023-08-09T08:00:12.361975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# GLOBAL_TH = 0.420\n# GLOBAL_TH = 0.425\nGLOBAL_TH = 0.460","metadata":{"execution":{"iopub.status.busy":"2023-08-09T08:00:12.364701Z","iopub.execute_input":"2023-08-09T08:00:12.36513Z","iopub.status.idle":"2023-08-09T08:00:12.375059Z","shell.execute_reply.started":"2023-08-09T08:00:12.365094Z","shell.execute_reply":"2023-08-09T08:00:12.373914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nrle_encode\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"def rle_encode(x, fg_val=1):\n    \"\"\"\n    Args:\n        x:  numpy array of shape (height, width), 1 - mask, 0 - background\n    Returns: run length encoding as list\n    \"\"\"\n\n    dots = np.where(\n        x.T.flatten() == fg_val)[0]  # .T sets Fortran order down-then-right\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if b > prev + 1:\n            run_lengths.extend((b + 1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return run_lengths\n\ndef list_to_string(x):\n    \"\"\"\n    Converts list to a string representation\n    Empty list returns '-'\n    \"\"\"\n    if x: # non-empty list\n        s = str(x).replace(\"[\", \"\").replace(\"]\", \"\").replace(\",\", \"\")\n    else:\n        s = '-'\n    return s","metadata":{"execution":{"iopub.status.busy":"2023-08-09T08:00:13.881671Z","iopub.execute_input":"2023-08-09T08:00:13.882044Z","iopub.status.idle":"2023-08-09T08:00:13.889927Z","shell.execute_reply.started":"2023-08-09T08:00:13.882016Z","shell.execute_reply":"2023-08-09T08:00:13.889035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>\nCreate Sub\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"print(\"[INFO]Model Num:\", all_pred_mask_num)\nprint(\"[INFO]Model WeightSum:\", all_pred_mask_w)\nprint(\"-\"*90)\nif all_pred_mask_w/all_pred_mask_num == 1.0:\n    print(\"---> Model Weight is OK:\", all_pred_mask_w/all_pred_mask_num)\nelse:\n    print(\">\"*50)\n    print(\">\"*50)\n    print(\"---> Model Weight is NG:\", all_pred_mask_w/all_pred_mask_num)\n    print(\">\"*50)\n    print(\">\"*50)\nprint(\"-\"*90)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T08:00:15.644317Z","iopub.execute_input":"2023-08-09T08:00:15.644682Z","iopub.status.idle":"2023-08-09T08:00:15.652047Z","shell.execute_reply.started":"2023-08-09T08:00:15.644652Z","shell.execute_reply":"2023-08-09T08:00:15.65119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(Paths.data + '/sample_submission.csv', index_col='record_id')\n\nfor i, (pred) in enumerate(zip(all_pred_mask_sum['logits'])):\n    rec = test_df['record_id'][i]\n    \n    \"\"\" Blend \"\"\"\n    mask = (pred[0]/all_pred_mask_num > GLOBAL_TH).astype(np.float32)\n    submission.loc[int(rec), 'encoded_pixels'] = list_to_string(rle_encode(mask))\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T08:00:16.485769Z","iopub.execute_input":"2023-08-09T08:00:16.486662Z","iopub.status.idle":"2023-08-09T08:00:16.515328Z","shell.execute_reply.started":"2023-08-09T08:00:16.486612Z","shell.execute_reply":"2023-08-09T08:00:16.514361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-09T08:00:18.320914Z","iopub.execute_input":"2023-08-09T08:00:18.321611Z","iopub.status.idle":"2023-08-09T08:00:18.327908Z","shell.execute_reply.started":"2023-08-09T08:00:18.321576Z","shell.execute_reply":"2023-08-09T08:00:18.326855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nEOF\n</b></h1> ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}