{"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":"LOCAL = False\nif LOCAL:\n    !pip install segmentation_models_pytorch\n    !pip install timm\nelse:\n    !pip install ../input/segmentation-models-pytorch-021/wheels/pretrainedmodels-0.7.4-py3-none-any.whl\n    !pip install ../input/segmentation-models-pytorch-021/wheels/timm-0.4.12-py3-none-any.whl\n    !pip install ../input/segmentation-models-pytorch-021/wheels/efficientnet_pytorch-0.6.3-py3-none-any.whl\n    !pip install ../input/segmentation-models-pytorch-021/wheels/segmentation_models_pytorch-0.2.1-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:51:08.280034Z","iopub.execute_input":"2022-09-10T15:51:08.280577Z","iopub.status.idle":"2022-09-10T15:52:58.462033Z","shell.execute_reply.started":"2022-09-10T15:51:08.280485Z","shell.execute_reply":"2022-09-10T15:52:58.460992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import tensorflow as tf; print(tf.__version__)\n# # 2.4.1\n# # 2.6.4 ","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:52:58.466148Z","iopub.execute_input":"2022-09-10T15:52:58.466399Z","iopub.status.idle":"2022-09-10T15:52:58.473463Z","shell.execute_reply.started":"2022-09-10T15:52:58.46637Z","shell.execute_reply":"2022-09-10T15:52:58.472614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install tensorflow==2.4.1","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:52:58.474928Z","iopub.execute_input":"2022-09-10T15:52:58.475304Z","iopub.status.idle":"2022-09-10T15:52:58.481839Z","shell.execute_reply.started":"2022-09-10T15:52:58.475271Z","shell.execute_reply":"2022-09-10T15:52:58.481078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/hubmapsegformerb5/tokenizers-0.12.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:52:58.483981Z","iopub.execute_input":"2022-09-10T15:52:58.484398Z","iopub.status.idle":"2022-09-10T15:53:26.313399Z","shell.execute_reply.started":"2022-09-10T15:52:58.484364Z","shell.execute_reply":"2022-09-10T15:53:26.312534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/hubmapsegformerb5/huggingface_hub-0.9.1-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:53:26.315253Z","iopub.execute_input":"2022-09-10T15:53:26.315586Z","iopub.status.idle":"2022-09-10T15:53:53.915606Z","shell.execute_reply.started":"2022-09-10T15:53:26.315539Z","shell.execute_reply":"2022-09-10T15:53:53.914723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/hubmapsegformerb5/transformers-4.21.2-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:53:53.917309Z","iopub.execute_input":"2022-09-10T15:53:53.917671Z","iopub.status.idle":"2022-09-10T15:54:26.791005Z","shell.execute_reply.started":"2022-09-10T15:53:53.917599Z","shell.execute_reply":"2022-09-10T15:54:26.79004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install transformers==4.21.2\n# !pip install --force-reinstall ../input/hubmapsegformerb5/transformers-4.21.2-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.792567Z","iopub.execute_input":"2022-09-10T15:54:26.79293Z","iopub.status.idle":"2022-09-10T15:54:26.798338Z","shell.execute_reply.started":"2022-09-10T15:54:26.792871Z","shell.execute_reply":"2022-09-10T15:54:26.797409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Efficientnet-B7","metadata":{}},{"cell_type":"code","source":"%%writefile efficient-b7.py\nimport segmentation_models_pytorch as smp\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nimport numpy as np\nimport cv2\nimport pandas as pd\nimport os\nimport tifffile\nfrom tifffile import TiffFile\nimport matplotlib.pyplot as plt\nimport gc\nfrom tqdm import tqdm\nimport rasterio\nfrom rasterio.windows import Window\nimport torch.nn.functional as F\nimport warnings; warnings.filterwarnings(\"ignore\")\nimport math","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.800105Z","iopub.execute_input":"2022-09-10T15:54:26.800493Z","iopub.status.idle":"2022-09-10T15:54:26.80991Z","shell.execute_reply.started":"2022-09-10T15:54:26.800455Z","shell.execute_reply":"2022-09-10T15:54:26.808999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\nclass config:\n    BATCH_SIZE = 1\n    TRAIN_SIZE = 256\n    TRAIN_PIXEL_SIZE = 0.4\n    VALID = False\n    FOLDS = [0, 1, 2, 3]\n    organ_type = {'lung':1, 'kidney':2, 'largeintestine':3, 'prostate':4, 'spleen':5}\n    TH = {'lung':0.2, 'kidney':0.5, 'largeintestine':0.5, 'prostate':0.5, 'spleen':0.5}\n    \n    ARCH = 'unetplusplus'\n    BACKBONE = 'efficientnet-b7'\n    WEIGHT = '../input/hubmap-2022-models/test/fold_0/02_PostTrain/Models/'\n    \n    ### TEST ###\n    TEST_CSV = '../input/hubmap-organ-segmentation/'\n    TEST_DATA = '../input/hubmap-organ-segmentation/test_images/'\n    \n    ### VALID ###\n    VALID_CSV = '../input/hubmap-my-misc/'\n    VALID_DATA = '../input/hubmap-organ-segmentation/train_images/'\n    NUM_WORKERS = 2","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.81181Z","iopub.execute_input":"2022-09-10T15:54:26.812424Z","iopub.status.idle":"2022-09-10T15:54:26.818863Z","shell.execute_reply.started":"2022-09-10T15:54:26.812389Z","shell.execute_reply":"2022-09-10T15:54:26.818126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\nif config.VALID:\n    IMG_DATA = '../input/hubmap-organ-segmentation/train_images/'\n    CSV_DATA = []\n    for fold in config.FOLDS:\n        CSV_DATA.append(config.VALID_CSV + f'fold_{fold}_valid.csv')\nelse:\n    IMG_DATA = '../input/hubmap-organ-segmentation/test_images/'\n    CSV_DATA = [config.TEST_CSV + 'test.csv']","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.823199Z","iopub.execute_input":"2022-09-10T15:54:26.823919Z","iopub.status.idle":"2022-09-10T15:54:26.831419Z","shell.execute_reply.started":"2022-09-10T15:54:26.823875Z","shell.execute_reply":"2022-09-10T15:54:26.830604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\nbs = 64\nsz = 256    # the size of tiles\nreduce = 4  # reduce the original images by 4 times\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.832644Z","iopub.execute_input":"2022-09-10T15:54:26.833202Z","iopub.status.idle":"2022-09-10T15:54:26.840337Z","shell.execute_reply.started":"2022-09-10T15:54:26.83317Z","shell.execute_reply":"2022-09-10T15:54:26.83932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\n# 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 = encs.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\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-10T15:54:26.842181Z","iopub.execute_input":"2022-09-10T15:54:26.842955Z","iopub.status.idle":"2022-09-10T15:54:26.848987Z","shell.execute_reply.started":"2022-09-10T15:54:26.842922Z","shell.execute_reply":"2022-09-10T15:54:26.848135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\n# https://www.kaggle.com/datasets/thedevastator/hubmap-2022-256x256\nmean = np.array([0.7720342, 0.74582646, 0.76392896])\nstd = np.array([0.24745085, 0.26182273, 0.25782376])\n\ns_th = 40  #saturation blancking threshold\np_th = 1000*(sz//256)**2 #threshold for the minimum number of pixels\nidentity = rasterio.Affine(1, 0, 0, 0, 1, 0)\n\ndef img2tensor(img,dtype:np.dtype=np.float32):\n    if img.ndim==2 : img = np.expand_dims(img,2)\n    img = np.transpose(img,(2,0,1))\n    return torch.from_numpy(img.astype(dtype, copy=False))\n\nclass HuBMAPDataset(Dataset):\n    def __init__(self, idx, sz=sz, reduce=reduce, pixel_size = 0.4):\n        \n        with TiffFile(os.path.join(IMG_DATA,idx+'.tiff')) as tif:\n            data = tif.asarray()\n        \n        scale = pixel_size / 0.4\n        w = int(data.shape[1] * scale)\n        h = int(data.shape[0] * scale)\n        data = cv2.resize(data, (w, h), interpolation = cv2.INTER_AREA)\n        tifffile.imsave(idx+'.tiff', data)\n        self.data = rasterio.open(idx+'.tiff', transform = identity,\n                            num_threads='all_cpus')\n        \"\"\"\n\n        self.data = rasterio.open(os.path.join(IMG_DATA,idx+'.tiff'), transform = identity,\n                            num_threads='all_cpus')\n        \"\"\"\n        # some images have issues with their format \n        # and must be saved correctly before reading with rasterio\n        if self.data.count != 3:\n            subdatasets = self.data.subdatasets\n            self.layers = []\n            if len(subdatasets) > 0:\n                for i, subdataset in enumerate(subdatasets, 0):\n                    self.layers.append(rasterio.open(subdataset))\n        self.shape = self.data.shape\n        self.reduce = reduce\n        self.sz = reduce*sz\n        self.hsz = self.sz // 2\n        \n        self.n0max = math.ceil(self.shape[0] / self.hsz) + 1\n        self.n1max = math.ceil(self.shape[1] / self.hsz) + 1\n        \n        os.remove(idx+'.tiff')\n        \n    def __len__(self):\n        return self.n0max*self.n1max\n    \n    def __getitem__(self, idx):\n        # the code below may be a little bit difficult to understand,\n        # but the thing it does is mapping the original image to\n        # tiles created with adding padding, as done in\n        # https://www.kaggle.com/iafoss/256x256-images ,\n        # and then the tiles are loaded with rasterio\n        # n0,n1 - are the x and y index of the tile (idx = n0*self.n1max + n1)\n        n0,n1 = idx//self.n1max, idx%self.n1max\n        # x0,y0 - are the coordinates of the lower left corner of the tile in the image\n        # negative numbers correspond to padding (which must not be loaded)\n        x0,y0 = -self.hsz + n0*self.hsz, -self.hsz + n1*self.hsz\n        # make sure that the region to read is within the image\n        p00,p01 = max(0,x0), min(x0+self.sz,self.shape[0])\n        p10,p11 = max(0,y0), min(y0+self.sz,self.shape[1])\n        img = np.zeros((self.sz,self.sz,3),np.uint8)\n        # mapping the loade region to the tile\n        if self.data.count == 3:\n            img[(p00-x0):(p01-x0),(p10-y0):(p11-y0)] = np.moveaxis(self.data.read([1,2,3],\n                window=Window.from_slices((p00,p01),(p10,p11))), 0, -1)\n        else:\n            for i,layer in enumerate(self.layers):\n                img[(p00-x0):(p01-x0),(p10-y0):(p11-y0),i] =\\\n                  layer.read(1,window=Window.from_slices((p00,p01),(p10,p11)))\n        \n        if self.reduce != 1:\n            img = cv2.resize(img,(self.sz//reduce,self.sz//reduce),\n                             interpolation = cv2.INTER_AREA)\n        #check for empty imges\n        \"\"\"\n        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n        h,s,v = cv2.split(hsv)\n        if (s>s_th).sum() <= p_th or img.sum() <= p_th:\n            #images with -1 will be skipped\n            return img2tensor((img/255.0 - mean)/std), -1\n        else:\n            return img2tensor((img/255.0 - mean)/std), idx\n        \"\"\"\n        return img2tensor((img/255.0 - mean)/std), idx","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.850648Z","iopub.execute_input":"2022-09-10T15:54:26.851123Z","iopub.status.idle":"2022-09-10T15:54:26.862812Z","shell.execute_reply.started":"2022-09-10T15:54:26.851091Z","shell.execute_reply":"2022-09-10T15:54:26.862086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\n#iterator like wrapper that returns predicted masks\nclass Model_pred:\n    def __init__(self, models, dl, tta:bool=True, half:bool=False):\n        self.models = models\n        self.dl = dl\n        self.tta = tta\n        self.half = half\n        \n    def __iter__(self):\n        count=0\n        with torch.no_grad():\n            for x,y in iter(self.dl):\n                if ((y>=0).sum() > 0): #exclude empty images\n                    x = x[y>=0].to(device)\n                    y = y[y>=0]\n                    if self.half: x = x.half()\n                    py = None\n                    for model in self.models:\n                        p = model(x)\n                        p = torch.sigmoid(p).detach()\n                        tmp = p\n                        #if py is None: py = p\n                        #else: py += p\n                        #else: py = torch.fmax(py, p)\n                        if self.tta:\n                            #x,y,xy flips as TTA\n                            flips = [[-1],[-2],[-2,-1]]\n                            for f in flips:\n                                xf = torch.flip(x,f)\n                                p = model(xf)\n                                p = torch.flip(p,f)\n                                #py += torch.sigmoid(p).detach()\n                                #py = torch.fmax(torch.sigmoid(p).detach(), p)\n                                tmp += torch.sigmoid(p).detach()\n                        #py /= (1+len(flips))        \n                        tmp /= (1+len(flips))\n                        if py is None: py = tmp.cpu().numpy()\n                        else: py = np.fmax(py, tmp.cpu().numpy())\n                    #py /= len(self.models)\n\n                    py = F.upsample(torch.from_numpy(py), scale_factor=reduce, mode=\"bilinear\")\n                    py = py.permute(0,2,3,1).float().cpu()\n                    \n                    batch_size = len(py)\n                    for i in range(batch_size):\n                        yield py[i],y[i]\n                        count += 1\n                    \n    def __len__(self):\n        return len(self.dl.dataset)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.864144Z","iopub.execute_input":"2022-09-10T15:54:26.864599Z","iopub.status.idle":"2022-09-10T15:54:26.876497Z","shell.execute_reply.started":"2022-09-10T15:54:26.864566Z","shell.execute_reply":"2022-09-10T15:54:26.87558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\ndef compute_dice_score(probability, mask, organ):\n    N = len(probability.flatten())\n    p = probability.reshape(N)\n    t = mask.reshape(N)\n\n    p = p>config.TH[organ]\n    t = t>0.00\n    uion = p.sum(-1) + t.sum(-1)\n    overlap = (p*t).sum(-1)\n    dice = 2*overlap/(uion+0.0001)\n    return dice\n\n# dice with automatic threshold selection\nclass Dice_th():\n    def __init__(self, ths=np.arange(0.0,1.0,0.05), axis=1): \n        self.axis = axis\n        self.ths = ths\n        \n    def reset(self): \n        self.inter = torch.zeros(len(self.ths))\n        self.union = torch.zeros(len(self.ths))\n        \n    def accumulate(self, prob, mask):\n        N = len(prob)\n        pred = prob.reshape(N,-1)\n        targ = mask.reshape(N,-1)\n        \n        for i,th in enumerate(self.ths):\n            p = (pred > th).float()\n            self.inter[i] += (p*targ).float().sum().item()\n            self.union[i] += (p+targ).float().sum().item()\n\n    @property\n    def value(self):\n        dices = torch.where(self.union > 0.0, \n                2.0*self.inter/self.union, torch.zeros_like(self.union))\n        return dices.max()\n\nclass Dice_th_pred():\n    def __init__(self, ths=np.arange(0.0,1.0,0.05), axis=1): \n        self.axis = axis\n        self.ths = ths\n        self.reset()\n        \n    def reset(self): \n        self.inter = torch.zeros(len(self.ths))\n        self.union = torch.zeros(len(self.ths))\n        \n    def accumulate(self, prob, mask):\n        N = len(prob)\n        pred = prob.reshape(N,-1)\n        targ = mask.reshape(N,-1)\n        for i,th in enumerate(self.ths):\n            p = (pred > th).float()\n            self.inter[i] += (p*targ).float().sum().item()\n            self.union[i] += (p+targ).float().sum().item()\n\n    @property\n    def value(self):\n        dices = torch.where(self.union > 0.0, 2.0*self.inter/self.union, \n                            torch.zeros_like(self.union))\n        return dices","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.878131Z","iopub.execute_input":"2022-09-10T15:54:26.878497Z","iopub.status.idle":"2022-09-10T15:54:26.888857Z","shell.execute_reply.started":"2022-09-10T15:54:26.878399Z","shell.execute_reply":"2022-09-10T15:54:26.887996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\ndef getModels():\n    MODELS = []\n    for fold in config.FOLDS:\n        model = smp.create_model(arch=config.ARCH, encoder_weights=None, encoder_name=config.BACKBONE, classes=1, activation=None)\n        #model_name = config.WEIGHT + f'model_fold{fold}_{config.BACKBONE}_109.pth'\n        model_name = f'../input/hubmap-2022-models/model_fold{fold}_efficientnet-b7_dice_score.pth'\n        state_dict = torch.load(model_name,map_location=torch.device('cpu'))\n        model.load_state_dict(state_dict)\n        model.float()\n        model.eval()\n        model.to(device)\n        MODELS.append(model)\n        \n    return MODELS\n\ndef getValidData(fold=0):\n    ds_v = HuBMAPDataset(fold=fold, train=False)\n    v_dataloader = DataLoader(dataset=ds_v, batch_size=config.bs, shuffle=False, num_workers=config.NUM_WORKERS)\n    \n    return v_dataloader","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.890421Z","iopub.execute_input":"2022-09-10T15:54:26.890932Z","iopub.status.idle":"2022-09-10T15:54:26.900594Z","shell.execute_reply.started":"2022-09-10T15:54:26.890884Z","shell.execute_reply":"2022-09-10T15:54:26.899562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\ndef margeMask(ds, mask):\n\n    mmask = torch.zeros(ds.hsz * (ds.n0max + 1),ds.hsz * (ds.n1max + 1),dtype=torch.float32)\n    for h in range(ds.n0max):\n        for w in range(ds.n1max):\n            sy = h*ds.hsz\n            sx = w*ds.hsz\n            mmask[sy:sy+ds.sz, sx:sx+ds.sz] += mask[h, w, :,:]\n\n    return mmask[ds.hsz:ds.hsz+ds.shape[0],ds.hsz:ds.hsz+ds.shape[1]] / 4.0","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.90217Z","iopub.execute_input":"2022-09-10T15:54:26.902601Z","iopub.status.idle":"2022-09-10T15:54:26.912217Z","shell.execute_reply.started":"2022-09-10T15:54:26.902569Z","shell.execute_reply":"2022-09-10T15:54:26.911353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\nmodels = getModels()\nnames,preds = [],[]\n\nif config.VALID:\n    scores = {}\n    metric = Dice_th_pred()\n    metric.reset()\n\n    organ_score = {}\n    for key in config.organ_type:\n        organ_score[key] = Dice_th_pred()\n        organ_score[key].reset()\n\nfor csv in CSV_DATA:\n    df = pd.read_csv(csv)\n    for idx,row in tqdm(df.iterrows(),total=len(df)):\n        idx = str(row['id'])\n        ds = HuBMAPDataset(idx, pixel_size=row['pixel_size'])\n        #rasterio cannot be used with multiple workers\n        dl = DataLoader(ds,bs,num_workers=0,shuffle=False,pin_memory=True)\n        mp = Model_pred(models,dl)\n        #generate masks\n        mask = torch.zeros(len(ds),ds.sz,ds.sz,dtype=torch.float32)\n        #for p,i in iter(mp): mask[i.item()] = p.squeeze(-1) > TH\n        for p,i in iter(mp): mask[i.item()] = p.squeeze(-1)\n        \n        #reshape tiled masks into a single mask and crop padding\n        mask = mask.view(ds.n0max,ds.n1max,ds.sz,ds.sz)\n        mask = margeMask(ds, mask)\n        \n        mask = torch.reshape(mask, (1,1, mask.shape[1], mask.shape[0]))\n        mask = F.interpolate(mask, (row['img_height'],row['img_width']), mode='bilinear', align_corners=False)\n        \n        #convert to rle\n        #https://www.kaggle.com/bguberfain/memory-aware-rle-encoding\n        rle = rle_encode_less_memory((mask > config.TH[row['organ']]).to(torch.uint8).numpy())\n        names.append(idx)\n        preds.append(rle)\n\n        \n        if config.VALID:\n            label = img2tensor(enc2mask(row['rle'], (row['img_width'], row['img_height'])))\n            score = compute_dice_score(mask, label, row['organ'])\n            tmp = scores.get(row['organ'], [])\n            tmp.append(score.item())\n            scores[row['organ']] = tmp\n            metric.accumulate(mask, label)\n            organ_score[row['organ']].accumulate(mask, label)\n        \n        del mask, ds, dl\n        gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.913713Z","iopub.execute_input":"2022-09-10T15:54:26.914552Z","iopub.status.idle":"2022-09-10T15:54:26.922211Z","shell.execute_reply.started":"2022-09-10T15:54:26.914495Z","shell.execute_reply":"2022-09-10T15:54:26.921341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficient-b7.py\ndf = pd.DataFrame({'id':names,'rle':preds})\ndf.to_csv('submission3.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.923526Z","iopub.execute_input":"2022-09-10T15:54:26.924091Z","iopub.status.idle":"2022-09-10T15:54:26.933307Z","shell.execute_reply.started":"2022-09-10T15:54:26.924058Z","shell.execute_reply":"2022-09-10T15:54:26.932399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 efficient-b7.py","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:54:26.934684Z","iopub.execute_input":"2022-09-10T15:54:26.935185Z","iopub.status.idle":"2022-09-10T15:55:07.926346Z","shell.execute_reply.started":"2022-09-10T15:54:26.935152Z","shell.execute_reply":"2022-09-10T15:55:07.925483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SegFormer","metadata":{}},{"cell_type":"code","source":"# from transformers import SegformerForSemanticSegmentation\nimport transformers\ntransformers.__version__\n# 4.5.1\n# 4.20.1","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:07.927944Z","iopub.execute_input":"2022-09-10T15:55:07.928234Z","iopub.status.idle":"2022-09-10T15:55:08.228198Z","shell.execute_reply.started":"2022-09-10T15:55:07.928195Z","shell.execute_reply":"2022-09-10T15:55:08.227497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile segformer.py\n\n\"\"\"\nLovasz-Softmax and Jaccard hinge loss in PyTorch\nMaxim Berman 2018 ESAT-PSI KU Leuven (MIT License)\n\"\"\"\nimport torch\nfrom torch.autograd import Variable\nimport torch.nn.functional as F\nimport torch.nn as nn\nimport numpy as np\ntry:\n    from itertools import  ifilterfalse\nexcept ImportError: # py3k\n    from itertools import  filterfalse","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.2293Z","iopub.execute_input":"2022-09-10T15:55:08.229542Z","iopub.status.idle":"2022-09-10T15:55:08.236165Z","shell.execute_reply.started":"2022-09-10T15:55:08.22951Z","shell.execute_reply":"2022-09-10T15:55:08.235302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nimport os\nimport gc\nimport cv2\nimport rasterio\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom tqdm import tqdm\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\nfrom rasterio.windows import Window\nfrom torch.utils.data import Dataset, DataLoader\nimport warnings; warnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.237276Z","iopub.execute_input":"2022-09-10T15:55:08.237997Z","iopub.status.idle":"2022-09-10T15:55:08.248551Z","shell.execute_reply.started":"2022-09-10T15:55:08.237968Z","shell.execute_reply":"2022-09-10T15:55:08.247593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nTH = 0.1\n\nDATA = '../input/hubmap-organ-segmentation/test_images/'\nCONFIG = '../input/hubmapsegformerb5/mit-b5.pickle'\nMODELS = [\n    \"../input/hubmapsegformerb5merge/merge-fold0.pth\",\n    \"../input/hubmapsegformerb5merge/merge-fold1.pth\",\n    \"../input/hubmapsegformerb5merge/merge-fold2.pth\",\n    \"../input/hubmapsegformerb5merge/merge-fold3.pth\",\n    \"../input/hubmapsegformerb5merge/merge-fold4.pth\",\n    \"../input/hubmapsegformerlast/merge-fold0.pth\",\n    \"../input/hubmapsegformerlast/merge-fold1.pth\",\n    \"../input/hubmapsegformerlast/merge-fold2.pth\",\n    \"../input/hubmapsegformerlast/merge-fold3.pth\",\n    \"../input/hubmapsegformerlast/merge-fold4.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold0.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold1.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold2.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold3.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold4.pth\",\n#     \"../input/hubmapsegformerb5/last-new-fold0.pth\",\n# #     \"../input/hubmapsegformerb5/last-new-fold1.pth\",\n#     \"../input/hubmapsegformerb5/last-fold0.pth\",\n#     \"../input/hubmapsegformerb5/last-fold1.pth\",\n#     \"../input/hubmapsegformerb5/last-fold2.pth\",\n#     \"../input/hubmapsegformerb5/last-fold3.pth\",\n#     \"../input/hubmapsegformerb5/last-fold4.pth\",\n#     \"../input/hubmapsegformerb5/model-b5-fold0.pth\",\n#     \"../input/hubmapsegformerb5/model-b5-fold1.pth\",\n#     \"../input/hubmapsegformerb5/model-b5-fold2.pth\",\n#     \"../input/hubmapsegformerb5/model-b5-fold3.pth\",\n#     \"../input/hubmapsegformerb5/model-b5-fold4.pth\",\n#     \"../input/hubmapsegformerb5/last-fold0-4.pth\",\n]\n# MODELS = [f'../input/hubmapunext50base/model_{i}.pth' for i in range(4)]\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/sample_submission.csv')\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.249762Z","iopub.execute_input":"2022-09-10T15:55:08.25058Z","iopub.status.idle":"2022-09-10T15:55:08.260311Z","shell.execute_reply.started":"2022-09-10T15:55:08.250534Z","shell.execute_reply":"2022-09-10T15:55:08.259286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nclass2idx = {'prostate': 1,\n  'spleen': 2,\n  'lung': 3,\n  'kidney': 4,\n  'largeintestine': 5,\n  'none': 0}\nidx2class = {1: 'prostate',\n  2: 'spleen',\n  3: 'lung',\n  4: 'kidney',\n  5: 'largeintestine',\n  0: 'none'}","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.261494Z","iopub.execute_input":"2022-09-10T15:55:08.261879Z","iopub.status.idle":"2022-09-10T15:55:08.276491Z","shell.execute_reply.started":"2022-09-10T15:55:08.261843Z","shell.execute_reply":"2022-09-10T15:55:08.275737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nfrom transformers import SegformerForSemanticSegmentation\n\nimport pickle\n\nconfig = {}\nwith open(CONFIG, mode=\"rb\") as f:\n    config = pickle.load(f)\n    \n    \n# from transformers import SegformerForSemanticSegmentation\n# from transformers import SegformerModel, SegformerConfig\n# MODEL_NAME=\"nvidia/segformer-b2-finetuned-ade-512-512\"\n# config = SegformerConfig.from_pretrained(MODEL_NAME,\n#                         num_labels=len(class2idx), \n#                         id2label=idx2class, \n#                         label2id=class2idx,\n# )\n\n# with open(\"mit-b2.pickle\", mode=\"wb\") as f :\n#     pickle.dump(config, f)\nmodels = []\nfor MODEL in MODELS:\n    model = SegformerForSemanticSegmentation(config)\n    model_path = MODEL\n    model.load_state_dict(torch.load(model_path))\n    model = model.cuda()\n    model.eval()\n    models.append(model)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.277946Z","iopub.execute_input":"2022-09-10T15:55:08.278186Z","iopub.status.idle":"2022-09-10T15:55:08.287727Z","shell.execute_reply.started":"2022-09-10T15:55:08.278147Z","shell.execute_reply":"2022-09-10T15:55:08.286989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nDATA = '../input/hubmap-organ-segmentation/test_images/'\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/test.csv').set_index('id')\n# DATA = '../input/hubmap-organ-segmentation/train_images/'\n# df_sample = pd.read_csv('../input/hubmap-organ-segmentation/train.csv').set_index('id')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.288945Z","iopub.execute_input":"2022-09-10T15:55:08.289444Z","iopub.status.idle":"2022-09-10T15:55:08.304209Z","shell.execute_reply.started":"2022-09-10T15:55:08.289336Z","shell.execute_reply":"2022-09-10T15:55:08.303307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\ndef rle_encode_less_memory(img):\n    pixels = img.T.flatten()\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    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.31126Z","iopub.execute_input":"2022-09-10T15:55:08.312156Z","iopub.status.idle":"2022-09-10T15:55:08.317527Z","shell.execute_reply.started":"2022-09-10T15:55:08.312122Z","shell.execute_reply":"2022-09-10T15:55:08.316729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\ndef calc_resize(w, p) :\n    a = 3000/512\n    target = int((w*p/0.4)/a + 0.5)\n    target = (target+31)//32 * 32\n    return target\n\ncalc_resize(2023, 0.4945), calc_resize(3000, 0.4)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.318947Z","iopub.execute_input":"2022-09-10T15:55:08.319458Z","iopub.status.idle":"2022-09-10T15:55:08.327269Z","shell.execute_reply.started":"2022-09-10T15:55:08.319424Z","shell.execute_reply":"2022-09-10T15:55:08.32635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\n# TH = 0.225\nfrom transformers import SegformerFeatureExtractor\n\nnames,preds = [],[]\nimgs, pd_mks = [],[]\ndebug = len(df_sample)<2\n# debug = True\n# tta = True\n\nmodel.eval()\n\ncnt = 0\nfile = \"\"\nfor idx,row in tqdm(df_sample.iterrows(),total=len(df_sample)):\n    image = Image.open(os.path.join(DATA,str(idx)+'.tiff'))\n    size = calc_resize(row.img_height, row.pixel_size)\n    feature_extractor = SegformerFeatureExtractor(reduce_labels=False, size=(size,size))\n    encoding = feature_extractor(image, return_tensors=\"pt\")\n#     print(idx)\n    pixel_values = encoding.pixel_values.cuda()\n#     print(pixel_values.shape)\n    organ = row.organ\n    height, width = row.img_height, row.img_width\n    index = class2idx[organ]\n    with torch.no_grad():\n        pred = []\n        for model in models:\n            model.eval()\n            with torch.no_grad() :\n                outputs = model(pixel_values=pixel_values)\n            upsampled_logits = nn.functional.interpolate(outputs['logits'],\n                        # size=image.size[::-1], # (height, width)\n                        (height, width),\n                        mode='bilinear',\n                        align_corners=False)\n            mask = upsampled_logits.argmax(dim=1)[0]\n            mask[mask != index] = 0\n            mask[mask == index] = 1\n            mask = mask * F.sigmoid(upsampled_logits[0][index])\n#             print(mask.shape, upsampled_logits.shape, mask.min(), mask.max())\n#             plt.imshow(mask.cpu().numpy())\n#             plt.show()\n            if len(pred) == 0 :\n                pred = mask.detach().cpu().numpy()\n            else :\n#                 pred += mask.detach().cpu().numpy()\n                pred = np.fmax(pred, mask.detach().cpu().numpy())\n#     pred /= len(models)\n#     plt.imshow(pred)\n#     plt.show()\n    pred[pred <= TH] = 0\n    pred[pred > TH] = 1\n#     plt.imshow(pred)\n#     plt.show()\n    rle = rle_encode_less_memory(pred)\n\n    names.append(str(idx))\n    preds.append(rle)\n    if debug:\n        imgs.append(image)\n        pd_mks.append(pred)\n    \n    if debug and cnt == 10:\n        file = os.path.join(DATA,str(idx)+'.tiff')\n        break\n    cnt+=1\n\n    del image, mask, rle, idx, row, pred, feature_extractor, encoding, pixel_values, upsampled_logits, outputs\n    gc.collect()   ","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.328483Z","iopub.execute_input":"2022-09-10T15:55:08.329061Z","iopub.status.idle":"2022-09-10T15:55:08.336936Z","shell.execute_reply.started":"2022-09-10T15:55:08.329029Z","shell.execute_reply":"2022-09-10T15:55:08.336231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\n#debug = True\nif debug:\n    import matplotlib.pyplot as plt\n    for img, mask in zip(imgs, pd_mks):\n        plt.figure(figsize=(12, 7))\n        plt.subplot(1, 3, 1); plt.imshow(img); plt.axis('OFF'); plt.title('image')\n        plt.subplot(1, 3, 2); plt.imshow(mask*255); plt.axis('OFF'); plt.title('mask')\n        plt.subplot(1, 3, 3); plt.imshow(img); plt.imshow(mask*255, alpha=0.4); plt.axis('OFF'); plt.title('overlay')\n        plt.tight_layout()\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.338257Z","iopub.execute_input":"2022-09-10T15:55:08.338956Z","iopub.status.idle":"2022-09-10T15:55:08.352617Z","shell.execute_reply.started":"2022-09-10T15:55:08.338921Z","shell.execute_reply":"2022-09-10T15:55:08.351821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\ndf = pd.DataFrame({'id':names,'rle':preds})\ndf.to_csv('submission1.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.353833Z","iopub.execute_input":"2022-09-10T15:55:08.357107Z","iopub.status.idle":"2022-09-10T15:55:08.365691Z","shell.execute_reply.started":"2022-09-10T15:55:08.357077Z","shell.execute_reply":"2022-09-10T15:55:08.364921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 segformer.py","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:55:08.366925Z","iopub.execute_input":"2022-09-10T15:55:08.367492Z","iopub.status.idle":"2022-09-10T15:56:30.255447Z","shell.execute_reply.started":"2022-09-10T15:55:08.367459Z","shell.execute_reply":"2022-09-10T15:56:30.254529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import sys\n\n# print(\"{}{: >25}{}{: >10}{}\".format('|','Variable Name','|','Memory','|'))\n# print(\" ------------------------------------ \")\n# for var_name in dir():\n#     if not var_name.startswith(\"_\") and sys.getsizeof(eval(var_name)) > 1000: #ここだけアレンジ\n#         print(\"{}{: >25}{}{: >10}{}\".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|'))","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:30.257191Z","iopub.execute_input":"2022-09-10T15:56:30.257509Z","iopub.status.idle":"2022-09-10T15:56:30.262927Z","shell.execute_reply.started":"2022-09-10T15:56:30.257473Z","shell.execute_reply":"2022-09-10T15:56:30.262183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:30.264332Z","iopub.execute_input":"2022-09-10T15:56:30.264583Z","iopub.status.idle":"2022-09-10T15:56:31.320718Z","shell.execute_reply.started":"2022-09-10T15:56:30.264553Z","shell.execute_reply":"2022-09-10T15:56:31.319782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# efficientnet","metadata":{}},{"cell_type":"markdown","source":"Copied from: https://www.kaggle.com/code/markwijkhuizen/hubmap-inference-tf-tpu-efficientnet-b7-640x640","metadata":{}},{"cell_type":"code","source":"%%writefile efficientnet.py\n\n# Import EfficientNet models with intermediate endpoints\nimport sys\nsys.path.append('../input/efficientnetv2-head-1x1-endpoint-v2/')\nsys.path.append('../input/efficientnetv2-head-1x1-endpoint-v2/efficientnetv2/')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.322639Z","iopub.execute_input":"2022-09-10T15:56:31.322937Z","iopub.status.idle":"2022-09-10T15:56:31.328844Z","shell.execute_reply.started":"2022-09-10T15:56:31.322883Z","shell.execute_reply":"2022-09-10T15:56:31.32816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow.keras.backend as K\nimport tensorflow_addons as tfa\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras.mixed_precision import experimental as mixed_precision\nfrom kaggle_datasets import KaggleDatasets\nfrom tqdm.notebook import tqdm\nfrom multiprocessing import cpu_count\nfrom sklearn import metrics\nfrom sklearn.model_selection import KFold\n\nimport effnetv2_model\nimport tifffile\nimport re\nimport os\nimport io\nimport time\nimport pickle\nimport math\nimport random\nimport sys\nimport cv2\nimport gc\n\nprint(f'tensorflow version: {tf.__version__}')\nprint(f'tensorflow keras version: {tf.keras.__version__}')\nprint(f'python version: P{sys.version}')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.330362Z","iopub.execute_input":"2022-09-10T15:56:31.332347Z","iopub.status.idle":"2022-09-10T15:56:31.340087Z","shell.execute_reply.started":"2022-09-10T15:56:31.332318Z","shell.execute_reply":"2022-09-10T15:56:31.339237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Seed all random number generators\ndef seed_everything(seed):\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\n    \nSEED = 42\nseed_everything(SEED)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.341544Z","iopub.execute_input":"2022-09-10T15:56:31.343205Z","iopub.status.idle":"2022-09-10T15:56:31.354318Z","shell.execute_reply.started":"2022-09-10T15:56:31.343177Z","shell.execute_reply":"2022-09-10T15:56:31.353636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Threshold to classify a pixel as mask\nTHRESHOLD = 0.400\nTHs = {'lung':0.32, 'kidney':0.52, 'largeintestine':0.51, 'prostate':0.30, 'spleen':0.45}\n#THs = {'lung':0.10, 'kidney':0.40, 'largeintestine':0.40, 'prostate':0.30, 'spleen':0.40}\n\n\nDEBUG = False\nIS_TPU = True\n\n# Image dimensions\nIMG_SIZE = 640\nPATCH_SIZE = 640\nN_CHANNELS = 3\nN_PATCHES_PER_IMAGE = (IMG_SIZE // PATCH_SIZE) ** 2\n\nINPUT_SHAPE = (PATCH_SIZE, PATCH_SIZE, N_CHANNELS)\n\n# EfficientNet version, b0/b1/b2/b3/s/m/l/xl/xxl\nEFN_SIZE = 'b8'\nLR_MAX = 0.02\nEPOCHS = 30\nMOMENTUM = 0.00\n\n# Batch size\nBATCH_SIZE = 64\n\n# Dataset Mean and Standard Deviation\nMEAN = np.load('/kaggle/input/hubmap-patched-tfrecords-300x300/MEAN.npy')\nSTD = np.load('/kaggle/input/hubmap-patched-tfrecords-300x300/STD.npy')\n\nprint(f'MEAN: {MEAN}, STD: {STD}')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.355881Z","iopub.execute_input":"2022-09-10T15:56:31.356482Z","iopub.status.idle":"2022-09-10T15:56:31.364991Z","shell.execute_reply.started":"2022-09-10T15:56:31.356447Z","shell.execute_reply":"2022-09-10T15:56:31.364226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Hardware Configuration","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Detect hardware, return appropriate distribution strategy\ntry:\n    TPU = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    print('Running on TPU ', TPU.master())\nexcept ValueError:\n    print('Running on GPU')\n    TPU = None\n\nif TPU:\n    tf.config.experimental_connect_to_cluster(TPU)\n    tf.tpu.experimental.initialize_tpu_system(TPU)\n    strategy = tf.distribute.experimental.TPUStrategy(TPU)\nelse:\n    strategy = tf.distribute.get_strategy() # default distribution strategy in Tensorflow. Works on CPU and single GPU.\n\nREPLICAS = strategy.num_replicas_in_sync\nprint(f'REPLICAS: {REPLICAS}')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.366464Z","iopub.execute_input":"2022-09-10T15:56:31.367023Z","iopub.status.idle":"2022-09-10T15:56:31.374091Z","shell.execute_reply.started":"2022-09-10T15:56:31.366989Z","shell.execute_reply":"2022-09-10T15:56:31.373173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# FPN","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\ndef FPN(xs, output_channels, last_layer, debug=False):\n    def _conv(x):\n        x = tf.keras.layers.ZeroPadding2D(padding=1)(x)\n        x = tf.keras.layers.Conv2D(output_channels * 2, 3, padding='SAME', kernel_initializer='he_normal', activation='relu')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = tf.keras.layers.ZeroPadding2D(padding=1)(x)\n        x = tf.keras.layers.Conv2D(output_channels, 3, padding='SAME', kernel_initializer='he_normal')(x)\n        x = tf.image.resize(x, size=target_size, method=tf.image.ResizeMethod.BILINEAR)\n        x = tf.nn.relu(x)\n        return x\n\n    target_size = last_layer.shape[1:3]\n    xs = tf.keras.layers.Concatenate()([_conv(x) for x in xs])\n    x = tf.keras.layers.Concatenate()([xs, last_layer])\n\n    if debug:\n        return x, xs\n    else:\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.375662Z","iopub.execute_input":"2022-09-10T15:56:31.376229Z","iopub.status.idle":"2022-09-10T15:56:31.385967Z","shell.execute_reply.started":"2022-09-10T15:56:31.376193Z","shell.execute_reply":"2022-09-10T15:56:31.3851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ASPP","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\ndef ASPP(x, mid_c=320, dilations=[1, 2, 3, 4], out_c=640, debug=False):\n    def _aspp_module(x, filters, kernel_size, padding, dilation, groups=1):\n        x = tf.keras.layers.ZeroPadding2D(padding=padding)(x)\n        x = tf.keras.layers.Conv2D(\n                filters=filters,\n                kernel_size=kernel_size,\n                dilation_rate=dilation,\n                groups=1 if IS_TPU else groups,\n                kernel_initializer='he_uniform',\n            )(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = tf.nn.relu(x)\n        \n        return x\n    \n    x0 = tf.math.reduce_max(x, axis=(1,2), keepdims=True)\n    x0 = tf.keras.layers.Conv2D(filters=mid_c, kernel_size=1, strides=1, kernel_initializer='he_uniform', use_bias=False)(x0)\n    x0 = tf.keras.layers.BatchNormalization(gamma_initializer=tf.constant_initializer(value=0.25))(x0)\n    x0 = tf.nn.relu(x0)\n                                  \n                                  \n    xs = (\n        [_aspp_module(x, mid_c, 1, padding=0, dilation=1)] +\n        [_aspp_module(x, mid_c, 3, padding=d, dilation=d, groups=4) for d in dilations]\n    )\n    \n    x0= tf.image.resize(x0, size=xs[0].shape[1:3])\n    x = tf.keras.layers.Concatenate()([x0] + xs)\n    x = tf.keras.layers.Conv2D(filters=out_c, kernel_size=1, kernel_initializer='he_uniform', use_bias=False)(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.nn.relu(x)\n                       \n    if debug:\n        return x, x0, xs\n    else:\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.386889Z","iopub.execute_input":"2022-09-10T15:56:31.38863Z","iopub.status.idle":"2022-09-10T15:56:31.396242Z","shell.execute_reply.started":"2022-09-10T15:56:31.3886Z","shell.execute_reply":"2022-09-10T15:56:31.395432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Upsample","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\ndef PixelShuffle(x, upscale_factor=2):\n    _, w, h, c = x.shape\n    n = -1\n\n    c_out = c // upscale_factor ** 2\n    w_out = w * upscale_factor\n    h_out = h * upscale_factor\n\n    x = tf.reshape(x, [-1, upscale_factor, upscale_factor, w, h, c_out])\n    x = tf.transpose(x, [0, 3, 1, 4, 2, 5])\n    x = tf.reshape(x, [-1, w_out, h_out, c_out])\n\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.397731Z","iopub.execute_input":"2022-09-10T15:56:31.398113Z","iopub.status.idle":"2022-09-10T15:56:31.408969Z","shell.execute_reply.started":"2022-09-10T15:56:31.398077Z","shell.execute_reply":"2022-09-10T15:56:31.408271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n\n# Inspiration: https://www.tensorflow.org/tutorials/generative/pix2pix#build_an_input_pipeline_with_tfdata\ndef upsample(x, concat, target_filters, name, conv2dt_kernel_init_max, relu=True, dropout=0, debug=False):\n#     x = PixelShuffle(x)\n\n    filters = concat.shape[-1]\n    x_up = tf.keras.layers.Conv2DTranspose(\n            filters, # Number of Convolutional Filters\n            kernel_size=4, # Kernel Size\n            strides=2, # Kernel Steps\n            padding='SAME', # linear scaling\n            name=f'Conv2DTranspose_{name}', # Name of Layer\n            kernel_initializer='he_uniform',\n            use_bias=False,\n        )(x)\n    \n    concat = tf.keras.layers.BatchNormalization(\n        gamma_initializer=tf.constant_initializer(value=0.25),\n        name=f'BatchNormalization_{name}'\n    )(concat)\n    x = tf.keras.layers.Concatenate(name=f'Concatenate_{name}')([x_up, concat])\n    x = tf.nn.relu(x)\n    \n        \n    x = tf.keras.layers.Conv2D(target_filters, 3, padding='SAME', kernel_initializer='he_uniform', activation='relu', name=f'Conv2D_1_{name}')(x)\n    x = tf.keras.layers.Conv2D(target_filters, 3, padding='SAME', kernel_initializer='he_uniform', name=f'Conv2D_2_{name}')(x)\n    \n    if relu:\n        x = tf.nn.relu(x)\n    \n    x = tf.keras.layers.Dropout(dropout, name=f'Dropout_{name}')(x)\n\n    if debug:\n        return x, x_up, concat\n    else:\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.410531Z","iopub.execute_input":"2022-09-10T15:56:31.410889Z","iopub.status.idle":"2022-09-10T15:56:31.419392Z","shell.execute_reply.started":"2022-09-10T15:56:31.410852Z","shell.execute_reply":"2022-09-10T15:56:31.418593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n\ndef get_model(dropout_decoder=0, dropout_cnn=0, file_path=None, lr=1e-3, eps=1e-7, clipnorm=5.0, wd_coef=1e-2, cnn_trainable=True):\n    with strategy.scope():\n        # EfficientNetV2 Backbone # \n        cnn = effnetv2_model.get_model(f'efficientnet-{EFN_SIZE}', include_top=False, weights=None, model_config={ 'conv_dropout': dropout_cnn })\n        cnn.trainable = cnn_trainable\n\n        # Inputs, note the names are equal to the dictionary keys in the dataset\n        image = tf.keras.layers.Input(INPUT_SHAPE, name='image', dtype=tf.float32)\n        image_norm = tf.cast(image, tf.float32) / 255\n        image_norm = tf.keras.layers.experimental.preprocessing.Normalization(mean=MEAN, variance=STD, dtype=tf.float32)(image_norm)\n\n        embedding, up6, up5, up4, up3, up2, up1 = cnn(image_norm, with_endpoints=True)\n        print(f'embedding shape: {embedding.shape} up1 shape: {up1.shape}, up2 shape: {up2.shape}')\n        print(f'up3 shape: {up3.shape}, up4 shape: {up4.shape}, up5 shape: {up5.shape}, up6 shape: {up6.shape}')\n        \n        dec0 = ASPP(up2)\n        dec0 = tf.keras.layers.Dropout(0.50)(dec0)\n\n        dec1 = upsample(dec0, up3, up4.shape[-1] * 4, 'upsample1', 0.02, dropout=dropout_decoder)\n        dec2 = upsample(dec1, up4, up5.shape[-1] * 2, 'upsample2', 0.02, dropout=dropout_decoder)\n        dec3 = upsample(dec2, up5, up6.shape[-1] * 2, 'upsample3', 0.02)\n        dec4 = upsample(dec3, up6, 64, 'upsample4', 0.02)\n        \n        print(f'dec0 shape: {dec0.shape}, dec1 shape: {dec1.shape}, dec2 shape: {dec2.shape}, dec3 shape: {dec3.shape}, dec4 shape: {dec4.shape}')\n        \n        dec_fpn = FPN([dec0, dec1, dec2, dec3], 32, dec4)\n        \n        print(f'dec_fpn shape: {dec_fpn.shape}')\n        \n        # Head\n        x = tf.keras.layers.Dropout(0.10)(dec_fpn)\n        x = tf.keras.layers.Conv2D(\n            filters=1,\n            kernel_size=1,\n            padding='SAME',\n            kernel_initializer=tf.random_normal_initializer(0.00, 0.05),\n            activation='sigmoid',\n            name='Conv2D_3_head'\n        )(x)\n        output = tf.image.resize(x, size=[IMG_SIZE, IMG_SIZE], method=tf.image.ResizeMethod.BILINEAR)\n        \n        model = tf.keras.models.Model(inputs=image, outputs=output)\n\n        if file_path:\n            print('Loading pretrained weights...')\n            model.load_weights(file_path)\n            \n        model.trainable = False\n\n        return model","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.420985Z","iopub.execute_input":"2022-09-10T15:56:31.421536Z","iopub.status.idle":"2022-09-10T15:56:31.432973Z","shell.execute_reply.started":"2022-09-10T15:56:31.421501Z","shell.execute_reply":"2022-09-10T15:56:31.432215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Pretrained File Path: '/kaggle/input/sartorius-training-dataset/model.h5'\nmodel = get_model(file_path='../input/hubmap-training-tf-tpu-efficientnet-b8-640640-p/model_0.h5')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.433988Z","iopub.execute_input":"2022-09-10T15:56:31.434738Z","iopub.status.idle":"2022-09-10T15:56:31.445535Z","shell.execute_reply.started":"2022-09-10T15:56:31.434708Z","shell.execute_reply":"2022-09-10T15:56:31.444732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\nmodels = [\n#     model,\n    get_model(file_path='../input/hubmapefficientnetb8/model_0.h5'),\n    get_model(file_path='../input/hubmapefficientnetb8/model_1.h5'),\n    get_model(file_path='../input/hubmapefficientnetb8/model_2.h5'),\n    get_model(file_path='../input/hubmapefficientnetb8/model_3.h5'),\n]","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.446636Z","iopub.execute_input":"2022-09-10T15:56:31.448165Z","iopub.status.idle":"2022-09-10T15:56:31.458701Z","shell.execute_reply.started":"2022-09-10T15:56:31.448057Z","shell.execute_reply":"2022-09-10T15:56:31.457588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Plot model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.460214Z","iopub.execute_input":"2022-09-10T15:56:31.460506Z","iopub.status.idle":"2022-09-10T15:56:31.4683Z","shell.execute_reply.started":"2022-09-10T15:56:31.460469Z","shell.execute_reply":"2022-09-10T15:56:31.467229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\ntf.keras.utils.plot_model(model, show_shapes=True, show_dtype=True, show_layer_names=True, expand_nested=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.469781Z","iopub.execute_input":"2022-09-10T15:56:31.470171Z","iopub.status.idle":"2022-09-10T15:56:31.478071Z","shell.execute_reply.started":"2022-09-10T15:56:31.470133Z","shell.execute_reply":"2022-09-10T15:56:31.477255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utility Funtions","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Resized a tensor to the specified size\ndef resize_tensor(tensor, size=IMG_SIZE, dtype=np.uint8):\n    return cv2.resize(tensor, [size, size], interpolation=cv2.INTER_CUBIC).astype(dtype)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.479728Z","iopub.execute_input":"2022-09-10T15:56:31.480044Z","iopub.status.idle":"2022-09-10T15:56:31.487564Z","shell.execute_reply.started":"2022-09-10T15:56:31.480006Z","shell.execute_reply":"2022-09-10T15:56:31.486777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef get_mask(image_id):\n    row = train.loc[train['id'] == image_id].squeeze()\n    h, w = row[['img_height', 'img_width']]\n    mask = np.zeros(shape=[h * w], dtype=np.uint8)\n    s = row['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    for lo, hi in zip(starts, ends):\n        mask[lo : hi] = 1\n        \n    mask = mask.reshape([h, w]).T\n        \n    mask = resize_tensor(mask)\n    \n    mask = np.expand_dims(mask, axis=2)\n        \n    return mask","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.489724Z","iopub.execute_input":"2022-09-10T15:56:31.490402Z","iopub.status.idle":"2022-09-10T15:56:31.497052Z","shell.execute_reply.started":"2022-09-10T15:56:31.490362Z","shell.execute_reply":"2022-09-10T15:56:31.496268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Reads an image and returns the image and original image size\ndef get_image(image_id, folder, negative=True):\n    image = tifffile.imread(f'/kaggle/input/hubmap-organ-segmentation/{folder}_images/{image_id}.tiff')\n    if len(image.shape) == 5:\n        image = image.squeeze().transpose(1, 2, 0)\n    \n    # Image Size\n    image_size, _, _ = image.shape\n    \n    # Reverse pixels to make tissue colored and background black\n    if negative:\n        image = image - image.min()\n        image = image / (image.max() - image.min())\n        image = image * 255\n        image = 255 - image.astype(np.uint8)\n        \n    # Resize\n    image = resize_tensor(image)\n    \n    return image, image_size","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.498403Z","iopub.execute_input":"2022-09-10T15:56:31.49883Z","iopub.status.idle":"2022-09-10T15:56:31.506893Z","shell.execute_reply.started":"2022-09-10T15:56:31.498793Z","shell.execute_reply":"2022-09-10T15:56:31.506144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# extract patches from an image\ndef extract_patches(image):\n    _, _, c = image.shape\n    image = tf.expand_dims(image, 0)\n    image_patches = tf.image.extract_patches(image, [1,PATCH_SIZE,PATCH_SIZE,1], [1, PATCH_SIZE, PATCH_SIZE, 1], [1, 1, 1, 1], padding='SAME')\n    image_patches = tf.reshape(image_patches, [N_PATCHES_PER_IMAGE, PATCH_SIZE, PATCH_SIZE, c])\n    image_patches = image_patches.numpy()\n\n    return image_patches","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.50841Z","iopub.execute_input":"2022-09-10T15:56:31.510037Z","iopub.status.idle":"2022-09-10T15:56:31.519246Z","shell.execute_reply.started":"2022-09-10T15:56:31.509998Z","shell.execute_reply":"2022-09-10T15:56:31.518429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\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-10T15:56:31.521455Z","iopub.execute_input":"2022-09-10T15:56:31.521746Z","iopub.status.idle":"2022-09-10T15:56:31.529006Z","shell.execute_reply.started":"2022-09-10T15:56:31.521722Z","shell.execute_reply":"2022-09-10T15:56:31.528177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Training DataFrame\ntrain = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/train.csv')\n\n#display(train.head())\n#display(train.info())","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.53024Z","iopub.execute_input":"2022-09-10T15:56:31.531082Z","iopub.status.idle":"2022-09-10T15:56:31.538992Z","shell.execute_reply.started":"2022-09-10T15:56:31.531047Z","shell.execute_reply":"2022-09-10T15:56:31.537793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Test DataFrame\ntest = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/test.csv')\n\n#display(test.head())\n#display(test.info())","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.540278Z","iopub.execute_input":"2022-09-10T15:56:31.54185Z","iopub.status.idle":"2022-09-10T15:56:31.547046Z","shell.execute_reply.started":"2022-09-10T15:56:31.541823Z","shell.execute_reply":"2022-09-10T15:56:31.546287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Reconstruct the original image from patches\ndef merge_patches(patches):\n    image = np.zeros(shape=[IMG_SIZE, IMG_SIZE, patches.shape[-1]], dtype=patches.dtype)\n    s = int(N_PATCHES_PER_IMAGE ** 0.50)\n    for r in range(s):\n        for c in range(s):\n            start_x = r * PATCH_SIZE\n            end_x = (r + 1) * PATCH_SIZE\n            start_y = c * PATCH_SIZE\n            end_y = (c + 1) * PATCH_SIZE\n            image[start_x:end_x, start_y:end_y] = patches[r * s + c]\n            \n    return image","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.548293Z","iopub.execute_input":"2022-09-10T15:56:31.548802Z","iopub.status.idle":"2022-09-10T15:56:31.559344Z","shell.execute_reply.started":"2022-09-10T15:56:31.548676Z","shell.execute_reply":"2022-09-10T15:56:31.558084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sanity Check","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Plot 10 predictions to verify the trained weights are correctly loaded\ntest_rows = []\nN = 10\n\nfor row_idx, row in tqdm(train[:N].iterrows(), total=N):\n    image, image_size = get_image(row['id'], 'train')\n    pred_all = []\n    \n    for model in models:\n#         model = models[0]\n        # Make Prediction\n        # Preprocess Image\n        image_patches = extract_patches(image)    \n        mask_patches_pred = model.predict(image_patches)\n        # Merge patches\n        mask_pred = merge_patches(mask_patches_pred)\n\n#         #------flip 0------\n#         image = np.flip(image, (0))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (0))\n#         image = np.flip(image, (0))\n\n#         #------flip 1------\n#         image = np.flip(image, (1))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (1))\n#         image = np.flip(image, (1))\n\n#         #------flip 0,1------\n#         image = np.flip(image, (0,1))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (0,1))\n#         image = np.flip(image, (0,1))\n#         mask_pred /= 4\n    \n        if len(pred_all) == 0 :\n            pred_all = mask_pred\n        else :\n            pred_all = np.fmax(pred_all, mask_pred)\n        \n#     pred_all /= len(models)\n\n    # Resize mask to original size\n    mask_pred_resized = resize_tensor(pred_all, size=image_size, dtype=np.float32)    \n    \n    fig, axes = plt.subplots(1,2, figsize=(8,4))\n    axes[0].imshow(mask_pred_resized > THs[row['organ']])\n    plt.title(row['id'])\n    axes[1].imshow(image)\n    plt.show()\n        \n    # Resize and Binarize Mask\n    mask_binary = (mask_pred_resized > THs[row['organ']]).astype(np.int8)\n    # Append to Result\n    test_rows.append({\n        'id': row['id'],\n        'rle': rle_encode_less_memory(mask_binary)\n    })","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.560422Z","iopub.execute_input":"2022-09-10T15:56:31.562126Z","iopub.status.idle":"2022-09-10T15:56:31.570397Z","shell.execute_reply.started":"2022-09-10T15:56:31.562027Z","shell.execute_reply":"2022-09-10T15:56:31.569558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Plot 10 predictions to verify the trained weights are correctly loaded\n# test_rows = []\n# N = 10\n\n# for row_idx, row in tqdm(train[:N].iterrows(), total=N):\n#     # Preprocess Image\n#     image, image_size = get_image(row['id'], 'train')\n#     image_patches = extract_patches(image)\n    \n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     mask_pred = merge_patches(mask_patches_pred)\n    \n#     #------flip 0------\n#     image = np.flip(image, (0))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (0))\n#     image = np.flip(image, (0))\n\n#     #------flip 1------\n#     image = np.flip(image, (1))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (1))\n#     image = np.flip(image, (1))\n\n#     #------flip 0,1------\n#     image = np.flip(image, (0,1))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (0,1))\n#     image = np.flip(image, (0,1))\n#     mask_pred /= 4\n    \n#     mask_pred_resized = resize_tensor(mask_pred, size=image_size, dtype=np.float32)\n    \n#     fig, axes = plt.subplots(1,2, figsize=(8,4))\n#     axes[0].imshow(mask_pred_resized)\n#     axes[1].imshow(image)\n#     plt.show()\n        \n#     # Resize and Binarize Mask\n#     mask_binary = (mask_pred_resized > THRESHOLD).astype(np.int8)\n#     # Append to Result\n#     test_rows.append({\n#         'id': row['id'],\n#         'rle': rle_encode_less_memory(mask_binary)\n#     })","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.572008Z","iopub.execute_input":"2022-09-10T15:56:31.57227Z","iopub.status.idle":"2022-09-10T15:56:31.582296Z","shell.execute_reply.started":"2022-09-10T15:56:31.572238Z","shell.execute_reply":"2022-09-10T15:56:31.581454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference Loop","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n#  Predictions are stored as a list of dictionaries\ntest_rows = []\n\n# Iterate over all test images\nfor row_idx, row in tqdm(test.iterrows(), total=len(test)):\n    # Preprocess Image\n    image, image_size = get_image(row['id'], 'test')\n    pred_all = []\n    \n    for model in models:\n        # Make Prediction\n        image_patches = extract_patches(image)\n        mask_patches_pred = model.predict(image_patches)\n        # Merge patches\n        mask_pred = merge_patches(mask_patches_pred)\n\n#         #------flip 0------\n#         image = np.flip(image, (0))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (0))\n#         image = np.flip(image, (0))\n\n#         #------flip 1------\n#         image = np.flip(image, (1))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (1))\n#         image = np.flip(image, (1))\n\n#         #------flip 0,1------\n#         image = np.flip(image, (0,1))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (0,1))\n#         image = np.flip(image, (0,1))\n#         mask_pred /= 4\n    \n        if len(pred_all) == 0 :\n            pred_all = mask_pred\n        else :\n            pred_all = np.fmax(pred_all, mask_pred)\n        \n#     pred_all /= len(models)\n\n    # Resize mask to original size\n    mask_pred_resized = resize_tensor(pred_all, size=image_size, dtype=np.float32)\n    \n    if row_idx == 0:\n        fig, axes = plt.subplots(1,2, figsize=(8,4))\n        axes[0].imshow(mask_pred_resized)\n        axes[1].imshow(image)\n        plt.show()\n        \n    # Resize and Binarize Mask\n    mask_binary = (mask_pred_resized > THs[row['organ']]).astype(np.int8)\n    # Append to Result\n    test_rows.append({\n        'id': row['id'],\n        'rle': rle_encode_less_memory(mask_binary)\n    })","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.583523Z","iopub.execute_input":"2022-09-10T15:56:31.583955Z","iopub.status.idle":"2022-09-10T15:56:31.596553Z","shell.execute_reply.started":"2022-09-10T15:56:31.583922Z","shell.execute_reply":"2022-09-10T15:56:31.595592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Predictions are stored as a list of dictionaries\n# test_rows = []\n\n# # Iterate over all test images\n# for row_idx, row in tqdm(test.iterrows(), total=len(test)):\n#     # Preprocess Image\n#     image, image_size = get_image(row['id'], 'test')\n#     image_patches = extract_patches(image)\n    \n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred = merge_patches(mask_patches_pred)\n    \n#     #------flip 0------\n#     image = np.flip(image, (0))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (0))\n#     image = np.flip(image, (0))\n\n#     #------flip 1------\n#     image = np.flip(image, (1))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (1))\n#     image = np.flip(image, (1))\n\n#     #------flip 0,1------\n#     image = np.flip(image, (0,1))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (0,1))\n#     image = np.flip(image, (0,1))\n#     mask_pred /= 4\n    \n#     # Resize mask to original size\n#     mask_pred_resized = resize_tensor(mask_pred, size=image_size, dtype=np.float32)\n    \n#     if row_idx == 0:\n#         fig, axes = plt.subplots(1,2, figsize=(8,4))\n#         axes[0].imshow(mask_pred_resized)\n#         axes[1].imshow(image)\n#         plt.show()\n        \n#     # Resize and Binarize Mask\n#     mask_binary = (mask_pred_resized > THRESHOLD).astype(np.int8)\n#     # Append to Result\n#     test_rows.append({\n#         'id': row['id'],\n#         'rle': rle_encode_less_memory(mask_binary)\n#     })","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.598095Z","iopub.execute_input":"2022-09-10T15:56:31.598528Z","iopub.status.idle":"2022-09-10T15:56:31.607688Z","shell.execute_reply.started":"2022-09-10T15:56:31.598489Z","shell.execute_reply":"2022-09-10T15:56:31.606835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make Submission CSV","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Make submission DataFrame\ntest_df = pd.DataFrame(test_rows)\n\n#display(test_df.head())\n#display(test_df.info())","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.609883Z","iopub.execute_input":"2022-09-10T15:56:31.610816Z","iopub.status.idle":"2022-09-10T15:56:31.620402Z","shell.execute_reply.started":"2022-09-10T15:56:31.610773Z","shell.execute_reply":"2022-09-10T15:56:31.619578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Write Submission CSV\ntest_df.to_csv('submission2.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.62145Z","iopub.execute_input":"2022-09-10T15:56:31.622176Z","iopub.status.idle":"2022-09-10T15:56:31.632743Z","shell.execute_reply.started":"2022-09-10T15:56:31.622112Z","shell.execute_reply":"2022-09-10T15:56:31.631983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 efficientnet.py","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:56:31.634209Z","iopub.execute_input":"2022-09-10T15:56:31.634474Z","iopub.status.idle":"2022-09-10T15:59:15.281672Z","shell.execute_reply.started":"2022-09-10T15:56:31.634441Z","shell.execute_reply":"2022-09-10T15:59:15.280774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import sys\n\n# print(\"{}{: >25}{}{: >10}{}\".format('|','Variable Name','|','Memory','|'))\n# print(\" ------------------------------------ \")\n# for var_name in dir():\n#     if not var_name.startswith(\"_\") and sys.getsizeof(eval(var_name)) > 1000: #ここだけアレンジ\n#         print(\"{}{: >25}{}{: >10}{}\".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|'))","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.285347Z","iopub.execute_input":"2022-09-10T15:59:15.285585Z","iopub.status.idle":"2022-09-10T15:59:15.290611Z","shell.execute_reply.started":"2022-09-10T15:59:15.285557Z","shell.execute_reply":"2022-09-10T15:59:15.289915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del image, image_patches, mask_binary, mask_patches_pred, mask_pred, mask_pred_resized, train\n# # gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.292306Z","iopub.execute_input":"2022-09-10T15:59:15.292881Z","iopub.status.idle":"2022-09-10T15:59:15.300353Z","shell.execute_reply.started":"2022-09-10T15:59:15.292847Z","shell.execute_reply":"2022-09-10T15:59:15.29962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ensamble","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.301396Z","iopub.execute_input":"2022-09-10T15:59:15.304218Z","iopub.status.idle":"2022-09-10T15:59:15.309473Z","shell.execute_reply.started":"2022-09-10T15:59:15.304187Z","shell.execute_reply":"2022-09-10T15:59:15.308686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1st = pd.read_csv(\"submission1.csv\")\ndf_1st","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.312458Z","iopub.execute_input":"2022-09-10T15:59:15.313009Z","iopub.status.idle":"2022-09-10T15:59:15.336275Z","shell.execute_reply.started":"2022-09-10T15:59:15.312977Z","shell.execute_reply":"2022-09-10T15:59:15.335486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_2nd = pd.read_csv(\"submission2.csv\")\ndf_2nd","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.337478Z","iopub.execute_input":"2022-09-10T15:59:15.337972Z","iopub.status.idle":"2022-09-10T15:59:15.349787Z","shell.execute_reply.started":"2022-09-10T15:59:15.337933Z","shell.execute_reply":"2022-09-10T15:59:15.349084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_3rd = pd.read_csv(\"submission3.csv\")\ndf_3rd","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.35101Z","iopub.execute_input":"2022-09-10T15:59:15.3514Z","iopub.status.idle":"2022-09-10T15:59:15.362961Z","shell.execute_reply.started":"2022-09-10T15:59:15.351366Z","shell.execute_reply":"2022-09-10T15:59:15.362256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/test.csv')\ntest","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.36426Z","iopub.execute_input":"2022-09-10T15:59:15.364698Z","iopub.status.idle":"2022-09-10T15:59:15.379699Z","shell.execute_reply.started":"2022-09-10T15:59:15.364665Z","shell.execute_reply":"2022-09-10T15:59:15.378982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1st.id = df_1st.id.astype(str)\ndf_2nd.id = df_2nd.id.astype(str)\ndf_3rd.id = df_3rd.id.astype(str)\ntest.id = test.id.astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.380839Z","iopub.execute_input":"2022-09-10T15:59:15.381269Z","iopub.status.idle":"2022-09-10T15:59:15.389758Z","shell.execute_reply.started":"2022-09-10T15:59:15.381235Z","shell.execute_reply":"2022-09-10T15:59:15.38896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.merge(df_1st, df_2nd, on=\"id\")\ndf = pd.merge(df, df_3rd, on='id')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.391147Z","iopub.execute_input":"2022-09-10T15:59:15.391586Z","iopub.status.idle":"2022-09-10T15:59:15.412878Z","shell.execute_reply.started":"2022-09-10T15:59:15.39153Z","shell.execute_reply":"2022-09-10T15:59:15.412243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.merge(df, test, on='id')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.414109Z","iopub.execute_input":"2022-09-10T15:59:15.414502Z","iopub.status.idle":"2022-09-10T15:59:15.424834Z","shell.execute_reply.started":"2022-09-10T15:59:15.414457Z","shell.execute_reply":"2022-09-10T15:59:15.424065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.426019Z","iopub.execute_input":"2022-09-10T15:59:15.426578Z","iopub.status.idle":"2022-09-10T15:59:15.442851Z","shell.execute_reply.started":"2022-09-10T15:59:15.426541Z","shell.execute_reply":"2022-09-10T15:59:15.441933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle2mask(mask_rle, shape=(3000,3000)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) 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).T","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.444554Z","iopub.execute_input":"2022-09-10T15:59:15.444876Z","iopub.status.idle":"2022-09-10T15:59:15.452244Z","shell.execute_reply.started":"2022-09-10T15:59:15.444844Z","shell.execute_reply":"2022-09-10T15:59:15.451273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode_less_memory(img):\n    pixels = img.T.flatten()\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    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.453481Z","iopub.execute_input":"2022-09-10T15:59:15.453783Z","iopub.status.idle":"2022-09-10T15:59:15.461371Z","shell.execute_reply.started":"2022-09-10T15:59:15.453739Z","shell.execute_reply":"2022-09-10T15:59:15.460476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rle = []\nfor i in range(len(df)):\n    id, rle1, rle2, rle3, _, _, h, w, _, _ = df.iloc[i]\n    img0 = rle2mask(rle1, shape=(w, h))\n    img1 = rle2mask(rle2, shape=(w, h))\n    img2 = rle2mask(rle3, shape=(w, h))\n    img = img0 + img1 + img2\n    img[img < 2] = 0\n    img[img > 1] = 1\n    rle.append(rle_encode_less_memory(img))\n    if i == 0 :\n        plt.imshow(img0)\n        plt.show()\n        plt.imshow(img1)\n        plt.show()        \n        plt.imshow(img2)\n        plt.show()\n        plt.imshow(img)\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:15.464957Z","iopub.execute_input":"2022-09-10T15:59:15.465167Z","iopub.status.idle":"2022-09-10T15:59:18.321911Z","shell.execute_reply.started":"2022-09-10T15:59:15.465145Z","shell.execute_reply":"2022-09-10T15:59:18.321189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['rle'] = rle\ndf","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:18.325865Z","iopub.execute_input":"2022-09-10T15:59:18.327861Z","iopub.status.idle":"2022-09-10T15:59:18.348586Z","shell.execute_reply.started":"2022-09-10T15:59:18.327821Z","shell.execute_reply":"2022-09-10T15:59:18.347876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[['id','rle']].to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T15:59:18.352096Z","iopub.execute_input":"2022-09-10T15:59:18.353979Z","iopub.status.idle":"2022-09-10T15:59:18.364468Z","shell.execute_reply.started":"2022-09-10T15:59:18.353938Z","shell.execute_reply":"2022-09-10T15:59:18.363613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}