{"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":"# smp importするために必要\nimport 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\")\n# Hide Warning\nimport warnings\nwarnings.filterwarnings('ignore', category=DeprecationWarning)\nwarnings.filterwarnings('ignore', category=FutureWarning)\nwarnings.filterwarnings('ignore', category=UserWarning)\n\n# Python Libraries\nimport os\nimport math\nimport random\nimport glob\nimport pickle\nimport gc\nfrom collections import defaultdict\nfrom pathlib import Path\n\n# Third party\nimport numpy as np\nimport pandas as pd\nimport polars as pl\nfrom tqdm.notebook import tqdm\n\n# Visualizations\n# from PIL import Image\n# import cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\n%matplotlib inline\nsns.set(style=\"whitegrid\")\n\n# Pytorch \nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader, WeightedRandomSampler\n\nfrom torchvision.io import read_image\nimport torchvision.transforms as T\n\n# Pytorch Lightning 新しいほうは import lightning as L\nimport pytorch_lightning as L\n\n# Pytorch Image Models\nimport timm\nimport segmentation_models_pytorch as smp","metadata":{"execution":{"iopub.status.busy":"2023-08-09T00:31:57.9767Z","iopub.execute_input":"2023-08-09T00:31:57.977067Z","iopub.status.idle":"2023-08-09T00:32:18.089633Z","shell.execute_reply.started":"2023-08-09T00:31:57.977036Z","shell.execute_reply":"2023-08-09T00:32:18.088698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndata = '/kaggle/input/google-research-identify-contrails-reduce-global-warming'\ndata_root = '/kaggle/input/google-research-identify-contrails-reduce-global-warming/validation/'\nsubmission = pd.read_csv(os.path.join(data, 'sample_submission.csv'), index_col='record_id')\n\nfilenames = os.listdir(data_root)\ntest_df = pd.DataFrame(filenames, columns=['record_id'])\ntest_df['path'] = data_root + test_df['record_id'].astype(str)\nTEST_ID = test_df[\"record_id\"].to_list()\n\n# アンサンブルするモデルの予測値を入れてく（辞書で\nglobal_preds = []\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T00:32:18.09155Z","iopub.execute_input":"2023-08-09T00:32:18.091885Z","iopub.status.idle":"2023-08-09T00:32:18.217317Z","shell.execute_reply.started":"2023-08-09T00:32:18.091853Z","shell.execute_reply":"2023-08-09T00:32:18.216348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## functions\n\n- submission 用に Run Length Encode\n- https://www.kaggle.com/code/inversion/contrails-rle-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\n\n# ====================================================\n# teyo comon prediction func\n# ====================================================\ndef predict(model, loader, TTA=False) -> dict:\n    \"\"\"\n    1fold分の予測を辞書型で返す\n    sigmoid通していいのかよく分からん\n    \"\"\"\n    model = model.to(device)\n    model = model.eval()\n    predicts = []\n    model_preds = {}\n    for data in tqdm(loader):\n        images, image_id = data\n        images = images.to(device)\n        with torch.no_grad():\n            predicted_mask = model(images[:, :, :, :],None)\n        # 元の画像サイズに戻す\n        if predicted_mask[0].shape[-1] != 256:\n            predicted_masks = torch.nn.functional.interpolate(predicted_mask, size=256, mode='bilinear')\n        # Batchsize, n_Class, H, W (2, 1, 256, 256)\n        predicted_masks = torch.sigmoid(predicted_masks).cpu().detach().numpy()\n        for cur_mask,cur_id in zip(predicted_masks,image_id):\n            model_preds[str(cur_id.item())] = cur_mask\n    \n    return model_preds","metadata":{"execution":{"iopub.status.busy":"2023-08-09T00:32:18.21873Z","iopub.execute_input":"2023-08-09T00:32:18.219155Z","iopub.status.idle":"2023-08-09T00:32:18.233305Z","shell.execute_reply.started":"2023-08-09T00:32:18.219122Z","shell.execute_reply":"2023-08-09T00:32:18.232334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- 学習は8フレーム目だけをRGBにした画像を直接読み込めたが、testデータは園処理をする必要がある\n    - https://www.kaggle.com/datasets/shashwatraman/contrails-images-ash-color","metadata":{}},{"cell_type":"code","source":"class ContrailsDataset(torch.utils.data.Dataset):\n    \"\"\"\n    Ashカラーを\n    \"\"\"\n    def __init__(self, df, image_size=256, train=True):\n        \n        self.df = df\n        self.trn = train\n#         self.df_idx: pd.DataFrame = pd.DataFrame({'idx': os.listdir(f'/kaggle/input/google-research-identify-contrails-reduce-global-warming/validaion')})\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 != 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(np.reshape(img, (256, 256, 3))).to(torch.float32).permute(2, 0, 1)\n        \n        if self.image_size != 256:\n            img = self.resize_image(img)\n        \n        img = self.normalize_image(img)\n        \n#         image_id = int(self.df_idx.iloc[index]['idx'])\n        image_id = int(self.df.iloc[index]['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-09T00:32:18.237658Z","iopub.execute_input":"2023-08-09T00:32:18.237912Z","iopub.status.idle":"2023-08-09T00:32:18.252759Z","shell.execute_reply.started":"2023-08-09T00:32:18.237889Z","shell.execute_reply":"2023-08-09T00:32:18.251754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## teyo models","metadata":{}},{"cell_type":"code","source":"class CustomUnet(nn.Module):\n    def __init__(self, cfg):\n        super().__init__()\n        self.cfg = cfg\n        \n        self.encoder = smp.Unet(\n            encoder_name=cfg.backbone, \n            encoder_weights=None,\n            in_channels=cfg.in_chans,\n            classes=cfg.target_size,\n            activation=None,\n            decoder_use_batchnorm=True,\n            decoder_attention_type='scse'\n        )\n        self.conv1 = nn.Conv2d(in_channels=9, out_channels=18, kernel_size=1)\n        self.conv2 = nn.Conv2d(in_channels=18, out_channels=3, kernel_size=1)\n\n    def forward(self, image,features):\n        output = self.encoder(image)\n        return output\n\nclass CustomUnetPlusPlus(nn.Module):\n    def __init__(self, cfg):\n        super().__init__()\n        self.cfg = cfg\n        \n        self.encoder = smp.UnetPlusPlus(\n            encoder_name=cfg.backbone, \n            encoder_weights=None,\n            in_channels=cfg.in_chans,\n            classes=cfg.target_size,\n            activation=None,\n            decoder_use_batchnorm=True,\n            decoder_attention_type='scse'\n        )\n        self.conv1 = nn.Conv2d(in_channels=9, out_channels=18, kernel_size=1)\n        self.conv2 = nn.Conv2d(in_channels=18, out_channels=3, kernel_size=1)\n\n    def forward(self, image,features):\n        output = self.encoder(image)\n        return output\n\nclass CustomDeepLabV3Plus(nn.Module):\n    def __init__(self, cfg):\n        super().__init__()\n        self.cfg = cfg\n        \n        self.encoder = smp.DeepLabV3Plus(\n            encoder_name=cfg.backbone, \n            encoder_weights=None,\n            in_channels=cfg.in_chans,\n            classes=cfg.target_size,\n            activation=None,\n        )\n        self.conv1 = nn.Conv2d(in_channels=9, out_channels=18, kernel_size=1)\n        self.conv2 = nn.Conv2d(in_channels=18, out_channels=3, kernel_size=1)\n\n    def forward(self, image,features):\n        output = self.encoder(image)\n        return output","metadata":{"execution":{"iopub.status.busy":"2023-08-09T00:32:18.254092Z","iopub.execute_input":"2023-08-09T00:32:18.25442Z","iopub.status.idle":"2023-08-09T00:32:18.266466Z","shell.execute_reply.started":"2023-08-09T00:32:18.25439Z","shell.execute_reply":"2023-08-09T00:32:18.265561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_thr(predicted, true_label):\n    # 予測値と正解ラベルの配列\n    # predicted = np.concatenate(preds)\n    # true_label = np.concatenate(masks)\n\n    # 予測値をバイナリに変換するための閾値\n    threshold = 0.5  # 閾値の初期値\n\n    # 最適な閾値を求める\n    best_dice = 0.0\n    scores = []\n    for t in tqdm(np.arange(0.0, 0.5, 0.01)):\n        predicted_binary = (predicted > t).astype(int)\n        intersection = np.sum(predicted_binary * true_label)\n        dice = (2.0 * intersection) / (np.sum(predicted_binary) + np.sum(true_label))\n        scores.append(dice)\n        if dice > best_dice:\n            best_dice = dice\n            threshold = t\n\n    print(\"最適な閾値:\", threshold)\n    print(\"最適なDice係数:\", best_dice)\n    return scores","metadata":{"execution":{"iopub.status.busy":"2023-08-09T00:32:18.267959Z","iopub.execute_input":"2023-08-09T00:32:18.268323Z","iopub.status.idle":"2023-08-09T00:32:18.280798Z","shell.execute_reply.started":"2023-08-09T00:32:18.268268Z","shell.execute_reply":"2023-08-09T00:32:18.279792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = []\nfor k in tqdm(test_df[\"record_id\"]):\n    mask = np.load(f\"/kaggle/input/google-research-identify-contrails-reduce-global-warming/validation/{k}/human_pixel_masks.npy\").flatten()\n    masks.append(mask)\nmasks = np.concatenate(masks)\nnp.save('masks', masks)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T00:32:18.282496Z","iopub.execute_input":"2023-08-09T00:32:18.282756Z","iopub.status.idle":"2023-08-09T00:32:30.67892Z","shell.execute_reply.started":"2023-08-09T00:32:18.282733Z","shell.execute_reply":"2023-08-09T00:32:30.677873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n# ==============================\n# teyo EXP020\n# ==============================\nclass CFG_exp020:\n    CV = 0\n#     model_name = 'Unet'\n    backbone = 'timm-efficientnet-b5'\n    in_chans = 3\n    target_size = 1\n    img_size = 384\n    # DataLoader\n    loader = {\n        \"batch_size\": 16,\n        \"num_workers\": 4,\n        \"shuffle\": False,\n        \"pin_memory\": True,\n        \"drop_last\": False,\n    }\n    \ndf_fold = pd.read_csv(\"/kaggle/input/contrails-teyo/teyo_exp020/fold.csv\")\ndf_fold = df_fold.iloc[-len(test_df):].reset_index(drop=True)\ndf_20 = pd.concat([test_df,df_fold],axis=1)\n\n_CFG = CFG_exp020\nfolds = [0,1,2,3,4]\n\n# fold分のデータを取得\npredictions = {}\nfor fold in tqdm(folds):\n    # fold_nの時のvalidationdataを取得\n    ds = ContrailsDataset(df_20[df_20[\"fold\"]==fold].reset_index(drop=True), _CFG.img_size,train = False)\n    loader = DataLoader(ds, **_CFG.loader)\n    model = CustomUnet(_CFG)\n    model.load_state_dict(torch.load(f\"/kaggle/input/contrails-teyo/teyo_exp020/exp020_seed29_fold{fold}_best.pth\",map_location=torch.device(device)))\n    p = predict(model, loader, False)\n    predictions.update(p)\n    del model,ds,loader\n    torch.cuda.empty_cache()\n    gc.collect()\ngc.collect()\npreds = []\nfor k in tqdm(test_df[\"record_id\"]):\n    pred = predictions[k].flatten()\n    preds.append(pred)\n    \npreds = np.concatenate(preds)\nprint(\"calculation thr\")\nscore = calc_thr(preds, masks)\nnp.save('oof20', preds)\ndel preds,predictions\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T00:32:30.680719Z","iopub.execute_input":"2023-08-09T00:32:30.681099Z","iopub.status.idle":"2023-08-09T00:34:46.75914Z","shell.execute_reply.started":"2023-08-09T00:32:30.681065Z","shell.execute_reply":"2023-08-09T00:34:46.758058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = np.array(score[1:])\nx = np.arange(len(values))\nplt.plot(x, values, marker='o')","metadata":{"execution":{"iopub.status.busy":"2023-08-09T00:36:29.705571Z","iopub.execute_input":"2023-08-09T00:36:29.705938Z","iopub.status.idle":"2023-08-09T00:36:30.069152Z","shell.execute_reply.started":"2023-08-09T00:36:29.705907Z","shell.execute_reply":"2023-08-09T00:36:30.068217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 最適な閾値: 0.01\n# 最適なDice係数: 0.631735523641685","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n# ==============================\n# teyo EXP024\n# ==============================\nclass CFG_exp024:\n    CV = 0.688\n#     model_name = 'Unet'\n    backbone = 'timm-resnest26d'\n    in_chans = 3\n    target_size = 1\n    img_size = 512\n    # DataLoader\n    loader = {\n        \"batch_size\": 16,\n        \"num_workers\": 4,\n        \"shuffle\": False,\n        \"pin_memory\": True,\n        \"drop_last\": False,\n    }\n\ndf_fold = pd.read_csv(\"/kaggle/input/contrails-teyo/teyo_exp024/fold.csv\")\ndf_fold = df_fold.iloc[-len(test_df):].reset_index(drop=True)\ndf_24 = pd.concat([test_df,df_fold],axis=1)\n    \n\n_CFG = CFG_exp024\nfolds = [0,1,2,3,4]\n\n# fold分のデータを取得\npredictions = {}\nfor fold in tqdm(folds):\n    # fold_nの時のvalidationdataを取得\n    ds = ContrailsDataset(df_24[df_24[\"fold\"]==fold].reset_index(drop=True), _CFG.img_size,train = False)\n    loader = DataLoader(ds, **_CFG.loader)\n    model = CustomUnet(_CFG)\n    model.load_state_dict(torch.load(f\"/kaggle/input/contrails-teyo/teyo_exp024/exp024_seed428_fold{fold}_best.pth\",map_location=torch.device(device)))\n    p = predict(model, loader, False)\n    predictions.update(p)\n    del model    \n    torch.cuda.empty_cache()\n    gc.collect()\n\ngc.collect()\npreds = []\nfor k in tqdm(test_df[\"record_id\"]):\n    pred = predictions[k].flatten()\n    preds.append(pred)\n    \npreds = np.concatenate(preds)\nprint(\"calculation thr\")\nscore = calc_thr(preds, masks)\nnp.save('oof24', preds)\ndel preds,predictions\ngc.collect()\ntorch.cuda.empty_cache()\nvalues = np.array(score[1:])\nx = np.arange(len(values))\nplt.plot(x, values, marker='o')","metadata":{"execution":{"iopub.status.busy":"2023-08-09T00:36:50.488471Z","iopub.execute_input":"2023-08-09T00:36:50.488836Z","iopub.status.idle":"2023-08-09T00:42:31.24452Z","shell.execute_reply.started":"2023-08-09T00:36:50.488804Z","shell.execute_reply":"2023-08-09T00:42:31.243357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n# ==============================\n# teyo EXP025\n# ==============================\nclass CFG_exp025:\n    CV = 0.668\n    THR = 0\n#     model_name = 'UnetPlusPlus'\n    backbone = 'timm-efficientnet-b2'\n    in_chans = 3\n    target_size = 1\n    img_size = 512\n    # DataLoader\n    loader = {\n        \"batch_size\": 16,\n        \"num_workers\": 4,\n        \"shuffle\": False,\n        \"pin_memory\": True,\n        \"drop_last\": False,\n    }\n    \n\n\ndf_fold = pd.read_csv(\"/kaggle/input/contrails-teyo/teyo_exp025/fold.csv\")\ndf_fold = df_fold.iloc[-len(test_df):].reset_index(drop=True)\ndf_25 = pd.concat([test_df,df_fold],axis=1)\n    \n_CFG = CFG_exp025\nfolds = [0,1,2,3,4]\n\n# fold分のデータを取得\npredictions = {}\nfor fold in tqdm(folds):\n    # fold_nの時のvalidationdataを取得\n    ds = ContrailsDataset(df_25[df_25[\"fold\"]==fold].reset_index(drop=True), _CFG.img_size,train = False)\n    loader = DataLoader(ds, **_CFG.loader)\n    model = CustomUnetPlusPlus(_CFG)\n    model.load_state_dict(torch.load(f\"/kaggle/input/contrails-teyo/teyo_exp025/exp025_seed1229_fold{fold}_best.pth\",map_location=torch.device(device)))\n    p = predict(model, loader, False)\n    predictions.update(p)\n    del model    \n    torch.cuda.empty_cache()\n    gc.collect()\n\ngc.collect()\npreds = []\nfor k in tqdm(test_df[\"record_id\"]):\n    pred = predictions[k].flatten()\n    preds.append(pred)\n    \npreds = np.concatenate(preds)\nprint(\"calculation thr\")\nscore = calc_thr(preds, masks)\nnp.save('oof25', preds)\ndel preds,predictions\ngc.collect()\ntorch.cuda.empty_cache()\nvalues = np.array(score[1:])\nx = np.arange(len(values))\nplt.plot(x, values, marker='o')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n# ==============================\n# teyo EXP026\n# ==============================\nclass CFG_exp026:\n    CV = 0.645\n    THR = 0\n#     model_name = 'DeepLabV3Plus'\n    backbone = 'timm-efficientnet-b4'\n    in_chans = 3\n    target_size = 1\n    img_size = 512\n    # DataLoader\n    loader = {\n        \"batch_size\": 16,\n        \"num_workers\": 4,\n        \"shuffle\": False,\n        \"pin_memory\": True,\n        \"drop_last\": False,\n    }\n    \n_CFG = CFG_exp026\n    \n\ndf_fold = pd.read_csv(\"/kaggle/input/contrails-teyo/teyo_exp026/fold.csv\")\ndf_fold = df_fold.iloc[-len(test_df):].reset_index(drop=True)\ndf_26 = pd.concat([test_df,df_fold],axis=1)\n\nfolds = [0,1,2,3,4]\n\n# fold分のデータを取得\npredictions = {}\nfor fold in tqdm(folds):\n    # fold_nの時のvalidationdataを取得\n    ds = ContrailsDataset(df_26[df_26[\"fold\"]==fold].reset_index(drop=True), _CFG.img_size,train = False)\n    loader = DataLoader(ds, **_CFG.loader)\n    model = CustomDeepLabV3Plus(_CFG)\n    model.load_state_dict(torch.load(f\"/kaggle/input/contrails-teyo/teyo_exp026/exp026_seed1229_fold{fold}_best.pth\",map_location=torch.device(device)))\n    p = predict(model, loader, False)\n    predictions.update(p)\n    del model    \n    torch.cuda.empty_cache()\n    gc.collect()\n\ngc.collect()\npreds = []\nfor k in tqdm(test_df[\"record_id\"]):\n    pred = predictions[k].flatten()\n    preds.append(pred)\n    \npreds = np.concatenate(preds)\nprint(\"calculation thr\")\nscore = calc_thr(preds, masks)\nnp.save('oof26', preds)\ndel preds,predictions\ngc.collect()\ntorch.cuda.empty_cache()\nvalues = np.array(score[1:])\nx = np.arange(len(values))\nplt.plot(x, values, marker='o')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ensemble\n\n- モデルごとの閾値、加重を調整できない・・・","metadata":{}},{"cell_type":"code","source":"# THR = 0.3\n# for index in submission.index.tolist():\n#     index = str(index)\n#     for i in range(len(global_preds)):\n#         if i == 0:\n#             predicted_mask = global_preds[0][index]\n#         else:\n#             predicted_mask += global_preds[0][index]\n#     predicted_mask = predicted_mask / len(global_preds)\n#     predicted_mask_with_threshold = np.zeros((256, 256))\n#     predicted_mask_with_threshold[predicted_mask[0, :, :] < THR] = 0\n#     predicted_mask_with_threshold[predicted_mask[0, :, :] >= THR] = 1 # add =\n#     submission.loc[int(index), 'encoded_pixels'] = list_to_string(rle_encode(predicted_mask_with_threshold))","metadata":{"execution":{"iopub.status.busy":"2023-08-07T13:27:36.043103Z","iopub.execute_input":"2023-08-07T13:27:36.04351Z","iopub.status.idle":"2023-08-07T13:27:36.05396Z","shell.execute_reply.started":"2023-08-07T13:27:36.043461Z","shell.execute_reply":"2023-08-07T13:27:36.052894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T13:27:37.444372Z","iopub.execute_input":"2023-08-07T13:27:37.445314Z","iopub.status.idle":"2023-08-07T13:27:37.456141Z","shell.execute_reply.started":"2023-08-07T13:27:37.445272Z","shell.execute_reply":"2023-08-07T13:27:37.455124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-07T13:27:41.946253Z","iopub.execute_input":"2023-08-07T13:27:41.946633Z","iopub.status.idle":"2023-08-07T13:27:41.95315Z","shell.execute_reply.started":"2023-08-07T13:27:41.946603Z","shell.execute_reply":"2023-08-07T13:27:41.951964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}