{"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-21T08:57:41.399154Z","iopub.execute_input":"2022-09-21T08:57:41.399425Z","iopub.status.idle":"2022-09-21T08:59:30.627549Z","shell.execute_reply.started":"2022-09-21T08:57:41.399348Z","shell.execute_reply":"2022-09-21T08:59:30.626657Z"},"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-21T08:59:30.629815Z","iopub.execute_input":"2022-09-21T08:59:30.630094Z","iopub.status.idle":"2022-09-21T08:59:30.633707Z","shell.execute_reply.started":"2022-09-21T08:59:30.630058Z","shell.execute_reply":"2022-09-21T08:59:30.633030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install tensorflow==2.4.1","metadata":{"execution":{"iopub.status.busy":"2022-09-21T08:59:30.634808Z","iopub.execute_input":"2022-09-21T08:59:30.635035Z","iopub.status.idle":"2022-09-21T08:59:30.643581Z","shell.execute_reply.started":"2022-09-21T08:59:30.634998Z","shell.execute_reply":"2022-09-21T08:59:30.642947Z"},"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-21T08:59:30.645662Z","iopub.execute_input":"2022-09-21T08:59:30.645959Z","iopub.status.idle":"2022-09-21T08:59:58.287513Z","shell.execute_reply.started":"2022-09-21T08:59:30.645928Z","shell.execute_reply":"2022-09-21T08:59:58.286553Z"},"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-21T08:59:58.289240Z","iopub.execute_input":"2022-09-21T08:59:58.289549Z","iopub.status.idle":"2022-09-21T09:00:25.546209Z","shell.execute_reply.started":"2022-09-21T08:59:58.289508Z","shell.execute_reply":"2022-09-21T09:00:25.545315Z"},"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-21T09:00:25.549157Z","iopub.execute_input":"2022-09-21T09:00:25.549463Z","iopub.status.idle":"2022-09-21T09:00:57.007717Z","shell.execute_reply.started":"2022-09-21T09:00:25.549423Z","shell.execute_reply":"2022-09-21T09:00:57.006883Z"},"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-21T09:00:57.009447Z","iopub.execute_input":"2022-09-21T09:00:57.009719Z","iopub.status.idle":"2022-09-21T09:00:57.014939Z","shell.execute_reply.started":"2022-09-21T09:00:57.009687Z","shell.execute_reply":"2022-09-21T09:00:57.014150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /tmp/score","metadata":{"execution":{"iopub.status.busy":"2022-09-21T09:00:57.016407Z","iopub.execute_input":"2022-09-21T09:00:57.017015Z","iopub.status.idle":"2022-09-21T09:00:57.963953Z","shell.execute_reply.started":"2022-09-21T09:00:57.016979Z","shell.execute_reply":"2022-09-21T09:00:57.962918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SCORE_DIR = '/tmp/score'","metadata":{"execution":{"iopub.status.busy":"2022-09-21T09:00:57.966439Z","iopub.execute_input":"2022-09-21T09:00:57.966682Z","iopub.status.idle":"2022-09-21T09:00:57.970935Z","shell.execute_reply.started":"2022-09-21T09:00:57.966652Z","shell.execute_reply":"2022-09-21T09:00:57.970152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Efficientnet-B7","metadata":{}},{"cell_type":"code","source":"%%writefile efficient-b7.py\nSCORE_DIR = '/tmp/score/'\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-21T09:00:57.975322Z","iopub.execute_input":"2022-09-21T09:00:57.975927Z","iopub.status.idle":"2022-09-21T09:00:57.984167Z","shell.execute_reply.started":"2022-09-21T09:00:57.975880Z","shell.execute_reply":"2022-09-21T09:00:57.983262Z"},"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.15, 'kidney':0.4, 'largeintestine':0.4, 'prostate':0.4, 'spleen':0.4}\n    \n    ARCH = 'unetplusplus'\n    BACKBONE = 'efficientnet-b7'\n    WEIGHT = '../input/hubmap-2022-model-v2/'\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-21T09:00:57.985883Z","iopub.execute_input":"2022-09-21T09:00:57.986218Z","iopub.status.idle":"2022-09-21T09:00:57.994105Z","shell.execute_reply.started":"2022-09-21T09:00:57.986184Z","shell.execute_reply":"2022-09-21T09:00:57.993143Z"},"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-21T09:00:57.995655Z","iopub.execute_input":"2022-09-21T09:00:57.995997Z","iopub.status.idle":"2022-09-21T09:00:58.008233Z","shell.execute_reply.started":"2022-09-21T09:00:57.995961Z","shell.execute_reply":"2022-09-21T09:00:58.007429Z"},"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-21T09:00:58.009787Z","iopub.execute_input":"2022-09-21T09:00:58.010117Z","iopub.status.idle":"2022-09-21T09:00:58.017968Z","shell.execute_reply.started":"2022-09-21T09:00:58.010083Z","shell.execute_reply":"2022-09-21T09:00:58.017199Z"},"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-21T09:00:58.019455Z","iopub.execute_input":"2022-09-21T09:00:58.019797Z","iopub.status.idle":"2022-09-21T09:00:58.029689Z","shell.execute_reply.started":"2022-09-21T09:00:58.019720Z","shell.execute_reply":"2022-09-21T09:00:58.028811Z"},"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-21T09:00:58.031244Z","iopub.execute_input":"2022-09-21T09:00:58.031778Z","iopub.status.idle":"2022-09-21T09:00:58.044376Z","shell.execute_reply.started":"2022-09-21T09:00:58.031726Z","shell.execute_reply":"2022-09-21T09:00:58.043410Z"},"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-21T09:00:58.045862Z","iopub.execute_input":"2022-09-21T09:00:58.046133Z","iopub.status.idle":"2022-09-21T09:00:58.059234Z","shell.execute_reply.started":"2022-09-21T09:00:58.046100Z","shell.execute_reply":"2022-09-21T09:00:58.058418Z"},"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-21T09:00:58.060718Z","iopub.execute_input":"2022-09-21T09:00:58.060982Z","iopub.status.idle":"2022-09-21T09:00:58.072556Z","shell.execute_reply.started":"2022-09-21T09:00:58.060952Z","shell.execute_reply":"2022-09-21T09:00:58.071825Z"},"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}_score_dice.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-21T09:00:58.073955Z","iopub.execute_input":"2022-09-21T09:00:58.074377Z","iopub.status.idle":"2022-09-21T09:00:58.086130Z","shell.execute_reply.started":"2022-09-21T09:00:58.074341Z","shell.execute_reply":"2022-09-21T09:00:58.085328Z"},"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-21T09:00:58.087058Z","iopub.execute_input":"2022-09-21T09:00:58.087254Z","iopub.status.idle":"2022-09-21T09:00:58.098426Z","shell.execute_reply.started":"2022-09-21T09:00:58.087232Z","shell.execute_reply":"2022-09-21T09:00:58.097679Z"},"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\nres_pattern = [256, 512, 768]\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        \n        mask = None\n            \n        for res in res_pattern:\n            ds = HuBMAPDataset(idx, sz=res, 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            if mask == None:\n                mask = _mask\n            else:\n                mask += _mask\n\n        mask /= len(res_pattern)\n        \n        mask0 = mask.reshape(mask.shape[2], mask.shape[3])\n        filaneme = f'{SCORE_DIR}{idx}.npy'\n        np.save(filaneme, mask0)\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-21T09:00:58.099882Z","iopub.execute_input":"2022-09-21T09:00:58.100199Z","iopub.status.idle":"2022-09-21T09:00:58.109413Z","shell.execute_reply.started":"2022-09-21T09:00:58.100167Z","shell.execute_reply":"2022-09-21T09:00:58.108418Z"},"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-21T09:00:58.110644Z","iopub.execute_input":"2022-09-21T09:00:58.110929Z","iopub.status.idle":"2022-09-21T09:00:58.122564Z","shell.execute_reply.started":"2022-09-21T09:00:58.110888Z","shell.execute_reply":"2022-09-21T09:00:58.121725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 efficient-b7.py","metadata":{"execution":{"iopub.status.busy":"2022-09-21T09:00:58.123913Z","iopub.execute_input":"2022-09-21T09:00:58.124304Z","iopub.status.idle":"2022-09-21T09:02:11.971087Z","shell.execute_reply.started":"2022-09-21T09:00:58.124272Z","shell.execute_reply":"2022-09-21T09:02:11.970139Z"},"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-21T09:02:11.973024Z","iopub.execute_input":"2022-09-21T09:02:11.973402Z","iopub.status.idle":"2022-09-21T09:02:12.280380Z","shell.execute_reply.started":"2022-09-21T09:02:11.973356Z","shell.execute_reply":"2022-09-21T09:02:12.279614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile segformer.py\nSCORE_DIR = '/tmp/score/'\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-21T09:02:12.281786Z","iopub.execute_input":"2022-09-21T09:02:12.282028Z","iopub.status.idle":"2022-09-21T09:02:12.287855Z","shell.execute_reply.started":"2022-09-21T09:02:12.281997Z","shell.execute_reply":"2022-09-21T09:02:12.287003Z"},"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-21T09:02:12.289102Z","iopub.execute_input":"2022-09-21T09:02:12.289843Z","iopub.status.idle":"2022-09-21T09:02:12.301629Z","shell.execute_reply.started":"2022-09-21T09:02:12.289807Z","shell.execute_reply":"2022-09-21T09:02:12.300806Z"},"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-21T09:02:12.303084Z","iopub.execute_input":"2022-09-21T09:02:12.303620Z","iopub.status.idle":"2022-09-21T09:02:12.313153Z","shell.execute_reply.started":"2022-09-21T09:02:12.303587Z","shell.execute_reply":"2022-09-21T09:02:12.312223Z"},"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-21T09:02:12.314523Z","iopub.execute_input":"2022-09-21T09:02:12.314813Z","iopub.status.idle":"2022-09-21T09:02:12.326967Z","shell.execute_reply.started":"2022-09-21T09:02:12.314781Z","shell.execute_reply":"2022-09-21T09:02:12.326128Z"},"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-21T09:02:12.333542Z","iopub.execute_input":"2022-09-21T09:02:12.333755Z","iopub.status.idle":"2022-09-21T09:02:12.341229Z","shell.execute_reply.started":"2022-09-21T09:02:12.333712Z","shell.execute_reply":"2022-09-21T09:02:12.340455Z"},"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-21T09:02:12.342373Z","iopub.execute_input":"2022-09-21T09:02:12.342724Z","iopub.status.idle":"2022-09-21T09:02:12.350845Z","shell.execute_reply.started":"2022-09-21T09:02:12.342692Z","shell.execute_reply":"2022-09-21T09:02:12.349913Z"},"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-21T09:02:12.352185Z","iopub.execute_input":"2022-09-21T09:02:12.352429Z","iopub.status.idle":"2022-09-21T09:02:12.361583Z","shell.execute_reply.started":"2022-09-21T09:02:12.352400Z","shell.execute_reply":"2022-09-21T09:02:12.360776Z"},"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-21T09:02:12.363046Z","iopub.execute_input":"2022-09-21T09:02:12.363394Z","iopub.status.idle":"2022-09-21T09:02:12.371372Z","shell.execute_reply.started":"2022-09-21T09:02:12.363270Z","shell.execute_reply":"2022-09-21T09:02:12.370538Z"},"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\n    filaneme = f'{SCORE_DIR}{idx}.npy'\n    mask0 = np.load(filaneme)\n    mask0 += pred    \n    np.save(filaneme, mask0)\n\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-21T09:02:12.372917Z","iopub.execute_input":"2022-09-21T09:02:12.373167Z","iopub.status.idle":"2022-09-21T09:02:12.381776Z","shell.execute_reply.started":"2022-09-21T09:02:12.373138Z","shell.execute_reply":"2022-09-21T09:02:12.380804Z"},"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-21T09:02:12.383022Z","iopub.execute_input":"2022-09-21T09:02:12.383451Z","iopub.status.idle":"2022-09-21T09:02:12.397381Z","shell.execute_reply.started":"2022-09-21T09:02:12.383397Z","shell.execute_reply":"2022-09-21T09:02:12.396711Z"},"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-21T09:02:12.398624Z","iopub.execute_input":"2022-09-21T09:02:12.399169Z","iopub.status.idle":"2022-09-21T09:02:12.406836Z","shell.execute_reply.started":"2022-09-21T09:02:12.399132Z","shell.execute_reply":"2022-09-21T09:02:12.405958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 segformer.py","metadata":{"execution":{"iopub.status.busy":"2022-09-21T09:02:12.408337Z","iopub.execute_input":"2022-09-21T09:02:12.408832Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"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\nSCORE_DIR = '/tmp/score/'\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":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Plot model summary\nmodel.summary()","metadata":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"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":{"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    idx = row['id']\n    filaneme = f'{SCORE_DIR}{idx}.npy'\n    mask0 = np.load(filaneme)\n    mask0 += mask_pred_resized    \n    np.save(filaneme, mask0)\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":{"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":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 efficientnet.py","metadata":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ensamble with Score","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt","metadata":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SCORE_DIR = '/tmp/score/'\nTH = {'lung':0.2, 'kidney':0.6, 'largeintestine':0.6, 'prostate':0.6, 'spleen':0.6}","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/test.csv')\ntest","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rle = []\nfor i in range(len(test)):\n    id, organ, source, h, w, _, _ = test.iloc[i]\n    if source != \"Hubmap\" :\n        rle.append(\"\")\n        continue\n\n    filepath = os.path.join(SCORE_DIR, f'{id}.npy')\n    mask_src = np.load(filepath) / 3.0    \n    mask = (mask_src > TH[organ]).astype(np.uint8)\n    _rle = rle_encode_less_memory(mask)\n    rle.append(_rle)\n    \n    if i == 0 :\n        plt.imshow(mask)\n        plt.show()\n        plt.imshow(mask_src)\n        plt.show()\n        \ntest['rle'] = rle\ntest","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[['id','rle']].to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}