{"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":"from time import time\nbegin = time()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:11:39.591244Z","iopub.execute_input":"2022-10-10T09:11:39.59169Z","iopub.status.idle":"2022-10-10T09:11:39.614895Z","shell.execute_reply.started":"2022-10-10T09:11:39.591602Z","shell.execute_reply":"2022-10-10T09:11:39.61379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install flash | grep -v 'already satisfied'\n!pip install '../input/flashwheels/flash-1.0.3-py3-none-any.whl' | grep -v 'already satisfied'\n\n# !pip install lightning-flash | grep -v 'already satisfied'\n!pip install '../input/flashwheels/jsonargparse-4.9.0-py3-none-any.whl' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/setuptools-59.5.0-py3-none-any.whl' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/docstring_parser-0.15-py3-none-any.whl' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/lightning_flash-0.8.0-py3-none-any.whl' | grep -v 'already satisfied'\n\n# !pip install 'lightning-flash[image]' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/pystiche-1.0.1-py3-none-any.whl' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/pretrainedmodels-0.7.4-py3-none-any.whl' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/efficientnet_pytorch-0.7.1-py3-none-any.whl' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/timm-0.4.12-py3-none-any.whl' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/segmentation_models_pytorch-0.3.0-py3-none-any.whl' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/lightning_bolts-0.5.0-py3-none-any.whl' | grep -v 'already satisfied'\n\n# !pip install cucim | grep -v 'already satisfied'\n!pip install '../input/flashwheels/click-8.1.3-py3-none-any.whl' | grep -v 'already satisfied'\n!pip install '../input/flashwheels/cucim-22.8.1-py3-none-manylinux2014_x86_64.whl' | grep -v 'already satisfied'","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:11:39.616983Z","iopub.execute_input":"2022-10-10T09:11:39.617341Z","iopub.status.idle":"2022-10-10T09:14:04.03382Z","shell.execute_reply.started":"2022-10-10T09:11:39.617304Z","shell.execute_reply":"2022-10-10T09:14:04.032597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from cv2 import resize as cvresize\nfrom cv2 import INTER_NEAREST\nfrom flash import Trainer\nfrom flash.image import ImageClassificationData, ImageClassifier\nfrom joblib import Parallel, delayed\nfrom openslide import OpenSlide\nfrom os import listdir, stat\nfrom PIL import Image\nfrom pytorch_lightning import seed_everything\nfrom skimage.io import imread\nfrom math import ceil\nfrom pandas import DataFrame\nfrom timeit import time as ittime\nfrom torch import tensor, concat, split, device, cuda, no_grad\nfrom tqdm import tqdm\nfrom warnings import filterwarnings","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:04.037408Z","iopub.execute_input":"2022-10-10T09:14:04.037742Z","iopub.status.idle":"2022-10-10T09:14:15.34917Z","shell.execute_reply.started":"2022-10-10T09:14:04.037712Z","shell.execute_reply":"2022-10-10T09:14:15.348135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from cupy import array, argsort, concatenate, dstack, ones, repeat, stack, uint8, where, zeros, zeros_like \nfrom numpy import uint8 as npuint8\nfrom numpy import array as nparray\nfrom numpy import where as npwhere\nfrom numpy import argsort as npargsort\nfrom numpy import concatenate as npconcatenate\nfrom numpy import ones as npones\nfrom numpy import zeros as npzeros\nfrom cucim.skimage.color import rgb2hed, hed2rgb, rgb2gray\nfrom cucim.skimage.exposure import match_histograms\nfrom cucim.skimage.filters import gaussian, threshold_otsu\nfrom cucim.skimage.measure import label, regionprops\nfrom cucim.skimage.morphology import dilation, disk, opening, square\nfrom cucim.skimage.segmentation import clear_border\nfrom cucim.skimage.transform import resize","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:15.350695Z","iopub.execute_input":"2022-10-10T09:14:15.352043Z","iopub.status.idle":"2022-10-10T09:14:16.073596Z","shell.execute_reply.started":"2022-10-10T09:14:15.352005Z","shell.execute_reply":"2022-10-10T09:14:16.072631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cuda_device = device(\"cuda:0\" if cuda.is_available else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:16.076143Z","iopub.execute_input":"2022-10-10T09:14:16.077685Z","iopub.status.idle":"2022-10-10T09:14:16.082905Z","shell.execute_reply.started":"2022-10-10T09:14:16.077645Z","shell.execute_reply":"2022-10-10T09:14:16.081639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:16.084444Z","iopub.execute_input":"2022-10-10T09:14:16.084816Z","iopub.status.idle":"2022-10-10T09:14:16.094933Z","shell.execute_reply.started":"2022-10-10T09:14:16.084771Z","shell.execute_reply":"2022-10-10T09:14:16.093951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.MAX_IMAGE_PIXELS = None\nRAM = 11e9\nRESOLUTION = 10\nSIZE_CAPS = [10e8, 12e8, 15e8, 20e8]\nSLIDE_PERC_TAKENS = [1.0, 0.9, 0.8, 0.6] # percentages taken refering to sizes\nSKIPPED_SIZE = 2e8 # keep this for the end (usually doesn't contain enought signal)\nDATA_DIR = '/kaggle/input/mayo-clinic-strip-ai/test/'","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:16.096192Z","iopub.execute_input":"2022-10-10T09:14:16.097007Z","iopub.status.idle":"2022-10-10T09:14:16.105983Z","shell.execute_reply.started":"2022-10-10T09:14:16.096971Z","shell.execute_reply":"2022-10-10T09:14:16.104922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FPVAL = 16\nSIGMA = 2\nMIN_AREA = 10000\nMAX_SLICE_AREA = 25000000 # 5000*5000","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:16.107122Z","iopub.execute_input":"2022-10-10T09:14:16.108653Z","iopub.status.idle":"2022-10-10T09:14:16.116586Z","shell.execute_reply.started":"2022-10-10T09:14:16.108615Z","shell.execute_reply":"2022-10-10T09:14:16.115738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TILE_SIZE = 224\nTILE_AREA = TILE_SIZE/RESOLUTION*TILE_SIZE/RESOLUTION # for segmentation from preview\nPIXEL_CAP = 240*3 # to be considered as white\nNOT_BLANK_PERC = 0.95\nNOT_BLANK_AREA = TILE_SIZE*TILE_SIZE*NOT_BLANK_PERC","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:16.117976Z","iopub.execute_input":"2022-10-10T09:14:16.118944Z","iopub.status.idle":"2022-10-10T09:14:16.127191Z","shell.execute_reply.started":"2022-10-10T09:14:16.118906Z","shell.execute_reply":"2022-10-10T09:14:16.126259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed_everything(12)\nREF_IMAGE = array(imread('../input/harmonizer-ref/har_ref.png'))\nPATH_TO_MODEL_WEIGHTS = \"../input/mayomodelsv2/decentmodelv3.ckpt\"\ntransform_kwargs = {\"image_size\": (224, 224), \"mean\": (0.5, 0.5, 0.5), \"std\": (0.5, 0.5, 0.5)}\nBATCH_SIZE = 64\nUNIQUE_LABELS = {\"CE\":0, \"LAA\":1}","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:16.129069Z","iopub.execute_input":"2022-10-10T09:14:16.129419Z","iopub.status.idle":"2022-10-10T09:14:21.544158Z","shell.execute_reply.started":"2022-10-10T09:14:16.129385Z","shell.execute_reply":"2022-10-10T09:14:21.543155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_preview(slide, loaded_perc=1.0):\n    width, height = slide.dimensions\n    width, height = int(width*loaded_perc), int(height*loaded_perc)\n    new_width = int(width/RESOLUTION)\n    new_height = int(height/RESOLUTION)\n    amount = ceil(2*(12*width*height + new_width*new_height*22)/ RAM)\n\n    w = int(width/amount) # tile width\n    h = int(height/amount) # tile height\n    nw = int(new_width/amount) # tile new width\n    nh = int(new_height/amount) # tile new height\n\n    preview = zeros((new_height, new_width, 3), dtype=uint8) + 255\n    for i in range(amount):\n        for j in range(amount):\n            preview[j*nh:(j+1)*nh, i*nw:(i+1)*nw] = array(cvresize(\n                nparray(slide.read_region((i*w,j*h), 0, (w, h)), dtype=npuint8)[:,:,:3], \n                dsize=(nw, nh), interpolation=INTER_NEAREST), dtype=uint8)\n    return preview","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:21.552046Z","iopub.execute_input":"2022-10-10T09:14:21.554417Z","iopub.status.idle":"2022-10-10T09:14:21.568818Z","shell.execute_reply.started":"2022-10-10T09:14:21.554378Z","shell.execute_reply":"2022-10-10T09:14:21.566767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_cleaned_binary(preview):\n    bw = rgb2gray(preview)\n    bw = threshold_otsu(bw) > gaussian(bw, SIGMA)\n    bw = opening(bw, square(FPVAL))\n    bw += clear_border(~bw)\n    bw = dilation(bw, disk(int(FPVAL*0.25)))\n    return bw","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:21.573834Z","iopub.execute_input":"2022-10-10T09:14:21.576434Z","iopub.status.idle":"2022-10-10T09:14:21.585363Z","shell.execute_reply.started":"2022-10-10T09:14:21.576386Z","shell.execute_reply":"2022-10-10T09:14:21.58448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_trainer = Trainer(enable_progress_bar=False, accelerator='gpu', devices=1, fast_dev_run=True)\nmodel = ImageClassifier.load_from_checkpoint(PATH_TO_MODEL_WEIGHTS)\n\ndef inference(imgs, batch_size):\n    datamodule = ImageClassificationData.from_tensors(predict_data=imgs,\n                     transform_kwargs=transform_kwargs,batch_size=batch_size) #dict(image_size=(224, 224)),batch_size=batch_size)\n    vec = [UNIQUE_LABELS[label] for label in my_trainer.predict(model, datamodule=datamodule, output='labels')[0]]\n    return tensor(nparray(vec)).to(cuda_device)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:21.590035Z","iopub.execute_input":"2022-10-10T09:14:21.59273Z","iopub.status.idle":"2022-10-10T09:14:26.753896Z","shell.execute_reply.started":"2022-10-10T09:14:21.592695Z","shell.execute_reply":"2022-10-10T09:14:26.752679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_ihc_hed(img):\n        ihc_hed = (rgb2hed(img))\n        null = zeros_like(ihc_hed[:, :, 0])\n        ihc_h = hed2rgb(stack((ihc_hed[:, :, 0], null, null), axis=-1))\n        ihc_e = hed2rgb(stack((null, ihc_hed[:, :, 1], null), axis=-1))\n        ihc_d = hed2rgb(stack((null, null, ihc_hed[:, :, 2]), axis=-1))\n        return ihc_h, ihc_e, ihc_d","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:26.755483Z","iopub.execute_input":"2022-10-10T09:14:26.756661Z","iopub.status.idle":"2022-10-10T09:14:26.764282Z","shell.execute_reply.started":"2022-10-10T09:14:26.756617Z","shell.execute_reply":"2022-10-10T09:14:26.763232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IHC_H_REF, IHC_E_RED, IHC_D_REF = get_ihc_hed(REF_IMAGE)\ndel REF_IMAGE","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:26.765749Z","iopub.execute_input":"2022-10-10T09:14:26.766202Z","iopub.status.idle":"2022-10-10T09:14:28.765196Z","shell.execute_reply.started":"2022-10-10T09:14:26.766159Z","shell.execute_reply":"2022-10-10T09:14:28.764212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def harmonize(img):\n    ihc_h, ihc_e, ihc_d = get_ihc_hed(img)\n    del img\n    return (dstack((match_histograms(ihc_h, IHC_H_REF)[:, :, 0],\n                    match_histograms(ihc_d, IHC_E_RED)[:, :, 1],\n                    match_histograms(ihc_e, IHC_D_REF)[:, :, 2])\n                    )*255).astype(uint8)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:28.766839Z","iopub.execute_input":"2022-10-10T09:14:28.767238Z","iopub.status.idle":"2022-10-10T09:14:28.77551Z","shell.execute_reply.started":"2022-10-10T09:14:28.767198Z","shell.execute_reply":"2022-10-10T09:14:28.772912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_slide(slide, bw):\n    with no_grad():\n        predicted_labels = tensor(nparray([])).to(cuda_device)\n        regions = [region for region in regionprops(label(bw)) if region.area > MIN_AREA]\n        if len(regions) == 0: regions = [region for region in regionprops(label(bw)) if region.area > TILE_AREA]\n        if len(regions) == 0: return predicted_labels\n\n        scaled_bboxes = []\n        bmasks = []\n        for region in regions:\n            old_x, old_y = region.bbox[0], region.bbox[1]\n            x, y = old_x*RESOLUTION, old_y*RESOLUTION\n            old_w, old_h = region.bbox[2] - old_x, region.bbox[3] - old_y\n            w, h = region.bbox[2]*RESOLUTION - x, region.bbox[3]*RESOLUTION - y\n            extra_x, extra_y = (w % TILE_SIZE), (h % TILE_SIZE)\n            x += ceil(extra_x/2)\n            w -= extra_x\n            y += ceil(extra_y/2)\n            h -= extra_y\n\n            if w*h > MAX_SLICE_AREA:\n                inf_half_w, sup_half_w = int(w/(TILE_SIZE*2))*TILE_SIZE, ceil(w/(TILE_SIZE*2))*TILE_SIZE\n                inf_half_h, sup_half_h = int(h/(TILE_SIZE*2))*TILE_SIZE, ceil(h/(TILE_SIZE*2))*TILE_SIZE\n                inf_b_half_w, inf_b_half_h = int(inf_half_w/RESOLUTION), int(inf_half_h/RESOLUTION)\n                scaled_bboxes.append((x, y, inf_half_w, inf_half_h))\n                bmasks.append(region.image[:inf_b_half_w, :inf_b_half_h])\n\n                scaled_bboxes.append((x+inf_half_w, y, sup_half_w, inf_half_h))\n                bmasks.append(region.image[inf_b_half_w:, :inf_b_half_h])\n\n                scaled_bboxes.append((x, y+inf_half_w, inf_half_w, sup_half_h))\n                bmasks.append(region.image[:inf_b_half_w, inf_b_half_h:])\n\n                scaled_bboxes.append((x+inf_half_w, y+inf_half_w, sup_half_w, sup_half_h))\n                bmasks.append(region.image[inf_b_half_w:, inf_b_half_h:])\n\n            else:\n                scaled_bboxes.append((x, y, w, h))\n                bmasks.append(region.image)\n        del regions\n\n        for (x,y,w,h), bmask in zip(scaled_bboxes, bmasks):\n\n            mask = resize(bmask, (w,h))\n            slice = array(nparray(slide.read_region((y,x), 0, (h, w)), dtype=npuint8)[:,:,:3], dtype=uint8)\n            for chanel in range(3):\n                slice[:,:,chanel] *= mask\n                slice[:,:,chanel] += (~mask*255).astype(uint8)\n            del mask\n\n            n, p = int(slice.shape[0]/TILE_SIZE), int(slice.shape[1]/TILE_SIZE)\n            tiles = slice.reshape((n,TILE_SIZE,p,TILE_SIZE,3))\n            del slice\n\n            kept_tiles = []\n            for i in range(n):\n                for j in range(p):\n                    if where((tiles[i,:,j,:].sum(axis=-1) < PIXEL_CAP))[0].shape[0] >= NOT_BLANK_AREA:\n                        kept_tiles.append(harmonize(tiles[i,:,j,:]).get())\n            kept_tiles = tensor(nparray(kept_tiles))\n            if kept_tiles.shape[0] == 0: continue\n\n            for infered_tiles in split(kept_tiles.to(cuda_device).permute(0,3,1,2), BATCH_SIZE):\n                predicted_labels = concat([predicted_labels, inference(infered_tiles, infered_tiles.shape[0])])\n            del kept_tiles\n\n    return predicted_labels","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:28.777068Z","iopub.execute_input":"2022-10-10T09:14:28.777476Z","iopub.status.idle":"2022-10-10T09:14:28.798852Z","shell.execute_reply.started":"2022-10-10T09:14:28.777438Z","shell.execute_reply":"2022-10-10T09:14:28.797883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = nparray([filename for filename in listdir(DATA_DIR) if filename.endswith(\".tif\")])\nsizes = nparray([stat(f'{DATA_DIR}{filename}').st_size for filename in filenames])","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:28.80128Z","iopub.execute_input":"2022-10-10T09:14:28.801669Z","iopub.status.idle":"2022-10-10T09:14:28.816435Z","shell.execute_reply.started":"2022-10-10T09:14:28.801632Z","shell.execute_reply":"2022-10-10T09:14:28.815467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = npargsort(sizes)\n\nfirst = npwhere(sizes[indices] > SKIPPED_SIZE)[0]\nlast = npwhere(sizes[indices] <= SKIPPED_SIZE)[0]\nindices = npconcatenate([indices[first], indices[last]])\n\nfilenames = filenames[indices]\nsizes = sizes[indices]","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:28.817645Z","iopub.execute_input":"2022-10-10T09:14:28.818408Z","iopub.status.idle":"2022-10-10T09:14:28.825578Z","shell.execute_reply.started":"2022-10-10T09:14:28.818368Z","shell.execute_reply":"2022-10-10T09:14:28.824513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_ids = []\nkept_indices = []\nfor i, filename in enumerate(filenames):\n    patient_id = filename[:filename.find('_')]\n    if patient_id not in patient_ids:\n        patient_ids.append(patient_id)\n        kept_indices.append(i)\n        \nkept_indices = nparray(kept_indices)\nfilenames[kept_indices] = filenames[kept_indices]\nsizes[kept_indices] = sizes[kept_indices]","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:28.827185Z","iopub.execute_input":"2022-10-10T09:14:28.827639Z","iopub.status.idle":"2022-10-10T09:14:28.836839Z","shell.execute_reply.started":"2022-10-10T09:14:28.827603Z","shell.execute_reply":"2022-10-10T09:14:28.835498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"load_percs = npones(sizes.shape)\nfor i, size in enumerate(sizes):\n    perc_taken = SLIDE_PERC_TAKENS[-1]\n    for index_taken, cap in enumerate(SIZE_CAPS):\n        if size < cap:\n            perc_taken = SLIDE_PERC_TAKENS[index_taken]\n            break\n    load_percs[i] = perc_taken\ndel SIZE_CAPS, SLIDE_PERC_TAKENS","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:28.838153Z","iopub.execute_input":"2022-10-10T09:14:28.839243Z","iopub.status.idle":"2022-10-10T09:14:28.846257Z","shell.execute_reply.started":"2022-10-10T09:14:28.839206Z","shell.execute_reply":"2022-10-10T09:14:28.845316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"percs = npzeros(len(patient_ids)) + 0.5\nlabelization = DataFrame({'patient_id': patient_ids, 'CE': percs, 'LAA': percs})\ndel kept_indices, patient_ids, percs","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:28.847775Z","iopub.execute_input":"2022-10-10T09:14:28.848122Z","iopub.status.idle":"2022-10-10T09:14:28.864841Z","shell.execute_reply.started":"2022-10-10T09:14:28.848087Z","shell.execute_reply":"2022-10-10T09:14:28.864009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labelization = labelization.sort_values('patient_id')","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:28.866236Z","iopub.execute_input":"2022-10-10T09:14:28.867367Z","iopub.status.idle":"2022-10-10T09:14:28.883524Z","shell.execute_reply.started":"2022-10-10T09:14:28.867329Z","shell.execute_reply":"2022-10-10T09:14:28.882414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for row, (filename, load_perc, slide_size) in enumerate(zip(filenames, load_percs, sizes)):\n    first_start = ittime.time_ns()\n    print(\"--------------------------------------------------------------------\")\n    print(f\"{round(slide_size/1e9, 2)}Go {filename}\")\n    if slide_size > 35e8:\n        continue\n    if time() - begin > 30600: # 8h30\n        break\n    slide = OpenSlide(f'{DATA_DIR}{filename}')\n    print(f\"Openslide took {round((ittime.time_ns() - first_start)/10e8, 5)} seconds\")\n    start = ittime.time_ns()\n    preview = get_preview(slide, loaded_perc=load_perc)\n    print(f\"Preview took {round((ittime.time_ns() - start)/10e8, 5)} seconds\")\n    start = ittime.time_ns()\n    bw = get_cleaned_binary(preview)\n    print(f\"Clean took {round((ittime.time_ns() - start)/10e8, 5)} seconds\")\n    start = ittime.time_ns()\n    del preview\n    vec = predict_slide(slide, bw)\n    del bw\n    print(vec.shape[0])\n    if vec.shape[0] > 0:\n        perc = 0.99999 if vec.mean() > 0.5 else 0.00001\n        labelization.loc[row, 'LAA'] = perc\n        labelization.loc[row, 'CE'] = 1 - perc\n        labelization.to_csv('/kaggle/working/submission.csv', index=False)\n    del vec\n    print(f\"Inference took {round((ittime.time_ns() - start)/10e8, 5)} seconds\")\n    start = ittime.time_ns()\n    print(f\"Everything took {round((ittime.time_ns() - first_start)/10e8, 5)} seconds\")","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:14:28.88519Z","iopub.execute_input":"2022-10-10T09:14:28.885985Z","iopub.status.idle":"2022-10-10T09:27:53.879073Z","shell.execute_reply.started":"2022-10-10T09:14:28.885943Z","shell.execute_reply":"2022-10-10T09:27:53.877906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labelization","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:27:53.881165Z","iopub.execute_input":"2022-10-10T09:27:53.881833Z","iopub.status.idle":"2022-10-10T09:27:53.899715Z","shell.execute_reply.started":"2022-10-10T09:27:53.881791Z","shell.execute_reply":"2022-10-10T09:27:53.898655Z"},"trusted":true},"execution_count":null,"outputs":[]}]}