{"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":"## Initialization","metadata":{}},{"cell_type":"code","source":"import sys\nsys.path.append('../input/contrails-models')\nsys.path.append('../input/contrails-model-def1')\nsys.path.append(\n    '/kaggle/input/timm-smp/pytorch-image-models-main/pytorch-image-models-main'\n)\nsys.path.append(\n    \"/kaggle/input/timm-smp/segmentation_models.pytorch-master/segmentation_models.pytorch-master\"\n)\n!pip install ../input/contrails-model-def1/einops-0.6.1-py3-none-any.whl -q\n!pip install --no-index --find-links /kaggle/input/contrails-wheels/ pretrainedmodels==0.7.4\n!pip install --no-index --find-links /kaggle/input/contrails-wheels/ efficientnet_pytorch==0.7.1\n#!python -m pip install --no-deps '/kaggle/input/mmcv161py3/addict-2.4.0-py3-none-any.whl'\n#!python -m pip install --no-deps '/kaggle/input/mmcv161py3/mmcv-1.6.1-py2.py3-none-any.whl'","metadata":{"_kg_hide-output":true,"scrolled":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-08-08T16:00:33.170297Z","iopub.execute_input":"2023-08-08T16:00:33.170837Z","iopub.status.idle":"2023-08-08T16:01:33.311826Z","shell.execute_reply.started":"2023-08-08T16:00:33.170799Z","shell.execute_reply":"2023-08-08T16:01:33.310636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport torch\nimport shutil\nimport warnings\nimport operator\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom functools import partial\nfrom tqdm.notebook import tqdm\nfrom torch.utils.data import DataLoader\n\nfrom src_inference1.data import ContrailsDataset,rle_encode_less_memory\nfrom src_inference1.CoaT_U import CoaT_U\nfrom src_inference1.NeXtViT_U import NeXtViT_U\nfrom src_inference1.CoaT_ULSTM import CoaT_ULSTM\nfrom src_inference1.NeXtViT_ULSTM import NeXtViT_ULSTM\nfrom src_inference1.CoaT_UT import CoaT_UT\nfrom src_inference1.SAM import SAM_U, SAM_USA, SAM_UV1, SAM_UV2, SAM_UV3\n# from src_inference1.model_zoo.models import build_model_theo\n\nwarnings.filterwarnings(\"ignore\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-08-08T16:01:33.314162Z","iopub.execute_input":"2023-08-08T16:01:33.314625Z","iopub.status.idle":"2023-08-08T16:01:39.958604Z","shell.execute_reply.started":"2023-08-08T16:01:33.314587Z","shell.execute_reply":"2023-08-08T16:01:39.957545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from src_inference1.model_zoo.models import *\n\ndef build_model_theo(exp_folder, folds=None):\n    config = Config(json.load(open(os.path.join(exp_folder,\"config.json\"), \"r\")))\n    models_list = []\n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    if folds is None:\n        folds = config.selected_folds\n    elif isinstance(folds, str):\n        folds = [folds]\n\n    for fold in folds:\n        model = define_model(\n            config.decoder_name,\n            config.encoder_name,\n            num_classes=config.num_classes,\n            n_channels=config.n_channels,\n            reduce_stride=config.reduce_stride,\n            use_pixel_shuffle=config.use_pixel_shuffle,\n            use_hypercolumns=config.use_hypercolumns,\n            center=config.center,\n            use_cls=config.loss_config['aux_loss_weight'] > 0,\n            frames=config.frames if hasattr(config, \"use_lstm\") else 4,\n            use_lstm=config.use_lstm if hasattr(config, \"use_lstm\") else False,\n            bidirectional=config.bidirectional if hasattr(config, \"bidirectional\") else False,\n            use_cnn=config.use_cnn if hasattr(config, \"use_cnn\") else False,\n            kernel_size=config.kernel_size if hasattr(config, \"kernel_size\") else 1,\n            use_transfo=config.use_transfo if hasattr(config, \"use_transfo\") else False,\n            two_layers=config.two_layers if hasattr(config, \"two_layers\") else False,\n            pretrained=False,\n        )\n        model = model.to(device).eval()\n\n        weights = os.path.join(exp_folder, f\"{config.decoder_name}_{config.encoder_name}_{fold}.pt\")\n        model = load_model_weights(model, weights, verbose=1)\n\n        models_list.append(model)\n    return models_list\n","metadata":{"_kg_hide-input":true,"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-08-08T16:01:39.96005Z","iopub.execute_input":"2023-08-08T16:01:39.960409Z","iopub.status.idle":"2023-08-08T16:01:40.337034Z","shell.execute_reply.started":"2023-08-08T16:01:39.960375Z","shell.execute_reply":"2023-08-08T16:01:40.336092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEBUG = False","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:01:40.339772Z","iopub.execute_input":"2023-08-08T16:01:40.340383Z","iopub.status.idle":"2023-08-08T16:01:40.346863Z","shell.execute_reply.started":"2023-08-08T16:01:40.340349Z","shell.execute_reply":"2023-08-08T16:01:40.345927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data","metadata":{}},{"cell_type":"code","source":"BS = 1\n\nDATA_PATH = \"../input/google-research-identify-contrails-reduce-global-warming/\"\nDATA = DATA_PATH + \"validation\" if DEBUG else DATA_PATH + 'test/'","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:01:40.350117Z","iopub.execute_input":"2023-08-08T16:01:40.350392Z","iopub.status.idle":"2023-08-08T16:01:40.35766Z","shell.execute_reply.started":"2023-08-08T16:01:40.350369Z","shell.execute_reply":"2023-08-08T16:01:40.356689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = ContrailsDataset(DATA)\ndl = DataLoader(ds, BS, shuffle=False, num_workers=min(2, BS))","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:01:40.359219Z","iopub.execute_input":"2023-08-08T16:01:40.359586Z","iopub.status.idle":"2023-08-08T16:01:40.489553Z","shell.execute_reply.started":"2023-08-08T16:01:40.359543Z","shell.execute_reply":"2023-08-08T16:01:40.488741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Models","metadata":{}},{"cell_type":"code","source":"TH = 0.47\n\nw_theo = 5 / 17","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:01:40.490608Z","iopub.execute_input":"2023-08-08T16:01:40.49086Z","iopub.status.idle":"2023-08-08T16:01:40.496469Z","shell.execute_reply.started":"2023-08-08T16:01:40.490839Z","shell.execute_reply":"2023-08-08T16:01:40.495389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODELS = []\n\n# CoaT\nMODELS += [  # 0.7039\n    (f'../input/contrails-models/Seq_CoaT_512_0_{i}.pth', CoaT_ULSTM, 1) for i in range(5)\n]\nMODELS += [  # 0.7055\n    (f'../input/contrails-models/Seq_CoaT_512_1_{i}.pth', CoaT_UT, 1) for i in range(5)\n]\n\n# MODELS += [  # 0.7064\n#     (f'../input/contrails-models/Seq_ExCoaT_512_0_{i}.pth', CoaT_ULSTM, 1) for i in range(5)\n# ]\n# # MODELS += [  # 0.704\n# #     (f'../input/contrails-models/Seq_ExCoaT_512_1_{i}.pth', CoaT_UT, 1) for i in range(5)\n# # ]\n\n# NextVit\nMODELS += [  # 0.7014\n    (f'../input/contrails-models/Seq_NextViT_512_0_{i}.pth', NeXtViT_ULSTM, 0.5) for i in range(5)\n]\nMODELS += [  # 0.7024\n    (f'../input/contrails-models/Seq_ExNextViT_512_0_{i}.pth', NeXtViT_ULSTM, 0.5) for i in range(5)\n]\n\n# v2-s\nMODELS += [  # 0.699\n    # 100 eps\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-07-28_9/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-07-29_1/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-07-29_13/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-07-30_0/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-07-30_6/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-04_23/\"), w_theo),\n    # 200 eps\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-07-28_10/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-01_27/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-02_2/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-02_3/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-02_4/\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-02_5/\"), w_theo),\n    # 100 eps fullfit\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-04_23/\", \"fullfit_0\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-04_23/\", \"fullfit_1\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-04_23/\", \"fullfit_2\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-04_23/\", \"fullfit_3\"), w_theo),\n    (None, partial(build_model_theo, \"/kaggle/input/contrail-weights-2d/2023-08-04_23/\", \"fullfit_4\"), w_theo),\n]\n\n# SAM\nMODELS += [  # 0.694\n    (\"/kaggle/input/sam-cusom/sam_u.pth\",   SAM_U,   1), \n    (\"/kaggle/input/sam-cusom/sam_usa.pth\", SAM_USA, 1), \n    (\"/kaggle/input/sam-cusom/sam_uv1.pth\", SAM_UV1, 1), \n    (\"/kaggle/input/sam-cusom/sam_uv2.pth\", SAM_UV2, 1), \n    (\"/kaggle/input/sam-cusom/sam_uv3.pth\", SAM_UV3, 1)\n]\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:01:40.497952Z","iopub.execute_input":"2023-08-08T16:01:40.498756Z","iopub.status.idle":"2023-08-08T16:01:40.535398Z","shell.execute_reply.started":"2023-08-08T16:01:40.498603Z","shell.execute_reply":"2023-08-08T16:01:40.534527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models, weights = [], []\nfor path, Model, w in MODELS:\n    model = Model()\n    if isinstance(model,list):\n        model = model[0]\n    if path is not None:\n        state_dict = torch.load(path,map_location=torch.device('cpu'))\n        model.load_state_dict(state_dict)\n        del state_dict\n    model.to(device).eval();\n    models.append(model)\n    weights.append(w)\ngc.collect()","metadata":{"scrolled":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-08T16:01:40.53712Z","iopub.execute_input":"2023-08-08T16:01:40.537492Z","iopub.status.idle":"2023-08-08T16:04:14.035976Z","shell.execute_reply.started":"2023-08-08T16:01:40.53746Z","shell.execute_reply":"2023-08-08T16:04:14.034838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names, preds = [], []\nprint('-> Inference ...\\n')\nfor x, y in tqdm(dl):\n    with torch.no_grad():\n        x = x.to(device)\n        p = torch.stack(\n            [m(x).sigmoid() * w for m, w in zip(models, weights)], 0\n        ).sum(0).squeeze(1) / sum(weights)\n    p = p.cpu().numpy()\n\n    if DEBUG:\n        preds.append(p.astype(np.float16))\n#         break\n        continue\n\n    for pi, yi in zip(p, y):\n        rle = rle_encode_less_memory(pi > TH)\n        if len(rle) == 0: rle = '-'\n        names.append(yi)\n        preds.append(rle)\n        \nprint('-> Inference completed ! \\n')","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:04:14.039217Z","iopub.execute_input":"2023-08-08T16:04:14.039656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if DEBUG:\n    sys.path.append('/kaggle/input/contrails-code/src/')\n    from data.dataset import ContrailInfDataset\n    from data.transforms import get_transfos\n    from util.metrics import dice_score\n\n    print('-> Evaluating ... \\n')\n    \n    dataset = ContrailInfDataset(\n        [os.path.join(DATA, p) for p in sorted(os.listdir(DATA))],\n        transforms=get_transfos(augment=False),\n        frames=4,\n    )\n\n    gts = []\n    for i in tqdm(range(len(dataset))):\n        image, mask, _ = dataset[i]\n        gts.append(mask.transpose(2, 0, 1))\n    gts = np.array(gts)\n\n    dices = {}\n    for th in tqdm(np.round(np.arange(0.45, 0.52, 0.01), 2)):\n        dices[th] = dice_score((np.concatenate(preds, 0) > th), gts)\n\n    th, dice = max(dices.items(), key=operator.itemgetter(1))\n    print(f'-> CV dice :  {dice:.4f}   (th={th:.2f})')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not DEBUG:\n    df = pd.DataFrame({'record_id': names, 'encoded_pixels': preds})\n    df.to_csv('submission.csv', index=False)\n    df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Done !","metadata":{}}]}