{"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":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append(\"/kaggle/input/pretrained-models-pytorch\")\nsys.path.append(\"/kaggle/input/efficientnet-pytorch\")\nsys.path.append(\"/kaggle/input/smp-github/segmentation_models.pytorch-master\")","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:17.44696Z","iopub.execute_input":"2023-08-02T12:10:17.447326Z","iopub.status.idle":"2023-08-02T12:10:17.461394Z","shell.execute_reply.started":"2023-08-02T12:10:17.447296Z","shell.execute_reply":"2023-08-02T12:10:17.460358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport math\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:17.463301Z","iopub.execute_input":"2023-08-02T12:10:17.463742Z","iopub.status.idle":"2023-08-02T12:10:17.47763Z","shell.execute_reply.started":"2023-08-02T12:10:17.463712Z","shell.execute_reply":"2023-08-02T12:10:17.476721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport segmentation_models_pytorch as smp\nimport pytorch_lightning as pl\nfrom pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping, TQDMProgressBar\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader\nfrom torch.optim.lr_scheduler import CosineAnnealingLR, ReduceLROnPlateau\nfrom torch.optim import AdamW\nfrom torchmetrics.functional import dice\nimport torchvision.transforms as T\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport os\nimport random\nimport numpy as np\nimport pandas as pd\nimport yaml","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:17.483742Z","iopub.execute_input":"2023-08-02T12:10:17.484016Z","iopub.status.idle":"2023-08-02T12:10:33.762848Z","shell.execute_reply.started":"2023-08-02T12:10:17.483987Z","shell.execute_reply":"2023-08-02T12:10:33.761856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir data\n!mkdir data/test","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:33.765004Z","iopub.execute_input":"2023-08-02T12:10:33.765791Z","iopub.status.idle":"2023-08-02T12:10:35.804993Z","shell.execute_reply.started":"2023-08-02T12:10:33.765753Z","shell.execute_reply":"2023-08-02T12:10:35.803671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_models = {\n    \"Unet\": smp.Unet,\n    \"UnetPlusPlus\": smp.UnetPlusPlus,\n    \"MAnet\": smp.MAnet,\n    \"Linknet\": smp.Linknet,\n    \"FPN\": smp.FPN,\n    \"PSPNet\": smp.PSPNet,\n    \"PAN\": smp.PAN,\n    \"DeepLabV3\": smp.DeepLabV3,\n    \"DeepLabV3Plus\": smp.DeepLabV3Plus,\n}","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:35.808055Z","iopub.execute_input":"2023-08-02T12:10:35.808475Z","iopub.status.idle":"2023-08-02T12:10:35.816305Z","shell.execute_reply.started":"2023-08-02T12:10:35.808422Z","shell.execute_reply":"2023-08-02T12:10:35.815351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    AUTHOR = 'takaito'\n    COMPETITION = 'google-research-identify-contrails-reduce-global-warming'\n    DATA_PATH = '/kaggle/input/google-research-identify-contrails-reduce-global-warming'\n    SEED = 2023\n    image_size = 256\n    model_num = 5","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:35.819543Z","iopub.execute_input":"2023-08-02T12:10:35.819986Z","iopub.status.idle":"2023-08-02T12:10:35.82606Z","shell.execute_reply.started":"2023-08-02T12:10:35.819957Z","shell.execute_reply":"2023-08-02T12:10:35.82501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    pl.seed_everything(seed)\n\nseed_everything(CFG.SEED)\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:35.82754Z","iopub.execute_input":"2023-08-02T12:10:35.82793Z","iopub.status.idle":"2023-08-02T12:10:35.855708Z","shell.execute_reply.started":"2023-08-02T12:10:35.827899Z","shell.execute_reply":"2023-08-02T12:10:35.854728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# make submission","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-02T12:10:35.857144Z","iopub.execute_input":"2023-08-02T12:10:35.857532Z","iopub.status.idle":"2023-08-02T12:10:35.864953Z","shell.execute_reply.started":"2023-08-02T12:10:35.857502Z","shell.execute_reply":"2023-08-02T12:10:35.863905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(f'{CFG.DATA_PATH}/sample_submission.csv', index_col='record_id')\npred_matrix_dict = {}\nfor record_id in submission.index:\n    pred_matrix_dict[record_id] = np.zeros((CFG.image_size, CFG.image_size), dtype=np.float32)\nstep1_pred_matrix_dict = {}\nfor record_id in submission.index:\n    step1_pred_matrix_dict[record_id] = np.zeros((9, CFG.image_size, CFG.image_size), dtype=np.float32)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:35.866509Z","iopub.execute_input":"2023-08-02T12:10:35.867192Z","iopub.status.idle":"2023-08-02T12:10:35.901185Z","shell.execute_reply.started":"2023-08-02T12:10:35.867163Z","shell.execute_reply":"2023-08-02T12:10:35.900348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# make input data","metadata":{}},{"cell_type":"code","source":"class fastnumpyio:\n    def load(file):\n        file=open(file,\"rb\")\n        header = file.read(128)\n        descr = str(header[19:25], 'utf-8').replace(\"'\",\"\").replace(\" \",\"\")\n        shape = tuple(int(num) for num in str(header[60:120], 'utf-8').replace(', }', '').replace('(', '').replace(')', '').split(','))\n        datasize = np.lib.format.descr_to_dtype(descr).itemsize\n        for dimension in shape:\n            datasize *= dimension\n        return np.ndarray(shape, dtype=descr, buffer=file.read(datasize))\n\ndef read_record(record_id, directory):\n    record_data = {}\n    for target in [\"band_11\", \"band_14\", \"band_15\", \"human_pixel_masks\", \"human_individual_masks\"]:\n        try:\n            record_data[target] = fastnumpyio.load(f'{directory}/{record_id}/{target}.npy')\n            # with open(f'{directory}/{record_id}/{target}.npy', 'rb') as f:\n            #     record_data[target] = np.load(f)\n            if target[:5] == 'band_':\n                record_data[target] = record_data[target][..., 3:6]\n        except Exception as e:\n            pass\n    return record_data\n\ndef normalize_range(data, bounds):\n    \"\"\"Maps data to the range [0, 1].\"\"\"\n    return (data - bounds[0]) / (bounds[1] - bounds[0])\n\ndef get_false_color(record_data):\n    _T11_BOUNDS = (243, 303)\n    _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n    _TDIFF_BOUNDS = (-4, 2)\n    r = normalize_range(record_data[\"band_15\"] - record_data[\"band_14\"], _TDIFF_BOUNDS)\n    g = normalize_range(record_data[\"band_14\"] - record_data[\"band_11\"], _CLOUD_TOP_TDIFF_BOUNDS)\n    b = normalize_range(record_data[\"band_14\"], _T11_BOUNDS)\n    false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n    return false_color\n\ndef read_meta_data(flag):\n    df = pd.read_json(f'{CFG.DATA_PATH}/{flag}_metadata.json')\n    df['month'] = df['timestamp'].dt.month\n    df['hour'] = df['timestamp'].dt.hour\n    df['flag'] = flag\n    df = df.drop(['projection_wkt'], axis=1)\n    return df\n\ndef read_record_id(flag):\n    df = pd.DataFrame({'record_id': os.listdir(f'{CFG.DATA_PATH}/{flag}')})\n    df['record_id'] = df['record_id'].astype(int)\n    df = df.sort_values('record_id').reset_index(drop=True)\n    labeler_count_list = []\n    for record_id in tqdm(df['record_id']):\n        record_data = read_record(record_id, f'{CFG.DATA_PATH}/{flag}')\n        false_color = get_false_color(record_data)\n        np.save(f'./data/{flag}/{record_id}_input_img', false_color)\n        if 'human_pixel_masks' in record_data:\n            np.save(f'./data/{flag}/{record_id}_human_pixel_masks', record_data['human_pixel_masks'])\n        if 'human_individual_masks' in record_data:\n            np.save(f'./data/{flag}/{record_id}_human_individual_masks', record_data['human_individual_masks'])\n        if flag == 'train':\n            labeler_count_list.append(record_data['human_individual_masks'].shape[-1])\n    if flag == 'train':\n        df['labeler_count'] = labeler_count_list\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:35.904061Z","iopub.execute_input":"2023-08-02T12:10:35.904312Z","iopub.status.idle":"2023-08-02T12:10:35.923164Z","shell.execute_reply.started":"2023-08-02T12:10:35.904291Z","shell.execute_reply":"2023-08-02T12:10:35.922057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = read_record_id('test')\ntest_df['img_path'] = './data/test/' + test_df['record_id'].astype(str) + '_input_img.npy'","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:35.924389Z","iopub.execute_input":"2023-08-02T12:10:35.926343Z","iopub.status.idle":"2023-08-02T12:10:36.272549Z","shell.execute_reply.started":"2023-08-02T12:10:35.926312Z","shell.execute_reply":"2023-08-02T12:10:36.27146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step1","metadata":{}},{"cell_type":"code","source":"class ContrailsDataset(torch.utils.data.Dataset):\n    def __init__(self, df, page=1, image_size=CFG.image_size, train=True):\n\n        self.df = df\n        self.page = page\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 = image_size\n        if image_size != CFG.image_size:\n            self.resize_image = T.transforms.Resize(image_size)\n\n    def __getitem__(self, index):\n        row = self.df.iloc[index]\n        img = fastnumpyio.load(row['img_path'])[..., self.page]\n        img = torch.tensor(np.reshape(img, (CFG.image_size, CFG.image_size, 3))).to(torch.float32).permute(2, 0, 1)\n        if self.image_size != CFG.image_size:\n            img = self.resize_image(img)\n        img = self.normalize_image(img)\n        image_id = int(row['record_id'])\n\n        return img.float(), torch.tensor(image_id)\n\n    def __len__(self):\n        return len(self.df)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:36.277116Z","iopub.execute_input":"2023-08-02T12:10:36.277399Z","iopub.status.idle":"2023-08-02T12:10:36.286829Z","shell.execute_reply.started":"2023-08-02T12:10:36.277374Z","shell.execute_reply":"2023-08-02T12:10:36.285643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LightningModule(pl.LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.model = seg_models[config['model']['seg_model']](\n                        encoder_name=config['model']['encoder_name'],\n                        encoder_weights=None,\n                        in_channels=3,\n                        classes=1,\n                        activation=None)\n\n    def forward(self, batch):\n        return self.model(batch)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:36.288186Z","iopub.execute_input":"2023-08-02T12:10:36.289247Z","iopub.status.idle":"2023-08-02T12:10:36.297891Z","shell.execute_reply.started":"2023-08-02T12:10:36.289216Z","shell.execute_reply":"2023-08-02T12:10:36.296766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fold_inference(config, test_df, model_flag):\n    config['test_bs'] = config['train_bs']\n    config['workers'] = 0\n    \n    for k in range(config['FOLD_NUM']):\n        model = LightningModule().load_from_checkpoint(config[f'model_path_fold{k}'])\n        device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n        model.to(device)\n        model.eval()\n        model.zero_grad()\n        \n        for page in range(3):\n            dataset_test = ContrailsDataset(test_df, page, config['model']['resize_image_size'], train=False)\n            data_loader_test = DataLoader(dataset_test, batch_size=config['test_bs'], num_workers=config['workers'])\n            for i, data in enumerate(data_loader_test):\n                images, image_id = data\n\n                # Predict mask for this instance\n                images = images.to(device)\n                with torch.no_grad():\n                    predicted_mask = model.forward(images)\n                if config['model']['resize_image_size'] != CFG.image_size:\n                    predicted_mask = torch.nn.functional.interpolate(predicted_mask, size=CFG.image_size, mode='bilinear')\n                predicted_mask = torch.sigmoid(predicted_mask).cpu().detach().numpy()\n                for img_num in range(0, images.shape[0]):\n                    current_image_id = image_id[img_num].item()\n                    step1_pred_matrix_dict[current_image_id][page * 3 + model_flag] += predicted_mask[img_num, 0, :, :] / config['FOLD_NUM']","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:36.301032Z","iopub.execute_input":"2023-08-02T12:10:36.30143Z","iopub.status.idle":"2023-08-02T12:10:36.311988Z","shell.execute_reply.started":"2023-08-02T12:10:36.301401Z","shell.execute_reply":"2023-08-02T12:10:36.311046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EfficientNet","metadata":{}},{"cell_type":"code","source":"file_path = '/kaggle/input/gr-icrgw-tuning-models/UnetPlusPlus_efficientnet-b7_takaito_model_seed2023_ver2/UnetPlusPlus_efficientnet-b7_takaito_model_seed2023_ver2'\nwith open(f'{file_path}.yml') as file:\n    config = yaml.safe_load(file.read())\nfor k in range(config['FOLD_NUM']):\n    config[f'model_path_fold{k}'] = f\"{file_path[:-4]}fold{k}_ver{config['VER']}.ckpt\"\nfold_inference(config, test_df, 1)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:10:36.315042Z","iopub.execute_input":"2023-08-02T12:10:36.315296Z","iopub.status.idle":"2023-08-02T12:11:35.537092Z","shell.execute_reply.started":"2023-08-02T12:10:36.315275Z","shell.execute_reply":"2023-08-02T12:11:35.536072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = '/kaggle/input/gr-icrgw-tuning-models/FPN_efficientnet-b7_takaito_model_seed2023_ver2/FPN_efficientnet-b7_takaito_model_seed2023_ver2'\nwith open(f'{file_path}.yml') as file:\n    config = yaml.safe_load(file.read())\nfor k in range(config['FOLD_NUM']):\n    config[f'model_path_fold{k}'] = f\"{file_path[:-4]}fold{k}_ver{config['VER']}.ckpt\"\nfold_inference(config, test_df, 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:11:35.538376Z","iopub.execute_input":"2023-08-02T12:11:35.53873Z","iopub.status.idle":"2023-08-02T12:12:22.683901Z","shell.execute_reply.started":"2023-08-02T12:11:35.538697Z","shell.execute_reply":"2023-08-02T12:12:22.682878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNeSt","metadata":{}},{"cell_type":"code","source":"file_path = '/kaggle/input/gr-icrgw-tuning-models/UnetPlusPlus_timm-resnest200e_takaito_model_seed2023_ver2/UnetPlusPlus_timm-resnest200e_takaito_model_seed2023_ver2'\nwith open(f'{file_path}.yml') as file:\n    config = yaml.safe_load(file.read())\nfor k in range(config['FOLD_NUM']):\n    config[f'model_path_fold{k}'] = f\"{file_path[:-4]}fold{k}_ver{config['VER']}.ckpt\"\nfold_inference(config, test_df, 0)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:12:22.685362Z","iopub.execute_input":"2023-08-02T12:12:22.685728Z","iopub.status.idle":"2023-08-02T12:13:45.79143Z","shell.execute_reply.started":"2023-08-02T12:12:22.685695Z","shell.execute_reply":"2023-08-02T12:13:45.790457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step2","metadata":{}},{"cell_type":"code","source":"class ContrailsDataset(torch.utils.data.Dataset):\n    def __init__(self, df, image_size=CFG.image_size, train=True):\n        self.df = df\n        self.trn = train\n        self.image_size = image_size\n        if image_size != CFG.image_size:\n            self.resize_image = T.transforms.Resize(image_size)\n\n    def __getitem__(self, index):\n        row = self.df.iloc[index]\n        image_id = int(row['record_id'])\n        img = torch.tensor(step1_pred_matrix_dict[image_id])\n        if self.image_size != CFG.image_size:\n            img = self.resize_image(img)\n        \n        return img.float(), torch.tensor(image_id)\n\n    def __len__(self):\n        return len(self.df)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:15:20.451725Z","iopub.execute_input":"2023-08-02T12:15:20.452433Z","iopub.status.idle":"2023-08-02T12:15:20.460886Z","shell.execute_reply.started":"2023-08-02T12:15:20.452399Z","shell.execute_reply":"2023-08-02T12:15:20.459966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LightningModule(pl.LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.model = seg_models[config['model']['seg_model']](\n                        encoder_name=config['model']['encoder_name'],\n                        encoder_depth=config['model']['encoder_depth'],\n                        encoder_weights=None,\n                        in_channels=9,\n                        classes=1,\n                        activation=None)\n    def forward(self, batch):\n        return self.model(batch)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:15:21.053907Z","iopub.execute_input":"2023-08-02T12:15:21.054233Z","iopub.status.idle":"2023-08-02T12:15:21.06068Z","shell.execute_reply.started":"2023-08-02T12:15:21.054207Z","shell.execute_reply":"2023-08-02T12:15:21.059792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fold_inference(config, test_df):\n    config['test_bs'] = config['train_bs']\n    config['workers'] = 0\n    dataset_test = ContrailsDataset(test_df, config['model']['resize_image_size'], train=False)\n    data_loader_test = DataLoader(dataset_test, batch_size=config['test_bs'], num_workers=config['workers'])\n    \n    for k in range(config['FOLD_NUM']):\n        model = LightningModule().load_from_checkpoint(config[f'model_path_fold{k}'])\n        device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n        model.to(device)\n        model.eval()\n        model.zero_grad()\n        for i, data in enumerate(data_loader_test):\n            images, image_id = data\n\n            # Predict mask for this instance\n            images = images.to(device)\n            with torch.no_grad():\n                predicted_mask = model.forward(images)\n            if config['model']['resize_image_size'] != CFG.image_size:\n                predicted_mask = torch.nn.functional.interpolate(predicted_mask, size=CFG.image_size, mode='bilinear')\n            predicted_mask = torch.sigmoid(predicted_mask).cpu().detach().numpy()\n            for img_num in range(0, images.shape[0]):\n                current_image_id = image_id[img_num].item()\n                pred_matrix_dict[current_image_id] += predicted_mask[img_num, 0, :, :]","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:15:21.532828Z","iopub.execute_input":"2023-08-02T12:15:21.533164Z","iopub.status.idle":"2023-08-02T12:15:21.544862Z","shell.execute_reply.started":"2023-08-02T12:15:21.533138Z","shell.execute_reply":"2023-08-02T12:15:21.543881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = '/kaggle/input/gr-icrgw-tuning-models/Unet_efficientnet-b1_takaito_model_seed2023_ver4/Unet_efficientnet-b1_takaito_model_seed2023_ver4'\nwith open(f'{file_path}.yml') as file:\n    config = yaml.safe_load(file.read())\nfor k in range(config['FOLD_NUM']):\n    config[f'model_path_fold{k}'] = f\"{file_path[:-4]}fold{k}_ver{config['VER']}.ckpt\"\nfold_inference(config, test_df)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:15:22.546079Z","iopub.execute_input":"2023-08-02T12:15:22.546428Z","iopub.status.idle":"2023-08-02T12:15:31.853595Z","shell.execute_reply.started":"2023-08-02T12:15:22.5464Z","shell.execute_reply":"2023-08-02T12:15:31.852626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ensemble","metadata":{}},{"cell_type":"code","source":"for current_image_id in pred_matrix_dict:\n    pred_matrix = pred_matrix_dict[current_image_id] / CFG.model_num\n    current_mask = pred_matrix >= 0.5\n    submission.loc[int(current_image_id), 'encoded_pixels'] = list_to_string(rle_encode(current_mask))","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:15:31.885529Z","iopub.execute_input":"2023-08-02T12:15:31.885867Z","iopub.status.idle":"2023-08-02T12:15:31.893847Z","shell.execute_reply.started":"2023-08-02T12:15:31.885837Z","shell.execute_reply":"2023-08-02T12:15:31.892941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:15:31.895106Z","iopub.execute_input":"2023-08-02T12:15:31.895745Z","iopub.status.idle":"2023-08-02T12:15:31.909444Z","shell.execute_reply.started":"2023-08-02T12:15:31.895711Z","shell.execute_reply":"2023-08-02T12:15:31.908318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r *\nsubmission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-02T12:15:31.911102Z","iopub.execute_input":"2023-08-02T12:15:31.911484Z","iopub.status.idle":"2023-08-02T12:15:33.005376Z","shell.execute_reply.started":"2023-08-02T12:15:31.911454Z","shell.execute_reply":"2023-08-02T12:15:33.003934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}