{"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":"MACENKO_STAIN = None # \"/root/dataset/MACENKO_STAIN\"\nVAHADANE_STAIN = None # \"/root/dataset/VAHADANE_STAIN\"\nTEST_FOLDER = \"../input/hubmap-organ-segmentation/test_images\"","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:01:59.661598Z","iopub.execute_input":"2022-09-22T12:01:59.662520Z","iopub.status.idle":"2022-09-22T12:01:59.688039Z","shell.execute_reply.started":"2022-09-22T12:01:59.662401Z","shell.execute_reply":"2022-09-22T12:01:59.687180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nif (MACENKO_STAIN or VAHADANE_STAIN) and not os.path.exists(\"/root/miniconda3\"):\n    !cp ../input/staintools/staintools.pt /tmp/\n    !cd /tmp/ && tar -zxvf staintools.pt 2>&1 > /dev/null\n    !bash ../input/miniconda/Miniconda3-latest-Linux-x86_64.sh -b 2>&1 > /dev/null\n\nif MACENKO_STAIN:\n    !mkdir -p $MACENKO_STAIN\n    !conda run -p /tmp/staintools python ../input/hpacode/scripts/stain.py \\\n        --df_path ../input/hubmap-organ-segmentation/test.csv \\\n        --target_file ../input/hubmap-organ-segmentation/test_images/10078.tiff \\\n        --image_folder ../input/hubmap-organ-segmentation/test_images \\\n        --output_folder $MACENKO_STAIN \\\n        --method macenko \\\n        --multiprocess\n\nif VAHADANE_STAIN:\n    !mkdir -p $VAHADANE_STAIN\n    !conda run -p /tmp/staintools python ../input/hpacode/scripts/stain.py \\\n        --df_path ../input/hubmap-organ-segmentation/test.csv \\\n        --target_file ../input/hubmap-organ-segmentation/test_images/10078.tiff \\\n        --image_folder ../input/hubmap-organ-segmentation/test_images \\\n        --output_folder $VAHADANE_STAIN \\\n        --method vahadane \\\n        --multiprocess","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:01:59.691998Z","iopub.execute_input":"2022-09-22T12:01:59.692287Z","iopub.status.idle":"2022-09-22T12:01:59.718435Z","shell.execute_reply.started":"2022-09-22T12:01:59.692261Z","shell.execute_reply":"2022-09-22T12:01:59.717311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /tmp/hpapackages\n!cp -r ../input/hpapackages /tmp/hpapackages\n\n!pip config set global.disable-pip-version-check true\n!pip install \\\n    /tmp/hpapackages/pretrainedmodels-0.7.4/pretrainedmodels-0.7.4 \\\n    /tmp/hpapackages/timm-0.4.12-py3-none-any.whl \\\n    /tmp/hpapackages/efficientnet_pytorch-0.7.1/efficientnet_pytorch-0.7.1 \\\n    /tmp/hpapackages/segmentation_models_pytorch-0.3.0-py3-none-any.whl \\\n    /tmp/hpapackages/ipdb-0.13.9/ipdb-0.13.9 \\\n    /tmp/hpapackages/fire-0.4.0/fire-0.4.0 \\\n    /tmp/hpapackages/einops-0.4.1-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:01:59.720111Z","iopub.execute_input":"2022-09-22T12:01:59.720524Z","iopub.status.idle":"2022-09-22T12:02:27.035609Z","shell.execute_reply.started":"2022-09-22T12:01:59.720476Z","shell.execute_reply":"2022-09-22T12:02:27.034352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport os\nsys.path.insert(0, \"../input/hpacode\")","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:02:27.038660Z","iopub.execute_input":"2022-09-22T12:02:27.039124Z","iopub.status.idle":"2022-09-22T12:02:27.045333Z","shell.execute_reply.started":"2022-09-22T12:02:27.039077Z","shell.execute_reply":"2022-09-22T12:02:27.044313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thresholds = {\n    \"kidney\": [\n        0.9563,\n        0.3\n    ],\n    \"largeintestine\": [\n        0.9361,\n        0.3\n    ],\n    \"lung\": [\n        0.6018,\n        0.2\n    ],\n    \"prostate\": [\n        0.9289,\n        0.3\n    ],\n    \"spleen\": [\n        0.8949,\n        0.3\n    ]\n}\n","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:02:27.046378Z","iopub.execute_input":"2022-09-22T12:02:27.046927Z","iopub.status.idle":"2022-09-22T12:02:27.060065Z","shell.execute_reply.started":"2022-09-22T12:02:27.046883Z","shell.execute_reply":"2022-09-22T12:02:27.058996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import transformers\nimport torch\nimport pandas as pd\nimport numpy as np\nfrom hpa import dataset, models, metrics, utils\nfrom dataclasses import dataclass\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom tqdm import tqdm\nimport cv2\nimport matplotlib.pyplot as plt\nimport functools\nfrom copy import copy\nimport random\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:02:27.062658Z","iopub.execute_input":"2022-09-22T12:02:27.062982Z","iopub.status.idle":"2022-09-22T12:02:35.472051Z","shell.execute_reply.started":"2022-09-22T12:02:27.062954Z","shell.execute_reply":"2022-09-22T12:02:35.471018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"../input/hubmap-organ-segmentation/test.csv\")\ntrain_df = pd.read_csv(\"../input/hubmap-organ-segmentation/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:02:35.473625Z","iopub.execute_input":"2022-09-22T12:02:35.474339Z","iopub.status.idle":"2022-09-22T12:02:35.812489Z","shell.execute_reply.started":"2022-09-22T12:02:35.474307Z","shell.execute_reply":"2022-09-22T12:02:35.811479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_former_b2_785 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b2-finetuned-cityscapes-1024-1024\"),\n    \"../input/hpa-test-models/1080_3e-07.pth\")\n\n# 1536 x 1536 no tta 0.787\ncoat_1_787 = utils.get_model(\n    models.coat_1(),\n    \"../input/hpa-final/coat_50.pth\"\n)\n\nother_coat1 = utils.get_model(\n    models.coat_1(),\n    \"../input/other-coat/f0_checkpoint_4017_dice0.8930.pt\"\n)\n\nother_coat2 = utils.get_model(\n    models.coat_1(),\n    \"../input/other-coat/f1_checkpoint_4469_dice0.8518.pt\"\n)\n\nother_coat3 = utils.get_model(\n    models.coat_1(),\n    \"../input/other-coat/f2_checkpoint_3485_dice0.8923.pt\"\n)\n\nother_coat4 = utils.get_model(\n    models.coat_1(),\n    \"../input/other-coat/f3_checkpoint_3936_dice0.8612.pt\"\n)\n\nother_coat5 = utils.get_model(\n    models.coat_1(),\n    \"../input/other-coat/f4_checkpoint_3120_dice0.8890.pt\"\n)\n\nother1 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b3-finetuned-cityscapes-1024-1024\"),\n    \"../input/otherorgan/checkpoint_3075_dice0.8606.pt\"\n)\nother2 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b3-finetuned-cityscapes-1024-1024\"),\n    \"../input/otherorgan/checkpoint_3900_dice0.8850.pt\"\n)\nother3 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b3-finetuned-cityscapes-1024-1024\"),\n    \"../input/otherorgan/checkpoint_4407_dice0.8861.pt\"\n)\nother4 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b3-finetuned-cityscapes-1024-1024\"),\n    \"../input/otherorgan/checkpoint_4920_dice0.8981.pt\"\n)\nother5 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b3-finetuned-cityscapes-1024-1024\"),\n    \"../input/otherorgan/checkpoint_5945_dice0.8449.pt\"\n)\n\nlung1 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b5-finetuned-cityscapes-1024-1024\"),\n    \"../input/lung33/checkpoint_2109_dice0.2686.pt\",\n)\nlung2 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b5-finetuned-cityscapes-1024-1024\"),\n    \"../input/lung33/checkpoint_3626_dice0.3073.pt\",\n)\nlung3 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b5-finetuned-cityscapes-1024-1024\"),\n    \"../input/lung33/checkpoint_3700_dice0.3451.pt\",\n)\nlung4 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b5-finetuned-cityscapes-1024-1024\"),\n    \"../input/lung33/checkpoint_3922_dice0.2925.pt\",\n)\nlung5 = utils.get_model(\n    models.SegFormer(\"../input/all-segformers/all-segformers/segformer-b5-finetuned-cityscapes-1024-1024\"),\n    \"../input/lung33/checkpoint_4600_dice0.3860.pt\",\n)\n\nother_1536coat1 = utils.get_model(\n    models.coat_1(),\n    \"../input/others1536/f0_checkpoint_3276_dice0.8914.pt\"\n)\nother_1536coat2 = utils.get_model(\n    models.coat_1(),\n    \"../input/others1536/f1_checkpoint_4920_dice0.8530.pt\"\n)\nother_1536coat3 = utils.get_model(\n    models.coat_1(),\n    \"../input/others1536/f2_checkpoint_2624_dice0.8948.pt\"\n)\nother_1536coat4 = utils.get_model(\n    models.coat_1(),\n    \"../input/others1536/f3_checkpoint_2870_dice0.8796.pt\"\n)\nother_1536coat5 = utils.get_model(\n    models.coat_1(),\n    \"../input/others1536/f4_checkpoint_3588_dice0.8816.pt\"\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:02:35.813797Z","iopub.execute_input":"2022-09-22T12:02:35.814175Z","iopub.status.idle":"2022-09-22T12:03:51.514056Z","shell.execute_reply.started":"2022-09-22T12:02:35.814119Z","shell.execute_reply":"2022-09-22T12:03:51.512892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_inferers = [\n    utils.infer(coat_1_787, 1536),\n    utils.infer(seg_former_b2_785, 1536, rotate=90),\n    utils.infer(seg_former_b2_785, scale=1024),\n    utils.infer(seg_former_b2_785, scale=1024, crop_size=1024, infer_klass=utils.CropInferer),\n    utils.infer(coat_1_787, 1024, rotate=270),\n    utils.infer(seg_former_b2_785, 1024, flip=\"vertical\"),\n]\n\nother_coat_inferers = [\n    (utils.infer(other_coat1, 1536, flip=\"vertical\", pad=True), dict(kidney=0.2, largeintestine=0.45, prostate=0.2, spleen=0.3)),\n    (utils.infer(other_coat1, 1024), dict(kidney=0.2, largeintestine=0.5, prostate=0.15, spleen=0.15)),\n    \n    (utils.infer(other_coat2, 1536, flip=\"vertical\", pad=True), dict(kidney=0.45, largeintestine=0.6, prostate=0.45, spleen=0.8)),\n    (utils.infer(other_coat2, 1024), dict(kidney=0.45, largeintestine=0.55, prostate=0.45, spleen=0.4)),\n    \n    (utils.infer(other_coat3, 1536, flip=\"vertical\", pad=True), dict(kidney=0.5, largeintestine=0.4, prostate=0.25, spleen=0.35)),\n    (utils.infer(other_coat3, 1024, flip=\"horizontal\"), dict(kidney=0.3, largeintestine=0.3, prostate=0.2, spleen=0.25)),\n    \n    (utils.infer(other_coat4, 1536, pad=True), dict(kidney=0.4, largeintestine=0.5, prostate=0.4, spleen=0.25)),\n    (utils.infer(other_coat4, 1024), dict(kidney=0.35, largeintestine=0.55, prostate=0.4, spleen=0.25)),\n    \n    (utils.infer(other_coat5, 1536, flip=\"horizontal\", pad=True), dict(kidney=0.55, largeintestine=0.6, prostate=0.55, spleen=0.75)),\n    (utils.infer(other_coat5, 1024, flip=\"vertical\"), dict(kidney=0.55, largeintestine=0.55, prostate=0.4, spleen=0.7)),\n]\n\nother_coat_1536_inferers = [\n    (utils.infer(other_1536coat1, 1536, flip=\"horizontal\"), dict(kidney=0.4, largeintestine=0.4, prostate=0.15, spleen=0.35)),\n    (utils.infer(other_1536coat1, 2048, rotate=90, pad=True), dict(kidney=0.4, largeintestine=0.45, prostate=0.15, spleen=0.4)),\n    \n    (utils.infer(other_1536coat2, 1536), dict(kidney=0.4, largeintestine=0.55, prostate=0.45, spleen=0.3)),\n    (utils.infer(other_1536coat2, 2048, rotate=180, pad=True), dict(kidney=0.5, largeintestine=0.5, prostate=0.45, spleen=0.55)),\n    \n    (utils.infer(other_1536coat3, 1536, rotate=270), dict(kidney=0.5, largeintestine=0.4, prostate=0.25, spleen=0.55)),\n    (utils.infer(other_1536coat3, 2048, pad=True), dict(kidney=0.55, largeintestine=0.4, prostate=0.3, spleen=0.6)),\n    \n    (utils.infer(other_1536coat4, 1536, flip=\"vertical\"), dict(kidney=0.45, largeintestine=0.4, prostate=0.35, spleen=0.25)),\n    (utils.infer(other_1536coat4, 2048, pad=True), dict(kidney=0.5, largeintestine=0.4, prostate=0.4, spleen=0.25)),\n    \n    (utils.infer(other_1536coat5, 1536, flip=\"horizontal\"), dict(kidney=0.35, largeintestine=0.4, prostate=0.4, spleen=0.3)),\n    (utils.infer(other_1536coat5, 2048, flip=\"vertical\", pad=True), dict(kidney=0.4, largeintestine=0.4, prostate=0.35, spleen=0.35)),\n]\n\n\nlung_inferers = [\n    utils.infer(lung1, 1024),\n    utils.infer(lung1, 1024, flip=\"vertical\"),\n    utils.infer(lung1, 1024, flip=\"horizontal\"),\n\n    utils.infer(lung2, 1024),\n    utils.infer(lung2, 1024, flip=\"vertical\"),\n    utils.infer(lung2, 1024, flip=\"horizontal\"),\n\n    utils.infer(lung3, 1024),\n    utils.infer(lung3, 1024, flip=\"vertical\"),\n    utils.infer(lung3, 1024, flip=\"horizontal\"),\n\n    utils.infer(lung4, 1024),\n    utils.infer(lung4, 1024, flip=\"vertical\"),\n    utils.infer(lung4, 1024, flip=\"horizontal\"),\n\n    utils.infer(lung5, 1024),\n    utils.infer(lung5, 1024, flip=\"vertical\"),\n    utils.infer(lung5, 1024, flip=\"horizontal\"),\n]","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:03:51.516168Z","iopub.execute_input":"2022-09-22T12:03:51.516561Z","iopub.status.idle":"2022-09-22T12:03:52.943087Z","shell.execute_reply.started":"2022-09-22T12:03:51.516529Z","shell.execute_reply":"2022-09-22T12:03:52.942094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k, (_, item) in enumerate(test_df.iterrows()):\n    \n    if item.organ == \"lung\":\n        ensemble_probs = torch.zeros((item.img_height, item.img_width))\n        for infer in lung_inferers:\n            ensemble_probs += infer.infer_item(item) > 0.1\n        test_df.loc[k, \"rle\"] = utils.rle_encode_less_memory(ensemble_probs >= 5)\n    else:\n        # from all_inferers\n        ensemble_probs = torch.zeros((item.img_height, item.img_width))\n        all_pred = torch.zeros((item.img_height, item.img_width))\n        for infer in all_inferers:\n            ensemble_probs += infer.infer_item(item)\n        ensemble_probs /= len(all_inferers)\n        t = thresholds[item.organ][1]\n        all_pred += ensemble_probs > t\n\n        # from other_coat_1536\n        ensemble_probs = torch.zeros((item.img_height, item.img_width))\n        for infer, thres in other_coat_1536_inferers:\n            t = max(0.15, min(0.5, thres[item.organ]) - 0.1)\n            ensemble_probs += infer.infer_item(item) > t\n        all_pred += ensemble_probs >= 4\n\n        # from other_coat\n        ensemble_probs = torch.zeros((item.img_height, item.img_width))\n        for infer, thres in other_coat_inferers:\n            t = max(0.15, min(0.5, thres[item.organ]) - 0.1)\n            ensemble_probs += infer.infer_item(item) > t\n        all_pred += ensemble_probs >= 4\n\n        final_pred = all_pred >= 2\n        test_df.loc[k, \"rle\"] = utils.rle_encode_less_memory(final_pred)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:11:26.852589Z","iopub.execute_input":"2022-09-22T12:11:26.853061Z","iopub.status.idle":"2022-09-22T12:11:49.534238Z","shell.execute_reply.started":"2022-09-22T12:11:26.853019Z","shell.execute_reply":"2022-09-22T12:11:49.533228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, axes = plt.subplots(1, 3, figsize=(20, 10))\naxes[0].imshow(ensemble_probs)\naxes[1].imshow(utils.rle_decode(test_df.iloc[0].rle, (2023, 2023)))\naxes[2].imshow(utils.load_image(test_df.iloc[0], \"../input/hubmap-organ-segmentation/test_images\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:11:49.540020Z","iopub.execute_input":"2022-09-22T12:11:49.540395Z","iopub.status.idle":"2022-09-22T12:11:51.850281Z","shell.execute_reply.started":"2022-09-22T12:11:49.540360Z","shell.execute_reply":"2022-09-22T12:11:51.849147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[[\"id\", \"rle\"]].to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T12:11:51.851778Z","iopub.execute_input":"2022-09-22T12:11:51.852490Z","iopub.status.idle":"2022-09-22T12:11:51.861285Z","shell.execute_reply.started":"2022-09-22T12:11:51.852446Z","shell.execute_reply":"2022-09-22T12:11:51.860202Z"},"trusted":true},"execution_count":null,"outputs":[]}]}