{"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":"raw","source":"## summary\n\n* 2.5d segmentation\n    *  segmentation_models_pytorch \n    *  Unet\n* use only 6 slices\n* slide inference","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score, accuracy_score, f1_score, log_loss\nimport pickle\nfrom torch.utils.data import DataLoader\nfrom torch.cuda.amp import autocast, GradScaler\nimport warnings\nimport sys\nimport pandas as pd\nimport os\nimport gc\nimport sys\nimport math\nimport time\nimport random\nimport shutil\nfrom pathlib import Path\nfrom contextlib import contextmanager\nfrom collections import defaultdict, Counter\nimport cv2\n\nimport scipy as sp\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nfrom tqdm.auto import tqdm\nfrom functools import partial\n\nimport argparse\nimport importlib\nimport torch\nimport torch.nn as nn\nfrom torch.optim import Adam, SGD, AdamW\n\nimport datetime\nimport wandb\n\nimport glob\n\nfrom skimage.morphology import remove_small_objects","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:14:21.878209Z","iopub.execute_input":"2023-06-05T03:14:21.879507Z","iopub.status.idle":"2023-06-05T03:14:26.462674Z","shell.execute_reply.started":"2023-06-05T03:14:21.879441Z","shell.execute_reply":"2023-06-05T03:14:26.461261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/files-whl/whlfiles/einops-0.6.1-py3-none-any.whl\n!pip install /kaggle/input/files-whl/whlfiles/iopath-0.1.10-py3-none-any.whl\n!pip install /kaggle/input/files-whl/whlfiles/fvcore-0.1.5.post20221221-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:14:26.465451Z","iopub.execute_input":"2023-06-05T03:14:26.466675Z","iopub.status.idle":"2023-06-05T03:15:54.561481Z","shell.execute_reply.started":"2023-06-05T03:14:26.466636Z","shell.execute_reply":"2023-06-05T03:15:54.559852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold = 0.47\nimg_size = 256\nin_chans = 22\nresnet_depth = 152\nweights_path_movinet = '/kaggle/input/inchans16-movineta5-longepochs-21252428'\nweights_path_resnet152 = '/kaggle/input/2023-06-05-12-22-clip-50-200'\nweights_path_resnet200 = '/kaggle/input/2023-06-08-12-22-clip50-200-ls03-resnet200'\nweights_path_resnext = '/kaggle/input/2023-06-07-12-22-clip50-200-ls03-resnext101'\nweights_path_resnet_tmp = 'weights-tmp'\n\n\n# LB Best 0.79 4th -> '/kaggle/input/2023-06-05-12-22-clip-50-200' & '/kaggle/input/2023-06-07-12-22-clip50-200-ls03-resnext101' & '/kaggle/input/2023-06-08-12-22-clip50-200-ls03-resnet200' stride 5 no remove\n# LB Best 0.79 -> '/kaggle/input/2023-06-05-12-22-clip-50-200' & '/kaggle/input/2023-06-07-12-22-clip50-200-ls03-resnext101'\n# LB Best 0.78 -> '/kaggle/input/2023-06-05-12-22-clip-50-200' & thr 0.5, no remove\n# LB Best 0.78 -> '/kaggle/input/2023-05-30-randomcrop12-22-randompaste-cutout2-50e' & threshold 0.6, remove 0.0003\n# resnet solo LB Best 0.77 -> '/kaggle/input/2023-05-30-randomcrop12-22-randompaste-cutout2-50e' & threshold 0.6, no remove\n# resnet solo LB Best 0.76 -> '/kaggle/input/2023-05-28-randomcrop14-18-inchans18-fromstart' (name before modification -> '/kaggle/input/2023-05-28-randomcrop16-18-inchans18-fromstart')\n# resnet solo LB Best 0.74 -> /kaggle/input/inchans16-resnet152-stride3\n# movinet LB best 0.63 -> inchans16-movineta5-50epoch\n# movinet LB ensemble best 0.72 -> inchans16-movineta5-longepochs-21252428\n# resnet LB best 0.71 -> weights-3dcnn-inchans16-resnetdepth152-4fold6281\nflip_test = False\nrot_test = False\n\nensemble = True\nresnet152_weight = 1\nresnet200_weight = 1\nresnext_weight = 1\nmovinet_weight = 1\nremove_small_objects_degree_rate = 0.0001  #0.0025\nstride_rate = 5\n\ntta_temporal = False\ntta_num = 3\n\nclipping = True\nclip_min = 50\nclip_max = 200\nCELOSS = True\nremove_small_object = True\nbatct_size = 16\n\nmodel_movinet = False\nmodel_resnet152 = True\nmodel_resnet200 = True\nmodel_resnext = True\n\nshort_test = False\ndenoise = False\niter_num = 50","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:54.563271Z","iopub.execute_input":"2023-06-05T03:15:54.563637Z","iopub.status.idle":"2023-06-05T03:15:54.571491Z","shell.execute_reply.started":"2023-06-05T03:15:54.563601Z","shell.execute_reply":"2023-06-05T03:15:54.570048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append('/kaggle/input/pretrainedmodels/pretrainedmodels-0.7.4')\nsys.path.append('/kaggle/input/efficientnet-pytorch/EfficientNet-PyTorch-master')\nsys.path.append('/kaggle/input/timm-pytorch-image-models/pytorch-image-models-master')\n# sys.path.append('/kaggle/input/segmentation-models-pytorch/segmentation_models.pytorch-master')\n# sys.path.append('/kaggle/input/segmentation/segmentation_models_pytorch_my')\nsys.path.append('/kaggle/input/resnet3d')\nsys.path.append('/kaggle/input/resnet')\nsys.path.append('/kaggle/input/movinet/MoViNet-pytorch')\n\n# import segmentation_models_pytorch as smp\n# from resnet3d import *\nfrom resnet import *","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:54.575235Z","iopub.execute_input":"2023-06-05T03:15:54.575701Z","iopub.status.idle":"2023-06-05T03:15:54.603529Z","shell.execute_reply.started":"2023-06-05T03:15:54.575657Z","shell.execute_reply":"2023-06-05T03:15:54.602387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append('/kaggle/input/efficient-3dcnns/Efficient-3DCNNs')\nfrom models.resnext import resnext101, resnext152","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:54.606298Z","iopub.execute_input":"2023-06-05T03:15:54.606832Z","iopub.status.idle":"2023-06-05T03:15:54.632613Z","shell.execute_reply.started":"2023-06-05T03:15:54.606787Z","shell.execute_reply":"2023-06-05T03:15:54.631549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom torch.utils.data import DataLoader, Dataset\nimport cv2\nimport torch\nimport os\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom albumentations import ImageOnlyTransform","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:54.633855Z","iopub.execute_input":"2023-06-05T03:15:54.634169Z","iopub.status.idle":"2023-06-05T03:15:55.801559Z","shell.execute_reply.started":"2023-06-05T03:15:54.634138Z","shell.execute_reply":"2023-06-05T03:15:55.800463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## config","metadata":{}},{"cell_type":"code","source":"IS_DEBUG = False\nmode = 'train' if IS_DEBUG else 'test'\nTH = 0.4","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:55.802728Z","iopub.execute_input":"2023-06-05T03:15:55.802978Z","iopub.status.idle":"2023-06-05T03:15:55.808980Z","shell.execute_reply.started":"2023-06-05T03:15:55.802952Z","shell.execute_reply":"2023-06-05T03:15:55.807832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:55.810387Z","iopub.execute_input":"2023-06-05T03:15:55.810672Z","iopub.status.idle":"2023-06-05T03:15:55.822634Z","shell.execute_reply.started":"2023-06-05T03:15:55.810620Z","shell.execute_reply":"2023-06-05T03:15:55.820930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## helper","metadata":{}},{"cell_type":"code","source":"import os\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\nclass CFG:\n    # ============== comp exp name =============\n    comp_name = 'vesuvius'\n\n    # comp_dir_path = './'\n    comp_dir_path = '/kaggle/input/'\n    comp_folder_name = 'vesuvius-challenge-ink-detection'\n    # comp_dataset_path = f'{comp_dir_path}datasets/{comp_folder_name}/'\n    comp_dataset_path = f'{comp_dir_path}{comp_folder_name}/'\n    \n    exp_name = 'vesuvius_2d_slide_exp002'\n\n    # ============== pred target =============\n    target_size = 1\n\n    # ============== model cfg =============\n    model_name = 'Unet'\n    backbone = 'efficientnet-b7'\n#     backbone = 'se_resnext50_32x4d'\n\n    in_chans = in_chans # 65\n    # ============== training cfg =============\n    size = img_size\n    tile_size = img_size\n    stride = tile_size // stride_rate\n\n    batch_size = batct_size # 32\n    use_amp = True\n\n    scheduler = 'GradualWarmupSchedulerV2'\n    # scheduler = 'CosineAnnealingLR'\n    epochs = 15\n\n    warmup_factor = 10\n    lr = 1e-4 / warmup_factor\n\n    # ============== fold =============\n    valid_id = 2\n\n    objective_cv = 'binary'  # 'binary', 'multiclass', 'regression'\n    metric_direction = 'maximize'  # maximize, 'minimize'\n    # metrics = 'dice_coef'\n\n    # ============== fixed =============\n    pretrained = True\n    inf_weight = 'best'  # 'best'\n\n    min_lr = 1e-6\n    weight_decay = 1e-6\n    max_grad_norm = 1000\n\n    print_freq = 50\n    num_workers = 2\n\n    seed = 42\n\n    # ============== augmentation =============\n    train_aug_list = [\n        # A.RandomResizedCrop(\n        #     size, size, scale=(0.85, 1.0)),\n        A.Resize(size, size),\n        A.HorizontalFlip(p=0.5),\n        A.VerticalFlip(p=0.5),\n        A.RandomBrightnessContrast(p=0.75),\n        A.ShiftScaleRotate(p=0.75),\n        A.OneOf([\n                A.GaussNoise(var_limit=[10, 50]),\n                A.GaussianBlur(),\n                A.MotionBlur(),\n                ], p=0.4),\n        A.GridDistortion(num_steps=5, distort_limit=0.3, p=0.5),\n        A.CoarseDropout(max_holes=1, max_width=int(size * 0.3), max_height=int(size * 0.3), \n                        mask_fill_value=0, p=0.5),\n        # A.Cutout(max_h_size=int(size * 0.6),\n        #          max_w_size=int(size * 0.6), num_holes=1, p=1.0),\n        A.Normalize(\n            mean= [0] * in_chans,\n            std= [1] * in_chans\n        ),\n        ToTensorV2(transpose_mask=True),\n    ]\n\n    valid_aug_list = [\n#         A.Resize(size, size),\n        A.Normalize(\n            mean= [0] * in_chans,\n            std= [1] * in_chans\n        ),\n        ToTensorV2(transpose_mask=True),\n    ]\n","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:55.824011Z","iopub.execute_input":"2023-06-05T03:15:55.824330Z","iopub.status.idle":"2023-06-05T03:15:55.838459Z","shell.execute_reply.started":"2023-06-05T03:15:55.824301Z","shell.execute_reply":"2023-06-05T03:15:55.837450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref.: https://www.kaggle.com/stainsby/fast-tested-rle\ndef rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    # pixels = (pixels >= thr).astype(int)\n    \n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:55.841474Z","iopub.execute_input":"2023-06-05T03:15:55.841784Z","iopub.status.idle":"2023-06-05T03:15:55.854307Z","shell.execute_reply.started":"2023-06-05T03:15:55.841757Z","shell.execute_reply":"2023-06-05T03:15:55.853412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## dataset","metadata":{}},{"cell_type":"code","source":"def read_image(fragment_id):\n    images = []\n    masks = []\n\n    # idxs = range(65)\n    mid = 65 // 2\n    start = mid - CFG.in_chans // 2\n    end = mid + CFG.in_chans // 2\n    idxs = range(start, end)\n#     idxs = [14, 15] + list(idxs)\n\n    for i in tqdm(idxs):\n        \n        image = cv2.imread(CFG.comp_dataset_path + f\"{mode}/{fragment_id}/surface_volume/{i:02}.tif\", 0)\n        pad0 = (CFG.tile_size - image.shape[0] % CFG.tile_size)\n        pad1 = (CFG.tile_size - image.shape[1] % CFG.tile_size)\n#         image = np.pad(image, [(0, pad0), (0, pad1)], constant_values=0)\n        images.append(image)\n        \n    mask = cv2.imread(CFG.comp_dataset_path + f\"{mode}/{fragment_id}/mask.png\", 0)\n    pad0 = (CFG.tile_size - mask.shape[0] % CFG.tile_size)\n    pad1 = (CFG.tile_size - mask.shape[1] % CFG.tile_size)\n#     mask = np.pad(mask, [(0, pad0), (0, pad1)], constant_values=0)\n        \n    images = np.stack(images, axis=2)\n    \n    \n    return images, mask\n","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:55.855836Z","iopub.execute_input":"2023-06-05T03:15:55.856165Z","iopub.status.idle":"2023-06-05T03:15:55.866783Z","shell.execute_reply.started":"2023-06-05T03:15:55.856129Z","shell.execute_reply":"2023-06-05T03:15:55.865723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_transforms(data, cfg):\n    if data == 'train':\n        aug = A.Compose(cfg.train_aug_list)\n    elif data == 'valid':\n        aug = A.Compose(cfg.valid_aug_list)\n\n    # print(aug)\n    return aug\n\nclass CustomDataset(Dataset):\n    def __init__(self, images, cfg, labels=None, transform=None):\n        self.images = images\n        self.cfg = cfg\n        self.labels = labels\n        self.transform = transform\n\n    def __len__(self):\n        # return len(self.xyxys)\n        return len(self.images)\n\n    def __getitem__(self, idx):\n        image = self.images[idx]\n        mask = self.labels[idx]\n        \n        if clipping:\n            image = np.clip(image, clip_min, clip_max)\n        \n        image_horizontal_flip = A.Compose([\n        A.HorizontalFlip(p=1),\n        A.Resize(CFG.size, CFG.size),\n        A.Normalize(\n            mean= [0] * in_chans,\n            std= [1] * in_chans\n        ),\n        ToTensorV2(transpose_mask=True),\n        ])(image = image.copy())['image'].unsqueeze(0)\n\n        \n        tta_total = []\n        if tta_temporal:\n            for i in range(tta_num):\n                left = (in_chans // 2) - ((in_chans - 2*i) // 2)\n                right = (in_chans // 2) + ((in_chans - 2*i) // 2)\n            \n                image_tmp = np.zeros_like(image)\n                image_tmp[..., 0 : (in_chans - 2*i)] = image[..., left : right]\n                data_tmp = self.transform(image=image_tmp)\n                image_tmp = data_tmp['image'].unsqueeze(0)\n                tta_total.append(image_tmp)\n                \n            data = self.transform(image=image)\n            image = data['image'].unsqueeze(0)\n\n            return image, image_horizontal_flip, tta_total, mask\n        \n        else:\n            data = self.transform(image=image)\n            image = data['image'].unsqueeze(0)\n        \n            return image, image_horizontal_flip, _, mask","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:23:49.518936Z","iopub.execute_input":"2023-06-05T03:23:49.519325Z","iopub.status.idle":"2023-06-05T03:23:49.533355Z","shell.execute_reply.started":"2023-06-05T03:23:49.519286Z","shell.execute_reply":"2023-06-05T03:23:49.532089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_test_dataset(fragment_id):\n    test_images, mask = read_image(fragment_id)\n    \n    x1_list = list(range(0, test_images.shape[1]-CFG.tile_size+1, CFG.stride))\n    y1_list = list(range(0, test_images.shape[0]-CFG.tile_size+1, CFG.stride))\n    \n    test_images_list = []\n    test_masks_list = []\n    xyxys = []\n    for y1 in y1_list:\n        for x1 in x1_list:\n            y2 = y1 + CFG.tile_size\n            x2 = x1 + CFG.tile_size\n            \n            test_images_list.append(test_images[y1:y2, x1:x2])\n            test_masks_list.append(mask[y1:y2, x1:x2])\n            xyxys.append((x1, y1, x2, y2))\n    xyxys = np.stack(xyxys)\n            \n    test_dataset = CustomDataset(test_images_list, CFG, test_masks_list, transform=get_transforms(data='valid', cfg=CFG))\n    \n    test_loader = DataLoader(test_dataset,\n                             batch_size=CFG.batch_size,\n                             shuffle=False,\n                             num_workers=CFG.num_workers, pin_memory=True, drop_last=False)\n    \n    return test_loader, xyxys","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:24:00.332883Z","iopub.execute_input":"2023-06-05T03:24:00.333247Z","iopub.status.idle":"2023-06-05T03:24:00.341551Z","shell.execute_reply.started":"2023-06-05T03:24:00.333215Z","shell.execute_reply":"2023-06-05T03:24:00.340599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## model","metadata":{}},{"cell_type":"code","source":"class CustomModel(nn.Module):\n    def __init__(self, cfg, weight=None):\n        super().__init__()\n        self.cfg = cfg\n\n        self.encoder = smp.UnetPlusPlus(\n            encoder_name=cfg.backbone, \n            encoder_weights=weight,\n            in_channels=cfg.in_chans,\n            classes=cfg.target_size,\n            activation=None,\n        )\n\n    def forward(self, image):\n        output = self.encoder(image)\n        output = output.squeeze(-1)\n        return output\n\n    \n    \n    \n\nclass Decoder(nn.Module):\n    def __init__(self, encoder_dims, upscale):\n        super().__init__()\n        self.convs = nn.ModuleList([\n            nn.Sequential(\n                nn.Conv2d(encoder_dims[i]+encoder_dims[i-1], encoder_dims[i-1], 3, 1, 1, bias=False),\n                nn.BatchNorm2d(encoder_dims[i-1]),\n                nn.ReLU(inplace=True)\n            ) for i in range(1, len(encoder_dims))])\n\n        if CELOSS:\n            self.logit = nn.Conv2d(encoder_dims[0], 2, 1, 1, 0)\n        else:\n            self.logit = nn.Conv2d(encoder_dims[0], 1, 1, 1, 0)\n        \n        self.up = nn.Upsample(scale_factor=upscale, mode=\"bilinear\")\n\n    def forward(self, feature_maps):\n        for i in range(len(feature_maps)-1, 0, -1):\n            f_up = F.interpolate(feature_maps[i], scale_factor=2, mode=\"bilinear\")\n            f = torch.cat([feature_maps[i-1], f_up], dim=1)\n            f_down = self.convs[i-1](f)\n            feature_maps[i-1] = f_down\n\n        x = self.logit(feature_maps[0])\n        mask = self.up(x)\n        return mask\n\n\nclass SegModel(nn.Module):\n    def __init__(self, depth):\n        super().__init__()\n#         self.encoder = generate_model(model_depth=18, n_input_channels=1)\n        \n        self.depth = depth\n    \n        self.encoder = generate_model(model_depth=self.depth,\n                              n_input_channels=1,\n                              shortcut_type='B',\n                              conv1_t_size=7,\n                              conv1_t_stride=1,\n                              widen_factor=1.0,\n                              n_classes=1039,\n                              no_max_pool=True)\n        \n        \n#         self.decoder = Decoder(encoder_dims=[64, 128, 256, 512], upscale=4)\n        self.decoder = Decoder(encoder_dims=[256, 512, 1024, 2048], upscale=4)\n        \n    def forward(self, x):\n        feat_maps = self.encoder(x)\n        feat_maps_pooled = [torch.mean(f, dim=2) for f in feat_maps]\n        pred_mask = self.decoder(feat_maps_pooled)\n        return pred_mask\n    \n    def load_pretrained_weights(self, state_dict):\n        # Convert 3 channel weights to single channel\n        # ref - https://timm.fast.ai/models#Case-1:-When-the-number-of-input-channels-is-1\n        conv1_weight = state_dict['conv1.weight']\n        state_dict['conv1.weight'] = conv1_weight.sum(dim=1, keepdim=True)\n        print(self.encoder.load_state_dict(state_dict, strict=False))\n\n\n    \nclass SegModel_resnext101(nn.Module):\n    def __init__(self):\n        super().__init__()\n        # original kaggle code\n        # self.encoder = generate_model(model_depth=args.resnet_depth, n_input_channels=1)\n\n        # original paper code\n        self.encoder = resnext101(sample_size=112,\n                                  sample_duration=16,\n                                  shortcut_type='B',\n                                  cardinality=32,\n                                  num_classes=600)\n\n        # original paper code\n        self.decoder = Decoder(encoder_dims=[256, 512, 1024, 2048], upscale=4)\n\n    def forward(self, x):\n        feat_maps = self.encoder(x)\n        feat_maps_pooled = [torch.mean(f, dim=2) for f in feat_maps]\n        pred_mask = self.decoder(feat_maps_pooled)\n        return pred_mask\n\n\nclass SegModel_resnext152(nn.Module):\n    def __init__(self):\n        super().__init__()\n        # original kaggle code\n        # self.encoder = generate_model(model_depth=args.resnet_depth, n_input_channels=1)\n\n        # original paper code\n        self.encoder = resnext152(sample_size=112,\n                                  sample_duration=16,\n                                  shortcut_type='B',\n                                  cardinality=32,\n                                  num_classes=600)\n\n        # original paper code\n        self.decoder = Decoder(encoder_dims=[256, 512, 1024, 2048], upscale=4)\n\n    def forward(self, x):\n        feat_maps = self.encoder(x)\n        feat_maps_pooled = [torch.mean(f, dim=2) for f in feat_maps]\n        pred_mask = self.decoder(feat_maps_pooled)\n        return pred_mask\n\n    \n    \n    \ndef build_model(cfg, weight=\"imagenet\"):\n    print('model_name', cfg.model_name)\n    print('backbone', cfg.backbone)\n\n    #model = CustomModel(cfg, weight)\n    model = SegModel()\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2023-06-05T03:15:55.888225Z","iopub.status.idle":"2023-06-05T03:15:55.888686Z","shell.execute_reply.started":"2023-06-05T03:15:55.888433Z","shell.execute_reply":"2023-06-05T03:15:55.888459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Movinet","metadata":{}},{"cell_type":"code","source":"from movinets import MoViNet\nfrom movinets.config import _C\nmodel = MoViNet(_C.MODEL.MoViNetA5, causal = False, pretrained=False)\nmodel.conv1.conv_1.conv3d = nn.Conv3d(1, 24, kernel_size=(1, 3, 3), stride=(1, 2, 2), bias=False)\nlayers = list(model.children())\n\n\n# class Decoder(nn.Module):\n#     def __init__(self, encoder_dims, upscale):\n#         super().__init__()\n#         self.convs = nn.ModuleList([\n#             nn.Sequential(\n#                 nn.Conv2d(encoder_dims[i] + encoder_dims[i - 1], encoder_dims[i - 1], 3, 1, 1, bias=False),\n#                 nn.BatchNorm2d(encoder_dims[i - 1]),\n#                 nn.ReLU(inplace=True)\n#             ) for i in range(1, len(encoder_dims))])\n\n#         self.logit = nn.Conv2d(encoder_dims[0], 1, 1, 1, 0)\n#         self.up = nn.Upsample(scale_factor=upscale, mode=\"bilinear\")\n\n#     def forward(self, feature_maps):\n#         for i in range(len(feature_maps) - 1, 0, -1):\n#             f_up = F.interpolate(feature_maps[i], scale_factor=2, mode=\"bilinear\")\n#             f = torch.cat([feature_maps[i - 1], f_up], dim=1)\n#             f_down = self.convs[i - 1](f)\n#             feature_maps[i - 1] = f_down\n\n#         x = self.logit(feature_maps[0])\n#         mask = self.up(x)\n#         return mask\n\nclass MoviNetA5_modified(nn.Module):\n    def __init__(self, layers):\n        super().__init__()\n\n        # A3\n#         block2_num = 4\n#         block3_num = 6\n#         block4_num = 5\n#         block5_num = 8\n#         block6_num = 10\n        \n        \n        # A5\n        block2_num = 6\n        block3_num = 11\n        block4_num = 13\n        block5_num = 11\n        block6_num = 18\n\n        self.layers = layers\n\n        self.block0 = self.layers[0]\n\n        self.block2 = self.layers[1][:block2_num]\n        self.block3 = self.layers[1][block2_num : block2_num + block3_num]\n        self.block4 = self.layers[1][block2_num + block3_num : block2_num + block3_num + block4_num]\n        self.block5 = self.layers[1][block2_num + block3_num + block4_num : block2_num + block3_num + block4_num + block5_num]\n        self.block6 = self.layers[1][block2_num + block3_num + block4_num + block5_num : block2_num + block3_num + block4_num + block5_num + block6_num]\n\n#         self.decoder = Decoder(encoder_dims=[16, 48, 88, 168], upscale=4)\n        self.decoder = Decoder(encoder_dims=[24, 64, 120, 224], upscale=4)\n\n\n    def forward(self, x):\n        x = self.block0(x)\n\n        x2 = self.block2(x)\n        x3 = self.block3(x2)\n        x4 = self.block4(x3)\n        x5 = self.block5(x4)\n        x6 = self.block6(x5)\n\n        x_total = [x2, x3, x5, x6]\n\n        x_total = [torch.mean(f, dim=2) for f in x_total]\n\n        result = self.decoder(x_total)\n\n        return result\n\n# model = MoviNetA5_modified(layers)\n# model = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-05-30T14:44:46.140731Z","iopub.execute_input":"2023-05-30T14:44:46.141389Z","iopub.status.idle":"2023-05-30T14:44:46.861135Z","shell.execute_reply.started":"2023-05-30T14:44:46.141329Z","shell.execute_reply":"2023-05-30T14:44:46.859482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-05-30T14:46:20.806444Z","iopub.execute_input":"2023-05-30T14:46:20.806934Z","iopub.status.idle":"2023-05-30T14:46:20.813871Z","shell.execute_reply.started":"2023-05-30T14:46:20.806876Z","shell.execute_reply":"2023-05-30T14:46:20.812170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-05-30T14:46:20.972854Z","iopub.execute_input":"2023-05-30T14:46:20.973327Z","iopub.status.idle":"2023-05-30T14:46:53.829817Z","shell.execute_reply.started":"2023-05-30T14:46:20.973285Z","shell.execute_reply":"2023-05-30T14:46:53.828414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EnsembleModel:\n    def __init__(self, use_tta=False):\n        self.models = []\n        self.use_tta = use_tta\n\n    def __call__(self, x):\n\n        if ensemble:\n            total = []\n            for i in range(len(self.models)):\n                model = self.models[i]\n                if i <= 3:\n                    if CELOSS:\n                        pred = (torch.softmax(model(x), 1))[:, 1, ...].to('cpu').detach().numpy() * resnext_weight\n                    else:\n                        pred = (torch.sigmoid(model(x))).to('cpu').detach().numpy() * resnext_weight\n                \n                \n                elif i > 3 and i <= 7:\n                    \n                    if CELOSS:\n                        pred = (torch.softmax(model(x), 1))[:, 1, ...].to('cpu').detach().numpy() * resnet152_weight\n                    else:\n                        pred = (torch.sigmoid(model(x))).to('cpu').detach().numpy() * resnet152_weight\n                \n\n                else:\n                    \n                    if CELOSS:\n                        pred = (torch.softmax(model(x), 1))[:, 1, ...].to('cpu').detach().numpy() * resnet200_weight\n                    else:\n                        pred = (torch.sigmoid(model(x))).to('cpu').detach().numpy() * resnet200_weight\n                    \n                total.append(pred)\n            outputs = total\n            \n            \n        else:\n            if CELOSS:\n                outputs = [torch.softmax(model(x), 1)[:, 1, ...].to('cpu').detach().numpy() for model in self.models]\n            else:\n                outputs = [torch.sigmoid(model(x)).to('cpu').detach().numpy() for model in self.models]\n        \n\n        avg_preds = np.mean(outputs, axis=0)\n        return avg_preds\n\n    def add_model(self, model):\n        self.models.append(model)\n\n        \n        \nmovinet_weights_list = glob.glob(os.path.join(weights_path_movinet, '*'))\nresnet152_weights_list = glob.glob(os.path.join(weights_path_resnet152, '*'))\nresnet200_weights_list = glob.glob(os.path.join(weights_path_resnet200, '*'))\nresnext_weights_list = glob.glob(os.path.join(weights_path_resnext, '*'))\n    \n        \ndef build_ensemble_model():\n    model = EnsembleModel()\n    \n    if model_movinet:\n    \n        for weights in movinet_weights_list:\n\n            _model = MoviNetA5_modified(layers)\n            _model.to(device)\n\n            state = torch.load(weights)['model']\n\n            _model.load_state_dict(state)\n            _model.eval()\n\n            model.add_model(_model)\n    \n    if model_resnext:\n        for weights in resnext_weights_list:\n\n            _model = SegModel_resnext101()\n            _model.encoder.conv1 = nn.Conv3d(1, 64, kernel_size=(7, 7, 7), stride=(1, 2, 2), padding=(3, 3, 3), bias=False)\n            _model.to(device)\n\n            state = torch.load(weights)['model']\n            _model.load_state_dict(state)\n            _model.eval()\n\n            model.add_model(_model)\n    \n    if model_resnet152:\n        for weights in resnet152_weights_list:\n\n            _model = SegModel(152)\n            _model.to(device)\n\n            state = torch.load(weights)['model']\n            _model.load_state_dict(state)\n            _model.eval()\n\n            model.add_model(_model)\n    \n    \n    if model_resnet200:\n        for weights in resnet200_weights_list:\n\n            _model = SegModel(200)\n            _model.to(device)\n\n            state = torch.load(weights)['model']\n            _model.load_state_dict(state)\n            _model.eval()\n\n            model.add_model(_model)\n    \n    \n    \n#     for i in range(4):\n        \n#         _model = SegModel()\n#         _model.to(device)\n        \n#         if i == 0:\n#             weights = '/kaggle/input/3fold-balance-fold1-tmp/Unet_fold1_epoch20_score0.6237474369132259.pth'\n#         elif i == 1:\n#             weights = '/kaggle/input/weights-3dcnn-inchans16-resnetdepth152-4fold6281/Unet_fold2_best.pth'\n#         elif i == 2:\n#             weights = '/kaggle/input/weights-3dcnn-inchans16-resnetdepth152-4fold6281/Unet_fold3_best.pth'\n#         else:\n#             weights = '/kaggle/input/weights-3dcnn-inchans16-resnetdepth152-4fold6281/Unet_fold4_best.pth'\n        \n    \n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-05-30T14:46:19.373938Z","iopub.execute_input":"2023-05-30T14:46:19.374608Z","iopub.status.idle":"2023-05-30T14:46:19.400258Z","shell.execute_reply.started":"2023-05-30T14:46:19.374525Z","shell.execute_reply":"2023-05-30T14:46:19.398713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def TTA(x:torch.Tensor,model:nn.Module):\n    #x.shape=(batch,c,h,w)\n    shape=x.shape\n    x=[x,*[torch.rot90(x,k=i,dims=(-2,-1)) for i in range(1,4)]]\n    x=torch.cat(x,dim=0)\n    x=model(x)\n    x=torch.from_numpy(x)\n    x=x.reshape(4,shape[0],1,*shape[-2:])\n    x=[torch.rot90(x[i],k=-i,dims=(-2,-1)) for i in range(4)]\n    x=torch.stack(x,dim=0)\n    return x.mean(0).numpy()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if mode == 'test':\n    fragment_ids = sorted(os.listdir(CFG.comp_dataset_path + mode))\nelse:\n    fragment_ids = [3]\n    \n    \nmodel = build_ensemble_model()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cupy as cp\n\nxp = cp\n# xp = np\n\ndelta_lookup = {\n    \"xx\": xp.array([[1, -2, 1]], dtype=float),\n    \"yy\": xp.array([[1], [-2], [1]], dtype=float),\n    \"xy\": xp.array([[1, -1], [-1, 1]], dtype=float),\n}\n\n\ndef operate_derivative(img_shape, pair):\n    assert len(img_shape) == 2\n    delta = delta_lookup[pair]\n    fft = xp.fft.fftn(delta, img_shape)\n    return fft * xp.conj(fft)\n\n\ndef soft_threshold(vector, threshold):\n    return xp.sign(vector) * xp.maximum(xp.abs(vector) - threshold, 0)\n\n\ndef back_diff(input_image, dim):\n    assert dim in (0, 1)\n    r, n = xp.shape(input_image)\n    size = xp.array((r, n))\n    position = xp.zeros(2, dtype=int)\n    temp1 = xp.zeros((r + 1, n + 1), dtype=float)\n    temp2 = xp.zeros((r + 1, n + 1), dtype=float)\n\n    temp1[position[0]:size[0], position[1]:size[1]] = input_image\n    temp2[position[0]:size[0], position[1]:size[1]] = input_image\n\n    size[dim] += 1\n    position[dim] += 1\n    temp2[position[0]:size[0], position[1]:size[1]] = input_image\n    temp1 -= temp2\n    size[dim] -= 1\n    return temp1[0:size[0], 0:size[1]]\n\n\ndef forward_diff(input_image, dim):\n    assert dim in (0, 1)\n    r, n = xp.shape(input_image)\n    size = xp.array((r, n))\n    position = xp.zeros(2, dtype=int)\n    temp1 = xp.zeros((r + 1, n + 1), dtype=float)\n    temp2 = xp.zeros((r + 1, n + 1), dtype=float)\n\n    size[dim] += 1\n    position[dim] += 1\n\n    temp1[position[0]:size[0], position[1]:size[1]] = input_image\n    temp2[position[0]:size[0], position[1]:size[1]] = input_image\n\n    size[dim] -= 1\n    temp2[0:size[0], 0:size[1]] = input_image\n    temp1 -= temp2\n    size[dim] += 1\n    return -temp1[position[0]:size[0], position[1]:size[1]]\n\n\ndef iter_deriv(input_image, b, scale, mu, dim1, dim2):\n    g = back_diff(forward_diff(input_image, dim1), dim2)\n    d = soft_threshold(g + b, 1 / mu)\n    b = b + (g - d)\n    L = scale * back_diff(forward_diff(d - b, dim2), dim1)\n    return L, b\n\n\ndef iter_xx(*args):\n    return iter_deriv(*args, dim1=1, dim2=1)\n\n\ndef iter_yy(*args):\n    return iter_deriv(*args, dim1=0, dim2=0)\n\n\ndef iter_xy(*args):\n    return iter_deriv(*args, dim1=0, dim2=1)\n\n\ndef iter_sparse(input_image, bsparse, scale, mu):\n    d = soft_threshold(input_image + bsparse, 1 / mu)\n    bsparse = bsparse + (input_image - d)\n    Lsparse = scale * (d - bsparse)\n    return Lsparse, bsparse\n\n\ndef denoise_image(input_image, iter_num=100, fidelity=150, sparsity_scale=10, continuity_scale=0.5, mu=1):\n    image_size = xp.shape(input_image)\n    # print(\"Initialize denoising\")\n    norm_array = (\n            operate_derivative(image_size, \"xx\") +\n            operate_derivative(image_size, \"yy\") +\n            2 * operate_derivative(image_size, \"xy\")\n    )\n    norm_array += (fidelity / mu) + sparsity_scale ** 2\n    b_arrays = {\n        \"xx\": xp.zeros(image_size, dtype=float),\n        \"yy\": xp.zeros(image_size, dtype=float),\n        \"xy\": xp.zeros(image_size, dtype=float),\n        \"L1\": xp.zeros(image_size, dtype=float),\n    }\n    g_update = xp.multiply(fidelity / mu, input_image)\n    for i in tqdm(range(iter_num), total=iter_num):\n        # print(f\"Starting iteration {i+1}\")\n        g_update = xp.fft.fftn(g_update)\n        if i == 0:\n            g = xp.fft.ifftn(g_update / (fidelity / mu)).real\n        else:\n            g = xp.fft.ifftn(xp.divide(g_update, norm_array)).real\n        g_update = xp.multiply((fidelity / mu), input_image)\n\n        # print(\"XX update\")\n        L, b_arrays[\"xx\"] = iter_xx(g, b_arrays[\"xx\"], continuity_scale, mu)\n        g_update += L\n\n        # print(\"YY update\")\n        L, b_arrays[\"yy\"] = iter_yy(g, b_arrays[\"yy\"], continuity_scale, mu)\n        g_update += L\n\n        # print(\"XY update\")\n        L, b_arrays[\"xy\"] = iter_xy(g, b_arrays[\"xy\"], 2 * continuity_scale, mu)\n        g_update += L\n\n        # print(\"L1 update\")\n        L, b_arrays[\"L1\"] = iter_sparse(g, b_arrays[\"L1\"], sparsity_scale, mu)\n        g_update += L\n\n    g_update = xp.fft.fftn(g_update)\n    g = xp.fft.ifftn(xp.divide(g_update, norm_array)).real\n\n    g[g < 0] = 0\n    g -= g.min()\n    g /= g.max()\n    return g\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-30T14:41:35.032390Z","iopub.execute_input":"2023-05-30T14:41:35.032856Z","iopub.status.idle":"2023-05-30T14:41:35.184264Z","shell.execute_reply.started":"2023-05-30T14:41:35.032811Z","shell.execute_reply":"2023-05-30T14:41:35.181581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## main","metadata":{}},{"cell_type":"code","source":"results = []\nfor fragment_id in fragment_ids:\n    \n    test_loader, xyxys = make_test_dataset(fragment_id)\n    \n    binary_mask = cv2.imread(CFG.comp_dataset_path + f\"{mode}/{fragment_id}/mask.png\", 0)\n    binary_mask = (binary_mask / 255).astype(int)\n    \n    ori_h = binary_mask.shape[0]\n    ori_w = binary_mask.shape[1]\n    # mask = mask / 255\n\n    pad0 = (CFG.tile_size - binary_mask.shape[0] % CFG.tile_size)\n    pad1 = (CFG.tile_size - binary_mask.shape[1] % CFG.tile_size)\n\n#     binary_mask = np.pad(binary_mask, [(0, pad0), (0, pad1)], constant_values=0)\n    \n    mask_pred = np.zeros(binary_mask.shape)\n    mask_count = np.zeros(binary_mask.shape)\n\n    for step, (images, image_horizontal_flip, images_tta, mask) in tqdm(enumerate(test_loader), total=len(test_loader)):\n        \n        if short_test:\n            if step == 1:\n                break\n        \n        \n        if mask.max() == 0:\n            continue\n        \n        images = images.to(device)\n        image_horizontal_flip = image_horizontal_flip.to(device)\n        \n        if tta_temporal:\n            images_tta = [i.to(device) for i in images_tta]\n            \n        batch_size = images.size(0)\n\n        with torch.no_grad():\n            \n            if flip_test:\n                y_preds_org = model(images)\n                y_preds_horizontal_flip = model(image_horizontal_flip) # (batch_size, 1, 16, 256, 256)\n                y_preds_horizontal_flip = [cv2.flip(i[0], 1)[None, ...] for i in y_preds_horizontal_flip]\n                y_preds_horizontal_flip = np.stack(y_preds_horizontal_flip)\n                \n                y_preds = (y_preds_org + y_preds_horizontal_flip) / 2\n\n            elif rot_test:\n                y_preds = TTA(images, model)\n                \n            else:\n                \n                if tta_temporal:\n                    y_preds_tta = []\n                    for k in range(len(images_tta)):\n                        y_preds_tta.append(model(images_tta[k]))\n                        \n                    y_preds_tta = np.concatenate(y_preds_tta, 1).mean(1)[:, None, :, :]\n                    y_preds = (y_preds_tta) / len(images_tta)\n                    \n                else:\n                    y_preds = model(images)\n                \n                \n                \n        start_idx = step*CFG.batch_size\n        end_idx = start_idx + batch_size\n        for i, (x1, y1, x2, y2) in enumerate(xyxys[start_idx:end_idx]):\n            if CELOSS:\n                mask_pred[y1:y2, x1:x2] += y_preds[i]#.squeeze(0)\n            else:\n                mask_pred[y1:y2, x1:x2] += y_preds[i].squeeze(0)\n            mask_count[y1:y2, x1:x2] += np.ones((CFG.tile_size, CFG.tile_size))\n    \n\n    mask_pred /= (mask_count + (1e-7))\n    \n    if denoise:\n        mask_pred=xp.array(mask_pred)\n        mask_pred=denoise_image(mask_pred, iter_num=iter_num)\n        mask_pred=mask_pred.get()\n    \n    \n    mask_pred = mask_pred[:ori_h, :ori_w]\n    binary_mask = binary_mask[:ori_h, :ori_w]\n    \n    remove_small_objects_degree = int((mask_pred.shape[0] * mask_pred.shape[1]) * remove_small_objects_degree_rate)\n    \n    \n    if remove_small_object:\n        mask_pred = remove_small_objects(mask_pred >= threshold, remove_small_objects_degree)\n    else:\n        mask_pred = remove_small_objects(mask_pred >= threshold, 1)\n    \n    mask_pred = mask_pred.astype(int)\n    mask_pred *= binary_mask\n    \n    inklabels_rle = rle(mask_pred)\n    \n    results.append((fragment_id, inklabels_rle))\n    \n\n    del mask_pred, mask_count\n    del test_loader\n    \n    gc.collect()\n    torch.cuda.empty_cache()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-19T03:47:04.440875Z","iopub.execute_input":"2023-05-19T03:47:04.441674Z","iopub.status.idle":"2023-05-19T03:54:39.502789Z","shell.execute_reply.started":"2023-05-19T03:47:04.441632Z","shell.execute_reply":"2023-05-19T03:54:39.500997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## submission","metadata":{}},{"cell_type":"code","source":"sub = pd.DataFrame(results, columns=['Id', 'Predicted'])","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:17:46.487259Z","iopub.status.idle":"2023-04-16T07:17:46.488148Z","shell.execute_reply.started":"2023-04-16T07:17:46.487871Z","shell.execute_reply":"2023-04-16T07:17:46.487899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:17:46.489574Z","iopub.status.idle":"2023-04-16T07:17:46.490328Z","shell.execute_reply.started":"2023-04-16T07:17:46.490065Z","shell.execute_reply":"2023-04-16T07:17:46.490092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv(CFG.comp_dataset_path + 'sample_submission.csv')\nsample_sub = pd.merge(sample_sub[['Id']], sub, on='Id', how='left')","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:17:46.491762Z","iopub.status.idle":"2023-04-16T07:17:46.492634Z","shell.execute_reply.started":"2023-04-16T07:17:46.492330Z","shell.execute_reply":"2023-04-16T07:17:46.492360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub","metadata":{"execution":{"iopub.status.busy":"2023-04-04T05:04:59.863677Z","iopub.execute_input":"2023-04-04T05:04:59.864045Z","iopub.status.idle":"2023-04-04T05:04:59.874408Z","shell.execute_reply.started":"2023-04-04T05:04:59.864009Z","shell.execute_reply":"2023-04-04T05:04:59.873278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-04T05:04:59.875995Z","iopub.execute_input":"2023-04-04T05:04:59.876637Z","iopub.status.idle":"2023-04-04T05:04:59.890511Z","shell.execute_reply.started":"2023-04-04T05:04:59.8766Z","shell.execute_reply":"2023-04-04T05:04:59.88919Z"},"trusted":true},"execution_count":null,"outputs":[]}]}