{"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":"# 🛠 Install Libraries","metadata":{}},{"cell_type":"code","source":"!pip install -q ../input/pytorch-segmentation-models-lib/pretrainedmodels-0.7.4/pretrainedmodels-0.7.4\n!pip install -q ../input/pytorch-segmentation-models-lib/efficientnet_pytorch-0.6.3/efficientnet_pytorch-0.6.3\n!pip install -q ../input/pytorch-segmentation-models-lib/timm-0.4.12-py3-none-any.whl\n# !pip install -q ../input/pytorch-segmentation-models-lib/segmentation_models_pytorch-0.2.0-py3-none-any.whl\n!pip install -q ../input/segmentation-models-pytorch-021/segmentation_models_pytorch-0.2.1-py3-none-any.whl\n# !pip install -q ../input/segmentation-tta-qubvel/ttach-0.0.3-py3-none-any.whl\n# !pip install -q ../input/ttachseg/ttach-master\n# !pip install ipdb","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:26:57.489968Z","iopub.execute_input":"2022-07-14T11:26:57.490348Z","iopub.status.idle":"2022-07-14T11:28:52.837296Z","shell.execute_reply.started":"2022-07-14T11:26:57.490250Z","shell.execute_reply":"2022-07-14T11:28:52.836445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📚 Import Libraries ","metadata":{}},{"cell_type":"code","source":"import re\nfrom collections import Counter\nimport numpy as np\nimport pandas as pd\npd.options.plotting.backend = \"plotly\"\nimport random\nfrom glob import glob\nimport os, shutil\nfrom tqdm import tqdm\ntqdm.pandas()\nimport time\nimport copy\nimport joblib\nfrom collections import defaultdict\nimport gc\nfrom IPython import display as ipd\n# import ttach as tta\nfrom pathlib import Path\nfrom joblib import Parallel, delayed\n\n# visualization\nimport cv2\nimport matplotlib.pyplot as plt\n\n# Sklearn\nfrom sklearn.model_selection import StratifiedKFold, KFold\n\n# PyTorch \nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.cuda import amp\nimport torch.nn.functional as F\n\nimport timm\n\n# Albumentations for augmentations\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\n# For colored terminal text\nfrom colorama import Fore, Back, Style\nc_  = Fore.GREEN\nsr_ = Style.RESET_ALL\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# For descriptive error messages\nos.environ['CUDA_LAUNCH_BLOCKING'] = \"1\"","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:28:52.840272Z","iopub.execute_input":"2022-07-14T11:28:52.840554Z","iopub.status.idle":"2022-07-14T11:29:03.458128Z","shell.execute_reply.started":"2022-07-14T11:28:52.840518Z","shell.execute_reply":"2022-07-14T11:29:03.457387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ⚙️ Configuration ","metadata":{}},{"cell_type":"code","source":"class CFG:\n    seed          = 101\n    debug         = False # set debug=False for Full Training\n    exp_name      = 'v4'\n    comment       = 'unet-efficientnet_b0-320x384'\n    bb_path       = []\n    model_name    = 'Unet'\n    backbone      = {'rsn269':'tu-resnest269e',\n                     'rrs420':'tu-resnetrs420', \n                     'v2l21':'tu-tf_efficientnetv2_l_in21ft1k', \n                     'b7':'efficientnet-b7', \n                     'b5':'efficientnet-b5', \n                     'tb8':'timm-efficientnet-b8', \n                     'rsn5040':'tu-resnest50d_4s2x40d',\n                     'ecnfl2':'tu-eca_nfnet_l2',\n                     'ecr269':'tu-ecaresnet269d',\n                    }\n    shared_enc    = True\n    train_bs      = 40\n    valid_bs      = train_bs*2\n    img_size      = [320, 384]\n    epochs        = 16\n    lr            = 2e-3\n    scheduler     = 'CosineAnnealingLR'\n    min_lr        = 1e-6\n    T_max         = int(30000/train_bs*epochs)+50\n    T_0           = 25\n    warmup_epochs = 0\n    wd            = 1e-6\n    n_accumulate  = max(1, 32//train_bs)\n    n_fold        = 5\n    folds         = [0]\n    num_classes   = 3\n    thr           = [0.33, 0.37, 0.4]\n    device        = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n    scaling_factor = 16384.0\n    TTA_FLAG      = False\n    cropped_analysis = True\n    \nBASE_PATH  = '/kaggle/input/uw-madison-gi-tract-image-segmentation'\n# CKPT_DIR = '/kaggle/input/uwmgi-unet-effnetb0-25d-320x384-ckpt-ds'\nCKPT_DIRS = [\n    '../input/full-rsn5040-bs80-e25',\n#     '../input/full-v2l21-bs48-e25',\n#     '../input/full-uppfpn-ecr269-c-bs48-e25',\n#     '../input/full-rsn269-scse-bs32-e25-3loss',\n    '../input/full-tb8-scse-bs32-e25-3loss',\n#     '../input/full-uppfpn-rrs420-lr-e25',\n#     '../input/full-uppfpn-rsn269-c-bs32-e25',\n#     '../input/full-uppfpn-tb8-lr-e25',\n    '../input/uppfpn-b7-c-bs48-e50',\n    '../input/uppfpn-tb8-c-bs32-e30',\n            ]\n\nimport os, shutil\nfrom pprint import pprint\nos.makedirs(\"/root/.cache/torch/hub/checkpoints/\", exist_ok=True)\n# shutil.copy(f\"{CFG.bb_path[0]}\", \"/root/.cache/torch/hub/checkpoints/\")\n# shutil.copy(\"/root/.cache/torch/hub/checkpoints/tf_efficientnetv2_l_21ft1k-60127a9d.pth\", \"./\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:29:03.459455Z","iopub.execute_input":"2022-07-14T11:29:03.459699Z","iopub.status.idle":"2022-07-14T11:29:03.535226Z","shell.execute_reply.started":"2022-07-14T11:29:03.459667Z","shell.execute_reply":"2022-07-14T11:29:03.534350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ❗ Reproducibility","metadata":{}},{"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(CFG.seed)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:29:03.537857Z","iopub.execute_input":"2022-07-14T11:29:03.538214Z","iopub.status.idle":"2022-07-14T11:29:03.558144Z","shell.execute_reply.started":"2022-07-14T11:29:03.538176Z","shell.execute_reply":"2022-07-14T11:29:03.557284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔨 Utility","metadata":{}},{"cell_type":"code","source":"def get_metadata(row):\n    data = row['id'].split('_')\n    case = int(data[0].replace('case',''))\n    day = int(data[1].replace('day',''))\n    slice_ = int(data[-1])\n    row['case'] = case\n    row['day'] = day\n    row['slice'] = slice_\n    return row\n\ndef path2info(row):\n    path = row['image_path']\n    data = path.split('/')\n    slice_ = int(data[-1].split('_')[1])\n    case = int(data[-3].split('_')[0].replace('case',''))\n    day = int(data[-3].split('_')[1].replace('day',''))\n    width = int(data[-1].split('_')[2])\n    height = int(data[-1].split('_')[3])\n    row['height'] = height\n    row['width'] = width\n    row['case'] = case\n    row['day'] = day\n    row['slice'] = slice_\n#     row['id'] = f'case{case}_day{day}_slice_{slice_}'\n    return row","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:29:03.560681Z","iopub.execute_input":"2022-07-14T11:29:03.561500Z","iopub.status.idle":"2022-07-14T11:29:03.571146Z","shell.execute_reply.started":"2022-07-14T11:29:03.561454Z","shell.execute_reply":"2022-07-14T11:29:03.570461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_img(path, size=CFG.img_size):\n    img = cv2.imread(path, cv2.IMREAD_UNCHANGED)\n    shape0 = np.array(img.shape[:2])\n    resize = np.array(size)\n    if np.any(shape0!=resize):\n        diff = resize - shape0\n        pad0 = diff[0]\n        pad1 = diff[1]\n        pady = [pad0//2, pad0//2 + pad0%2]\n        padx = [pad1//2, pad1//2 + pad1%2]\n        img = np.pad(img, [pady, padx])\n        img = img.reshape((*resize))\n    return img, shape0\n\ndef load_imgs(img_paths, size=CFG.img_size, scaling_factor = CFG.scaling_factor):\n    imgs = np.zeros((*size, len(img_paths)), dtype=np.float32)\n    for i, img_path in enumerate(img_paths):\n        if i==0:\n            img, shape0 = load_img(img_path, size=size)\n        else:\n            img, _ = load_img(img_path, size=size)\n        img = img.astype('float32') # original is uint16\n        img /= float(scaling_factor) # scale image to [0, 1]\n        imgs[..., i]+=img\n    return imgs, shape0\n\ndef load_msk(path, size=CFG.img_size):\n    msk = np.load(path)\n    shape0 = np.array(msk.shape[:2])\n    resize = np.array(size)\n    if np.any(shape0!=resize):\n        diff = resize - shape0\n        pad0 = diff[0]\n        pad1 = diff[1]\n        pady = [pad0//2, pad0//2 + pad0%2]\n        padx = [pad1//2, pad1//2 + pad1%2]\n        msk = np.pad(msk, [pady, padx, [0,0]])\n        msk = msk.reshape((*resize, 3))\n    msk = msk.astype('float32')\n    msk/=255.0\n    return msk\n\ndef show_img(img, mask=None):\n    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))\n    img = clahe.apply(img)\n    plt.imshow(img, cmap='bone')\n    \n    if mask is not None:\n        # plt.imshow(np.ma.masked_where(mask!=1, mask), alpha=0.5, cmap='autumn')\n        plt.imshow(mask, alpha=0.5)\n        handles = [Rectangle((0,0),1,1, color=_c) for _c in [(0.667,0.0,0.0), (0.0,0.667,0.0), (0.0,0.0,0.667)]]\n        labels = [\"Large Bowel\", \"Small Bowel\", \"Stomach\"]\n        plt.legend(handles,labels)\n    plt.axis('off')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:29:03.572699Z","iopub.execute_input":"2022-07-14T11:29:03.572956Z","iopub.status.idle":"2022-07-14T11:29:03.591032Z","shell.execute_reply.started":"2022-07-14T11:29:03.572924Z","shell.execute_reply":"2022-07-14T11:29:03.590320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:29:03.593681Z","iopub.execute_input":"2022-07-14T11:29:03.593948Z","iopub.status.idle":"2022-07-14T11:29:03.604667Z","shell.execute_reply.started":"2022-07-14T11:29:03.593916Z","shell.execute_reply":"2022-07-14T11:29:03.603872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test","metadata":{}},{"cell_type":"code","source":"sub_df = pd.read_csv('../input/uw-madison-gi-tract-image-segmentation/sample_submission.csv')\nif not len(sub_df):\n    debug = True\n    sub_df = pd.read_csv('../input/uw-madison-gi-tract-image-segmentation/train.csv')[:1000*3]\n    sub_df = sub_df.drop(columns=['class','segmentation']).drop_duplicates()\nelse:\n    debug = False\n    sub_df = sub_df.drop(columns=['class','predicted']).drop_duplicates()\nsub_df = sub_df.progress_apply(get_metadata,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:29:03.607758Z","iopub.execute_input":"2022-07-14T11:29:03.607961Z","iopub.status.idle":"2022-07-14T11:29:05.857842Z","shell.execute_reply.started":"2022-07-14T11:29:03.607938Z","shell.execute_reply":"2022-07-14T11:29:05.856907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if debug:\n    paths = glob(f'/kaggle/input/uw-madison-gi-tract-image-segmentation/train/**/*png',recursive=True)\n    fold_val = '/kaggle/input/uw-madison-gi-tract-image-segmentation/train/'\n    paths = sorted(paths)\nelse:\n    paths = glob(f'/kaggle/input/uw-madison-gi-tract-image-segmentation/test/**/*png',recursive=True)\n    fold_val = '/kaggle/input/uw-madison-gi-tract-image-segmentation/test/'\n    paths = sorted(paths)\npath_df = pd.DataFrame(paths, columns=['image_path'])\npath_df = path_df.progress_apply(path2info, axis=1)\npath_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:29:05.859448Z","iopub.execute_input":"2022-07-14T11:29:05.860040Z","iopub.status.idle":"2022-07-14T11:30:46.671979Z","shell.execute_reply.started":"2022-07-14T11:29:05.859991Z","shell.execute_reply":"2022-07-14T11:30:46.671242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im_path_ls = list(paths)\nlen(im_path_ls)\nim_path_ls = sorted(im_path_ls)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:30:46.675142Z","iopub.execute_input":"2022-07-14T11:30:46.675537Z","iopub.status.idle":"2022-07-14T11:30:46.681513Z","shell.execute_reply.started":"2022-07-14T11:30:46.675505Z","shell.execute_reply":"2022-07-14T11:30:46.680819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Intensity DF","metadata":{}},{"cell_type":"code","source":"def assess_im(path):\n    img = cv2.imread(str(path), cv2.IMREAD_UNCHANGED)\n    img = img.astype('float32') # original is uint16\n    return img.min(), img.max(), Path(path).parent.parent.stem\n\nim_dims = Parallel(n_jobs=-1, backend='threading')(delayed(assess_im)(path_)\\\n                                                   for path_ in tqdm(im_path_ls, total=len(im_path_ls)))","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:30:46.682969Z","iopub.execute_input":"2022-07-14T11:30:46.683433Z","iopub.status.idle":"2022-07-14T11:33:35.892988Z","shell.execute_reply.started":"2022-07-14T11:30:46.683384Z","shell.execute_reply":"2022-07-14T11:33:35.892143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"intensity_tmp_df = pd.DataFrame(columns=['min_value', 'max_value', 'name'])\nfor item in im_dims:\n    intensity_tmp_df.loc[len(intensity_tmp_df)] = item\nintensity_tmp_df['min_min'] = -1\nintensity_tmp_df['min_max'] = -1\nintensity_tmp_df['max_min'] = -1\nintensity_tmp_df['max_max'] = -1\nfor case in intensity_tmp_df.name.unique():\n    intensity_tmp_df.loc[intensity_tmp_df.name == case, 'min_min'] = float(intensity_tmp_df[intensity_tmp_df.name == case].min_value.min())\n    intensity_tmp_df.loc[intensity_tmp_df.name == case, 'min_max'] = float(intensity_tmp_df[intensity_tmp_df.name == case].min_value.max())\n    intensity_tmp_df.loc[intensity_tmp_df.name == case, 'max_min'] = float(intensity_tmp_df[intensity_tmp_df.name == case].max_value.min())\n    intensity_tmp_df.loc[intensity_tmp_df.name == case, 'max_max'] = float(intensity_tmp_df[intensity_tmp_df.name == case].max_value.max())\nintensity_df = intensity_tmp_df.copy()\nintensity_df = intensity_df.drop(columns=['min_value', 'max_value'])\nintensity_df = intensity_df.sort_values(by ='max_max')\nintensity_df = intensity_df.drop_duplicates()#.reset_index()\nintensity_df = intensity_df.reset_index(drop=True)\nintensity_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:33:35.894281Z","iopub.execute_input":"2022-07-14T11:33:35.894578Z","iopub.status.idle":"2022-07-14T11:35:19.208843Z","shell.execute_reply.started":"2022-07-14T11:33:35.894542Z","shell.execute_reply":"2022-07-14T11:35:19.208114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"intensity_df[['case', 'day']] = intensity_df['name'].str.split('_', 1, expand=True)\nintensity_df['case'] = intensity_df.case.str.replace('case','').astype(int)\nintensity_df['day'] = intensity_df.day.str.replace('day','').astype(int)\nintensity_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:19.212918Z","iopub.execute_input":"2022-07-14T11:35:19.214818Z","iopub.status.idle":"2022-07-14T11:35:19.238277Z","shell.execute_reply.started":"2022-07-14T11:35:19.214780Z","shell.execute_reply":"2022-07-14T11:35:19.237675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Cropping","metadata":{}},{"cell_type":"code","source":"name_ls = [x.replace(fold_val,'').split('/')[1] for x in im_path_ls if re.match('case[0-9]+_day[0-9]+',x.replace(fold_val,'').split('/')[1])]\nassert len(name_ls) == len(im_path_ls)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:19.242535Z","iopub.execute_input":"2022-07-14T11:35:19.244676Z","iopub.status.idle":"2022-07-14T11:35:19.407548Z","shell.execute_reply.started":"2022-07-14T11:35:19.244639Z","shell.execute_reply":"2022-07-14T11:35:19.406873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = sub_df.merge(path_df, on=['case','day','slice'], how='left')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:19.408624Z","iopub.execute_input":"2022-07-14T11:35:19.408865Z","iopub.status.idle":"2022-07-14T11:35:19.437118Z","shell.execute_reply.started":"2022-07-14T11:35:19.408833Z","shell.execute_reply":"2022-07-14T11:35:19.436325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_df = pd.DataFrame.from_dict(dict(Counter(name_ls)), orient='index', columns=['req_slice']).reset_index()\nslice_df['req_slice'] = 'slice_00' + (slice_df['req_slice']/ 2).astype(int).astype(str)\nslice_df['slice_name'] = slice_df['index'].apply(lambda x: im_path_ls[name_ls.index(x)])\nlen([x for x in  list(slice_df['slice_name']) if re.match('slice_0001' ,Path(x).stem)]) == slice_df.shape[0]\nslice_df['nslice_name'] = slice_df.apply(lambda x: x['slice_name'].replace('slice_0001', x.req_slice), axis =1)\nassert slice_df.shape[0] == len([x for x in list(slice_df['nslice_name']) if Path(x).exists()])\n\nslice_df['name'] = slice_df['index']\nslice_df.drop(columns=['index'])\nslice_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:19.438431Z","iopub.execute_input":"2022-07-14T11:35:19.438742Z","iopub.status.idle":"2022-07-14T11:35:20.054955Z","shell.execute_reply.started":"2022-07-14T11:35:19.438706Z","shell.execute_reply":"2022-07-14T11:35:20.054278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_df = slice_df.merge(intensity_df, on=['name'])\nslice_df","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:20.056078Z","iopub.execute_input":"2022-07-14T11:35:20.056754Z","iopub.status.idle":"2022-07-14T11:35:20.077717Z","shell.execute_reply.started":"2022-07-14T11:35:20.056716Z","shell.execute_reply":"2022-07-14T11:35:20.077062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Cropping and Saving Coordinates","metadata":{}},{"cell_type":"code","source":"tol =0.1\npos = 50\npos_val = 0\ndef crop_borders(im_path, max_max, plot_flag=False):\n    im, _ = load_img(im_path)    \n#     im = cv2.imread(im_path, cv2.IMREAD_UNCHANGED)\n    im = im.astype('float32') # original is uint16\n    img = im / float(max_max)\n    mask = img>tol\n    mask[:pos,:pos] =pos_val\n    mask[-pos:,-pos:] =pos_val \n    mask[-pos:,:pos] =pos_val  \n    mask[:pos,-pos:] = pos_val\n    if plot_flag:\n        plt.imshow(mask)\n        plt.show()\n\n    m,n = img.shape\n    mask0,mask1 = mask.any(0),mask.any(1)\n    col_start,col_end = mask0.argmax(),n-mask0[::-1].argmax()\n    row_start,row_end = mask1.argmax(),m-mask1[::-1].argmax()\n    new_im = img[row_start:row_end,col_start:col_end]\n\n#     f, axarr = plt.subplots(1, 2)\n#     axarr[0].imshow(img)\n#     axarr[1].imshow(new_im)\n#     plt.show()\n\n#     print(im_path, img.shape, max_max, new_im.shape, row_start,row_end,col_start,col_end)\n\n    assert (row_end - row_start) == new_im.shape[0]\n    assert (col_end - col_start) == new_im.shape[1]\n    return new_im.shape[0], new_im.shape[1], row_start,row_end,col_start,col_end","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:20.079369Z","iopub.execute_input":"2022-07-14T11:35:20.079826Z","iopub.status.idle":"2022-07-14T11:35:20.088637Z","shell.execute_reply.started":"2022-07-14T11:35:20.079783Z","shell.execute_reply":"2022-07-14T11:35:20.087974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnt = 0\nfor ind, row in tqdm(slice_df.iterrows()):\n    im_path = row['nslice_name']\n    new_size = crop_borders(im_path, row['max_max'], plot_flag=False)\n    slice_df.loc[ind, ['new_w', 'new_h', 'row_1', 'row_2', 'col_1', 'col_2']] = new_size\n#     cnt += 1\n#     if cnt == 3:\n#         break","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-14T11:35:20.089788Z","iopub.execute_input":"2022-07-14T11:35:20.090709Z","iopub.status.idle":"2022-07-14T11:35:21.586335Z","shell.execute_reply.started":"2022-07-14T11:35:20.090669Z","shell.execute_reply":"2022-07-14T11:35:21.585638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_updated_df(tmp):\n    for ind, row in tqdm(tmp.iterrows()):\n        if row[field_name] > max_val:\n            s1 = row[f'{name}1'] + abs(np.ceil((row[field_name] - max_val) /2))\n            s2 = row[f'{name}2'] - abs(np.floor((row[field_name] - max_val) /2))\n            assert s1 >= 0\n            if s2 > im_max:\n              diff = s2 - im_max\n              s1 = s1 + diff\n        else:\n            s1 = row[f'{name}1'] - abs(np.ceil((row[field_name] - max_val) /2))\n            s2 = row[f'{name}2'] + abs(np.floor((row[field_name] - max_val) /2))\n            \n            if s1 < 0:\n              diff = abs(s1)\n              s1 = 0 \n              s2 = s2 + diff\n\n            if s2 > im_max:\n              diff = s2 - im_max\n              s1 = s1 - diff\n              s2 = im_max\n            \n            if (s2 - s1) > max_val:\n              import ipdb\n              ipdb.set_trace()\n\n        tmp.loc[ind, [f'n_{name}1', f'n_{name}2']] = s1, s2\n    \n    assert (tmp[f'{name}1'] >=0 ).all()\n    assert (tmp[f'{name}2'] < im_max ).all()\n    tmp[f'{name}diff'] = tmp[f'n_{name}2'] - tmp[f'n_{name}1']\n    assert len(tmp[tmp[f'{name}diff'] > max_val])  == 0\n    return tmp","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:21.587542Z","iopub.execute_input":"2022-07-14T11:35:21.587949Z","iopub.status.idle":"2022-07-14T11:35:21.600593Z","shell.execute_reply.started":"2022-07-14T11:35:21.587907Z","shell.execute_reply":"2022-07-14T11:35:21.599725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_w = 224\nmax_h = 320\nim_w = CFG.img_size[0]\nim_h = CFG.img_size[1]\n\nfield_name = 'new_w'\nname = 'row_'\nmax_val = max_w\nim_max = im_w\nslice_df = get_updated_df(slice_df)\n\nfield_name = 'new_h'\nname = 'col_'\nmax_val = max_h\nim_max = im_h\nslice_df = get_updated_df(slice_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:21.602315Z","iopub.execute_input":"2022-07-14T11:35:21.602875Z","iopub.status.idle":"2022-07-14T11:35:22.298788Z","shell.execute_reply.started":"2022-07-14T11:35:21.602836Z","shell.execute_reply":"2022-07-14T11:35:22.297470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_df['row_diff'].unique(), slice_df['col_diff'].unique(),","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:22.299873Z","iopub.execute_input":"2022-07-14T11:35:22.300199Z","iopub.status.idle":"2022-07-14T11:35:22.310031Z","shell.execute_reply.started":"2022-07-14T11:35:22.300159Z","shell.execute_reply":"2022-07-14T11:35:22.309274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = sub_df.merge(path_df, on=['case','day','slice'], how='left')\ntest_df = test_df.merge(slice_df, on=['case','day'])\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:22.312467Z","iopub.execute_input":"2022-07-14T11:35:22.312709Z","iopub.status.idle":"2022-07-14T11:35:22.351537Z","shell.execute_reply.started":"2022-07-14T11:35:22.312677Z","shell.execute_reply":"2022-07-14T11:35:22.350565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert (slice_df.n_row_1 >= 0).all() \nassert (slice_df.n_row_2 > 0).all()\nassert (slice_df.n_col_1 >= 0).all() \nassert (slice_df.n_col_2 > 0).all()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:22.352837Z","iopub.execute_input":"2022-07-14T11:35:22.353136Z","iopub.status.idle":"2022-07-14T11:35:22.359621Z","shell.execute_reply.started":"2022-07-14T11:35:22.353101Z","shell.execute_reply":"2022-07-14T11:35:22.358659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create 2.5D MetaData","metadata":{}},{"cell_type":"code","source":"channels=3\nstride=2\nfor i in range(channels):\n    test_df[f'image_path_{i:02}'] = test_df.groupby(['case','day'])['image_path'].shift(-i*stride).fillna(method=\"ffill\")\ntest_df['image_paths'] = test_df[[f'image_path_{i:02d}' for i in range(channels)]].values.tolist()\ntest_df.image_paths[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:22.361199Z","iopub.execute_input":"2022-07-14T11:35:22.361681Z","iopub.status.idle":"2022-07-14T11:35:22.384954Z","shell.execute_reply.started":"2022-07-14T11:35:22.361624Z","shell.execute_reply":"2022-07-14T11:35:22.384290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🍚 Dataset","metadata":{}},{"cell_type":"code","source":"class BuildDataset(torch.utils.data.Dataset):\n    def __init__(self, df, label=False, transforms=None):\n        self.df         = df\n        self.label      = label\n        self.img_paths  = df['image_paths'].tolist()\n        self.ids        = df['id'].tolist()\n        self.scales     = df['max_max'].tolist()\n        if CFG.cropped_analysis:\n            self.r1 = df['n_row_1'].tolist()\n            self.r2 = df['n_row_2'].tolist()\n            self.c1 = df['n_col_1'].tolist()\n            self.c2 = df['n_col_2'].tolist()\n        else:\n            self.r1 = None\n            self.r2 = None\n            self.c1 = None\n            self.c2 = None\n        \n        if 'msk_path' in df.columns:\n            self.msk_paths  = df['mask_path'].tolist()\n        else:\n            self.msk_paths = None\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_path  = self.img_paths[index]\n        id_       = self.ids[index]\n        scales    = self.scales[index]\n        img = []\n        img, shape0 = load_imgs(img_path, scaling_factor =scales)\n        h, w = shape0\n        pos_info = {'r1': self.r1[index],\n                    'r2': self.r2[index],\n                    'c1': self.c1[index],\n                    'c2': self.c2[index],\n                    'height': h,\n                    'width': w\n                   }\n        img = img[int(pos_info['r1']):int(pos_info['r2']), int(pos_info['c1']):int(pos_info['c2']), :]\n        if self.label:\n            msk_path = self.msk_paths[index]\n            msk = load_msk(msk_path)\n            if self.transforms:\n                data = self.transforms(image=img, mask=msk)\n                img  = data['image']\n                msk  = data['mask']\n            img = np.transpose(img, (2, 0, 1))\n            msk = np.transpose(msk, (2, 0, 1))\n            return torch.tensor(img), torch.tensor(msk)\n        else:\n            if self.transforms:\n                data = self.transforms(image=img)\n                img  = data['image']\n            img = np.transpose(img, (2, 0, 1))\n            return torch.tensor(img), pos_info, id_, h, w","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:35:22.385988Z","iopub.execute_input":"2022-07-14T11:35:22.386228Z","iopub.status.idle":"2022-07-14T11:35:22.402093Z","shell.execute_reply.started":"2022-07-14T11:35:22.386196Z","shell.execute_reply":"2022-07-14T11:35:22.401340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🌈 Augmentations","metadata":{}},{"cell_type":"code","source":"data_transforms = {\n    \"train\": A.Compose([\n#         A.Resize(*CFG.img_size, interpolation=cv2.INTER_NEAREST),\n        A.HorizontalFlip(p=0.5),\n        A.VerticalFlip(p=0.5),\n#         A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.05, rotate_limit=5, p=0.5),\n        A.OneOf([\n            A.GridDistortion(num_steps=5, distort_limit=0.05, p=1.0),\n# #             A.OpticalDistortion(distort_limit=0.05, shift_limit=0.05, p=1.0),\n            A.ElasticTransform(alpha=1, sigma=50, alpha_affine=50, p=1.0)\n        ], p=0.25),\n#         A.CoarseDropout(max_holes=8, max_height=CFG.img_size[0]//20, max_width=CFG.img_size[1]//20,\n#                          min_holes=5, fill_value=0, mask_fill_value=0, p=0.5),\n        ], p=1.0),\n    \n    \"valid\": A.Compose([\n#         A.Resize(*CFG.img_size, interpolation=cv2.INTER_NEAREST),\n        ], p=1.0)\n}","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:35:22.403317Z","iopub.execute_input":"2022-07-14T11:35:22.403798Z","iopub.status.idle":"2022-07-14T11:35:22.417082Z","shell.execute_reply.started":"2022-07-14T11:35:22.403717Z","shell.execute_reply":"2022-07-14T11:35:22.416376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📦 Model\n","metadata":{}},{"cell_type":"code","source":"import segmentation_models_pytorch as smp\n\nclass model_segmentation(nn.Module):\n\n    def __init__(self, path, backbone):\n        super(model_segmentation, self).__init__()\n\n#         if \"upp\" not in path:\n#             self.model_unet = smp.Unet(\n#                                 encoder_name=backbone,#CFG.backbone,\n#     #                             wt_file_path=CFG.bb_path,\n#                                 encoder_weights=None,\n#                                 in_channels=3, \n#                                 classes=CFG.num_classes, \n#                                 activation=None\n#                              )\n#         else:\n        if 'scse' in path:\n            CFG.attention = 'scse'\n        else:\n            CFG.attention = None\n        self.model_unet = smp.UnetPlusPlus(\n                            encoder_name=backbone,#CFG.backbone, \n#                             wt_file_path=CFG.bb_path,\n                            encoder_weights=None,\n                            in_channels=3, \n                            classes=CFG.num_classes, \n                            activation=None,\n                            decoder_attention_type=CFG.attention,\n                         )\n\n        self.model_fpn = smp.FPN(\n                            encoder_name=backbone,#CFG.backbone, \n#                             wt_file_path=CFG.bb_path,\n                            encoder_weights=None,\n                            in_channels=3, \n                            classes=CFG.num_classes, \n                            activation=None\n                         )\n\n    def forward(self, x):\n        \n        global_features = self.model_unet.encoder(x)\n            \n        seg_feature_unet = self.model_unet.decoder(*global_features)\n        seg_feature_fpn = self.model_fpn.decoder(*global_features)\n        \n        out_u = self.model_unet.segmentation_head(seg_feature_unet)\n        out_f = self.model_fpn.segmentation_head(seg_feature_fpn)\n        \n        return 0.65 * out_u + 0.35 * out_f\n\n\ndef build_model(backbone, path):\n    if CFG.shared_enc == False:\n        model = smp.Unet(\n            encoder_name=backbone,      # choose encoder, e.g. mobilenet_v2 or efficientnet-b7\n            encoder_weights='imagenet',     # use `imagenet` pre-trained weights for encoder initialization\n            in_channels=CFG.num_slices,                  # model input channels (1 for gray-scale images, 3 for RGB, etc.)\n            classes=CFG.num_classes,        # model output channels (number of classes in your dataset)\n            activation=None,\n            decoder_attention_type=CFG.attention,\n        )\n    else:\n        model = model_segmentation(path, backbone)\n\n    model.to(CFG.device)\n    return model\n\ndef load_model(path):\n    \n    if \"ecnfl2\" in path.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['ecnfl2'], path)\n    elif \"b7\" in path.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['b7'], path)\n    elif \"b5\" in path:#.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['b5'], path)\n    elif \"tb8\" in path.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['tb8'], path)\n    elif \"rrs420\" in path.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['rrs420'], path)\n    elif \"rns2e\" in path.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['rns2e'], path)\n    elif \"v2l21\" in path.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['v2l21'], path)\n    elif \"rsn5040\" in path.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['rsn5040'], path)\n    elif \"ecr269\" in path.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['ecr269'], path)\n    elif \"rsn269\" in path.split('.')[-2].split('/')[-1]:\n        model = build_model(CFG.backbone['rsn269'], path)\n#     model = build_model(path)\n#     if \"eeeee\" in path:\n#         model.load_state_dict(torch.load(path)['model'])\n#     else:\n    model.load_state_dict(torch.load(path))\n    model.eval()\n    return model","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:35:22.421608Z","iopub.execute_input":"2022-07-14T11:35:22.422112Z","iopub.status.idle":"2022-07-14T11:35:23.565146Z","shell.execute_reply.started":"2022-07-14T11:35:22.422055Z","shell.execute_reply":"2022-07-14T11:35:23.564417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔨 Helper","metadata":{}},{"cell_type":"code","source":"import cupy as cp\n\ndef mask2rle(msk, thr=0.5):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    msk    = cp.array(msk)\n    pixels = msk.flatten()\n    pad    = cp.array([0])\n    pixels = cp.concatenate([pad, pixels, pad])\n    runs   = cp.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\ndef masks2rles(msks, ids, heights, widths, pos_info):\n    pred_strings = []; pred_ids = []; pred_classes = [];\n    for idx in range(msks.shape[0]):\n        nmsk = np.zeros([CFG.img_size[0], CFG.img_size[1], 3])\n#         import ipdb\n#         ipdb.set_trace()\n        msk = msks[idx]\n        nmsk[int(pos_info['r1'][idx]):int(pos_info['r2'][idx]), int(pos_info['c1'][idx]):int(pos_info['c2'][idx]), :] = msk\n        \n        height = heights[idx].item()\n        width = widths[idx].item()\n        shape0 = np.array([height, width])\n        resize = np.array([CFG.img_size[0], CFG.img_size[1]])\n        if np.any(shape0!=resize):\n            diff = resize - shape0\n            pad0 = diff[0]\n            pad1 = diff[1]\n            pady = [pad0//2, pad0//2 + pad0%2]\n            padx = [pad1//2, pad1//2 + pad1%2]\n            nmsk = nmsk[pady[0]:-pady[1], padx[0]:-padx[1], :]\n            nmsk = nmsk.reshape((*shape0, 3))\n        rle = [None]*3\n        for midx in [0, 1, 2]:\n            rle[midx] = mask2rle(nmsk[...,midx])\n        pred_strings.extend(rle)\n        pred_ids.extend([ids[idx]]*len(rle))\n        pred_classes.extend(['large_bowel', 'small_bowel', 'stomach'])\n    return pred_strings, pred_ids, pred_classes","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:35:23.566617Z","iopub.execute_input":"2022-07-14T11:35:23.566887Z","iopub.status.idle":"2022-07-14T11:35:24.888367Z","shell.execute_reply.started":"2022-07-14T11:35:23.566851Z","shell.execute_reply":"2022-07-14T11:35:24.887548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔭 Inference","metadata":{}},{"cell_type":"code","source":"# transforms = tta.Compose(\n#     [   tta.Scale(scales=[0.9, 1.0, 1.1], interpolation=\"nearest\"),\n#         tta.Multiply(factors=[0.9, 1, 1.1]),\n#     ]\n# )\nfrom time import time","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:24.890366Z","iopub.execute_input":"2022-07-14T11:35:24.890686Z","iopub.status.idle":"2022-07-14T11:35:24.896265Z","shell.execute_reply.started":"2022-07-14T11:35:24.890644Z","shell.execute_reply":"2022-07-14T11:35:24.895535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@torch.no_grad()\ndef infer(model_paths, test_loader, num_log=1, thr=CFG.thr):\n    msks = []; imgs = [];\n    pred_strings = []; pred_ids = []; pred_classes = [];\n    for idx, (img, pos_info, ids, heights, widths) in enumerate(tqdm(test_loader, total=len(test_loader), desc='Infer ')):\n        img = img.to(CFG.device, dtype=torch.float) # .squeeze(0)\n        size = img.size()\n        msk = []\n        msk = torch.zeros((size[0], 3, size[2], size[3]), device=CFG.device, dtype=torch.float32)\n        for path in model_paths:\n            model = load_model(path)            \n            if CFG.TTA_FLAG:\n                tta_model = tta.SegmentationTTAWrapper(model, transforms)\n                out   = tta_model(img) # .squeeze(0) # removing batch axis\n            else:\n                out = model(img) # .squeeze(0) # removing batch axis\n            out   = nn.Sigmoid()(out) # removing channel axis\n            msk+=out/len(model_paths)\n            del model, out\n            torch.cuda.empty_cache()\n#         msk = (msk.permute((0,2,3,1))>thr).to(torch.uint8).cpu().detach().numpy() # shape: (n, h, w, c)\n        msk = (msk.permute((0,2,3,1)))\n        # print(f\"after {msk.shape}\")\n        msk[:,:,:,0] = (msk[:,:,:,0] > CFG.thr[0])\n        msk[:,:,:,1] = (msk[:,:,:,1] > CFG.thr[1]) \n        msk[:,:,:,2] = (msk[:,:,:,2] > CFG.thr[2])\n        msk = msk.to(torch.uint8).cpu().detach().numpy() # shape: (n, h, w, c)\n        result = masks2rles(msk, ids, heights, widths , pos_info)\n        pred_strings.extend(result[0])\n        pred_ids.extend(result[1])\n        pred_classes.extend(result[2])\n#         print(\"Post time: \", (time() - post_time))\n        if idx<num_log:\n            img = img.permute((0,2,3,1)).cpu().detach().numpy()\n            imgs.append(img[:10])\n            msks.append(msk[:10])\n        del img, msk, result#, out, model\n        gc.collect()\n        torch.cuda.empty_cache()\n#         break\n    return pred_strings, pred_ids, pred_classes, imgs, msks","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:35:24.897617Z","iopub.execute_input":"2022-07-14T11:35:24.898063Z","iopub.status.idle":"2022-07-14T11:35:24.914408Z","shell.execute_reply.started":"2022-07-14T11:35:24.898028Z","shell.execute_reply":"2022-07-14T11:35:24.913734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = BuildDataset(test_df, transforms=data_transforms['valid'])\ntest_loader  = DataLoader(test_dataset, batch_size=CFG.valid_bs, drop_last =False,\n                          num_workers=4, shuffle=False, pin_memory=False)\n# model_paths  = glob(f'{CKPT_DIR}/*.bin')\nmodel_paths = []\nfor ckpt_dir in CKPT_DIRS:\n    \n    if 'tb8' in ckpt_dir and 'scse' not in ckpt_dir:\n        model_path  = sorted(list(glob(f'{ckpt_dir}/*/*.bin')))\n    else:\n        model_path  = sorted(list(glob(f'{ckpt_dir}/*.bin')))\n    model_paths.extend(model_path)\n\n# model_paths.pop(-5)\nprint(model_paths)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:24.915942Z","iopub.execute_input":"2022-07-14T11:35:24.916491Z","iopub.status.idle":"2022-07-14T11:35:24.958636Z","shell.execute_reply.started":"2022-07-14T11:35:24.916456Z","shell.execute_reply":"2022-07-14T11:35:24.957950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:24.959887Z","iopub.execute_input":"2022-07-14T11:35:24.960341Z","iopub.status.idle":"2022-07-14T11:35:24.964143Z","shell.execute_reply.started":"2022-07-14T11:35:24.960306Z","shell.execute_reply":"2022-07-14T11:35:24.963442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_strings, pred_ids, pred_classes, imgs, msks = infer(model_paths, test_loader)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:35:24.966069Z","iopub.execute_input":"2022-07-14T11:35:24.966649Z","iopub.status.idle":"2022-07-14T11:52:09.630135Z","shell.execute_reply.started":"2022-07-14T11:35:24.966611Z","shell.execute_reply":"2022-07-14T11:52:09.629231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img, msk in zip(imgs[0][:5], msks[0][:5]):\n    plt.figure(figsize=(12, 7))\n    plt.subplot(1, 3, 1); plt.imshow(img, cmap='bone');\n    plt.axis('OFF'); plt.title('image')\n    plt.subplot(1, 3, 2); plt.imshow(msk*255); plt.axis('OFF'); plt.title('mask')\n    plt.subplot(1, 3, 3); plt.imshow(img, cmap='bone'); plt.imshow(msk*255, alpha=0.4);\n    plt.axis('OFF'); plt.title('overlay')\n    plt.tight_layout()\n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:52:09.631641Z","iopub.execute_input":"2022-07-14T11:52:09.632550Z","iopub.status.idle":"2022-07-14T11:52:11.559959Z","shell.execute_reply.started":"2022-07-14T11:52:09.632508Z","shell.execute_reply":"2022-07-14T11:52:11.559218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del imgs, msks\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:52:11.561252Z","iopub.execute_input":"2022-07-14T11:52:11.561645Z","iopub.status.idle":"2022-07-14T11:52:11.800835Z","shell.execute_reply.started":"2022-07-14T11:52:11.561609Z","shell.execute_reply":"2022-07-14T11:52:11.800068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📝 Submission","metadata":{}},{"cell_type":"code","source":"pred_df = pd.DataFrame({\n    \"id\":pred_ids,\n    \"class\":pred_classes,\n    \"predicted\":pred_strings\n})\nif not debug:\n    sub_df = pd.read_csv('../input/uw-madison-gi-tract-image-segmentation/sample_submission.csv')\n    del sub_df['predicted']\nelse:\n    sub_df = pd.read_csv('../input/uw-madison-gi-tract-image-segmentation/train.csv')[:1000*3]\n    del sub_df['segmentation']\n    \nsub_df = sub_df.merge(pred_df, on=['id','class'])\nsub_df.to_csv('submission.csv',index=False)\ndisplay(sub_df.head(5))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-14T11:52:11.802135Z","iopub.execute_input":"2022-07-14T11:52:11.802414Z","iopub.status.idle":"2022-07-14T11:52:12.307914Z","shell.execute_reply.started":"2022-07-14T11:52:11.802364Z","shell.execute_reply":"2022-07-14T11:52:12.307249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}