{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### mask 5 6 7 ","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as A","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numba, cv2, gc\nimport pathlib, random, time\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport rasterio\nfrom rasterio.windows import Window\n\nfrom fastai.vision.widgets import *\n#from tqdm import tqdm\nfrom fastai.data.all import *\nfrom fastai.vision.all import *\nfrom fastai import *\nimport gc\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.utils.data as D\n\nimport torchvision\nfrom torchvision import transforms as T","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /tmp/pip/cache/","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from shutil import copyfile\n\nfor file in glob.glob('../input/segmentation-models/*.*'):\n    filem=file.replace('../input/segmentation-models','/tmp/pip/cache').replace('xyz','tar.gz')\n    print(file,filem)\n    copyfile(file,filem)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --no-index --find-links /tmp/pip/cache/ segmentation-models-pytorch","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import segmentation_models_pytorch as smp","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_PATH = '../input/hubmap-kidney-segmentation'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu' \ndevice\n# device= 'cpu'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## this class makes it possible to use custom augmentations\nclass AlbumentationsTransform(DisplayedTransform):\n    split_idx,order=0,2\n    def __init__(self, train_aug): store_attr()\n    \n    def encodes(self, img: PILImage):\n        aug_img = self.train_aug(image=np.array(img))['image']\n        return PILImage.create(aug_img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### grid functions","metadata":{}},{"cell_type":"code","source":"###### used for converting the decoded image to rle mask\ndef rle_encode(im):\n    '''\n    im: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = im.flatten(order = 'F')\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\ndef rle_decode(mask_rle, shape=(256, 256)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape, order='F')\n\n@numba.njit()\ndef rle_numba(pixels):\n    size = len(pixels)\n    points = []\n    if pixels[0] == 1:\n        flag = False\n        points.append(1)\n    else:\n        flag = True\n    for i in range(1, size):\n        if pixels[i] != pixels[i-1]:\n            if flag:\n                points.append(i+1)\n                flag = False\n            else:\n                points.append(i+1 - points[-1])\n                flag = True\n    if pixels[-1] == 1: points.append(size-points[-1]+1)    \n    return points\n\ndef rle_numba_encode(image):\n    pixels = image.flatten(order = 'F')\n    points = rle_numba(pixels)\n    return ' '.join(str(x) for x in points)\n\ndef make_grid(x,y, window=1024, min_overlap=0):\n    \"\"\"\n        Return Array of size (N,4), where N - number of tiles,\n        2nd axis represente slices: x1,x2,y1,y2 \n    \"\"\"\n    nx = x // (window - min_overlap) + 1\n    x1 = np.linspace(0, x, num=nx, endpoint=False, dtype=np.int64)\n    x1[-1] = x - window\n    x2 = (x1 + window).clip(0, x)\n    ny = y // (window - min_overlap) + 1\n    y1 = np.linspace(0, y, num=ny, endpoint=False, dtype=np.int64)\n    y1[-1] = y - window\n    y2 = (y1 + window).clip(0, y)\n    slices = np.zeros((nx,ny, 4), dtype=np.int64)\n    \n    for i in range(nx):\n        for j in range(ny):\n            slices[i,j] = x1[i], x2[i], y1[j], y2[j]    \n    return slices.reshape(nx*ny,4)\n\ndef make_grid_small(shape, window_large=1024):\n    \"\"\"\n        return 2 arrays... of (N,4), where N - number of tiles,\n        2nd axis represente slices: x1,x2,y1,y2 \n        first array is the visible tiles, second holds, the real gridcuts..\n        prediction will be on first array tiles, but only the real gridcuts (half of the original size)\n        will be considered\n        \n    \"\"\"\n    window=(window_large//8) * 4  #this is where I will pick the real results.\n    fazlalik=window_large-window\n    birtaraftan=fazlalik//2\n    print('window=',window)\n    x, y = shape\n    nx = x // (window) + 1\n    x1 = np.linspace(0, x, num=nx, endpoint=False, dtype=np.int64)\n    x1[-1] = x - window\n    x2 = (x1 + window).clip(0, x)\n    ny = y // (window) + 1\n    y1 = np.linspace(0, y, num=ny, endpoint=False, dtype=np.int64)\n    y1[-1] = y - window\n    y2 = (y1 + window).clip(0, y)\n\n    slices = np.zeros((nx,ny, 4), dtype=np.int64)\n    cut_slices=np.zeros((nx,ny,4),dtype=np.int64)\n    for i in range(nx):\n        for j in range(ny):\n            slices[i,j] = x1[i], x2[i], y1[j], y2[j]\n            cut_slices[i,j]=x1[i]-birtaraftan,x2[i]+birtaraftan,y1[j]-birtaraftan,y2[j]+birtaraftan\n    slices=slices.reshape(nx*ny,4)  \n    cut_slices=cut_slices.reshape(nx*ny,4) \n    \n    for line in cut_slices:\n        if line[0]<0:\n            line[:2]=line[:2]-line[0] ## sifirdan kucukse kendini cikar 0 olsun\n\n        if line[2]<0:\n            line[2:]=line[2:]-line[2]  #sifirdan kucukse kendini cikar 0 olsun\n\n\n        if line[1]>x:\n            line[:2]=line[:2]-(line[1]-x) #xten buyukse..xden buyuk oldugu kadar geri al...\n\n        if line[3]>y:\n            line[2:]=line[2:]-(line[3]-y) #yten buyukse..yden buyuk oldugu kadar geri al...\n            \n\n    position=np.vstack((cut_slices[:,0],cut_slices[:,0],cut_slices[:,2],cut_slices[:,2])).T\n    position=slices-position\n    \n    return cut_slices, slices, position","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"identity = rasterio.Affine(1, 0, 0, 0, 1, 0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### necessary for the exports","metadata":{}},{"cell_type":"code","source":"def label_func(file):\n    sx=list(file.parts)  #path objectin partlarini aliyor ve tuple yapiyor, ilgili bolumu degistirmek icin...\n    sx[1]='labels'\n    sx[3]='labels'\n    return Path('/'.join(sx))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### fillpoly only collat","metadata":{}},{"cell_type":"code","source":"## BU VERSIYONDA BURAYA PREDS 0 1 OLARAK GELIYOR....(preds>.5).astype(np.uint8).... BUNA GEREK YOK\n\ndef collate_predsF(preds): #PREDS TEK BASINA GITMEYECEK... BURAYA PREDS GELECEK..\n    contours,hierarchy=cv2.findContours(preds,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)\n    maske=np.zeros_like(preds).astype(np.uint8) # preds buraya logit gelirse, bunu sen yine de astype.(np.uint8)\n    maske=maske.copy()  #bazen cv2 input hatasi veriyor diye boyle bir check yaptim,, gerek de olmayabilir\n    for c in contours:\n        maske=cv2.fillPoly(maske,[c],1)\n    return maske","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_slices(WINDOW,x1,x2,y1,y2,x,y): #x here is dataset height, y is the dataset width\n    windows=(WINDOW//8) * 4  #this is where I will pick the real results.\n    fazlalik=WINDOW-windows\n    birtaraftan=fazlalik//2  \n\n    slices=np.zeros((4,4),np.int)\n    cut_slices=np.zeros((4,4),np.int)\n    \n    xfark=x2-x1\n    yfark=y2-y1\n    \n    slices[0]=[x1,x1+xfark//2,y1,y1+yfark//2]\n    slices[1]=[x1,x1+xfark//2,y1+yfark//2,y1+yfark]\n    slices[2]=[x1+xfark//2,x1+xfark,y1,y1+yfark//2]\n    slices[3]=[x1+xfark//2,x1+xfark,y1+yfark//2,y1+yfark]\n\n    for sm in range(4):\n        cut_slices[sm]=[slices[sm,0]-birtaraftan,slices[sm,1]+birtaraftan,\n                        slices[sm,2]-birtaraftan,slices[sm,3]+birtaraftan]\n\n    for line in cut_slices:\n        if line[0]<0:\n            line[:2]=line[:2]-line[0] ## sifirdan kucukse kendini cikar 0 olsun\n\n        if line[2]<0:\n            line[2:]=line[2:]-line[2]  #sifirdan kucukse kendini cikar 0 olsun\n\n\n        if line[1]>x:\n            line[:2]=line[:2]-(line[1]-x) #xten buyukse..xden buyuk oldugu kadar geri al...\n\n        if line[3]>y:\n            line[2:]=line[2:]-(line[3]-y) #yten buyukse..yden buyuk oldugu kadar geri al...\n            \n    position=np.vstack((cut_slices[:,0],cut_slices[:,0],cut_slices[:,2],cut_slices[:,2])).T\n    position=slices-position\n    \n    return cut_slices,slices,position","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MIN_OVERLAP=0\n\ndef stage1(dataset, classify,classify_c, segment_1,WINDOW): #IKI SEGMENT(COLOR NONC) ILE BASLAYALIM...\n    h,w=(dataset.height,dataset.width)\n    slices = make_grid(h,w, window=WINDOW, min_overlap=MIN_OVERLAP)\n    preds = np.zeros(dataset.shape,dtype=np.uint8)\n\n    for (x1,x2,y1,y2) in slices:\n\n        if dataset.count==3:\n            image = dataset.read([1,2,3],window=Window.from_slices((x1,x2),(y1,y2)))#iki format var..\n            image = np.moveaxis(image, 0, -1)\n\n        else:\n            subdatasets=dataset.subdatasets\n            if len(subdatasets)>0:\n                image=np.zeros((x2-x1,y2-y1,3),dtype=np.uint8)\n                for j, subdataset in enumerate(subdatasets, 0): #enumerate default 0 dan baslar zaten...\n                    with rasterio.open(subdataset) as layer:\n                        image[:,:,j]=layer.read(1,window=Window.from_slices((x1,x2),(y1,y2)))\n                        \n        ##########image saturation check....\n        \n        if cv2.cvtColor(image,cv2.COLOR_BGR2HSV)[:,:,1].mean() < 12.5:\n            continue\n     \n     \n        image=torch.from_numpy(np.array(image))\n        image.to(device)\n        \n#         ###CLASSIFIER DEVREDe\n#         var_yok=np.array(classify.predict(image)[2][1])\n#         var_yok_c=np.array(classify_c.predict(image)[2][1])\n        \n#         if var_yok < .0007 and var_yok_c < .0007 :\n#             continue\n            \n        \n        ################pick the 4 quadrant tiles... and their true and relative positions\n        slices,originals,positions=make_slices(WINDOW,x1,x2,y1,y2,h,w)\n        \n        \n        ################these 4 slices are given positive in classifier.....\n        for ((x1,x2,y1,y2),(orgx1,orgx2,orgy1,orgy2),(posx1,posx2,posy1,posy2)) in zip(slices,originals,positions):\n            if dataset.count==3:\n                image = dataset.read([1,2,3],window=Window.from_slices((x1,x2),(y1,y2)))#iki format var..\n                image = np.moveaxis(image, 0, -1)\n\n            else:\n                subdatasets=dataset.subdatasets\n                if len(subdatasets)>0:\n                    image=np.zeros((x2-x1,y2-y1,3),dtype=np.uint8)\n                    for j, subdataset in enumerate(subdatasets, 0): #enumerate default 0 dan baslar zaten...\n                        with rasterio.open(subdataset) as layer:\n                            image[:,:,j]=layer.read(1,window=Window.from_slices((x1,x2),(y1,y2)))\n            \n            image=np.array(image)\n            score = np.zeros((image.shape[0],image.shape[1]),dtype=np.uint8)\n            \n            image=torch.from_numpy(image)\n            image.to(device)\n            \n            #kactanbuyukbir=3    #basic giriste 7 tane var... 5 6 7 256liklari ve klasikbestb5\n            for i,model in enumerate(segment_1):\n                score_1a=model.predict(image)\n                score_1a=score_1a[2][1]\n                \n                score_1a = score_1a.cpu().numpy() #cikti tensormask tipinde dolayisi ile once numpy yap... bu dogrudan cikti 0 ve 1...\n                score_1a = (score_1a>.5).astype(np.uint8)\n                score_1a = cv2.resize(score_1a, (WINDOW, WINDOW))\n                \n                score = score + score_1a\n                #print(np.sum(score_1a),np.sum(score))\n        \n            #print('max in score......',np.amax(score),'kac model kullandim........',(i+1),'kaca buyuk esitse 1 kabuledilecek......',((i+1)//2))\n            score = np.where(score>=((i+1)/2),1,0)\n            #print('birledikten sonra toplam.....',np.sum(score))\n            preds[orgx1:orgx2,orgy1:orgy2]=score[posx1:posx2,posy1:posy2]\n    \n    return preds","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef stage2(preds,dataset,segment_2,window):\n    ## PREDS now comes binary so (preds>.5).astype(np.uint8) not necessary\n    contours,hierarchy=cv2.findContours(preds, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)\n    \n    maske=np.zeros_like(preds)\n    maske=maske.copy()\n    \n    yarisi=window//2\n    \n    a,b=preds.shape\n    \n    for c in contours:\n        aa,bb,cc,dd=cv2.boundingRect(c)\n        \n        M=cv2.moments(c)\n        if M[\"m00\"] != 0:\n            cX = int(M[\"m10\"] / M[\"m00\"])\n            cY = int(M[\"m01\"] / M[\"m00\"])\n        else:\n            continue\n        \n        \n        x=cY \n        x1=max((x-yarisi),0)\n        x2=min((x1+window),a)\n        if x2==a: x1=a-window\n            \n        rowOrta=x-x1\n            \n        y=cX\n        y1=max((y-yarisi),0)\n        y2=min((y1+window),b)\n        if y2==b: y1=b-window\n        \n        columnOrta=y-y1\n     \n        #extract the image\n        if dataset.count==3:\n            image = dataset.read([1,2,3],window=Window.from_slices((x1,x2),(y1,y2)))#iki format var..\n            image = np.moveaxis(image, 0, -1)\n              \n        else:\n            h,w=(dataset.height,dataset.width)\n            subdatasets=dataset.subdatasets\n            if len(subdatasets)>0:\n                image=np.zeros((x2-x1,y2-y1,3),dtype=np.uint8)\n                for j, subdataset in enumerate(subdatasets, 0): #enumerate default 0 dan baslar zaten...\n                    with rasterio.open(subdataset) as layer:\n                        image[:,:,j]=layer.read(1,window=Window.from_slices((x1,x2),(y1,y2)))\n          \n        \n        image=np.array(image)\n        score = np.zeros((512,512),dtype=np.float32)  # burada modellerin ciktisina gore score'u hardcode yaptim....\n\n        image=torch.from_numpy(image)\n        image.to(device)\n        \n\n        for i, model in enumerate(segment_2):\n            \n        ####################check score calculation and resize options\n            score_2a=model.predict(image)\n            score_2a=score_2a[2][1]\n\n            score_2a = score_2a.cpu().numpy() #cikti tensormask tipinde dolayisi ile once numpy yap... bu dogrudan cikti 0 ve 1...\n\n            score_2a= score_2a.astype(np.float32)\n            \n            score = score + score_2a\n        \n        \n        #print('max in score in stage2......',np.amax(score),'kac model kullandim........',(i+1))\n\n        score = score / (i+1)  # i+1 model var,,, enumerate sonucuna gore,,,\n        score = (score>.455).astype(np.uint8)\n        score = cv2.resize(score, (window, window))  \n        \n\n        #score raw olarak devam ediyorsa...contour alirken astype. expression \n        conturs,hierarchy=cv2.findContours(score, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)\n        \n        \n        for cc in conturs: #bu dongude score-tile icinden sadece merkez contour icin hull convex cizecek\n            eksen1,eksen2,w,h=cv2.boundingRect(cc)\n            if columnOrta in range(eksen1-150,eksen1+w+150) and rowOrta in range(eksen2-150,eksen2+h+150):\n                \n                maske[x1:x2,y1:y2][eksen2:eksen2+h,eksen1:eksen1+w] = score[eksen2:eksen2+h,eksen1:eksen1+w]\n    \n\n\n    return maske\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nWINDOW=1024\np = pathlib.Path(DATA_PATH)\n\n#***********************if no privateset then cut it short:\ntestct=0\nfor filename in p.glob('test/*.tiff'):\n    testct+=1\n    \nsignal=True\nif testct>5:\n    signal=False\n# **********************   \n\nclassify = load_learner('../input/11nisan-firstclassifier/11nisan_classifier_firststage.pkl',cpu=False)# \nclassify_c = load_learner('../input/11nisan-classifier/11nisan_classifier_firststage__RENKLI_ikinciGRUP__fullartifullDATA.pkl',cpu=False)\n\nsegment_1a = load_learner('../input/11mayis-256-5-6-7/B5_256_18ep19_clsfFULLdata_20654_9686.pkl',cpu=False)\nsegment_1b = load_learner('../input/11mayis-256-5-6-7/B5_256_18ep19_clsfVAL_18464_9620.pkl',cpu=False)\nsegment_1c = load_learner('../input/11mayis-256-5-6-7/B6_18ep19_CLSF256fulld_19031_9688.pkl',cpu=False)\nsegment_1d = load_learner('../input/11mayis-256-5-6-7/B6_256_18ep19_CLSFVAL_18739_9615.pkl',cpu=False)\nsegment_1e = load_learner('../input/11mayis-256-5-6-7/B7_17ep19_CLSF256fulld_20018_9692.pkl',cpu=False)\nsegment_1f = load_learner('../input/11mayis-256-5-6-7/B7_256_15ep19_CLSFVAL_20506_9604.pkl',cpu=False)\nsegment_1g = load_learner('../input/11mayis-256-b4/B4_256_18ep19_clsfFULLdata_20017_9684.pkl',cpu=False)\nsegment_1h = load_learner('../input/11mayis-256-b4/B4_256_18ep19_clsfVALdata_19643_9601.pkl',cpu=False)\nsegment_1i = load_learner('../input/11mayis-256b3/B3_256_16ep19_clsfFULLdata_20879_9682.pkl',cpu=False)\nsegment_1j = load_learner('../input/11mayis-256b3/B3_256_18ep19_clsfVALdata_20385_9611.pkl',cpu=False)\n\n\n\nsegment_1x = load_learner('../input/29nisan-512sub-2epochoverbaseline/29nisan_smp_effb5_fitonecycle_11epoch_28349__2epoch_of3e5_28956.pkl',cpu=False)\n\n\n\n\nsegment_2a = load_learner('../input/09mayis-b5s/B5_17ep19_xR_CLSF_18522_9706.pkl',cpu=False)\nsegment_2b = load_learner('../input/6mayis-b6-clsf-options/B6_17ep19_xR_CLSF_17074_9710.pkl',cpu=False)\nsegment_2c = load_learner('../input/08mayis-tumdata-b7ler/B7_st3_RCLSF_17020_9704.pkl',cpu=False)\nsegment_2d = load_learner('../input/11mayis-bx-validation/B5_09ep19_R_CLSFval_18875_9630.pkl',cpu=False)\nsegment_2e = load_learner('../input/11mayis-bx-validation/B6_12ep19_R_CLSFval_18783_9599.pkl',cpu=False)\nsegment_2f = load_learner('../input/11mayis-bx-validation/B7_16ep19_R_CLSFvalV2_17385_9588.pkl',cpu=False)\n\nbest7 = load_learner('../input/14mayis-hubmaplast/aftermath_bestb7.pkl',cpu=False)\n\n#segment_1 = [segment_1a,segment_1b,segment_1c,segment_1d,segment_1e,segment_1f]\nsegment_1 = [best7]\n#segment_1 = [segment_1a,segment_1b,segment_1c,segment_1d]\n#segment_1 = [segment_1a,segment_1b,segment_1g,segment_1h,segment_1i,segment_1j]\n\n\n#segment_2 = [segment_2a,segment_2b,segment_2c,segment_2d,segment_2e,segment_2f,segment_1x]\nsegment_2 = [best7]\n\n\nsubm = {}\nfor i, filename in enumerate(p.glob('test/*.tiff')):\n    dataset = rasterio.open(filename.as_posix(), transform = identity)\n    \n\n    preds=stage1(dataset,classify,classify_c, segment_1,WINDOW)    \n    preds=collate_predsF(preds)\n    \n    \n    preds=stage2(preds,dataset,segment_2,WINDOW)\n    preds=collate_predsF(preds)\n    \n    subm[i] = {'id':filename.stem, 'predicted': rle_numba_encode(preds)}\n    del preds\n    gc.collect();\n    \n    if signal: break  #of only for commit... cut it short","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame.from_dict(subm, orient='index')\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}