{"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":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nfrom skimage import io,img_as_float\nimport cv2\nimport torch\nfrom torch.utils.data import Dataset,DataLoader\nfrom sklearn.model_selection import StratifiedKFold, KFold, StratifiedGroupKFold\nimport rasterio\nimport albumentations as A\n\nimport os\nimport gc\nimport cv2\nimport glob\nfrom tqdm import notebook\nimport tifffile as tiff \n\nfrom albumentations.pytorch import ToTensorV2\nimport random\nfrom torchvision.utils import make_grid\nfrom torchvision.io import read_image\nfrom pathlib import Path\nimport torchvision.transforms.functional as F\nimport time\nimport copy\nfrom collections import defaultdict\nimport gc\n# import segmentation_models_pytorch as smp\nfrom torch.optim import lr_scheduler\nimport transformers \n\nimport torch.nn as nn\nfrom tqdm import tqdm\nfrom torchmetrics.functional import dice\nimport torch.optim as optim\nimport argparse\n\nfrom yaml import parse\n\nfrom torch.cuda import amp\nimport albumentations as A\nfrom torch.nn import functional as nnF\n\nfrom colorama import Fore, Back, Style\nc_  = Fore.GREEN\nsr_ = Style.RESET_ALL\n\nplt.rcParams[\"savefig.bbox\"] = 'tight'","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:18.796816Z","iopub.execute_input":"2022-09-07T20:35:18.797245Z","iopub.status.idle":"2022-09-07T20:35:18.807721Z","shell.execute_reply.started":"2022-09-07T20:35:18.797210Z","shell.execute_reply":"2022-09-07T20:35:18.806248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Installing SPAMS","metadata":{}},{"cell_type":"code","source":"!pip install /kaggle/input/staintools-offline/spams-2.6.5.4-cp37-cp37m-linux_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:18.822570Z","iopub.execute_input":"2022-09-07T20:35:18.822921Z","iopub.status.idle":"2022-09-07T20:35:48.150275Z","shell.execute_reply.started":"2022-09-07T20:35:18.822894Z","shell.execute_reply":"2022-09-07T20:35:48.149039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Utility**","metadata":{}},{"cell_type":"code","source":"class CFG:\n    def __init__(self,n_fold = 5,seed = 42,batch_size = 8,debug=False,kaggle = True):\n        \n        # Directories\n        self.root_dir = \"../input/hubmap-organ-segmentation\" if kaggle else '/media/harish/4tb_hdd/mustaffa/saharsh_hubmap/hubmap-organ-segmentation'\n#         self.IMAGES   = '../input/hubmap-2022-256x256/train' if kaggle else '/media/harish/4tb_hdd/mustaffa/saharsh_hubmap/hubmap-2022-256x256/train' \n#         self.MASKS    = \"../input/hubmap-2022-256x256/masks\" if kaggle else '/media/harish/4tb_hdd/mustaffa/saharsh_hubmap/hubmap-2022-256x256/masks'\n        \n        self.IMAGES   = '../input/hubmap-hpa-2022-png-dataset/train_images_png'\n        self.MASKS    = '../input/hubmap-hpa-2022-png-dataset/train_masks_png'\n        \n        self.fold_no       = 0\n        self.n_fold        = n_fold\n        self.folds_to_run  = [0,1,2,3,4]\n        self.seed          = seed\n        self.batch_size    = batch_size\n        self.train_bs      = batch_size\n        self.valid_bs      = batch_size\n        self.debug         = debug\n        self.img_size      = [1024,1024]\n        self.exp_name      = 'Hubmap256-training'\n        self.epochs        = 60\n        self.lr            = 4e-4 #6e-5\n        self.sz            = 1024\n        self.optimizer     = 'Adam'\n        # For scheduler\n        self.scheduler     = \"CosineAnnealingLR\"\n        self.min_lr        = 5e-5\n        self.T_max         = int(30000/self.batch_size*self.epochs)+50\n        self.T_0           = 25\n        self.warmup_epochs = 2\n        self.weight_decay  = 1e-6\n        self.wd            = 1e-6\n        self.n_accumulate  = 1 #max(1, 32//self.batch_size)\n        \n        self.num_classes   = 1\n        self.device        = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n        \n        self.hub_model_id  = 'nvidia/mit-b2'\n        self.model_name    = \"segformer\"\n        \ndef initialize_config(debug=False,batch_size=4):\n    cfg = CFG(batch_size = batch_size,debug = debug)    \n#     set_seed(cfg.seed)\n#     cfg.display()\n    return cfg","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:48.154601Z","iopub.execute_input":"2022-09-07T20:35:48.154981Z","iopub.status.idle":"2022-09-07T20:35:48.167797Z","shell.execute_reply.started":"2022-09-07T20:35:48.154946Z","shell.execute_reply":"2022-09-07T20:35:48.166795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# functions to convert encoding to mask and mask to encoding\ndef enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for m,enc in enumerate(encs):\n        if isinstance(enc,np.float) and np.isnan(enc): continue\n        s = enc.split()\n        for i in range(len(s)//2):\n            start = int(s[2*i]) - 1\n            length = int(s[2*i+1])\n            img[start:start+length] = 1 + m\n    return img.reshape(shape).T\n\ndef mask2enc(mask, n=1):\n    pixels = mask.T.flatten()\n    encs = []\n    for i in range(1,n+1):\n        p = (pixels == i).astype(np.int8)\n        if p.sum() == 0: encs.append(np.nan)\n        else:\n            p = np.concatenate([[0], p, [0]])\n            runs = np.where(p[1:] != p[:-1])[0] + 1\n            runs[1::2] -= runs[::2]\n            encs.append(' '.join(str(x) for x in runs))\n    return encs\n\n#https://www.kaggle.com/bguberfain/memory-aware-rle-encoding\n#with transposed mask\ndef rle_encode_less_memory(img):\n    #the image should be transposed\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":{"execution":{"iopub.status.busy":"2022-09-07T20:35:48.169568Z","iopub.execute_input":"2022-09-07T20:35:48.170032Z","iopub.status.idle":"2022-09-07T20:35:48.184336Z","shell.execute_reply.started":"2022-09-07T20:35:48.169985Z","shell.execute_reply":"2022-09-07T20:35:48.183270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Vahadane Stain normalisation**","metadata":{}},{"cell_type":"code","source":"import spams\nimport numpy as np\nimport cv2 as cv\nimport spams\nimport matplotlib.pyplot as plt\nimport tifffile as tiff\n\ndef read_image_cv(path):\n    \n    im = cv.imread(path)\n    im = cv.cvtColor(im, cv.COLOR_BGR2RGB)\n    print('image read..')\n    return im\n\ndef read_image(path):\n    \"\"\"\n    Read an image to RGB uint8\n    :param path:\n    :return:\n    \"\"\"\n    im = tiff.imread(path)\n    # im = cv.imread(path)\n    # im = cv.cvtColor(im, cv.COLOR_BGR2RGB)\n    \n    return im\n\ndef show_colors(C):\n    \"\"\"\n    Shows rows of C as colors (RGB)\n    :param C:\n    :return:\n    \"\"\"\n    n = C.shape[0]\n    for i in range(n):\n        if C[i].max() > 1.0:\n            plt.plot([0, 1], [n - 1 - i, n - 1 - i], c=C[i] / 255, linewidth=20)\n        else:\n            plt.plot([0, 1], [n - 1 - i, n - 1 - i], c=C[i], linewidth=20)\n        plt.axis('off')\n        plt.axis([0, 1, -1, n])\n\ndef show(image, now=True, fig_size=(10, 10)):\n    \"\"\"\n    Show an image (np.array).\n    Caution! Rescales image to be in range [0,1].\n    :param image:\n    :param now:\n    :param fig_size:\n    :return:\n    \"\"\"\n    image = image.astype(np.float32)\n    m, M = image.min(), image.max()\n    if fig_size != None:\n        plt.rcParams['figure.figsize'] = (fig_size[0], fig_size[1])\n    plt.imshow((image - m) / (M - m), cmap='gray')\n    plt.axis('off')\n    if now == True:\n        plt.show()\n\ndef build_stack(tup):\n    \"\"\"\n    Build a stack of images from a tuple of images\n    :param tup:\n    :return:\n    \"\"\"\n    N = len(tup)\n    if len(tup[0].shape) == 3:\n        h, w, c = tup[0].shape\n        stack = np.zeros((N, h, w, c))\n    if len(tup[0].shape) == 2:\n        h, w = tup[0].shape\n        stack = np.zeros((N, h, w))\n    for i in range(N):\n        stack[i] = tup[i]\n    return stack\n\ndef patch_grid(ims, width=5, sub_sample=None, rand=False, save_name=None):\n    \"\"\"\n    Display a grid of patches\n    :param ims:\n    :param width:\n    :param sub_sample:\n    :param rand:\n    :return:\n    \"\"\"\n    N0 = np.shape(ims)[0]\n    if sub_sample == None:\n        N = N0\n        stack = ims\n    elif sub_sample != None and rand == False:\n        N = sub_sample\n        stack = ims[:N]\n    elif sub_sample != None and rand == True:\n        N = sub_sample\n        idx = np.random.choice(range(N), sub_sample, replace=False)\n        stack = ims[idx]\n    height = np.ceil(float(N) / width).astype(np.uint16)\n    plt.rcParams['figure.figsize'] = (18, (18 / width) * height)\n    plt.figure()\n    for i in range(N):\n        plt.subplot(height, width, i + 1)\n        im = stack[i]\n        show(im, now=False, fig_size=None)\n    if save_name != None:\n        plt.savefig(save_name)\n    plt.show()\n\ndef standardize_brightness(I):\n    \"\"\"\n\n    :param I:\n    :return:\n    \"\"\"\n    p = np.percentile(I, 90)\n    return np.clip(I * 255.0 / p, 0, 255).astype(np.uint8)\n\n\ndef remove_zeros(I):\n    \"\"\"\n    Remove zeros, replace with 1's.\n    :param I: uint8 array\n    :return:\n    \"\"\"\n    mask = (I == 0)\n    I[mask] = 1\n    return I\n\n\ndef RGB_to_OD(I):\n    \"\"\"\n    Convert from RGB to optical density\n    :param I:\n    :return:\n    \"\"\"\n    I = remove_zeros(I)\n    return -1 * np.log(I / 255)\n\n\ndef OD_to_RGB(OD):\n    \"\"\"\n    Convert from optical density to RGB\n    :param OD:\n    :return:\n    \"\"\"\n    return (255 * np.exp(-1 * OD)).astype(np.uint8)\n\n\ndef normalize_rows(A):\n    \"\"\"\n    Normalize rows of an array\n    :param A:\n    :return:\n    \"\"\"\n    return A / np.linalg.norm(A, axis=1)[:, None]\n\ndef notwhite_mask(I, thresh=0.8):\n    \"\"\"\n    Get a binary mask where true denotes 'not white'\n    :param I:\n    :param thresh:\n    :return:\n    \"\"\"\n    I_LAB = cv.cvtColor(I, cv.COLOR_RGB2LAB)\n    L = I_LAB[:, :, 0] / 255.0\n    return (L < thresh)\n\n\ndef sign(x):\n    \"\"\"\n    Returns the sign of x\n    :param x:\n    :return:\n    \"\"\"\n    if x > 0:\n        return +1\n    elif x < 0:\n        return -1\n    elif x == 0:\n        return 0\n\ndef get_concentrations(I, stain_matrix, lamda=0.01):\n    \"\"\"\n    Get concentrations, a npix x 2 matrix\n    :param I:\n    :param stain_matrix: a 2x3 stain matrix\n    :return:\n    \"\"\"\n    OD = RGB_to_OD(I).reshape((-1, 3))\n    return spams.lasso(OD.T, D=stain_matrix.T, mode=2, lambda1=lamda, pos=True).toarray().T\n\ndef get_stain_matrix(I, threshold=0.8, lamda=0.1):\n    \"\"\"\n    Get 2x3 stain matrix. First row H and second row E\n    :param I:\n    :param threshold:\n    :param lamda:\n    :return:\n    \"\"\"\n    mask = notwhite_mask(I, thresh=threshold).reshape((-1,))\n    OD = RGB_to_OD(I).reshape((-1, 3))\n    OD = OD[mask]\n    dictionary = spams.trainDL(OD.T, K=2, lambda1=lamda, mode=2, modeD=0, posAlpha=True, posD=True, verbose=False).T\n    if dictionary[0, 0] < dictionary[1, 0]:\n        dictionary = dictionary[[1, 0], :]\n    dictionary = normalize_rows(dictionary)\n    return dictionary\n\n##########################\n\nclass normalizer(object):\n    \"\"\"\n    A stain normalization object\n    \"\"\"\n\n    def __init__(self):\n        self.stain_matrix_target = None\n\n    def fit(self, target):\n        target = standardize_brightness(target)\n        self.stain_matrix_target = get_stain_matrix(target)\n\n    def target_stains(self):\n        return OD_to_RGB(self.stain_matrix_target)\n\n    def transform(self, I):\n        I = standardize_brightness(I)\n        stain_matrix_source = get_stain_matrix(I)\n        source_concentrations = get_concentrations(I, stain_matrix_source)\n        return (255 * np.exp(-1 * np.dot(source_concentrations, self.stain_matrix_target).reshape(I.shape))).astype(\n            np.uint8)\n\n    def hematoxylin(self, I):\n        I = standardize_brightness(I)\n        h, w, c = I.shape\n        stain_matrix_source = get_stain_matrix(I)\n        source_concentrations = get_concentrations(I, stain_matrix_source)\n        H = source_concentrations[:, 0].reshape(h, w)\n        H = np.exp(-1 * H)\n        return H\n\n\ndef assure_path_exists(path):\n    dir = os.path.dirname(path)\n    if not os.path.exists(dir):\n        try:\n            os.makedirs(dir)\n        except OSError as e:\n            if e.errno != errno.EEXIS:\n                raise\n\ndef transform_img(image):\n    i1 = read_image_cv('../input/stain-normalize-image/mosaic.png') # ../input/stain-normalize-image/mosaic.png #\n    n = normalizer()\n    n.fit(i1)\n    t_image = n.transform(image)\n    \n    return t_image\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:48.187485Z","iopub.execute_input":"2022-09-07T20:35:48.187927Z","iopub.status.idle":"2022-09-07T20:35:48.224053Z","shell.execute_reply.started":"2022-09-07T20:35:48.187890Z","shell.execute_reply":"2022-09-07T20:35:48.223028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Model**","metadata":{}},{"cell_type":"code","source":"from transformers import SegformerForSemanticSegmentation\nimport json\nfrom huggingface_hub import cached_download, hf_hub_url\nimport torch\nfrom torch import nn\nfrom sklearn.metrics import accuracy_score\nfrom tqdm.notebook import tqdm\nfrom datasets import load_metric\n\ndef build_model():\n\n    model = SegformerForSemanticSegmentation.from_pretrained(\"../input/segformer-mit-b2\",num_labels=1,ignore_mismatched_sizes=True)    \n    return model\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:48.225917Z","iopub.execute_input":"2022-09-07T20:35:48.226736Z","iopub.status.idle":"2022-09-07T20:35:48.239465Z","shell.execute_reply.started":"2022-09-07T20:35:48.226696Z","shell.execute_reply":"2022-09-07T20:35:48.238186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Dataset**","metadata":{}},{"cell_type":"code","source":"#Imagenet \n# mean = np.array([0.485,0.456,0.406])\n# std = np.array([0.229,0.224,0.225])\n\n# Mean and std of the training dataset R,G,B 768 size\n# mean_t = np.array([0.82784239, 0.80224416, 0.82012869]) #0.82784239, 0.80224415, 0.82012868\n# std_t  = np.array([0.1534639,  0.17716865, 0.15953616]) #0.15346391, 0.17716866, 0.15953616\n\n#Mean and std of test image R,G,B 768 size\n# mean = np.array([0.68457557, 0.38944713, 0.52313348])\n# std  = np.array([0.17411125, 0.20695598, 0.18981366])\n\n\nclass HuBMAPDataset(Dataset):\n    def __init__(self, idx,transforms = True):\n        self.data = os.path.join(DATA,idx+'.tiff')   \n\n        self.originalshape = cv2.cvtColor(cv2.imread(self.data), cv2.COLOR_BGR2RGB).shape[:-1]\n        self.transforms = transforms\n        self.ds  = pd.read_csv('../input/hubmap-organ-segmentation/test.csv') # TRIAL pd.read_csv('../input/random-testcsv/sample_test.csv')\n\n        self.organ = self.ds[self.ds['id']==int(idx)]['organ'].tolist()\n        self.data_src = self.ds[self.ds['id']==int(idx)]['data_source'].tolist()\n        self.pixel_size = self.ds[self.ds['id']==int(idx)]['pixel_size'].tolist()\n        print('Init pixel size: ',self.pixel_size)\n        \n        \n    def __len__(self):\n        return 1\n    \n    def __getitem__(self,idx):\n        \n        try:\n            organ      = self.organ[0]\n            data_src   = self.data_src[0]\n            image_size = cfg.img_size[0]\n            pixel_size = self.pixel_size[0]\n            print('Self pixel size: ',pixel_size)\n            # ------------Read Image------------------------\n            image = cv2.cvtColor(cv2.imread(self.data), cv2.COLOR_BGR2RGB) \n            H,W,_ = image.shape\n            print('H w ',H,W)\n#            # ------------Stain normalise-------------------\n            image = transform_img(image)\n                \n            # Resize and normalise pixel values \n            '''\n            Trained on 768x768, what happens when inference on different size?\n            \n            As model is based on Transformers/Attention then you no longer have translation equivariance, and the receptive field of your network becomes the entire input. \n            Thus, changing the input size will result in a different prediction.\n            '''\n#             print('b4 s')\n#             s = pixel_size/0.4 * (image_size/3000)\n#             print('S is:',s)\n#             h = int(np.ceil(int(H*s)/32)*32)\n#             w = int(np.ceil(int(W*s)/32)*32) \n            \n# #             if H <= 1536:            \n#             image = cv2.resize(image,dsize=(w,h),interpolation=cv2.INTER_CUBIC)\n            \n#             else:\n#                 image = cv2.resize(image,dsize=(image_size,image_size),interpolation=cv2.INTER_CUBIC)\n            \n            image = cv2.resize(image,dsize=(image_size,image_size),interpolation=cv2.INTER_LINEAR)\n            \n            image = image.astype(np.float32)/255\n                        \n            # ----------------Santiy check------------------\n            print('4 - Shapes: img',image.shape,image.dtype)\n#             plt.imshow(image)  \n            # ----------------------------------------------\n\n            image = np.transpose(image, (2, 0, 1))\n\n            return torch.torch.from_numpy(image), idx, organ, data_src\n           \n        except:\n            print(idx,\"error occured in dataloader/dataset\")\n            return None\n            ","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:48.241411Z","iopub.execute_input":"2022-09-07T20:35:48.241793Z","iopub.status.idle":"2022-09-07T20:35:48.256512Z","shell.execute_reply.started":"2022-09-07T20:35:48.241748Z","shell.execute_reply":"2022-09-07T20:35:48.255379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn.functional as Fx\n\n\nclass MixUpSample(nn.Module):\n    def __init__( self, scale_factor=4, originalshape = True):\n        super().__init__()\n        self.mixing = nn.Parameter(torch.tensor(0.5))\n        self.scale_factor = scale_factor\n        self.originalshape = originalshape\n        \n    def forward(self, x, originalshape):\n        \n        if self.originalshape:\n\n            x = self.mixing *Fx.interpolate(x, size = originalshape,mode='bilinear', align_corners=False) \\\n                + (1-self.mixing )*Fx.interpolate(x, size = originalshape, mode='nearest')\n\n            return x\n\n#         else:\n            \n#             x = self.mixing *Fx.interpolate(x, scale_factor=self.scale_factor, mode='bilinear', align_corners=False) \\\n#                 + (1-self.mixing )*Fx.interpolate(x, scale_factor=self.scale_factor, mode='nearest')\n#             return x\n#         print('before reutrn',x.shape)\n        \n    \n","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:48.258081Z","iopub.execute_input":"2022-09-07T20:35:48.258545Z","iopub.status.idle":"2022-09-07T20:35:48.271590Z","shell.execute_reply.started":"2022-09-07T20:35:48.258507Z","shell.execute_reply":"2022-09-07T20:35:48.270566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Inference**","metadata":{}},{"cell_type":"code","source":"import torchvision.transforms.functional as F\n\n# Global initilaization \ndf_sample   =  pd.read_csv(\"../input/hubmap-organ-segmentation/sample_submission.csv\") # TRIAL pd.read_csv('../input/random-testcsv/trial_sub.csv') \nDATA        =  '../input/hubmap-organ-segmentation/test_images/'                       # TRIAL '../input/random-testcsv/sample_test' \n\nMODELS      =['../input/train-mit-b2-segformer/segformer_mit-b2_1024_lr-1e-4_200ep-49.bin'] #[ f'../input/train-mit-b2-segformer/segformer_mit-b2_768__best_epoch-00-epoch-57.bin'] # ../input/segformer-mitb2-stain-norm-run/segformer_mit-b2_768_stain-data_best_epoch-00-epoch-46.bin\nTH_default  = 0.225 \ndevice      = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ncfg         = initialize_config()   # Image resize based on training config\n\nnames,preds = [],[]\nupsample_obj = MixUpSample()    \n\n# Load the model\nmodels = []\nfor path in MODELS:\n    state_dict = torch.load(path,map_location=torch.device('cpu'))\n    model = build_model()\n    model.load_state_dict(state_dict)\n    model.float()\n    model.eval()\n    model.to(device)\n    models.append(model)\n\ndel state_dict\n\n# Iterate over the test.csv row and predict masks on each sample\n\nfor idx,row in tqdm(df_sample.iterrows(),total=len(df_sample)):\n    TH = TH_default # default threshold\n\n    idx = str(row['id'])\n    print('id: ',idx)\n    ds = HuBMAPDataset(idx, transforms=False)\n    dl = DataLoader(ds,batch_size=1,num_workers=0,shuffle=False,pin_memory=True)\n    print('-------------')\n\n    mask = None #torch.zeros(1,size=ds.originalshape,dtype=torch.int8)\n    with torch.no_grad():\n            for x,y,organ,data_src in iter(dl): # image, index, organ, datasource\n\n                    organ_name = organ[0]\n                    data_src = data_src[0]\n                    print('Data Source: ',data_src)\n                    print('Organ is:', organ_name)\n\n                    # ---------------Santiy check---------------\n                    plot_img = (Fx.interpolate(x, size = ds.originalshape, mode='nearest')).squeeze().permute(1,2,0).detach().float().cpu()\n#                     plt.imshow(plot_img)\n                    # ------------------------------------------\n\n                    x = x.to(device)                        \n                    py = None\n\n                    outputs = model(x)\n                    p = outputs.logits\n                    p=torch.sigmoid(p)\n                    print('p shape b4:',p.shape)\n#                     p = upsample_obj(p,originalshape = ds.originalshape)\n                    \n                    p = Fx.interpolate(p, size = ds.originalshape, mode='nearest')\n                    print('p shape after:',p.shape)\n                    \n                    p = p.detach()\n\n                    py = p.permute(0,2,3,1).float().cpu()\n\n                    if data_src == 'HPA':\n\n                        if organ_name == 'kidney':\n                            TH = 0.22\n                        elif organ_name == 'largeintestine':\n                            TH = 0.4\n                        elif organ_name == 'lung':\n                            TH = 0.15\n                        elif organ_name == 'prostate':\n                            TH = 0.4\n                        elif organ_name == 'spleen':\n                            TH = 0.4\n\n                    else: #Hubmap\n                        \n                        if organ_name == 'kidney':\n                            TH = 0.3\n                        elif organ_name == 'largeintestine':\n                            TH = 0.3\n                        elif organ_name == 'lung':\n                            TH = 0.1\n                        elif organ_name == 'prostate':\n                            TH = 0.3\n                        elif organ_name == 'spleen':\n                            TH = 0.3\n#                         TH = TH_default # default threshold (redundant)\n\n                    print('TH is..',TH)\n                    mask = py.squeeze().squeeze() > TH\n\n    mask = mask.numpy()\n    \n    plt.figure(figsize = (15,15))\n    plt.subplot(1, 3, 1)\n    plt.imshow(plot_img, interpolation='none')\n    plt.subplot(1, 3, 2)\n    plt.imshow(mask, cmap = 'gray')\n    plt.subplot(1, 3, 3)\n    plt.imshow(plot_img, 'gray', interpolation='none')\n    plt.imshow(mask, 'jet', interpolation='none', alpha=0.7)\n    \n    plt.show()\n    \n    print('mask: ',mask.shape)\n    print(\"1 Len: \",len(mask[np.where(mask == 1)]))\n    print('---------------Done---------------')\n\n    # RLE encode predictions\n    #https://www.kaggle.com/bguberfain/memory-aware-rle-encoding\n    rle = rle_encode_less_memory(mask)\n    names.append(idx)\n    preds.append(rle)\n    del mask, ds, dl\n    gc.collect()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:48.273815Z","iopub.execute_input":"2022-09-07T20:35:48.274217Z","iopub.status.idle":"2022-09-07T20:35:56.918058Z","shell.execute_reply.started":"2022-09-07T20:35:48.274182Z","shell.execute_reply":"2022-09-07T20:35:56.917042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CREATE SUBMISSION FILE\n\ndf = pd.DataFrame({'id':names,'rle':preds})\ndf.to_csv('submission.csv',index=False)\nprint('Submission file: ')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:56.919550Z","iopub.execute_input":"2022-09-07T20:35:56.920444Z","iopub.status.idle":"2022-09-07T20:35:56.936220Z","shell.execute_reply.started":"2022-09-07T20:35:56.920400Z","shell.execute_reply":"2022-09-07T20:35:56.935329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **TRAIN DATA TRIALS**","metadata":{}},{"cell_type":"code","source":"# idx = '1229' # prostate '1229'# LI '12471'# spleen'10610'# lung \"11064\"\n# img = cv2.cvtColor(cv2.imread(f\"../input/hubmap-hpa-2022-png-dataset/train_images_png/{idx}.png\"),cv2.COLOR_BGR2RGB)\n#  # ------------Stain normalise-------------------\n# img = transform_img(img)\n# img = cv2.resize(img,(1024,1024),cv2.INTER_LINEAR)\n# mask =io.imread(f\"../input/hubmap-hpa-2022-png-dataset/train_masks_png/{idx}.png\")\n# #spleen -> 0.15 0.3,0.999999, largeintestine -> 0.999 #prostate -> 0.8 #kidney -> 0.3\n\n# img = img.astype(np.float32)/255\n# plt.imshow(img)\n# img = np.transpose(img,(2,0,1))#/255\n# img = torch.tensor(img).unsqueeze(0).to(dtype = torch.float)\n# img.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:56.939670Z","iopub.execute_input":"2022-09-07T20:35:56.940232Z","iopub.status.idle":"2022-09-07T20:35:56.945042Z","shell.execute_reply.started":"2022-09-07T20:35:56.940201Z","shell.execute_reply":"2022-09-07T20:35:56.943738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(mask,cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:56.946447Z","iopub.execute_input":"2022-09-07T20:35:56.947481Z","iopub.status.idle":"2022-09-07T20:35:56.960943Z","shell.execute_reply.started":"2022-09-07T20:35:56.947439Z","shell.execute_reply":"2022-09-07T20:35:56.959554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = models[0]\n# outputs = model(img.to('cuda'))\n# p = outputs.logits\n# print('Output logits:')\n# print(p)\n# print('======================================')\n# p = torch.sigmoid(p)\n# print(p)\n# pred = p > 0.3\n\n# pred = pred.squeeze().squeeze().detach().cpu()\n# pred = pred.numpy()\n# plt.imshow(pred,cmap = 'gray')\n# del outputs ,p, pred","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:56.962420Z","iopub.execute_input":"2022-09-07T20:35:56.963162Z","iopub.status.idle":"2022-09-07T20:35:56.971531Z","shell.execute_reply.started":"2022-09-07T20:35:56.963115Z","shell.execute_reply":"2022-09-07T20:35:56.970316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = models[0]\n# outputs = model(img.to('cuda'))\n# p = outputs.logits\n# print('Output logits:')\n# print(p)\n# print('======================================')\n# p = torch.sigmoid(p)\n# pred = p > 0.8\n# pred = pred.squeeze().squeeze().detach().cpu()\n# pred = pred.numpy()\n# plt.imshow(pred,cmap = 'gray')\n# del outputs, p, pred","metadata":{"execution":{"iopub.status.busy":"2022-09-07T20:35:56.973029Z","iopub.execute_input":"2022-09-07T20:35:56.973737Z","iopub.status.idle":"2022-09-07T20:35:56.982533Z","shell.execute_reply.started":"2022-09-07T20:35:56.973698Z","shell.execute_reply":"2022-09-07T20:35:56.981494Z"},"trusted":true},"execution_count":null,"outputs":[]}]}