{"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":"## 30temmuz_randombox_bline_trials_ensemble\n## this makes ensemble from the weighted_masks_fusion function of ensemble boxes.\n## I will bring that to here for completeness\n\n\n# Define the weighted_masks_fusion function and auxiliary functions\n\nimport warnings\nimport numpy as np\nfrom numba import jit\n\n@jit(nopython=True)\ndef bb_intersection_over_union(A, B) -> float:\n    xA = max(A[0], B[0])\n    yA = max(A[1], B[1])\n    xB = min(A[2], B[2])\n    yB = min(A[3], B[3])\n\n    # compute the area of intersection rectangle\n    interArea = max(0, xB - xA) * max(0, yB - yA)\n\n    if interArea == 0:\n        return 0.0\n\n    # compute the area of both the prediction and ground-truth rectangles\n    boxAArea = (A[2] - A[0]) * (A[3] - A[1])\n    boxBArea = (B[2] - B[0]) * (B[3] - B[1])\n\n    iou = interArea / float(boxAArea + boxBArea - interArea)\n    return iou\n\ndef get_weighted_mask(masks, scores, inmodels, conf_type):\n    mask = np.zeros(masks[0].shape, dtype=np.float32)\n    conf = 0\n    conf_list = []\n    for m, s, im in zip(masks, scores, inmodels):\n        if conf_type == 'model_weight2':\n            mask += s * im * m\n            conf += s * im\n        else:\n            mask += s * m\n            conf += s\n        conf_list.append(s)\n    score = np.max(conf_list)\n    mask = mask / conf\n    return mask, score, conf_list\n\ndef get_weighted_box(boxes, scores, inmodels, conf_type):\n    box = np.zeros(4, dtype=np.float32)\n    conf = 0\n    conf_list = []\n    for b, s, im in zip(boxes, scores, inmodels):\n        if conf_type == 'model_weight2':\n            box += s * im * b\n            conf += s * im\n        else:\n            box += s * b\n            conf += s\n        conf_list.append(s)\n    score = np.max(conf_list)\n    box = box / conf\n    return box, score\n\ndef find_matching_box(boxes_list, new_box, match_iou):\n    best_iou = match_iou\n    best_index = -1\n    for i in range(len(boxes_list)):\n        box = boxes_list[i]\n        iou = bb_intersection_over_union(box, new_box)\n        if iou > best_iou:\n            best_index = i\n            best_iou = iou\n\n    return best_index, best_iou\n\ndef weighted_masks_fusion(masks, boxes, scores, models, iou_thr=0.7, skip_mask_thr=0.0, \n                        conf_type='max_weight', soft_weight=5, thresh_type=None, model_weights=1,\n                        num_thresh=4, num_models=5):\n    masks = masks[scores > skip_mask_thr]\n    boxes = boxes[scores > skip_mask_thr]\n    models = models[scores > skip_mask_thr]\n    scores = scores[scores > skip_mask_thr]\n    \n    new_masks = []\n    new_boxes = []\n    new_scores = []\n    inmodels = []\n    weighted_boxes = []\n    weighted_scores = []\n    # Clusterize boxes\n    for i in range(len(masks)):\n            \n        index, best_iou = find_matching_box(weighted_boxes, boxes[i], iou_thr)\n        if index != -1:\n            new_masks[index].append(masks[i])\n            new_boxes[index].append(boxes[i])\n            new_scores[index].append(scores[i])\n            inmodels[index].append(models[i])\n            weighted_boxes[index], weighted_scores[index] = get_weighted_box(new_boxes[index], new_scores[index], inmodels[index], conf_type)\n        else:\n            new_masks.append([masks[i]])\n            new_boxes.append([boxes[i].copy()])\n            new_scores.append([scores[i].copy()])\n            inmodels.append([models[i]])\n            weighted_boxes.append(boxes[i].copy())\n            weighted_scores.append(scores[i].copy())\n            \n    ens_masks = []\n    ens_scores = []\n    ens_boxes = []\n    for nmasks, nscores, wbox, inms in zip(new_masks, new_scores, weighted_boxes, inmodels):\n        mask, score, conf_list = get_weighted_mask(nmasks, nscores, inms, conf_type)\n        if thresh_type == 'num_thresh':\n            if len(conf_list) >= num_thresh:\n                ens_masks.append(mask)\n                ens_boxes.append(wbox)\n            else:\n                continue\n        else:\n            ens_masks.append(mask)\n            ens_boxes.append(wbox)\n\n        if conf_type =='max_weight':\n            ens_scores.append(score * min(len(conf_list), num_models) / num_models)\n        elif conf_type == 'max':\n            ens_scores.append(score)\n        elif conf_type == 'soft_weight':\n            ens_scores.append(score * (min(len(conf_list), num_models) + soft_weight) / (soft_weight + num_models))\n        elif conf_type == 'model_weight' or conf_type == 'model_weight2':\n            this_weights = [model_weights[i] for i in inms]\n            ens_scores.append(score * (min(np.sum(this_weights), np.sum(model_weights)) + soft_weight) / (soft_weight + np.sum(model_weights)))\n\n    return ens_masks, ens_scores, ens_boxes\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:17:35.786001Z","iopub.execute_input":"2023-07-31T10:17:35.786344Z","iopub.status.idle":"2023-07-31T10:17:36.660005Z","shell.execute_reply.started":"2023-07-31T10:17:35.786314Z","shell.execute_reply":"2023-07-31T10:17:36.659039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, glob\nimport sys\nimport json\nfrom PIL import Image\nfrom collections import Counter\n\nimport numpy as np\nimport pandas as pd\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm\nimport torch\nimport cv2\nfrom skimage.morphology import binary_dilation\n\nimport pandas as pd\n\nfrom sklearn.model_selection import KFold\n\nsys.path.append(\"/kaggle/input/detection-wheel\")","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:17:36.661997Z","iopub.execute_input":"2023-07-31T10:17:36.662357Z","iopub.status.idle":"2023-07-31T10:17:43.098242Z","shell.execute_reply.started":"2023-07-31T10:17:36.662326Z","shell.execute_reply":"2023-07-31T10:17:43.097183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install pycocotools package\nimport os\n!mkdir /kaggle/working/packages\n!cp -r /kaggle/input/pycocotools/* /kaggle/working/packages\nos.chdir(\"/kaggle/working/packages/pycocotools-2.0.6/\")\n!python setup.py install -q\n!pip install . --no-index --find-links /kaggle/working/packages/ -q\nos.chdir(\"/kaggle/working\")","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:17:43.103272Z","iopub.execute_input":"2023-07-31T10:17:43.105954Z","iopub.status.idle":"2023-07-31T10:18:34.090146Z","shell.execute_reply.started":"2023-07-31T10:17:43.105919Z","shell.execute_reply":"2023-07-31T10:18:34.08892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\n\ndef encode_binary_mask(mask: np.ndarray) -> t.Text:\n  \"\"\"Converts a binary mask into OID challenge encoding ascii text.\"\"\"\n\n  # check input mask --\n  if mask.dtype != bool:\n    raise ValueError(\n        \"encode_binary_mask expects a binary mask, received dtype == %s\" %\n        mask.dtype)\n\n  mask = np.squeeze(mask)\n  if len(mask.shape) != 2:\n    raise ValueError(\n        \"encode_binary_mask expects a 2d mask, received shape == %s\" %\n        mask.shape)\n\n  # convert input mask to expected COCO API input --\n  mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)\n  mask_to_encode = mask_to_encode.astype(np.uint8)\n  mask_to_encode = np.asfortranarray(mask_to_encode)\n\n  # RLE encode mask --\n  encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n\n  # compress and base64 encoding --\n  binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n  base64_str = base64.b64encode(binary_str)\n  return base64_str","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:34.094533Z","iopub.execute_input":"2023-07-31T10:18:34.094843Z","iopub.status.idle":"2023-07-31T10:18:34.111175Z","shell.execute_reply.started":"2023-07-31T10:18:34.094814Z","shell.execute_reply":"2023-07-31T10:18:34.110166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## glumoril issue preperation","metadata":{}},{"cell_type":"code","source":"## glumorolus issue;\nimport json\nwith open('/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl', 'r') as json_file:\n    json_list = list(json_file)\n    \ntiles_dicts = []\nfor json_str in json_list:\n    tiles_dicts.append(json.loads(json_str))\n    \n## once test setteki glumorillere bakalim var mi yokmu.. tile isminden bailacak\ndiles_dict = {}\nfor tile in tiles_dicts:\n#     print(tile)\n    diles_dict[tile['id']] = [x['coordinates'] for x in tile['annotations'] if x['type']=='glomerulus']","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:34.112688Z","iopub.execute_input":"2023-07-31T10:18:34.113339Z","iopub.status.idle":"2023-07-31T10:18:38.783416Z","shell.execute_reply.started":"2023-07-31T10:18:34.113306Z","shell.execute_reply":"2023-07-31T10:18:38.782431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport torch\nfrom PIL import Image\n\n\nclass PennFudanDataset(torch.utils.data.Dataset):\n    def __init__(self, imgs, transforms):\n        self.transforms = transforms\n        # load all image files, sorting them to\n        # ensure that they are aligned\n        self.imgs = imgs\n        self.name_indices = [os.path.splitext(os.path.basename(i))[0] for i in imgs]\n\n    def __getitem__(self, idx):\n        # load images and masks\n        img_path = self.imgs[idx]\n        name = self.name_indices[idx]\n        array = tiff.imread(img_path)\n        img = Image.fromarray(array)\n        \n        img, _ = self.transforms(img, img)\n\n        return img, name\n\n    def __len__(self):\n        return len(self.imgs)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:38.784849Z","iopub.execute_input":"2023-07-31T10:18:38.78531Z","iopub.status.idle":"2023-07-31T10:18:38.793846Z","shell.execute_reply.started":"2023-07-31T10:18:38.785272Z","shell.execute_reply":"2023-07-31T10:18:38.792977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\nimport torchvision\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom torchvision.models.detection.mask_rcnn import MaskRCNNPredictor\n\ndef get_model_instance_segmentation(num_classes):\n    # load an instance segmentation model pre-trained on COCO\n    model = torchvision.models.detection.maskrcnn_resnet50_fpn_v2(weights=None, weights_backbone=None)\n\n    # get number of input features for the classifier\n    in_features = model.roi_heads.box_predictor.cls_score.in_features\n    # replace the pre-trained head with a new one\n    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n\n    # now get the number of input features for the mask classifier\n    in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels\n    hidden_layer = 256\n    # and replace the mask predictor with a new one\n    model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask,\n                                                       hidden_layer,\n                                                       num_classes)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:38.795536Z","iopub.execute_input":"2023-07-31T10:18:38.796477Z","iopub.status.idle":"2023-07-31T10:18:39.03489Z","shell.execute_reply.started":"2023-07-31T10:18:38.796444Z","shell.execute_reply":"2023-07-31T10:18:39.033583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import transforms as T\n\ndef get_transform(train):\n    transforms = []\n    transforms.append(T.PILToTensor())\n    transforms.append(T.ConvertImageDtype(torch.float))\n    return T.Compose(transforms)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:39.036474Z","iopub.execute_input":"2023-07-31T10:18:39.036808Z","iopub.status.idle":"2023-07-31T10:18:39.055219Z","shell.execute_reply.started":"2023-07-31T10:18:39.036775Z","shell.execute_reply":"2023-07-31T10:18:39.054159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from engine import train_one_epoch, evaluate\nimport utils","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:39.056735Z","iopub.execute_input":"2023-07-31T10:18:39.057069Z","iopub.status.idle":"2023-07-31T10:18:39.083287Z","shell.execute_reply.started":"2023-07-31T10:18:39.057038Z","shell.execute_reply":"2023-07-31T10:18:39.082479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:39.088862Z","iopub.execute_input":"2023-07-31T10:18:39.089152Z","iopub.status.idle":"2023-07-31T10:18:39.120416Z","shell.execute_reply.started":"2023-07-31T10:18:39.089103Z","shell.execute_reply":"2023-07-31T10:18:39.119552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## multi model\nmodelix = ['/kaggle/input/31temmuz-512-agile-data/epoch18.pth',\n           '/kaggle/input/31temmuz-512-agile-data/epoch17.pth',\n           '/kaggle/input/31temmuz-512-agile-data/epoch16.pth',\n           '/kaggle/input/31temmuz-512-agile-data/epoch15.pth',\n           '/kaggle/input/31temmuz-512-agile-data/epoch14.pth',\n          ]\n\nmodellerim = []\nfor model_path in modelix:\n    model = get_model_instance_segmentation(num_classes=2)\n    model.to(device)\n    model.load_state_dict(torch.load(model_path,map_location=device))\n    model.eval()\n    modellerim.append(model)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:39.122126Z","iopub.execute_input":"2023-07-31T10:18:39.123023Z","iopub.status.idle":"2023-07-31T10:18:55.196416Z","shell.execute_reply.started":"2023-07-31T10:18:39.122992Z","shell.execute_reply":"2023-07-31T10:18:55.195415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## this is done with train set","metadata":{}},{"cell_type":"code","source":"all_imgs = glob.glob('/kaggle/input/hubmap-hacking-the-human-vasculature/test/*.tif')\ndataset_test = PennFudanDataset(all_imgs, get_transform(train=False))\ntest_dl = torch.utils.data.DataLoader(\n        dataset_test, batch_size=1, shuffle=False, num_workers=os.cpu_count(), pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:55.1982Z","iopub.execute_input":"2023-07-31T10:18:55.198553Z","iopub.status.idle":"2023-07-31T10:18:55.209262Z","shell.execute_reply.started":"2023-07-31T10:18:55.198519Z","shell.execute_reply":"2023-07-31T10:18:55.20835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nheights = []\nwidths = []\nprediction_strings = []","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:55.210899Z","iopub.execute_input":"2023-07-31T10:18:55.21192Z","iopub.status.idle":"2023-07-31T10:18:55.217637Z","shell.execute_reply.started":"2023-07-31T10:18:55.211888Z","shell.execute_reply":"2023-07-31T10:18:55.216704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## box and mask analysis to assess to be removed ones","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\n\ndef keep_closest_contour(mask,center): ## center is a tuple of x,y which is width and height of the center...\n    # Compute the center of the image\n#     h, w = mask.shape\n#     center = (int(w/2), int(h/2))\n    \n    # Find contours in the mask\n    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    \n    if not contours:\n        return mask  # Return original mask if no contours found\n    \n    # Compute the centroid of each contour and keep the closest to the center\n    min_distance = float('inf')\n    closest_contour = None\n    for contour in contours:\n        M = cv2.moments(contour)\n        # Check for case where m00 is zero to avoid division by zero\n        if M[\"m00\"] != 0:\n            cX = int(M[\"m10\"] / M[\"m00\"])\n            cY = int(M[\"m01\"] / M[\"m00\"])\n        else:\n            cX, cY = 0, 0\n        distance = (cX - center[0])**2 + (cY - center[1])**2\n        if distance < min_distance:\n            min_distance = distance\n            closest_contour = contour\n    \n    # Create an empty mask to draw the closest contour\n    closest_mask = np.zeros_like(mask)\n    cv2.drawContours(closest_mask, [closest_contour], -1, 1, thickness=cv2.FILLED)\n    \n    ## added this...\n#     mask[closest_mask==0]=0\n    \n    return closest_mask # mask # closest_mask... bakalim o gecisli konu duzelecek mi...Mask guncelledim AMA OLMADI...","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:55.218918Z","iopub.execute_input":"2023-07-31T10:18:55.219468Z","iopub.status.idle":"2023-07-31T10:18:55.230793Z","shell.execute_reply.started":"2023-07-31T10:18:55.219436Z","shell.execute_reply":"2023-07-31T10:18:55.229693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def all_mask_iou(masks):\n    masks_flat = np.array([mask.flatten() for mask in masks], dtype=float)\n    area = np.sum(masks_flat, axis=1)\n\n    intersection = np.einsum('ij, kj -> ik', masks_flat, masks_flat)\n    \n    union = area[:, None] + area - intersection\n\n    iou = intersection / union.astype(float)\n    return iou\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:55.232438Z","iopub.execute_input":"2023-07-31T10:18:55.233289Z","iopub.status.idle":"2023-07-31T10:18:55.245057Z","shell.execute_reply.started":"2023-07-31T10:18:55.233258Z","shell.execute_reply":"2023-07-31T10:18:55.244236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## this is to calculate the bounding boxes as we enter the wmf function\ndef calculate_bounding_boxes(masks):\n    bounding_boxes = []\n    for mask in masks:\n        # Find the axis-aligned bounding box around the mask\n        # np.where returns the indices where mask!=0 i.e., the mask boundaries\n        rows = np.any(mask, axis=1)\n        cols = np.any(mask, axis=0)\n        ymin, ymax = np.where(rows)[0][[0, -1]]\n        xmin, xmax = np.where(cols)[0][[0, -1]]\n        \n        # Store the bounding box\n        bounding_boxes.append([xmin, ymin, xmax, ymax])\n    \n    return bounding_boxes","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:55.246561Z","iopub.execute_input":"2023-07-31T10:18:55.246924Z","iopub.status.idle":"2023-07-31T10:18:55.256065Z","shell.execute_reply.started":"2023-07-31T10:18:55.246894Z","shell.execute_reply":"2023-07-31T10:18:55.255288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms.functional as TF","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:55.257519Z","iopub.execute_input":"2023-07-31T10:18:55.257894Z","iopub.status.idle":"2023-07-31T10:18:55.27079Z","shell.execute_reply.started":"2023-07-31T10:18:55.257863Z","shell.execute_reply":"2023-07-31T10:18:55.269878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## image ve glumasklari bir yerde toplayayim\n## me maslari da\n# ali_images = []\n# ali_glumasks = []\n# ali_masks = []\n# ali_boxes = []\n# ali_names = []\n\n# ali_mask_before = []\n# ali_mask_after = []","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:55.272297Z","iopub.execute_input":"2023-07-31T10:18:55.272633Z","iopub.status.idle":"2023-07-31T10:18:55.281264Z","shell.execute_reply.started":"2023-07-31T10:18:55.272601Z","shell.execute_reply":"2023-07-31T10:18:55.280347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = None\nindices_thold = .56\nwith torch.no_grad():\n    for img, idx in tqdm(test_dl):\n#         ali_names.append(idx[0])\n#         ali_images.append(img)\n        # Initialize lists to store predictions.... THIS IS FOR ENSEMBLING boxes scores masks per image\n        all_boxes  = []\n        all_scores = []\n        all_masks  = []\n        all_models = []\n        \n        # Store the image's original shape... that would be necessary to flip back the results...\n        orig_shape = img.shape[-2:]\n        \n        ##31temmuz: glumoril checkler icin buraya alidm...........................\n        glu_mask = np.zeros((512, 512), dtype=np.uint8) ## 31temmuz glumoril olmazsa glu_mask yine de uygula zeros\n        if idx[0] in diles_dict: ## maskin ilgili bolumu off yapiliyor....\n            if len(diles_dict[idx[0]])!=0:  ## bos degilse\n                for cords in diles_dict[idx[0]]: ## burada biden fazla glumoril de olabilir!! her biri bir glumoril\n                    for cd in cords:\n                        rr, cc = np.array([i[1] for i in cd]), np.asarray([i[0] for i in cd])\n                        glu_mask[rr, cc] = 1\n                contours,_ = cv2.findContours((glu_mask*255).astype(np.uint8), 1, 2)\n                for p in contours:\n                    cv2.fillPoly(glu_mask, [p], 1)\n                    \n#         ali_glumasks.append(glu_mask) ## bunlari da gorsel amacli topluyorum...\n\n                    \n        # Convert glu_mask back to boolean type\n        glu_mask_bool = glu_mask.astype(bool)\n        \n        \n                \n        \n        # Loop over each model\n        for model_idx, model in enumerate(modellerim):\n            for flip in [True,False]:\n                # FLIPPED IMAGE ILE BASLIYORUZ.. HER ZAMAN.. DOLAYISI ILE 1 FLIPPED IMAGE 2 IS NORMAL\n                ## yani hep flip ediyoruz... ama ne zaman ceviripyroz... true oldugunda... yani ilk geciste..\n                ## 2nci gecis duzeltmis oluor...\n                img = TF.hflip(img)\n                \n                img = img.to(device)\n                pred = model(img)\n                if sample is None: sample=pred\n                pred_string = ''\n                skorlar = pred[0]['scores'].detach().cpu().numpy()\n                skorix  = [] ## burada da kalan skorlari toplayacagim... bazisi gidecek belki...\n                masks= [] ## burada masklari toplayacagim... 26temmuz: only center refine\n                \n#                 # Convert pred masks to a numpy array\n#                 pred_masks_np = pred[0]['masks'].squeeze().detach().cpu().numpy()\n#                 # Now you can use broadcasting to zero out the values in pred_masks_np where glu_mask_float is 1\n                \n#                 # Apply the mask using logical operation\n#                 pred_masks_np = pred_masks_np * (~glu_mask_bool)\n                \n                pred_masks_np = pred[0]['masks'].squeeze().detach().cpu().numpy()\n                pred_boxes    = pred[0]['boxes'].detach().cpu().numpy()\n            \n                if flip: ## he x need be reversed and the mask of course...\n                    pred_boxes = [[orig_shape[1]-bbox[2], bbox[1], orig_shape[1]-bbox[0], bbox[3]] for bbox in pred_boxes]\n                    pred_masks_np = [mask[:, ::-1].copy() for mask in pred_masks_np]\n                \n                for m in range(len(pred_masks_np)):\n\n                    score= skorlar[m]\n\n                    ## tip1 de bu yok.. 471 de yok bunlari sonra ilave edecegim...,.................................\n                    ## en basa sunu koyalim...\n#                     if score < .19: continue ## skor .19 alti ise hic bakmaya gerek yok...\n\n                    ## mask okuyalim...\n                    mask = pred_masks_np[m]\n#                     mask = pred_masks_np[m]\n#                     print(mask.shape)\n    #                 if centerim: ## BURASI 512LIK YAPAN YER.....................................\n    #                     mask = mask[256:768,256:768]\n                    ##............GLUMORIL KARSILASTIRMASI BURAYA GIREBILIR...\n#                     print(idx[0])\n\n#                     try: ## 31temmuz: check icin try except cikardim....\n                           ## 31temmuz: bunu bir kere yapabilirsi..\n#                     ali_mask_before.append(mask)\n#                     print('mask')\n#                     print(mask)\n#                     print('glumask')\n#                     print(glu_mask)\n#                     ali_mask_after.append(mask)\n#                     except:\n#                         pass\n                    mask[glu_mask==1]=0\n    \n                    mask = np.where(mask>0.44, 1, 0).astype(np.uint8)\n                    \n                    if np.sum(mask)<(mask.shape[0] * mask.shape[1] * .00012): ## diger sart 26temmuz ufaklari atiyorum\n                        continue\n\n                    ## box centerlari lazim... oralama icin.....ama ortalinca da tekrar box hesabi gerekecek...\n                    box = pred_boxes[m]\n                    \n                    ## hemen burada flip yap... sonra diger islemleri yap...\n                    \n                    \n                    \n                    \n                    box = [int(x) for x in box]\n                    x,y,w,h= box[0],box[1],box[2]-box[0],box[3]-box[1]\n                    centerix = x + w//2,y + h//2\n\n                    \n                    \n                    mask = keep_closest_contour(mask,centerix)\n                    \n\n                    ## buna gerek kalmadi....\n    #                 mask = np.expand_dims(mask,-1)\n                    mask = binary_dilation(mask)\n                    masks.append(mask) ## 26temmuz sub1 icin lazimdi bunu da cikardim...\n                    skorix.append(score) ## bu lazim... kalan skorlar... \n\n                ## IKINCI TURDA BU REFINEMENT DA OLACAK.....................................................\n                ##29temmuz iou uzerinde birlesen varsa.. bunlardan confidence az olanlari gonder...\n                result = all_mask_iou(masks) ## iou sonuclari\n                mask_pairs = np.triu_indices_from(result, k=1)  # k=1 excludes the diagonal.. and this takes only upper tringle\n                                                                ## otherwise i j and ji indices would repeat below...\n                indices = []\n                for i, j in zip(*mask_pairs):\n                    if result[i, j] > indices_thold:  # Only print if IoU is above threshold: .6 yaptim...\n                #         print(f\"Masks at indices {i} and {j} have IoU: {result[i, j]}\")\n                        indices.append(((i,j),result[i, j]))\n\n                indices.sort(key = lambda x:x[1],reverse=True)\n\n                ## indices icinde atacaklarim var sirasi ila atayim.....\n                atilacaklar = []\n                for grup in indices:\n                    atilacaklar.append(grup[0][1])\n                atilacaklar = set(atilacaklar)\n\n                ## smdi bunlari hem mask icinden hem de skorlar icinden atalim\n                masks  = [element for i, element in enumerate(masks) if i not in atilacaklar]\n                skorix = [element for i, element in enumerate(skorix) if i not in atilacaklar]\n               #.............................................................................................\n                ## 30temmuz for each mask you also need to make a new bbox after all these revisioons..\n                # Calculate the bounding boxes for these masks\n                bounding_boxes = calculate_bounding_boxes(masks)\n                \n#                 if flip: ## he x need be reversed and the mask of course...\n#                     bounding_boxes = [[orig_shape[1]-bbox[2], bbox[1], orig_shape[1]-bbox[0], bbox[3]] for bbox in bounding_boxes]\n#                     masks = [mask[:, ::-1].copy() for mask in masks]\n\n                # Store the predictions and the model that made them.. the model is just an index\n                ## that will be useful if you want to weight some models because they are better.\n                all_boxes.extend(bounding_boxes)\n                all_masks.extend(masks)\n                all_scores.extend(skorix)\n                all_models.extend([model_idx]*len(skorix))\n            \n        # Now you can use bounding_boxes along with masks and scores in weighted_masks_fusion\n        # Use the weighted_masks_fusion function to combine the predictions\n        ens_masks, ens_scores, ens_boxes = weighted_masks_fusion(\n            np.array(all_masks), \n            np.array(all_boxes), \n            np.array(all_scores), \n            np.array(all_models), \n#             model_weights=[1] * len(modellerim) ## hepsine 1 verdim oncelikli olarak \n            model_weights=[5,4,4,3,3], ## 5 model yaptim\n            iou_thr=0.45,\n            skip_mask_thr=0.15,\n            conf_type='soft_weight',\n            thresh_type='num_thresh',\n            num_thresh=4,\n            num_models=5,\n            \n            \n        )\n        \n#         def weighted_masks_fusion(masks, boxes, scores, models, iou_thr=0.7, skip_mask_thr=0.0, \n#                         conf_type='max_weight', soft_weight=5, thresh_type=None, model_weights=1,\n#                         num_thresh=4, num_models=5):\n        \n#         ali_masks.append(ens_masks)\n#         ali_boxes.append(ens_boxes)\n\n\n\n        scores_and_masks = [(x,y) for (x,y) in zip(ens_scores,ens_masks) if x>.19]\n        ens_scores = [x[0] for x in scores_and_masks]\n        ens_masks  = [x[1] for x in scores_and_masks]\n\n\n        \n        ## encode yapmadan once.... cok dusuk masklar yine atilabilir.... ensemble sirasinda oluyor...............\n        ##........................................................................................................\n        for (skr, maske) in zip(ens_scores,ens_masks):\n            encoded = encode_binary_mask(maske.astype(bool))\n            if m==0:\n                pred_string += f\"0 {skr} {encoded.decode('utf-8')}\"\n\n            else:\n                pred_string += f\" 0 {skr} {encoded.decode('utf-8')}\"\n                \n                \n        b, c, h, w = img.shape\n        ids.append(idx[0])\n        heights.append(h)\n        widths.append(w)\n        prediction_strings.append(pred_string)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:18:55.282952Z","iopub.execute_input":"2023-07-31T10:18:55.283363Z","iopub.status.idle":"2023-07-31T10:19:17.523481Z","shell.execute_reply.started":"2023-07-31T10:18:55.283333Z","shell.execute_reply":"2023-07-31T10:19:17.522491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## refine mask gireblir... cok kucuk ve cok buyukleri elemek icin.....","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:19:17.525437Z","iopub.execute_input":"2023-07-31T10:19:17.525801Z","iopub.status.idle":"2023-07-31T10:19:17.532279Z","shell.execute_reply.started":"2023-07-31T10:19:17.525765Z","shell.execute_reply":"2023-07-31T10:19:17.531305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"array = tiff.imread('/kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif')\nplt.imshow(array)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:19:17.533759Z","iopub.execute_input":"2023-07-31T10:19:17.534415Z","iopub.status.idle":"2023-07-31T10:19:17.941241Z","shell.execute_reply.started":"2023-07-31T10:19:17.53438Z","shell.execute_reply":"2023-07-31T10:19:17.93654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if len(all_imgs)==1:\n    masks = masks[:30]\n    pred_img = np.zeros((512,512), dtype=np.float32)\n    for i, j in enumerate(ens_masks):\n        pred_img += j * (1 - 1/len(masks)*i)\n        pred_img = np.clip(pred_img, 0, 1)\n        print(ens_scores[i])\n        plt.imshow(j)\n        plt.show()\n        \n    plt.imshow(pred_img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:19:17.942445Z","iopub.execute_input":"2023-07-31T10:19:17.942794Z","iopub.status.idle":"2023-07-31T10:19:28.818849Z","shell.execute_reply.started":"2023-07-31T10:19:17.942765Z","shell.execute_reply":"2023-07-31T10:19:28.817994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if len(all_imgs)==1:\n#     top20 = [sample[0]['masks'][i].cpu().numpy().reshape(512, 512) for i in range(min(20,len(sample[0]['masks'])))]\n    \n#     pred_img = np.zeros((512,512), dtype=np.float32)\n#     for i, j in enumerate(top20):\n#         pred_img += j * (1 - 1/len(top20)*i)\n#         pred_img = np.clip(pred_img, 0, 1)\n#         print(sample[0]['scores'][i].cpu().numpy())\n#         plt.imshow(j)\n#         plt.show()\n        \n#     plt.imshow(pred_img)\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:19:28.820493Z","iopub.execute_input":"2023-07-31T10:19:28.821188Z","iopub.status.idle":"2023-07-31T10:19:28.82593Z","shell.execute_reply.started":"2023-07-31T10:19:28.821137Z","shell.execute_reply":"2023-07-31T10:19:28.825038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame()\nsubmission['id'] = ids\nsubmission['height'] = heights\nsubmission['width'] = widths\nsubmission['prediction_string'] = prediction_strings\nsubmission = submission.set_index('id')\nsubmission.to_csv(\"submission.csv\")\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:19:28.827752Z","iopub.execute_input":"2023-07-31T10:19:28.828071Z","iopub.status.idle":"2023-07-31T10:19:28.866958Z","shell.execute_reply.started":"2023-07-31T10:19:28.828042Z","shell.execute_reply":"2023-07-31T10:19:28.866047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 21temmuz submissionda kac tane var ona bakalim.\nstringo = submission.iloc[0].prediction_string\nstringo","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:19:28.86814Z","iopub.execute_input":"2023-07-31T10:19:28.86847Z","iopub.status.idle":"2023-07-31T10:19:28.879031Z","shell.execute_reply.started":"2023-07-31T10:19:28.86844Z","shell.execute_reply":"2023-07-31T10:19:28.878082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# stringo.count('0 0') ## 66 tane box var o zaman kalan 46 tane box nedir o zaman?","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:19:28.8805Z","iopub.execute_input":"2023-07-31T10:19:28.881043Z","iopub.status.idle":"2023-07-31T10:19:28.886139Z","shell.execute_reply.started":"2023-07-31T10:19:28.881012Z","shell.execute_reply":"2023-07-31T10:19:28.885273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if len(all_imgs)==1:\n#     topall = [sample[0]['masks'][i].cpu().numpy().reshape(512, 512) for i in range(len(sample[0]['masks']))]\n    \n#     pred_img = np.zeros((512,512), dtype=np.float32)\n#     for i, j in enumerate(topall):\n#         pred_img += j * (1 - 1/len(topall)*i)\n#         pred_img = np.clip(pred_img, 0, 1)\n#         print(sample[0]['scores'][i].cpu().numpy())\n#         plt.imshow(j)\n#         plt.show()\n        \n#     plt.imshow(pred_img)\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:19:28.887954Z","iopub.execute_input":"2023-07-31T10:19:28.88836Z","iopub.status.idle":"2023-07-31T10:19:28.89975Z","shell.execute_reply.started":"2023-07-31T10:19:28.888329Z","shell.execute_reply":"2023-07-31T10:19:28.895627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for f in glob.glob('*'):\n    if not f.startswith('subm'):\n        !rm -rf {f}","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:19:28.905557Z","iopub.execute_input":"2023-07-31T10:19:28.905823Z","iopub.status.idle":"2023-07-31T10:19:29.915181Z","shell.execute_reply.started":"2023-07-31T10:19:28.9058Z","shell.execute_reply":"2023-07-31T10:19:29.913806Z"},"trusted":true},"execution_count":null,"outputs":[]}]}