{"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":" This Notebook is a fork version of this [notebook](https://www.kaggle.com/code/vexxingbanana/hubmap-unet-semantic-approach-train) by [@vexxingbanana](https://www.kaggle.com/vexxingbanana).","metadata":{}},{"cell_type":"markdown","source":"\n\n\n<h3 style=\"text-align:center; background-color:#C8FF33;padding:40px;border-radius: 30px;\">\n<p style=\"text-align: left\">See also this notebook:</p>\n    <p style=\"text-align: left\"><b><a href=\"https://www.kaggle.com/code/soumya9977/hubmap-multiorgan-segmentation-1-3-data-prep\"> &nbsp; HuBMAP Multiorgan Segmentation 1/3 [data prep]</a></b></p>\n    <p style=\"text-align: left\"><b><a href=\"https://www.kaggle.com/code/soumya9977/hubmap-vanilla-unet-w-b-pytorch-2-3-train\"> &nbsp; HuBMAP: Vanilla Unet + W&B + Pytorch 2/3 [train]</a></b></p>\n    <p style=\"text-align: left\"><b>* HuBMAP: Vanilla Unet Pytorch 3/3 [inference]</b></p>\n</h3>\n\n\n## Please _DO_ upvote!","metadata":{}},{"cell_type":"markdown","source":"### Changelog\n\n|| Version | Comments | LB |\n|---|  --- | --- | --- |\n|**Best**|8| Vanilla Unet w/ 256x256, 40 epoch, no aug, no lr sch, no pre/post processing, L2 norm [[NB](https://www.kaggle.com/code/soumya9977/hubmap-vanilla-unet-w-b-pytorch-2-3-train)]| `0.04` |","metadata":{}},{"cell_type":"markdown","source":"# **Import Libraries**","metadata":{"papermill":{"duration":0.010262,"end_time":"2022-07-23T18:38:51.640326","exception":false,"start_time":"2022-07-23T18:38:51.630064","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!cp -r ../input/pytorch-segmentation-models-lib/ ./","metadata":{"execution":{"iopub.status.busy":"2022-08-14T20:15:17.228189Z","iopub.execute_input":"2022-08-14T20:15:17.228625Z","iopub.status.idle":"2022-08-14T20:15:18.685146Z","shell.execute_reply.started":"2022-08-14T20:15:17.228590Z","shell.execute_reply":"2022-08-14T20:15:18.683703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip config set global.disable-pip-version-check true","metadata":{"execution":{"iopub.status.busy":"2022-08-14T20:15:18.690887Z","iopub.execute_input":"2022-08-14T20:15:18.693232Z","iopub.status.idle":"2022-08-14T20:15:20.369745Z","shell.execute_reply.started":"2022-08-14T20:15:18.693180Z","shell.execute_reply":"2022-08-14T20:15:20.368450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q ./pytorch-segmentation-models-lib/pretrainedmodels-0.7.4/pretrainedmodels-0.7.4\n!pip install -q ./pytorch-segmentation-models-lib/efficientnet_pytorch-0.6.3/efficientnet_pytorch-0.6.3\n!pip install -q ./pytorch-segmentation-models-lib/timm-0.4.12-py3-none-any.whl\n!pip install -q ./pytorch-segmentation-models-lib/segmentation_models_pytorch-0.2.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-08-14T20:15:20.371650Z","iopub.execute_input":"2022-08-14T20:15:20.372025Z","iopub.status.idle":"2022-08-14T20:16:03.854577Z","shell.execute_reply.started":"2022-08-14T20:15:20.371985Z","shell.execute_reply":"2022-08-14T20:16:03.853396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport time\nimport matplotlib.pyplot as plt\nimport cv2\nimport glob\nimport os\nimport shutil\n# import timm\nimport random\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nfrom torch.cuda import amp\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nimport transformers\nfrom sklearn.model_selection import StratifiedKFold, KFold, StratifiedGroupKFold\nimport multiprocessing as mp\nimport segmentation_models_pytorch as smp\nimport copy\nfrom collections import defaultdict\nimport gc\nfrom tqdm import tqdm\nimport tifffile\nfrom colorama import Fore, Back, Style","metadata":{"papermill":{"duration":14.527502,"end_time":"2022-07-23T18:39:06.178272","exception":false,"start_time":"2022-07-23T18:38:51.650770","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:16:29.050549Z","iopub.execute_input":"2022-08-14T20:16:29.051470Z","iopub.status.idle":"2022-08-14T20:16:29.058753Z","shell.execute_reply.started":"2022-08-14T20:16:29.051432Z","shell.execute_reply":"2022-08-14T20:16:29.057644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Config**","metadata":{"papermill":{"duration":0.010197,"end_time":"2022-07-23T18:39:06.198309","exception":false,"start_time":"2022-07-23T18:39:06.188112","status":"completed"},"tags":[]}},{"cell_type":"code","source":"CONFIG = {\n    \"in_channels\" :3,\n    \"num_classes\": 1,\n    \"BATCH_SIZE\" : 8,\n    \"NUM_EPOCHS\" : 40,\n    \"n_accumulate\": 1,\n    \"competition\": \"HuBMAP-Kaggle\", # HuBMAP-Kaggle\n    \"model_name\": \"Vanilla_Unet\",\n    \"LEARNING_RATE\": 1e-4,\n    \"DEVICE\": \"cuda\" if torch.cuda.is_available() else \"cpu\", \n    \"AUG\": \"No\",\n    \"SEED\": 2022,\n    \"opt\": 'Adam',\n    \"Normalization\": \"L2\",\n    \"img_size\": 512\n}","metadata":{"execution":{"iopub.status.busy":"2022-08-14T20:16:35.016190Z","iopub.execute_input":"2022-08-14T20:16:35.016907Z","iopub.status.idle":"2022-08-14T20:16:35.088766Z","shell.execute_reply.started":"2022-08-14T20:16:35.016870Z","shell.execute_reply":"2022-08-14T20:16:35.087618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed = 42):\n    '''Sets the seed of the entire notebook so results are the same every time we run.\n    This is for REPRODUCIBILITY.'''\n    np.random.seed(seed)\n    random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    # When running on the CuDNN backend, two further options must be set\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    # Set a fixed value for the hash seed\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    print('> SEEDING DONE')\n    \nset_seed(CONFIG[\"SEED\"])","metadata":{"execution":{"iopub.status.busy":"2022-08-14T20:16:35.549424Z","iopub.execute_input":"2022-08-14T20:16:35.550431Z","iopub.status.idle":"2022-08-14T20:16:35.560721Z","shell.execute_reply.started":"2022-08-14T20:16:35.550385Z","shell.execute_reply":"2022-08-14T20:16:35.559381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    seed = 42\n    batch_size = 8\n    head = \"UNet\"\n#     backbone = \"efficientnet-b0\"\n    img_size = [512, 512]\n    lr = 1e-4\n#     scheduler = 'CosineAnnealingLR' #['CosineAnnealingLR']\n    epochs = 40\n#     warmup_epochs = 2\n#     n_folds = 5\n#     folds_to_run = [0]\n    device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n    base_path = '../input/hubmap-organ-segmentation'\n    num_workers = mp.cpu_count()\n    num_classes = 1\n    n_accumulate = max(1, 8//batch_size)\n    loss = 'Dice'\n    optimizer = 'Adam'\n    weight_decay = 1e-5\n    ckpt_path = '../input/kaggle-hubmap-model-weights/Vanilla_Unet-0.0001-No-33-8.pth' #Checkpoint path\n    threshold = 0.5","metadata":{"papermill":{"duration":0.097239,"end_time":"2022-07-23T18:39:06.304604","exception":false,"start_time":"2022-07-23T18:39:06.207365","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:16:36.304937Z","iopub.execute_input":"2022-08-14T20:16:36.305833Z","iopub.status.idle":"2022-08-14T20:16:36.312729Z","shell.execute_reply.started":"2022-08-14T20:16:36.305785Z","shell.execute_reply":"2022-08-14T20:16:36.311637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Helper Functions**","metadata":{"papermill":{"duration":0.009378,"end_time":"2022-07-23T18:39:06.323503","exception":false,"start_time":"2022-07-23T18:39:06.314125","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef rle_decode(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape)  # Needed to align to RLE direction\n\n\n# ref.: https://www.kaggle.com/stainsby/fast-tested-rle\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\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)\n\n#ref: https://www.kaggle.com/code/bguberfain/memory-aware-rle-encoding/notebook\ndef rle_encode_less_memory(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    This simplified method requires first and last pixel to be zero\n    '''\n    pixels = img.T.flatten()\n    \n    # This simplified method requires first and last pixel to be zero\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    \n    return ' '.join(str(x) for x in runs)","metadata":{"papermill":{"duration":0.026983,"end_time":"2022-07-23T18:39:06.359639","exception":false,"start_time":"2022-07-23T18:39:06.332656","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:16:39.517013Z","iopub.execute_input":"2022-08-14T20:16:39.517397Z","iopub.status.idle":"2022-08-14T20:16:39.528650Z","shell.execute_reply.started":"2022-08-14T20:16:39.517362Z","shell.execute_reply":"2022-08-14T20:16:39.527126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_tiff(path, scale=None, verbose=0): #Modified from https://www.kaggle.com/code/abhinand05/hubmap-extensive-eda-what-are-we-hacking\n    image = tifffile.imread(path)\n    if len(image.shape) == 5:\n        image = image.squeeze().transpose(1, 2, 0)\n    \n    if verbose:\n        print(f\"[{path}] Image shape: {image.shape}\")\n    \n    if scale:\n        new_size = (image.shape[1] // scale, image.shape[0] // scale)\n        image = cv2.resize(image, new_size)\n        \n        if verbose:\n            print(f\"[{path}] Resized Image shape: {image.shape}\")\n        \n    mx = np.max(image)\n    image = image.astype(np.float32)\n    if mx:\n        image /= mx # scale image to [0, 1]\n    return image","metadata":{"papermill":{"duration":0.021345,"end_time":"2022-07-23T18:39:06.390353","exception":false,"start_time":"2022-07-23T18:39:06.369008","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:16:40.660161Z","iopub.execute_input":"2022-08-14T20:16:40.660774Z","iopub.status.idle":"2022-08-14T20:16:40.668354Z","shell.execute_reply.started":"2022-08-14T20:16:40.660730Z","shell.execute_reply":"2022-08-14T20:16:40.667385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Grab Metadata**","metadata":{"papermill":{"duration":0.009343,"end_time":"2022-07-23T18:39:06.408505","exception":false,"start_time":"2022-07-23T18:39:06.399162","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df = pd.read_csv(\"../input/hubmap-organ-segmentation/test.csv\")\ndf.head()","metadata":{"papermill":{"duration":0.046184,"end_time":"2022-07-23T18:39:06.463831","exception":false,"start_time":"2022-07-23T18:39:06.417647","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:16:50.743126Z","iopub.execute_input":"2022-08-14T20:16:50.743506Z","iopub.status.idle":"2022-08-14T20:16:50.771620Z","shell.execute_reply.started":"2022-08-14T20:16:50.743474Z","shell.execute_reply":"2022-08-14T20:16:50.770554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Processing**","metadata":{"papermill":{"duration":0.009357,"end_time":"2022-07-23T18:39:06.482965","exception":false,"start_time":"2022-07-23T18:39:06.473608","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df['image_path'] = df['id'].apply(lambda x: os.path.join(CFG.base_path, 'test_images', str(x) + '.tiff'))","metadata":{"papermill":{"duration":0.023867,"end_time":"2022-07-23T18:39:06.516481","exception":false,"start_time":"2022-07-23T18:39:06.492614","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:16:53.100690Z","iopub.execute_input":"2022-08-14T20:16:53.101396Z","iopub.status.idle":"2022-08-14T20:16:53.111556Z","shell.execute_reply.started":"2022-08-14T20:16:53.101348Z","shell.execute_reply":"2022-08-14T20:16:53.110610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Dataset**","metadata":{"papermill":{"duration":0.00951,"end_time":"2022-07-23T18:39:06.535616","exception":false,"start_time":"2022-07-23T18:39:06.526106","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class HuBMAP_Dataset(torch.utils.data.Dataset):\n    def __init__(self, df, labeled=True, transforms=None):\n        self.df = df\n        self.labeled = labeled\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_path = self.df.loc[index, 'image_path']\n        img_height = self.df.loc[index, 'img_height']\n        img_width = self.df.loc[index, 'img_width']\n        id_ = self.df.loc[index, 'id']\n        img = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)\n        \n        if self.labeled:\n            rle_mask = self.df.loc[index, 'rle']\n            mask = rle_decode(rle_mask, (img_height, img_width))\n            print(np.add_docstringunique(mask))\n            if self.transforms:\n                data = self.transforms(image=img, mask=mask)\n                img  = data['image']\n                mask  = data['mask']\n            \n            mask = np.expand_dims(mask, axis=0)\n            img = np.transpose(img, (2, 0, 1))\n            \n            return torch.tensor(img), torch.tensor(mask)\n        \n        else:\n            if self.transforms:\n                data = self.transforms(image=img)\n                img  = data['image']\n                \n            img = np.transpose(img, (2, 0, 1))\n            \n            return torch.tensor(img), img_height, img_width, id_","metadata":{"papermill":{"duration":0.025973,"end_time":"2022-07-23T18:39:06.570845","exception":false,"start_time":"2022-07-23T18:39:06.544872","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:16:58.806637Z","iopub.execute_input":"2022-08-14T20:16:58.807101Z","iopub.status.idle":"2022-08-14T20:16:58.821398Z","shell.execute_reply.started":"2022-08-14T20:16:58.807062Z","shell.execute_reply":"2022-08-14T20:16:58.820330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Augmentations**","metadata":{"papermill":{"duration":0.009713,"end_time":"2022-07-23T18:39:06.590118","exception":false,"start_time":"2022-07-23T18:39:06.580405","status":"completed"},"tags":[]}},{"cell_type":"code","source":"data_transforms = {\n    \"inference\": A.Compose([\n        A.Resize(*CFG.img_size, interpolation=cv2.INTER_NEAREST),\n        ], p=1.0)\n}","metadata":{"papermill":{"duration":0.019119,"end_time":"2022-07-23T18:39:06.618776","exception":false,"start_time":"2022-07-23T18:39:06.599657","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:17:00.416032Z","iopub.execute_input":"2022-08-14T20:17:00.416520Z","iopub.status.idle":"2022-08-14T20:17:00.423482Z","shell.execute_reply.started":"2022-08-14T20:17:00.416475Z","shell.execute_reply":"2022-08-14T20:17:00.422545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Models**","metadata":{"papermill":{"duration":0.009236,"end_time":"2022-07-23T18:39:06.637406","exception":false,"start_time":"2022-07-23T18:39:06.628170","status":"completed"},"tags":[]}},{"cell_type":"code","source":"BCELoss = smp.losses.SoftBCEWithLogitsLoss()\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n\ndef imread(path):\n    return cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB)\n\ndef init_model():\n    model =  smp.Unet(\n                 encoder_name='efficientnet-b0',\n                 encoder_weights=None,\n                 in_channels=3,\n                 classes=1)\n    return model\n\ndef load_model(path):\n    model = init_model()\n    model.load_state_dict(torch.load(path, map_location=device)['model_state_dict'])\n    model.eval()\n    return model.cuda()\n\n\nmodel = load_model('../input/kaggle-hubmap-model-weights/effnetb0_unet-0.0001-No-174-8.pth')","metadata":{"papermill":{"duration":0.020696,"end_time":"2022-07-23T18:39:06.717237","exception":false,"start_time":"2022-07-23T18:39:06.696541","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:17:07.458216Z","iopub.execute_input":"2022-08-14T20:17:07.458618Z","iopub.status.idle":"2022-08-14T20:17:12.200890Z","shell.execute_reply.started":"2022-08-14T20:17:07.458577Z","shell.execute_reply":"2022-08-14T20:17:12.199879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Dataloader**","metadata":{"papermill":{"duration":0.00931,"end_time":"2022-07-23T18:39:06.736044","exception":false,"start_time":"2022-07-23T18:39:06.726734","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def prepare_loaders():\n\n    infer_dataset = HuBMAP_Dataset(df, labeled=False, transforms=data_transforms['inference'])\n\n    \n    infer_loader = torch.utils.data.DataLoader(infer_dataset, batch_size=CFG.batch_size,\n                              num_workers=CFG.num_workers, shuffle=False, pin_memory=True, drop_last=False)\n    \n    return infer_loader","metadata":{"papermill":{"duration":0.020475,"end_time":"2022-07-23T18:39:06.765884","exception":false,"start_time":"2022-07-23T18:39:06.745409","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:17:12.202632Z","iopub.execute_input":"2022-08-14T20:17:12.203007Z","iopub.status.idle":"2022-08-14T20:17:12.211123Z","shell.execute_reply.started":"2022-08-14T20:17:12.202972Z","shell.execute_reply":"2022-08-14T20:17:12.209844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !tree ../input/hubmap-hpa-vanilla-unet-2-3-train","metadata":{"papermill":{"duration":0.018702,"end_time":"2022-07-23T18:39:06.794053","exception":false,"start_time":"2022-07-23T18:39:06.775351","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:15:12.839068Z","iopub.status.idle":"2022-08-14T20:15:12.840143Z","shell.execute_reply.started":"2022-08-14T20:15:12.839794Z","shell.execute_reply":"2022-08-14T20:15:12.839828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Inference**","metadata":{"papermill":{"duration":0.009089,"end_time":"2022-07-23T18:39:06.812485","exception":false,"start_time":"2022-07-23T18:39:06.803396","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import torch.nn.functional as F","metadata":{"papermill":{"duration":0.018937,"end_time":"2022-07-23T18:39:06.840812","exception":false,"start_time":"2022-07-23T18:39:06.821875","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:17:13.415658Z","iopub.execute_input":"2022-08-14T20:17:13.416984Z","iopub.status.idle":"2022-08-14T20:17:13.422304Z","shell.execute_reply.started":"2022-08-14T20:17:13.416910Z","shell.execute_reply":"2022-08-14T20:17:13.421225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #PyTorch\n# class DiceLoss(nn.Module):\n#     def __init__(self, weight=None, size_average=True):\n#         super(DiceLoss, self).__init__()\n\n#     def forward(self, inputs, targets, smooth=1):\n        \n#         #comment out if your model contains a sigmoid or equivalent activation layer\n#         inputs = F.sigmoid(inputs)       \n        \n#         #flatten label and prediction tensors\n#         inputs = inputs.view(-1)\n#         targets = targets.view(-1)\n        \n#         intersection = (inputs * targets).sum()                            \n#         dice = (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth)  \n        \n#         return 1 - dice","metadata":{"papermill":{"duration":0.021222,"end_time":"2022-07-23T18:39:06.871611","exception":false,"start_time":"2022-07-23T18:39:06.850389","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:15:12.845265Z","iopub.status.idle":"2022-08-14T20:15:12.850110Z","shell.execute_reply.started":"2022-08-14T20:15:12.849737Z","shell.execute_reply":"2022-08-14T20:15:12.849772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"infer_loader = prepare_loaders()\nfor (images, heights, widths, ids) in infer_loader:\n    print(images.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T20:17:18.531272Z","iopub.execute_input":"2022-08-14T20:17:18.531653Z","iopub.status.idle":"2022-08-14T20:17:18.976656Z","shell.execute_reply.started":"2022-08-14T20:17:18.531620Z","shell.execute_reply":"2022-08-14T20:17:18.975498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.ndimage import binary_opening\nfrom skimage.morphology import disk\n\ninfer_loader = prepare_loaders()\n# model = load_model('../input/hubmap-model-files/venilla_Unet_model_3.pth')\n\npred_ids = []\npred_rles = []\npred_mask_t = []\npred_mask = []\nwith torch.no_grad():\n    for (images, heights, widths, ids) in infer_loader:\n        images = images.float().to(CFG.device)\n        output = model(images)\n        output = nn.Sigmoid()(output)\n        msks = (output.permute((0,2,3,1))>CFG.threshold).to(torch.uint8).cpu().detach().numpy()\n\n        for idx in range(msks.shape[0]):\n            height = heights[idx].item()\n            width = widths[idx].item()\n            id_ = ids[idx].item()\n            print(width, height)\n            \n            msk = cv2.resize(msks[idx].squeeze(), \n                             dsize=(width, height), \n                             interpolation=cv2.INTER_NEAREST)\n            \n            seg = (msk >= 1).astype(np.uint8)  # binary mask\n            msk = binary_opening(seg, structure=disk(6)).astype(np.uint8)\n            \n            pred_mask.append(msk)\n            pred_mask_t.append(msk.T)\n            \n            rle = rle_encode_less_memory(msk.T)\n            pred_rles.append(rle)\n            pred_ids.append(id_)\n\n        gc.collect()\n        torch.cuda.empty_cache()","metadata":{"papermill":{"duration":15.766012,"end_time":"2022-07-23T18:39:22.647003","exception":false,"start_time":"2022-07-23T18:39:06.880991","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:24:27.703228Z","iopub.execute_input":"2022-08-14T20:24:27.703829Z","iopub.status.idle":"2022-08-14T20:24:28.890828Z","shell.execute_reply.started":"2022-08-14T20:24:27.703785Z","shell.execute_reply":"2022-08-14T20:24:28.889726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(pred_mask[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-14T20:24:28.982470Z","iopub.execute_input":"2022-08-14T20:24:28.983044Z","iopub.status.idle":"2022-08-14T20:24:29.500545Z","shell.execute_reply.started":"2022-08-14T20:24:28.983012Z","shell.execute_reply":"2022-08-14T20:24:29.499535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_temp = plt.imread(\"../input/hubmap-organ-segmentation/train_images/10044.tiff\")\nimg_temp = cv2.resize(img_temp,(512,512))\nimg_temp = torch.from_numpy(img_temp)\n\nimg_temp = img_temp.permute(2,1,0).unsqueeze(0)\n\npred = model(img_temp.float().cuda())\npred = nn.Sigmoid()(pred)\n\npred = pred.squeeze(0).permute(1,2,0).cpu().detach().numpy()\n\npred.shape","metadata":{"papermill":{"duration":0.594904,"end_time":"2022-07-23T18:39:23.251983","exception":false,"start_time":"2022-07-23T18:39:22.657079","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:15:12.868064Z","iopub.status.idle":"2022-08-14T20:15:12.869162Z","shell.execute_reply.started":"2022-08-14T20:15:12.868809Z","shell.execute_reply":"2022-08-14T20:15:12.868844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(pred) # [pred on test img]","metadata":{"papermill":{"duration":0.238826,"end_time":"2022-07-23T18:39:23.501007","exception":false,"start_time":"2022-07-23T18:39:23.262181","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:15:12.871223Z","iopub.status.idle":"2022-08-14T20:15:12.872310Z","shell.execute_reply.started":"2022-08-14T20:15:12.871963Z","shell.execute_reply":"2022-08-14T20:15:12.871997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### This is how bad the prediction is coming T-T.","metadata":{}},{"cell_type":"code","source":"pred_df = pd.DataFrame({\n    \"id\":pred_ids,\n    \"rle\":pred_rles\n})\npred_df.to_csv('submission.csv',index=False)\ndisplay(pred_df.head(5))","metadata":{"papermill":{"duration":0.031791,"end_time":"2022-07-23T18:39:23.572824","exception":false,"start_time":"2022-07-23T18:39:23.541033","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-14T20:25:22.212827Z","iopub.execute_input":"2022-08-14T20:25:22.213639Z","iopub.status.idle":"2022-08-14T20:25:22.234773Z","shell.execute_reply.started":"2022-08-14T20:25:22.213591Z","shell.execute_reply":"2022-08-14T20:25:22.233637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.009933,"end_time":"2022-07-23T18:39:23.592758","exception":false,"start_time":"2022-07-23T18:39:23.582825","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}