{"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":{"papermill":{"duration":112.374219,"end_time":"2022-09-15T22:09:09.937036","exception":false,"start_time":"2022-09-15T22:07:17.562817","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:33:00.993253Z","iopub.execute_input":"2022-09-21T03:33:00.993506Z","iopub.status.idle":"2022-09-21T03:34:50.173977Z","shell.execute_reply.started":"2022-09-21T03:33:00.993431Z","shell.execute_reply":"2022-09-21T03:34:50.173085Z"},"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":{"papermill":{"duration":0.07179,"end_time":"2022-09-15T22:09:10.074419","exception":false,"start_time":"2022-09-15T22:09:10.002629","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:34:50.177458Z","iopub.execute_input":"2022-09-21T03:34:50.177703Z","iopub.status.idle":"2022-09-21T03:34:50.181102Z","shell.execute_reply.started":"2022-09-21T03:34:50.177655Z","shell.execute_reply":"2022-09-21T03:34:50.180405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install tensorflow==2.4.1","metadata":{"papermill":{"duration":0.071521,"end_time":"2022-09-15T22:09:10.210196","exception":false,"start_time":"2022-09-15T22:09:10.138675","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:34:50.183454Z","iopub.execute_input":"2022-09-21T03:34:50.183703Z","iopub.status.idle":"2022-09-21T03:34:50.196376Z","shell.execute_reply.started":"2022-09-21T03:34:50.183657Z","shell.execute_reply":"2022-09-21T03:34:50.195684Z"},"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":{"papermill":{"duration":28.164619,"end_time":"2022-09-15T22:09:38.522793","exception":false,"start_time":"2022-09-15T22:09:10.358174","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:34:50.198056Z","iopub.execute_input":"2022-09-21T03:34:50.198258Z","iopub.status.idle":"2022-09-21T03:35:17.482081Z","shell.execute_reply.started":"2022-09-21T03:34:50.198226Z","shell.execute_reply":"2022-09-21T03:35:17.481224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/hubmapsegformerb5/huggingface_hub-0.9.1-py3-none-any.whl","metadata":{"papermill":{"duration":28.612124,"end_time":"2022-09-15T22:10:07.209689","exception":false,"start_time":"2022-09-15T22:09:38.597565","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:35:17.483970Z","iopub.execute_input":"2022-09-21T03:35:17.484252Z","iopub.status.idle":"2022-09-21T03:35:44.507633Z","shell.execute_reply.started":"2022-09-21T03:35:17.484206Z","shell.execute_reply":"2022-09-21T03:35:44.506752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/hubmapsegformerb5/transformers-4.21.2-py3-none-any.whl","metadata":{"papermill":{"duration":32.602238,"end_time":"2022-09-15T22:10:39.877674","exception":false,"start_time":"2022-09-15T22:10:07.275436","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:35:44.509472Z","iopub.execute_input":"2022-09-21T03:35:44.509772Z","iopub.status.idle":"2022-09-21T03:36:15.957822Z","shell.execute_reply.started":"2022-09-21T03:35:44.509736Z","shell.execute_reply":"2022-09-21T03:36:15.956822Z"},"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":{"papermill":{"duration":0.073419,"end_time":"2022-09-15T22:10:40.018917","exception":false,"start_time":"2022-09-15T22:10:39.945498","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:15.959859Z","iopub.execute_input":"2022-09-21T03:36:15.960142Z","iopub.status.idle":"2022-09-21T03:36:15.964324Z","shell.execute_reply.started":"2022-09-21T03:36:15.960105Z","shell.execute_reply":"2022-09-21T03:36:15.963301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Efficientnet-B7","metadata":{"papermill":{"duration":0.067446,"end_time":"2022-09-15T22:10:40.152985","exception":false,"start_time":"2022-09-15T22:10:40.085539","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.076169,"end_time":"2022-09-15T22:10:40.295970","exception":false,"start_time":"2022-09-15T22:10:40.219801","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:15.967757Z","iopub.execute_input":"2022-09-21T03:36:15.968211Z","iopub.status.idle":"2022-09-21T03:36:15.976984Z","shell.execute_reply.started":"2022-09-21T03:36:15.968171Z","shell.execute_reply":"2022-09-21T03:36:15.976338Z"},"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":{"papermill":{"duration":0.079211,"end_time":"2022-09-15T22:10:40.443780","exception":false,"start_time":"2022-09-15T22:10:40.364569","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:15.978298Z","iopub.execute_input":"2022-09-21T03:36:15.978551Z","iopub.status.idle":"2022-09-21T03:36:15.987746Z","shell.execute_reply.started":"2022-09-21T03:36:15.978519Z","shell.execute_reply":"2022-09-21T03:36:15.986805Z"},"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":{"papermill":{"duration":0.076767,"end_time":"2022-09-15T22:10:40.589926","exception":false,"start_time":"2022-09-15T22:10:40.513159","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:15.990798Z","iopub.execute_input":"2022-09-21T03:36:15.990993Z","iopub.status.idle":"2022-09-21T03:36:16.003465Z","shell.execute_reply.started":"2022-09-21T03:36:15.990971Z","shell.execute_reply":"2022-09-21T03:36:16.002742Z"},"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":{"papermill":{"duration":0.077467,"end_time":"2022-09-15T22:10:40.738324","exception":false,"start_time":"2022-09-15T22:10:40.660857","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.004442Z","iopub.execute_input":"2022-09-21T03:36:16.004644Z","iopub.status.idle":"2022-09-21T03:36:16.012530Z","shell.execute_reply.started":"2022-09-21T03:36:16.004618Z","shell.execute_reply":"2022-09-21T03:36:16.011732Z"},"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":{"papermill":{"duration":0.081482,"end_time":"2022-09-15T22:10:40.888259","exception":false,"start_time":"2022-09-15T22:10:40.806777","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.014084Z","iopub.execute_input":"2022-09-21T03:36:16.014432Z","iopub.status.idle":"2022-09-21T03:36:16.021995Z","shell.execute_reply.started":"2022-09-21T03:36:16.014402Z","shell.execute_reply":"2022-09-21T03:36:16.021234Z"},"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":{"papermill":{"duration":0.080534,"end_time":"2022-09-15T22:10:41.038879","exception":false,"start_time":"2022-09-15T22:10:40.958345","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.023463Z","iopub.execute_input":"2022-09-21T03:36:16.023869Z","iopub.status.idle":"2022-09-21T03:36:16.036422Z","shell.execute_reply.started":"2022-09-21T03:36:16.023836Z","shell.execute_reply":"2022-09-21T03:36:16.035527Z"},"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":{"papermill":{"duration":0.081923,"end_time":"2022-09-15T22:10:41.191295","exception":false,"start_time":"2022-09-15T22:10:41.109372","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.037856Z","iopub.execute_input":"2022-09-21T03:36:16.038107Z","iopub.status.idle":"2022-09-21T03:36:16.050479Z","shell.execute_reply.started":"2022-09-21T03:36:16.038075Z","shell.execute_reply":"2022-09-21T03:36:16.049565Z"},"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":{"papermill":{"duration":0.078789,"end_time":"2022-09-15T22:10:41.340070","exception":false,"start_time":"2022-09-15T22:10:41.261281","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.051933Z","iopub.execute_input":"2022-09-21T03:36:16.052285Z","iopub.status.idle":"2022-09-21T03:36:16.063592Z","shell.execute_reply.started":"2022-09-21T03:36:16.052147Z","shell.execute_reply":"2022-09-21T03:36:16.062747Z"},"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":{"papermill":{"duration":0.094505,"end_time":"2022-09-15T22:10:41.504988","exception":false,"start_time":"2022-09-15T22:10:41.410483","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.064745Z","iopub.execute_input":"2022-09-21T03:36:16.065118Z","iopub.status.idle":"2022-09-21T03:36:16.076066Z","shell.execute_reply.started":"2022-09-21T03:36:16.065084Z","shell.execute_reply":"2022-09-21T03:36:16.075290Z"},"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":{"papermill":{"duration":0.081283,"end_time":"2022-09-15T22:10:41.657013","exception":false,"start_time":"2022-09-15T22:10:41.575730","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.077181Z","iopub.execute_input":"2022-09-21T03:36:16.077485Z","iopub.status.idle":"2022-09-21T03:36:16.089240Z","shell.execute_reply.started":"2022-09-21T03:36:16.077454Z","shell.execute_reply":"2022-09-21T03:36:16.088325Z"},"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":{"papermill":{"duration":0.079731,"end_time":"2022-09-15T22:10:41.809607","exception":false,"start_time":"2022-09-15T22:10:41.729876","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.090633Z","iopub.execute_input":"2022-09-21T03:36:16.091313Z","iopub.status.idle":"2022-09-21T03:36:16.100328Z","shell.execute_reply.started":"2022-09-21T03:36:16.091279Z","shell.execute_reply":"2022-09-21T03:36:16.099395Z"},"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":{"papermill":{"duration":0.080098,"end_time":"2022-09-15T22:10:41.960612","exception":false,"start_time":"2022-09-15T22:10:41.880514","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.101518Z","iopub.execute_input":"2022-09-21T03:36:16.101950Z","iopub.status.idle":"2022-09-21T03:36:16.114185Z","shell.execute_reply.started":"2022-09-21T03:36:16.101916Z","shell.execute_reply":"2022-09-21T03:36:16.113413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 efficient-b7.py","metadata":{"papermill":{"duration":44.137256,"end_time":"2022-09-15T22:11:26.169388","exception":false,"start_time":"2022-09-15T22:10:42.032132","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:16.115489Z","iopub.execute_input":"2022-09-21T03:36:16.115828Z","iopub.status.idle":"2022-09-21T03:36:57.594044Z","shell.execute_reply.started":"2022-09-21T03:36:16.115796Z","shell.execute_reply":"2022-09-21T03:36:57.593124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SegFormer","metadata":{"papermill":{"duration":0.071282,"end_time":"2022-09-15T22:11:26.311792","exception":false,"start_time":"2022-09-15T22:11:26.240510","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# from transformers import SegformerForSemanticSegmentation\nimport transformers\ntransformers.__version__\n# 4.5.1\n# 4.20.1","metadata":{"papermill":{"duration":0.370082,"end_time":"2022-09-15T22:11:26.753142","exception":false,"start_time":"2022-09-15T22:11:26.383060","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.595617Z","iopub.execute_input":"2022-09-21T03:36:57.595925Z","iopub.status.idle":"2022-09-21T03:36:57.896393Z","shell.execute_reply.started":"2022-09-21T03:36:57.595889Z","shell.execute_reply":"2022-09-21T03:36:57.895745Z"},"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":{"papermill":{"duration":0.085815,"end_time":"2022-09-15T22:11:26.913008","exception":false,"start_time":"2022-09-15T22:11:26.827193","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.897580Z","iopub.execute_input":"2022-09-21T03:36:57.899119Z","iopub.status.idle":"2022-09-21T03:36:57.905877Z","shell.execute_reply.started":"2022-09-21T03:36:57.899091Z","shell.execute_reply":"2022-09-21T03:36:57.905162Z"},"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":{"papermill":{"duration":0.080181,"end_time":"2022-09-15T22:11:27.066976","exception":false,"start_time":"2022-09-15T22:11:26.986795","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.911152Z","iopub.execute_input":"2022-09-21T03:36:57.911711Z","iopub.status.idle":"2022-09-21T03:36:57.928587Z","shell.execute_reply.started":"2022-09-21T03:36:57.911680Z","shell.execute_reply":"2022-09-21T03:36:57.927615Z"},"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-fold0-4.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/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":{"papermill":{"duration":0.081442,"end_time":"2022-09-15T22:11:27.221271","exception":false,"start_time":"2022-09-15T22:11:27.139829","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.929901Z","iopub.execute_input":"2022-09-21T03:36:57.930417Z","iopub.status.idle":"2022-09-21T03:36:57.940622Z","shell.execute_reply.started":"2022-09-21T03:36:57.930381Z","shell.execute_reply":"2022-09-21T03:36:57.939954Z"},"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":{"papermill":{"duration":0.08015,"end_time":"2022-09-15T22:11:27.376767","exception":false,"start_time":"2022-09-15T22:11:27.296617","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.941968Z","iopub.execute_input":"2022-09-21T03:36:57.942234Z","iopub.status.idle":"2022-09-21T03:36:57.952508Z","shell.execute_reply.started":"2022-09-21T03:36:57.942196Z","shell.execute_reply":"2022-09-21T03:36:57.951645Z"},"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":{"papermill":{"duration":0.082289,"end_time":"2022-09-15T22:11:27.531073","exception":false,"start_time":"2022-09-15T22:11:27.448784","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.953919Z","iopub.execute_input":"2022-09-21T03:36:57.954321Z","iopub.status.idle":"2022-09-21T03:36:57.962986Z","shell.execute_reply.started":"2022-09-21T03:36:57.954287Z","shell.execute_reply":"2022-09-21T03:36:57.961761Z"},"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":{"papermill":{"duration":0.124105,"end_time":"2022-09-15T22:11:27.727391","exception":false,"start_time":"2022-09-15T22:11:27.603286","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.964371Z","iopub.execute_input":"2022-09-21T03:36:57.965160Z","iopub.status.idle":"2022-09-21T03:36:57.976210Z","shell.execute_reply.started":"2022-09-21T03:36:57.965127Z","shell.execute_reply":"2022-09-21T03:36:57.975462Z"},"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":{"papermill":{"duration":0.080299,"end_time":"2022-09-15T22:11:27.881169","exception":false,"start_time":"2022-09-15T22:11:27.800870","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.977530Z","iopub.execute_input":"2022-09-21T03:36:57.977968Z","iopub.status.idle":"2022-09-21T03:36:57.985653Z","shell.execute_reply.started":"2022-09-21T03:36:57.977925Z","shell.execute_reply":"2022-09-21T03:36:57.984804Z"},"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":{"papermill":{"duration":0.082612,"end_time":"2022-09-15T22:11:28.037465","exception":false,"start_time":"2022-09-15T22:11:27.954853","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.987040Z","iopub.execute_input":"2022-09-21T03:36:57.987395Z","iopub.status.idle":"2022-09-21T03:36:57.996657Z","shell.execute_reply.started":"2022-09-21T03:36:57.987364Z","shell.execute_reply":"2022-09-21T03:36:57.995743Z"},"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":{"papermill":{"duration":0.083992,"end_time":"2022-09-15T22:11:28.195298","exception":false,"start_time":"2022-09-15T22:11:28.111306","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:57.998112Z","iopub.execute_input":"2022-09-21T03:36:57.998559Z","iopub.status.idle":"2022-09-21T03:36:58.008753Z","shell.execute_reply.started":"2022-09-21T03:36:57.998526Z","shell.execute_reply":"2022-09-21T03:36:58.007953Z"},"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":{"papermill":{"duration":0.082057,"end_time":"2022-09-15T22:11:28.362991","exception":false,"start_time":"2022-09-15T22:11:28.280934","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:58.009864Z","iopub.execute_input":"2022-09-21T03:36:58.010527Z","iopub.status.idle":"2022-09-21T03:36:58.023566Z","shell.execute_reply.started":"2022-09-21T03:36:58.010489Z","shell.execute_reply":"2022-09-21T03:36:58.022725Z"},"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":{"papermill":{"duration":0.082703,"end_time":"2022-09-15T22:11:28.519341","exception":false,"start_time":"2022-09-15T22:11:28.436638","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:58.024862Z","iopub.execute_input":"2022-09-21T03:36:58.025300Z","iopub.status.idle":"2022-09-21T03:36:58.034291Z","shell.execute_reply.started":"2022-09-21T03:36:58.025268Z","shell.execute_reply":"2022-09-21T03:36:58.033590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 segformer.py","metadata":{"papermill":{"duration":132.971818,"end_time":"2022-09-15T22:13:41.566166","exception":false,"start_time":"2022-09-15T22:11:28.594348","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:36:58.035953Z","iopub.execute_input":"2022-09-21T03:36:58.036154Z","iopub.status.idle":"2022-09-21T03:38:55.226525Z","shell.execute_reply.started":"2022-09-21T03:36:58.036129Z","shell.execute_reply":"2022-09-21T03:38:55.225676Z"},"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":{"papermill":{"duration":0.081528,"end_time":"2022-09-15T22:13:41.746135","exception":false,"start_time":"2022-09-15T22:13:41.664607","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:38:55.229704Z","iopub.execute_input":"2022-09-21T03:38:55.229990Z","iopub.status.idle":"2022-09-21T03:38:55.233870Z","shell.execute_reply.started":"2022-09-21T03:38:55.229960Z","shell.execute_reply":"2022-09-21T03:38:55.232961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"papermill":{"duration":1.111024,"end_time":"2022-09-15T22:13:42.931451","exception":false,"start_time":"2022-09-15T22:13:41.820427","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:38:55.234892Z","iopub.execute_input":"2022-09-21T03:38:55.235140Z","iopub.status.idle":"2022-09-21T03:38:56.226445Z","shell.execute_reply.started":"2022-09-21T03:38:55.235109Z","shell.execute_reply":"2022-09-21T03:38:56.225639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SegFormer768x768","metadata":{"papermill":{"duration":0.086222,"end_time":"2022-09-15T22:13:43.103273","exception":false,"start_time":"2022-09-15T22:13:43.017051","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%%writefile segformer768.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\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\")\nTH = 0.1\n\nDATA = '../input/hubmap-organ-segmentation/test_images/'\nCONFIG = '../input/hubmapsegformerb5/mit-b5.pickle'\nMODELS = [\n#     \"../input/hubmapsegformerlast/768-merge-fold0.pth\",\n#     \"../input/hubmapsegformerlast/768-merge-fold1.pth\",\n#     \"../input/hubmapsegformerlast/768-merge-fold2.pth\",\n    \"../input/hubmapsegformerlast/768-model-b5.pth\",\n    \"../input/hubmapsegformerlast/768-model-b5-2.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')\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'}\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)\nDATA = '../input/hubmap-organ-segmentation/test_images/'\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/test.csv').set_index('id')\n\n# DATA = '../input/hubmap-organ-segmentation/train_images/'\n# df_sample = pd.read_csv('../input/hubmap-organ-segmentation/train.csv').set_index('id')\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)\n\n\ndef calc_resize(w, p) :\n    a = 3000/768 # <------------------------------------------ 768\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)\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()   \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()\ndf = pd.DataFrame({'id':names,'rle':preds})\ndf.to_csv('submission4.csv',index=False)","metadata":{"papermill":{"duration":0.094817,"end_time":"2022-09-15T22:13:43.282143","exception":false,"start_time":"2022-09-15T22:13:43.187326","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:38:56.229924Z","iopub.execute_input":"2022-09-21T03:38:56.230172Z","iopub.status.idle":"2022-09-21T03:38:56.241367Z","shell.execute_reply.started":"2022-09-21T03:38:56.230142Z","shell.execute_reply":"2022-09-21T03:38:56.240213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 segformer768.py","metadata":{"papermill":{"duration":22.001308,"end_time":"2022-09-15T22:14:05.367394","exception":false,"start_time":"2022-09-15T22:13:43.366086","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:38:56.242784Z","iopub.execute_input":"2022-09-21T03:38:56.243092Z","iopub.status.idle":"2022-09-21T03:39:16.684616Z","shell.execute_reply.started":"2022-09-21T03:38:56.243060Z","shell.execute_reply":"2022-09-21T03:39:16.683801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# efficientnet","metadata":{"papermill":{"duration":0.085339,"end_time":"2022-09-15T22:14:05.539193","exception":false,"start_time":"2022-09-15T22:14:05.453854","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"Copied from: https://www.kaggle.com/code/markwijkhuizen/hubmap-inference-tf-tpu-efficientnet-b7-640x640","metadata":{"papermill":{"duration":0.085427,"end_time":"2022-09-15T22:14:05.709690","exception":false,"start_time":"2022-09-15T22:14:05.624263","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.09768,"end_time":"2022-09-15T22:14:05.892686","exception":false,"start_time":"2022-09-15T22:14:05.795006","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.687554Z","iopub.execute_input":"2022-09-21T03:39:16.688170Z","iopub.status.idle":"2022-09-21T03:39:16.693696Z","shell.execute_reply.started":"2022-09-21T03:39:16.688130Z","shell.execute_reply":"2022-09-21T03:39:16.693007Z"},"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":{"papermill":{"duration":0.093644,"end_time":"2022-09-15T22:14:06.070825","exception":false,"start_time":"2022-09-15T22:14:05.977181","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.694762Z","iopub.execute_input":"2022-09-21T03:39:16.695076Z","iopub.status.idle":"2022-09-21T03:39:16.704492Z","shell.execute_reply.started":"2022-09-21T03:39:16.695042Z","shell.execute_reply":"2022-09-21T03:39:16.703762Z"},"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":{"papermill":{"duration":0.102828,"end_time":"2022-09-15T22:14:06.260096","exception":false,"start_time":"2022-09-15T22:14:06.157268","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.705900Z","iopub.execute_input":"2022-09-21T03:39:16.706728Z","iopub.status.idle":"2022-09-21T03:39:16.718013Z","shell.execute_reply.started":"2022-09-21T03:39:16.706654Z","shell.execute_reply":"2022-09-21T03:39:16.717087Z"},"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":{"papermill":{"duration":0.097155,"end_time":"2022-09-15T22:14:06.448453","exception":false,"start_time":"2022-09-15T22:14:06.351298","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.719452Z","iopub.execute_input":"2022-09-21T03:39:16.720420Z","iopub.status.idle":"2022-09-21T03:39:16.728175Z","shell.execute_reply.started":"2022-09-21T03:39:16.720394Z","shell.execute_reply":"2022-09-21T03:39:16.727406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Hardware Configuration","metadata":{"papermill":{"duration":0.085555,"end_time":"2022-09-15T22:14:06.621501","exception":false,"start_time":"2022-09-15T22:14:06.535946","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.096092,"end_time":"2022-09-15T22:14:06.803622","exception":false,"start_time":"2022-09-15T22:14:06.707530","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.729550Z","iopub.execute_input":"2022-09-21T03:39:16.730105Z","iopub.status.idle":"2022-09-21T03:39:16.739115Z","shell.execute_reply.started":"2022-09-21T03:39:16.730071Z","shell.execute_reply":"2022-09-21T03:39:16.738419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# FPN","metadata":{"papermill":{"duration":0.086331,"end_time":"2022-09-15T22:14:06.977731","exception":false,"start_time":"2022-09-15T22:14:06.891400","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.094044,"end_time":"2022-09-15T22:14:07.157883","exception":false,"start_time":"2022-09-15T22:14:07.063839","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.740442Z","iopub.execute_input":"2022-09-21T03:39:16.741360Z","iopub.status.idle":"2022-09-21T03:39:16.751050Z","shell.execute_reply.started":"2022-09-21T03:39:16.741302Z","shell.execute_reply":"2022-09-21T03:39:16.750168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ASPP","metadata":{"papermill":{"duration":0.171561,"end_time":"2022-09-15T22:14:07.440402","exception":false,"start_time":"2022-09-15T22:14:07.268841","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.141775,"end_time":"2022-09-15T22:14:07.713063","exception":false,"start_time":"2022-09-15T22:14:07.571288","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.752450Z","iopub.execute_input":"2022-09-21T03:39:16.753467Z","iopub.status.idle":"2022-09-21T03:39:16.761408Z","shell.execute_reply.started":"2022-09-21T03:39:16.753441Z","shell.execute_reply":"2022-09-21T03:39:16.760486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Upsample","metadata":{"papermill":{"duration":0.092041,"end_time":"2022-09-15T22:14:07.942368","exception":false,"start_time":"2022-09-15T22:14:07.850327","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.09483,"end_time":"2022-09-15T22:14:08.123804","exception":false,"start_time":"2022-09-15T22:14:08.028974","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.762777Z","iopub.execute_input":"2022-09-21T03:39:16.763230Z","iopub.status.idle":"2022-09-21T03:39:16.775083Z","shell.execute_reply.started":"2022-09-21T03:39:16.763198Z","shell.execute_reply":"2022-09-21T03:39:16.774129Z"},"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":{"papermill":{"duration":0.131955,"end_time":"2022-09-15T22:14:08.345228","exception":false,"start_time":"2022-09-15T22:14:08.213273","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.776423Z","iopub.execute_input":"2022-09-21T03:39:16.777007Z","iopub.status.idle":"2022-09-21T03:39:16.786034Z","shell.execute_reply.started":"2022-09-21T03:39:16.776973Z","shell.execute_reply":"2022-09-21T03:39:16.785349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"papermill":{"duration":0.086892,"end_time":"2022-09-15T22:14:08.520256","exception":false,"start_time":"2022-09-15T22:14:08.433364","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.098848,"end_time":"2022-09-15T22:14:08.705681","exception":false,"start_time":"2022-09-15T22:14:08.606833","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.787315Z","iopub.execute_input":"2022-09-21T03:39:16.787571Z","iopub.status.idle":"2022-09-21T03:39:16.799829Z","shell.execute_reply.started":"2022-09-21T03:39:16.787539Z","shell.execute_reply":"2022-09-21T03:39:16.798876Z"},"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":{"papermill":{"duration":0.101392,"end_time":"2022-09-15T22:14:08.896336","exception":false,"start_time":"2022-09-15T22:14:08.794944","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.801118Z","iopub.execute_input":"2022-09-21T03:39:16.801473Z","iopub.status.idle":"2022-09-21T03:39:16.813578Z","shell.execute_reply.started":"2022-09-21T03:39:16.801441Z","shell.execute_reply":"2022-09-21T03:39:16.812854Z"},"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":{"papermill":{"duration":0.097224,"end_time":"2022-09-15T22:14:09.084481","exception":false,"start_time":"2022-09-15T22:14:08.987257","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.815022Z","iopub.execute_input":"2022-09-21T03:39:16.815283Z","iopub.status.idle":"2022-09-21T03:39:16.826477Z","shell.execute_reply.started":"2022-09-21T03:39:16.815251Z","shell.execute_reply":"2022-09-21T03:39:16.825579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Plot model summary\nmodel.summary()","metadata":{"papermill":{"duration":0.096615,"end_time":"2022-09-15T22:14:09.270625","exception":false,"start_time":"2022-09-15T22:14:09.174010","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.827933Z","iopub.execute_input":"2022-09-21T03:39:16.828186Z","iopub.status.idle":"2022-09-21T03:39:16.837679Z","shell.execute_reply.started":"2022-09-21T03:39:16.828155Z","shell.execute_reply":"2022-09-21T03:39:16.836863Z"},"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":{"papermill":{"duration":0.096915,"end_time":"2022-09-15T22:14:09.456963","exception":false,"start_time":"2022-09-15T22:14:09.360048","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.838893Z","iopub.execute_input":"2022-09-21T03:39:16.839177Z","iopub.status.idle":"2022-09-21T03:39:16.847800Z","shell.execute_reply.started":"2022-09-21T03:39:16.839142Z","shell.execute_reply":"2022-09-21T03:39:16.847061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utility Funtions","metadata":{"papermill":{"duration":0.087359,"end_time":"2022-09-15T22:14:09.634159","exception":false,"start_time":"2022-09-15T22:14:09.546800","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.096793,"end_time":"2022-09-15T22:14:09.817819","exception":false,"start_time":"2022-09-15T22:14:09.721026","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.848926Z","iopub.execute_input":"2022-09-21T03:39:16.849717Z","iopub.status.idle":"2022-09-21T03:39:16.856691Z","shell.execute_reply.started":"2022-09-21T03:39:16.849657Z","shell.execute_reply":"2022-09-21T03:39:16.855942Z"},"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":{"papermill":{"duration":0.098988,"end_time":"2022-09-15T22:14:10.007828","exception":false,"start_time":"2022-09-15T22:14:09.908840","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.857861Z","iopub.execute_input":"2022-09-21T03:39:16.858486Z","iopub.status.idle":"2022-09-21T03:39:16.865687Z","shell.execute_reply.started":"2022-09-21T03:39:16.858443Z","shell.execute_reply":"2022-09-21T03:39:16.864907Z"},"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":{"papermill":{"duration":0.097827,"end_time":"2022-09-15T22:14:10.195522","exception":false,"start_time":"2022-09-15T22:14:10.097695","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.866913Z","iopub.execute_input":"2022-09-21T03:39:16.867343Z","iopub.status.idle":"2022-09-21T03:39:16.875655Z","shell.execute_reply.started":"2022-09-21T03:39:16.867310Z","shell.execute_reply":"2022-09-21T03:39:16.875084Z"},"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":{"papermill":{"duration":0.097918,"end_time":"2022-09-15T22:14:10.381595","exception":false,"start_time":"2022-09-15T22:14:10.283677","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.876986Z","iopub.execute_input":"2022-09-21T03:39:16.877759Z","iopub.status.idle":"2022-09-21T03:39:16.890713Z","shell.execute_reply.started":"2022-09-21T03:39:16.877722Z","shell.execute_reply":"2022-09-21T03:39:16.889768Z"},"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":{"papermill":{"duration":0.101009,"end_time":"2022-09-15T22:14:10.572725","exception":false,"start_time":"2022-09-15T22:14:10.471716","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.891914Z","iopub.execute_input":"2022-09-21T03:39:16.892710Z","iopub.status.idle":"2022-09-21T03:39:16.902430Z","shell.execute_reply.started":"2022-09-21T03:39:16.892673Z","shell.execute_reply":"2022-09-21T03:39:16.901683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{"papermill":{"duration":0.089993,"end_time":"2022-09-15T22:14:10.753544","exception":false,"start_time":"2022-09-15T22:14:10.663551","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.098414,"end_time":"2022-09-15T22:14:10.940955","exception":false,"start_time":"2022-09-15T22:14:10.842541","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.903585Z","iopub.execute_input":"2022-09-21T03:39:16.904306Z","iopub.status.idle":"2022-09-21T03:39:16.911528Z","shell.execute_reply.started":"2022-09-21T03:39:16.904272Z","shell.execute_reply":"2022-09-21T03:39:16.910759Z"},"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":{"papermill":{"duration":0.102571,"end_time":"2022-09-15T22:14:11.133180","exception":false,"start_time":"2022-09-15T22:14:11.030609","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.912846Z","iopub.execute_input":"2022-09-21T03:39:16.913640Z","iopub.status.idle":"2022-09-21T03:39:16.920547Z","shell.execute_reply.started":"2022-09-21T03:39:16.913602Z","shell.execute_reply":"2022-09-21T03:39:16.919932Z"},"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":{"papermill":{"duration":0.100463,"end_time":"2022-09-15T22:14:11.324099","exception":false,"start_time":"2022-09-15T22:14:11.223636","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.921843Z","iopub.execute_input":"2022-09-21T03:39:16.922462Z","iopub.status.idle":"2022-09-21T03:39:16.933236Z","shell.execute_reply.started":"2022-09-21T03:39:16.922429Z","shell.execute_reply":"2022-09-21T03:39:16.932509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sanity Check","metadata":{"papermill":{"duration":0.090816,"end_time":"2022-09-15T22:14:11.504129","exception":false,"start_time":"2022-09-15T22:14:11.413313","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.10205,"end_time":"2022-09-15T22:14:11.696381","exception":false,"start_time":"2022-09-15T22:14:11.594331","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.934643Z","iopub.execute_input":"2022-09-21T03:39:16.935266Z","iopub.status.idle":"2022-09-21T03:39:16.943434Z","shell.execute_reply.started":"2022-09-21T03:39:16.935223Z","shell.execute_reply":"2022-09-21T03:39:16.942741Z"},"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":{"papermill":{"duration":0.099001,"end_time":"2022-09-15T22:14:11.887059","exception":false,"start_time":"2022-09-15T22:14:11.788058","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.944993Z","iopub.execute_input":"2022-09-21T03:39:16.945247Z","iopub.status.idle":"2022-09-21T03:39:16.974081Z","shell.execute_reply.started":"2022-09-21T03:39:16.945216Z","shell.execute_reply":"2022-09-21T03:39:16.973335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference Loop","metadata":{"papermill":{"duration":0.090172,"end_time":"2022-09-15T22:14:12.068029","exception":false,"start_time":"2022-09-15T22:14:11.977857","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.100179,"end_time":"2022-09-15T22:14:12.258044","exception":false,"start_time":"2022-09-15T22:14:12.157865","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.975392Z","iopub.execute_input":"2022-09-21T03:39:16.976238Z","iopub.status.idle":"2022-09-21T03:39:16.988783Z","shell.execute_reply.started":"2022-09-21T03:39:16.976202Z","shell.execute_reply":"2022-09-21T03:39:16.987810Z"},"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":{"papermill":{"duration":0.097417,"end_time":"2022-09-15T22:14:12.446464","exception":false,"start_time":"2022-09-15T22:14:12.349047","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:16.990071Z","iopub.execute_input":"2022-09-21T03:39:16.990917Z","iopub.status.idle":"2022-09-21T03:39:17.000956Z","shell.execute_reply.started":"2022-09-21T03:39:16.990883Z","shell.execute_reply":"2022-09-21T03:39:17.000171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make Submission CSV","metadata":{"papermill":{"duration":0.089447,"end_time":"2022-09-15T22:14:12.626962","exception":false,"start_time":"2022-09-15T22:14:12.537515","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.101042,"end_time":"2022-09-15T22:14:12.818352","exception":false,"start_time":"2022-09-15T22:14:12.717310","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:17.002020Z","iopub.execute_input":"2022-09-21T03:39:17.002414Z","iopub.status.idle":"2022-09-21T03:39:17.015382Z","shell.execute_reply.started":"2022-09-21T03:39:17.002381Z","shell.execute_reply":"2022-09-21T03:39:17.014597Z"},"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":{"papermill":{"duration":0.099765,"end_time":"2022-09-15T22:14:13.010501","exception":false,"start_time":"2022-09-15T22:14:12.910736","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:17.016593Z","iopub.execute_input":"2022-09-21T03:39:17.017375Z","iopub.status.idle":"2022-09-21T03:39:17.027724Z","shell.execute_reply.started":"2022-09-21T03:39:17.017306Z","shell.execute_reply":"2022-09-21T03:39:17.026773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 efficientnet.py","metadata":{"papermill":{"duration":174.945079,"end_time":"2022-09-15T22:17:08.048116","exception":false,"start_time":"2022-09-15T22:14:13.103037","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:39:17.029161Z","iopub.execute_input":"2022-09-21T03:39:17.029938Z","iopub.status.idle":"2022-09-21T03:41:53.217443Z","shell.execute_reply.started":"2022-09-21T03:39:17.029903Z","shell.execute_reply":"2022-09-21T03:41:53.216611Z"},"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":{"papermill":{"duration":0.147029,"end_time":"2022-09-15T22:17:08.297860","exception":false,"start_time":"2022-09-15T22:17:08.150831","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:41:53.219279Z","iopub.execute_input":"2022-09-21T03:41:53.219559Z","iopub.status.idle":"2022-09-21T03:41:53.224386Z","shell.execute_reply.started":"2022-09-21T03:41:53.219523Z","shell.execute_reply":"2022-09-21T03:41:53.223579Z"},"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":{"papermill":{"duration":0.107588,"end_time":"2022-09-15T22:17:08.507312","exception":false,"start_time":"2022-09-15T22:17:08.399724","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:41:53.231697Z","iopub.execute_input":"2022-09-21T03:41:53.231926Z","iopub.status.idle":"2022-09-21T03:41:53.235075Z","shell.execute_reply.started":"2022-09-21T03:41:53.231904Z","shell.execute_reply":"2022-09-21T03:41:53.234298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ensamble","metadata":{"papermill":{"duration":0.101725,"end_time":"2022-09-15T22:17:08.709678","exception":false,"start_time":"2022-09-15T22:17:08.607953","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"papermill":{"duration":0.10805,"end_time":"2022-09-15T22:17:08.918312","exception":false,"start_time":"2022-09-15T22:17:08.810262","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:27.241181Z","iopub.execute_input":"2022-09-21T03:44:27.241464Z","iopub.status.idle":"2022-09-21T03:44:27.245791Z","shell.execute_reply.started":"2022-09-21T03:44:27.241434Z","shell.execute_reply":"2022-09-21T03:44:27.244794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1st = pd.read_csv(\"submission1.csv\")\ndf_1st","metadata":{"papermill":{"duration":0.129742,"end_time":"2022-09-15T22:17:09.149209","exception":false,"start_time":"2022-09-15T22:17:09.019467","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:27.337027Z","iopub.execute_input":"2022-09-21T03:44:27.337243Z","iopub.status.idle":"2022-09-21T03:44:27.349394Z","shell.execute_reply.started":"2022-09-21T03:44:27.337219Z","shell.execute_reply":"2022-09-21T03:44:27.348577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_2nd = pd.read_csv(\"submission2.csv\")\ndf_2nd","metadata":{"papermill":{"duration":0.11896,"end_time":"2022-09-15T22:17:09.369256","exception":false,"start_time":"2022-09-15T22:17:09.250296","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:27.764597Z","iopub.execute_input":"2022-09-21T03:44:27.765040Z","iopub.status.idle":"2022-09-21T03:44:27.780043Z","shell.execute_reply.started":"2022-09-21T03:44:27.765002Z","shell.execute_reply":"2022-09-21T03:44:27.779386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_3rd = pd.read_csv(\"submission3.csv\")\ndf_3rd","metadata":{"papermill":{"duration":0.116101,"end_time":"2022-09-15T22:17:09.587803","exception":false,"start_time":"2022-09-15T22:17:09.471702","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:27.781940Z","iopub.execute_input":"2022-09-21T03:44:27.782430Z","iopub.status.idle":"2022-09-21T03:44:27.792898Z","shell.execute_reply.started":"2022-09-21T03:44:27.782394Z","shell.execute_reply":"2022-09-21T03:44:27.792068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_4th = pd.read_csv(\"submission4.csv\")\ndf_4th","metadata":{"papermill":{"duration":0.118409,"end_time":"2022-09-15T22:17:09.808195","exception":false,"start_time":"2022-09-15T22:17:09.689786","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:28.097417Z","iopub.execute_input":"2022-09-21T03:44:28.097749Z","iopub.status.idle":"2022-09-21T03:44:28.115456Z","shell.execute_reply.started":"2022-09-21T03:44:28.097715Z","shell.execute_reply":"2022-09-21T03:44:28.114727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/test.csv')\ntest","metadata":{"papermill":{"duration":0.124822,"end_time":"2022-09-15T22:17:10.038018","exception":false,"start_time":"2022-09-15T22:17:09.913196","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:28.116759Z","iopub.execute_input":"2022-09-21T03:44:28.117415Z","iopub.status.idle":"2022-09-21T03:44:28.136338Z","shell.execute_reply.started":"2022-09-21T03:44:28.117380Z","shell.execute_reply":"2022-09-21T03:44:28.135750Z"},"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)\ndf_4th.id = df_4th.id.astype(str)\ntest.id = test.id.astype(str)","metadata":{"papermill":{"duration":0.116637,"end_time":"2022-09-15T22:17:10.259337","exception":false,"start_time":"2022-09-15T22:17:10.142700","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:28.490560Z","iopub.execute_input":"2022-09-21T03:44:28.491324Z","iopub.status.idle":"2022-09-21T03:44:28.498547Z","shell.execute_reply.started":"2022-09-21T03:44:28.491290Z","shell.execute_reply":"2022-09-21T03:44:28.497707Z"},"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')\ndf = pd.merge(df, df_4th, on='id')","metadata":{"papermill":{"duration":0.136388,"end_time":"2022-09-15T22:17:10.498733","exception":false,"start_time":"2022-09-15T22:17:10.362345","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:28.895551Z","iopub.execute_input":"2022-09-21T03:44:28.896200Z","iopub.status.idle":"2022-09-21T03:44:28.912557Z","shell.execute_reply.started":"2022-09-21T03:44:28.896169Z","shell.execute_reply":"2022-09-21T03:44:28.911773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.merge(df, test, on='id')","metadata":{"papermill":{"duration":0.117431,"end_time":"2022-09-15T22:17:10.720194","exception":false,"start_time":"2022-09-15T22:17:10.602763","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:29.213200Z","iopub.execute_input":"2022-09-21T03:44:29.213452Z","iopub.status.idle":"2022-09-21T03:44:29.222462Z","shell.execute_reply.started":"2022-09-21T03:44:29.213425Z","shell.execute_reply":"2022-09-21T03:44:29.221644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"papermill":{"duration":0.124019,"end_time":"2022-09-15T22:17:10.947979","exception":false,"start_time":"2022-09-15T22:17:10.823960","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:29.713400Z","iopub.execute_input":"2022-09-21T03:44:29.713655Z","iopub.status.idle":"2022-09-21T03:44:29.728341Z","shell.execute_reply.started":"2022-09-21T03:44:29.713626Z","shell.execute_reply":"2022-09-21T03:44:29.727472Z"},"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":{"papermill":{"duration":0.113925,"end_time":"2022-09-15T22:17:11.167588","exception":false,"start_time":"2022-09-15T22:17:11.053663","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:32.796446Z","iopub.execute_input":"2022-09-21T03:44:32.797175Z","iopub.status.idle":"2022-09-21T03:44:32.802830Z","shell.execute_reply.started":"2022-09-21T03:44:32.797139Z","shell.execute_reply":"2022-09-21T03:44:32.802010Z"},"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":{"papermill":{"duration":0.112191,"end_time":"2022-09-15T22:17:11.383956","exception":false,"start_time":"2022-09-15T22:17:11.271765","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:33.732108Z","iopub.execute_input":"2022-09-21T03:44:33.733851Z","iopub.status.idle":"2022-09-21T03:44:33.739566Z","shell.execute_reply.started":"2022-09-21T03:44:33.733804Z","shell.execute_reply":"2022-09-21T03:44:33.738723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rle = []\nfor i in range(len(df)):\n    id, rle1, rle2, rle3, rle4, _, source, h, w, _, _ = df.iloc[i]\n    if source != \"Hubmap\" :\n        rle.append(\"\")\n        continue\n    img0 = rle2mask(rle1, shape=(w, h))\n    img1 = rle2mask(rle2, shape=(w, h))\n    img2 = rle2mask(rle3, shape=(w, h))\n    img3 = rle2mask(rle4, shape=(w, h))\n    img = img0*2 + img1*2 + img2*2 + img3\n    img[img <  3] = 0\n    img[img != 0] = 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(img3)\n        plt.show()\n        plt.imshow(img)\n        plt.show()","metadata":{"papermill":{"duration":3.596039,"end_time":"2022-09-15T22:17:15.082779","exception":false,"start_time":"2022-09-15T22:17:11.486740","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:37.574350Z","iopub.execute_input":"2022-09-21T03:44:37.574617Z","iopub.status.idle":"2022-09-21T03:44:37.583017Z","shell.execute_reply.started":"2022-09-21T03:44:37.574590Z","shell.execute_reply":"2022-09-21T03:44:37.582153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['rle'] = rle\ndf","metadata":{"papermill":{"duration":0.124904,"end_time":"2022-09-15T22:17:15.313941","exception":false,"start_time":"2022-09-15T22:17:15.189037","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:38.420023Z","iopub.execute_input":"2022-09-21T03:44:38.420383Z","iopub.status.idle":"2022-09-21T03:44:38.439927Z","shell.execute_reply.started":"2022-09-21T03:44:38.420338Z","shell.execute_reply":"2022-09-21T03:44:38.438973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[['id','rle']].to_csv('submission.csv', index=False)","metadata":{"papermill":{"duration":0.119569,"end_time":"2022-09-15T22:17:15.541536","exception":false,"start_time":"2022-09-15T22:17:15.421967","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-21T03:44:21.596602Z","iopub.execute_input":"2022-09-21T03:44:21.598537Z","iopub.status.idle":"2022-09-21T03:44:21.607605Z","shell.execute_reply.started":"2022-09-21T03:44:21.598499Z","shell.execute_reply":"2022-09-21T03:44:21.606721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.106055,"end_time":"2022-09-15T22:17:15.755106","exception":false,"start_time":"2022-09-15T22:17:15.649051","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}