{"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":"import torch","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-03T15:20:31.952211Z","iopub.execute_input":"2023-08-03T15:20:31.952725Z","iopub.status.idle":"2023-08-03T15:20:36.422946Z","shell.execute_reply.started":"2023-08-03T15:20:31.952697Z","shell.execute_reply":"2023-08-03T15:20:36.421981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:36.424477Z","iopub.execute_input":"2023-08-03T15:20:36.425024Z","iopub.status.idle":"2023-08-03T15:20:36.435737Z","shell.execute_reply.started":"2023-08-03T15:20:36.424993Z","shell.execute_reply":"2023-08-03T15:20:36.434256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ultralytics -q","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:36.438647Z","iopub.execute_input":"2023-08-03T15:20:36.440091Z","iopub.status.idle":"2023-08-03T15:20:50.079061Z","shell.execute_reply.started":"2023-08-03T15:20:36.440056Z","shell.execute_reply":"2023-08-03T15:20:50.077739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\nfrom ultralytics import YOLO\nwandb.init(mode=\"disabled\")","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:50.082558Z","iopub.execute_input":"2023-08-03T15:20:50.083282Z","iopub.status.idle":"2023-08-03T15:20:53.442390Z","shell.execute_reply.started":"2023-08-03T15:20:50.083244Z","shell.execute_reply":"2023-08-03T15:20:53.441424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\ndef bbox_filler(img, boxes):\n#     print(boxes[1])\n    mask = np.zeros(img.shape[:2], dtype=\"uint8\")\n    for box in boxes:\n        if box[-1]==3:\n#             print(box[0])\n            cv2.rectangle(mask, (box[0:2]), (box[2:4]), 255, -1)\n    masked = cv2.bitwise_and(img, img, mask=mask)\n#     masked = masked//255\n    return np.array(masked/255, dtype=bool)","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.443993Z","iopub.execute_input":"2023-08-03T15:20:53.444900Z","iopub.status.idle":"2023-08-03T15:20:53.452999Z","shell.execute_reply.started":"2023-08-03T15:20:53.444864Z","shell.execute_reply":"2023-08-03T15:20:53.451927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\ndef rle_encode(mask: np.ndarray) -> str:\n    # flatten the mask column wise\n    pixels = mask.T.flatten()\n\n    # pad the list of pixels to ensure they have leading and trailing 0\n    use_padding = False\n    if pixels[0] or pixels[-1]:\n        use_padding = True\n        pixel_padded = np.zeros([len(pixels) + 2], dtype=pixels.dtype)\n        pixel_padded[1:-1] = pixels\n        pixels = pixel_padded\n\n    # get the pixel indices where consecutive pixels don't match => binary masks begin or end\n    # +1 to convert to inclusive start indices, exclusive end indices of masks\n    # +1 to convert to 1-indexed pixel ordering\n    rle = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    if use_padding:\n        rle = rle - 1  # remove the extra offset introduced by padding\n\n    # end pixel indices of masks - start pixel indices of masks = length of RLE runs\n    # store the RLE run lengths replacing the end pixel indices \n    rle[1::2] = rle[1::2] - rle[:-1:2]\n\n    return ' '.join(str(x) for x in rle)","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.454621Z","iopub.execute_input":"2023-08-03T15:20:53.455337Z","iopub.status.idle":"2023-08-03T15:20:53.464658Z","shell.execute_reply.started":"2023-08-03T15:20:53.455306Z","shell.execute_reply":"2023-08-03T15:20:53.463953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndf1 = pd.read_csv('/kaggle/input/dlsprint2/sample_submission.csv')\n# df2 = pd.read_csv('/kaggle/input/dlsprint2/sample_submission.csv')\ndf1 = df1.set_index('Id')\n# df2 = df2.set_index('Id')","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.466166Z","iopub.execute_input":"2023-08-03T15:20:53.466869Z","iopub.status.idle":"2023-08-03T15:20:53.559383Z","shell.execute_reply.started":"2023-08-03T15:20:53.466837Z","shell.execute_reply":"2023-08-03T15:20:53.558486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def channel_shift(img, img1):\n    img2 = np.zeros_like(img)\n    img2[:,:,0] = img1\n    img2[:,:,1] = img1\n    img2[:,:,2] = img1\n    return img2","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.560843Z","iopub.execute_input":"2023-08-03T15:20:53.561200Z","iopub.status.idle":"2023-08-03T15:20:53.566243Z","shell.execute_reply.started":"2023-08-03T15:20:53.561168Z","shell.execute_reply":"2023-08-03T15:20:53.565361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2;\nimport cv2;\ndef histEq(imgPath):\n    img= cv2.imread(imgPath,cv2.IMREAD_GRAYSCALE);\n    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))\n    cl1 = clahe.apply(img)\n    return cl1;","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.567883Z","iopub.execute_input":"2023-08-03T15:20:53.568556Z","iopub.status.idle":"2023-08-03T15:20:53.575924Z","shell.execute_reply.started":"2023-08-03T15:20:53.568525Z","shell.execute_reply":"2023-08-03T15:20:53.575086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def binarize(img):\n    gim = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    gim = cv2.adaptiveThreshold(gim, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 45, 11)\n    g3im = cv2.cvtColor(gim, cv2.COLOR_GRAY2BGR)\n    return g3im","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.580751Z","iopub.execute_input":"2023-08-03T15:20:53.581080Z","iopub.status.idle":"2023-08-03T15:20:53.587180Z","shell.execute_reply.started":"2023-08-03T15:20:53.581020Z","shell.execute_reply":"2023-08-03T15:20:53.586244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def empty_mask(img):\n    mask = np.zeros(img.shape[:2], dtype=\"uint8\")\n    return np.array(mask, dtype=bool)","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.588686Z","iopub.execute_input":"2023-08-03T15:20:53.589237Z","iopub.status.idle":"2023-08-03T15:20:53.595489Z","shell.execute_reply.started":"2023-08-03T15:20:53.589207Z","shell.execute_reply":"2023-08-03T15:20:53.594407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_mask(masks, boxes, clss, cls):\n    indices = torch.where(clss==cls)\n    c_masks = masks[indices]\n    mask_arr = torch.any(c_masks, dim=0).bool()\n    mask_arr = mask_arr.cpu().detach().numpy()\n    mask = mask_arr\n    return mask","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.596961Z","iopub.execute_input":"2023-08-03T15:20:53.597460Z","iopub.status.idle":"2023-08-03T15:20:53.604315Z","shell.execute_reply.started":"2023-08-03T15:20:53.597428Z","shell.execute_reply":"2023-08-03T15:20:53.603425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_exists(masks, boxes, clss, cls=2):\n    indices = torch.where(clss==cls)\n    return len(indices[0])>0","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.605676Z","iopub.execute_input":"2023-08-03T15:20:53.606257Z","iopub.status.idle":"2023-08-03T15:20:53.613303Z","shell.execute_reply.started":"2023-08-03T15:20:53.606225Z","shell.execute_reply":"2023-08-03T15:20:53.612331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tbl_exists(masks, boxes, clss, cls=3):\n    indices = torch.where(clss==cls)\n    return len(indices[0])>0","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.614880Z","iopub.execute_input":"2023-08-03T15:20:53.615277Z","iopub.status.idle":"2023-08-03T15:20:53.621668Z","shell.execute_reply.started":"2023-08-03T15:20:53.615247Z","shell.execute_reply":"2023-08-03T15:20:53.620741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_img_mask(img_model, img, sz, img_conf, rm=True):\n#     res = img_model.predict(source=img, verbose=False, retina_masks=True, imgsz=IMGSZ, conf=0.35)\n#     res = res[0]\n    try:\n        for res in img_model.predict(source=img, verbose=False, retina_masks=rm, imgsz=sz, conf=img_conf, stream=True):\n            try:\n                masks = res.masks.data\n                boxes = res.boxes.data\n            except Exception as e:\n#                 print('a', e)\n                return empty_mask(img)\n            # extract classes\n            clss = boxes[:, 5]\n            mask = extract_mask(masks, boxes, clss, 0)\n            return mask\n    except:\n#         print('b', e)\n        global ff\n        ff = True","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.623096Z","iopub.execute_input":"2023-08-03T15:20:53.623544Z","iopub.status.idle":"2023-08-03T15:20:53.632980Z","shell.execute_reply.started":"2023-08-03T15:20:53.623513Z","shell.execute_reply":"2023-08-03T15:20:53.632085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if perfomance mode is True, then all the documents won't be checked by the image model\nperfomance = True","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.634201Z","iopub.execute_input":"2023-08-03T15:20:53.634465Z","iopub.status.idle":"2023-08-03T15:20:53.640325Z","shell.execute_reply.started":"2023-08-03T15:20:53.634425Z","shell.execute_reply":"2023-08-03T15:20:53.639407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_predictions(model, img2, sz, conf, classes, rm=True):\n    try:\n        for result in model.predict(source=img2, verbose=False, retina_masks=rm, imgsz=sz,conf=conf, stream=True, classes=classes):\n            try:\n                # if nothing is detected, results.masks.data throws exception\n                masks = result.masks.data\n                boxes = result.boxes.data\n                del result\n                return masks, boxes\n            except Exception as e:\n#                 print('c', e)\n                return False\n    except:\n#         print('d', e)\n        global ff\n        ff=True","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.641750Z","iopub.execute_input":"2023-08-03T15:20:53.642443Z","iopub.status.idle":"2023-08-03T15:20:53.649680Z","shell.execute_reply.started":"2023-08-03T15:20:53.642413Z","shell.execute_reply":"2023-08-03T15:20:53.648796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.transform import resize\nfrom skimage import img_as_bool","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:53.650890Z","iopub.execute_input":"2023-08-03T15:20:53.651253Z","iopub.status.idle":"2023-08-03T15:20:54.064380Z","shell.execute_reply.started":"2023-08-03T15:20:53.651222Z","shell.execute_reply":"2023-08-03T15:20:54.063254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_masks(filename, model, model_2, img_model, hist, bz, sz1, sz2, img_conf, conf1, conf2, rm=True):\n    ans_masks = []\n    img_path = os.path.join(img_dir, filename)\n    img = cv2.imread(img_path)\n    img2 = img\n    if hist:\n        img1 = histEq(img_path)\n        img2 = channel_shift(img, img1)\n    if bz:\n        img2 = binarize(img)\n        \n    try:\n        res = get_predictions(model, img2, sz1, conf1, [0, 1], rm)\n        if ff:\n            return\n        masks, boxes = res\n        clss = boxes[:, 5]\n        for cls in range(2):\n            mask = extract_mask(masks, boxes, clss, cls)\n            ans_masks.append(mask)\n    except Exception as e:\n#         print('e', e)\n        for i in range(2): ans_masks.append(empty_mask(img))\n            \n    try:\n        res = get_predictions(model_2, img2, sz2, conf2, [3], rm)\n        if ff:\n            return\n        masks, boxes = res\n        clss = boxes[:, 5]\n        img_mask = extract_img_mask(img_model, img, sz1, img_conf, rm)\n        if ff:\n            return\n#         else:\n#             img_mask = empty_mask(img)\n        \n        ans_masks.append(img_mask)\n        if tbl_exists(masks, boxes, clss):\n            tab_mask = bbox_filler(np.array(ans_masks[0], dtype=int)*0+255, np.array(boxes.cpu(), dtype=int))\n        else:\n            tab_mask = empty_mask(img)\n        \n        ans_masks.append(tab_mask)   \n    except Exception as e:\n#         print('f', e)\n        for i in range(2): ans_masks.append(empty_mask(img))\n    if not rm:\n        h, w, c = img.shape\n        for i in range(4):\n            ans_masks[i] = img_as_bool(resize(ans_masks[i], (h, w)))\n    return ans_masks","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:54.065874Z","iopub.execute_input":"2023-08-03T15:20:54.066247Z","iopub.status.idle":"2023-08-03T15:20:54.080099Z","shell.execute_reply.started":"2023-08-03T15:20:54.066214Z","shell.execute_reply":"2023-08-03T15:20:54.079105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef eval(df, files, hist, bz, sz1, sz2, img_conf, conf1, conf2, rm=True):\n    mo = 0\n    exc = []\n    if len(files)==0 or sz1<128:\n        return\n    for file in tqdm(files):\n        global ff\n        ff = False\n        try:\n            masks = get_masks(\n                filename=file, model=model1, model_2=model2,\\\n                img_model=m_img,hist= hist, \\\n                bz=bz, sz1= sz1, sz2= sz2,\\\n                img_conf=img_conf, conf1=conf1, conf2=conf2,\n            rm=rm)\n            if ff:\n                raise Exception('memory out')\n            img_id =[x['id'] for x in img_data if x['file_name']==file][0]\n            for i, mask in enumerate(masks):\n                rle = rle_encode(mask)\n                df.loc[str(img_id)+'_'+str(i), 'Predicted'] = rle\n        except Exception as e:\n#             print('g', e)\n#             print('hehe')\n            exc.append(file)\n            if mo>100:\n                print('Too many memory out exceptions')\n#                 print('h', e)\n                global failed\n                failed = True\n                return\n            mo += 1\n    eval(df, exc, hist, bz, sz1, sz2, img_conf, conf1, conf2, False)\n#     print('memory out:', mo)\n        ","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:54.081998Z","iopub.execute_input":"2023-08-03T15:20:54.082851Z","iopub.status.idle":"2023-08-03T15:20:54.094253Z","shell.execute_reply.started":"2023-08-03T15:20:54.082818Z","shell.execute_reply":"2023-08-03T15:20:54.093154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport json\ntest_labels = open('/kaggle/input/dlsprint2/badlad/badlad-test-metadata.json')\nlabels = json.loads(test_labels.read())\nimg_data = labels['images']\nimg_dir = '/kaggle/input/dlsprint2/badlad/images/test'\nRETINA_FLAG = True\nSZ1  = 640\nSZ2  = 640\n\nimg_conf = 0.35\nconf1 = 0.25\nconf2 = 0.4\n\nHIST = False\nbz = False\nff = False\nfailed = False","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:54.095754Z","iopub.execute_input":"2023-08-03T15:20:54.096858Z","iopub.status.idle":"2023-08-03T15:20:54.168200Z","shell.execute_reply.started":"2023-08-03T15:20:54.096789Z","shell.execute_reply":"2023-08-03T15:20:54.167311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '/kaggle/input/yoloft/'\nmodel_name = \"e50_shrunk.pt\"\nimg_model_name = 'e100_img.pt'\nmodel2_name = 'e50_aug_1.pt'\nmodel1 = YOLO(base_dir+model_name)\nm_img = YOLO(base_dir+img_model_name)\nmodel2 = YOLO(base_dir+model2_name)","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:54.169902Z","iopub.execute_input":"2023-08-03T15:20:54.170357Z","iopub.status.idle":"2023-08-03T15:20:56.208615Z","shell.execute_reply.started":"2023-08-03T15:20:54.170322Z","shell.execute_reply":"2023-08-03T15:20:56.207592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(df1, os.listdir(img_dir), HIST, bz, SZ1, SZ2, img_conf, conf1, conf2, True)\n# eval(df, exc, hist, bz, sz1, sz2, img_conf, conf1, conf2)\n# eval(model2,True,df2)","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:20:56.210924Z","iopub.execute_input":"2023-08-03T15:20:56.211664Z","iopub.status.idle":"2023-08-03T15:23:27.561288Z","shell.execute_reply.started":"2023-08-03T15:20:56.211615Z","shell.execute_reply":"2023-08-03T15:23:27.559159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not failed:\n    df1.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-03T15:23:27.562532Z","iopub.status.idle":"2023-08-03T15:23:27.562981Z","shell.execute_reply.started":"2023-08-03T15:23:27.562752Z","shell.execute_reply":"2023-08-03T15:23:27.562774Z"},"trusted":true},"execution_count":null,"outputs":[]}]}