{"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 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 import tqdm\nimport torch\nimport cv2\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-16T03:30:17.103446Z","iopub.execute_input":"2023-07-16T03:30:17.103839Z","iopub.status.idle":"2023-07-16T03:30:21.530941Z","shell.execute_reply.started":"2023-07-16T03:30:17.103805Z","shell.execute_reply":"2023-07-16T03:30:21.529823Z"},"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\n\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/addict-2.4.0-py3-none-any.whl\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/yapf-0.32.0-py2.py3-none-any.whl\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/terminal-0.4.0-py3-none-any.whl\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/terminaltables-3.1.10-py2.py3-none-any.whl\n\n!pip install /kaggle/input/mmdet3-wheels/mmcv_full-1.7.1-cp310-cp310-linux_x86_64.whl\n!cp -r /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/mmdetection/ /kaggle/working/\n%cd /kaggle/working/mmdetection\n!pip install -e . --no-deps\n%cd /kaggle/working/\n\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/mmdet-2.26.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-07-16T03:30:22.514745Z","iopub.execute_input":"2023-07-16T03:30:22.515169Z","iopub.status.idle":"2023-07-16T03:35:08.816729Z","shell.execute_reply.started":"2023-07-16T03:30:22.51512Z","shell.execute_reply":"2023-07-16T03:35:08.815496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/internimage-sourcecode/ops_dcnv3 /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-07-16T03:35:08.819653Z","iopub.execute_input":"2023-07-16T03:35:08.820011Z","iopub.status.idle":"2023-07-16T03:35:09.932318Z","shell.execute_reply.started":"2023-07-16T03:35:08.819977Z","shell.execute_reply":"2023-07-16T03:35:09.931033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/ops_dcnv3","metadata":{"execution":{"iopub.status.busy":"2023-07-16T03:35:09.934283Z","iopub.execute_input":"2023-07-16T03:35:09.934654Z","iopub.status.idle":"2023-07-16T03:35:09.942991Z","shell.execute_reply.started":"2023-07-16T03:35:09.934615Z","shell.execute_reply":"2023-07-16T03:35:09.942088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!sh ./make.sh","metadata":{"execution":{"iopub.status.busy":"2023-07-16T03:35:09.944614Z","iopub.execute_input":"2023-07-16T03:35:09.94538Z","iopub.status.idle":"2023-07-16T03:36:43.669226Z","shell.execute_reply.started":"2023-07-16T03:35:09.945346Z","shell.execute_reply":"2023-07-16T03:36:43.667516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-07-16T03:36:43.698981Z","iopub.execute_input":"2023-07-16T03:36:43.699403Z","iopub.status.idle":"2023-07-16T03:36:43.709121Z","shell.execute_reply.started":"2023-07-16T03:36:43.699367Z","shell.execute_reply":"2023-07-16T03:36:43.70796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf mmdetection\n!rm -rf packages\n!rm -rf ops_dcnv3","metadata":{"execution":{"iopub.status.busy":"2023-07-16T03:36:43.713868Z","iopub.execute_input":"2023-07-16T03:36:43.714184Z","iopub.status.idle":"2023-07-16T03:36:46.850723Z","shell.execute_reply.started":"2023-07-16T03:36:43.714151Z","shell.execute_reply":"2023-07-16T03:36:46.849316Z"},"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 != np.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-16T03:36:46.856298Z","iopub.execute_input":"2023-07-16T03:36:46.858777Z","iopub.status.idle":"2023-07-16T03:36:46.878606Z","shell.execute_reply.started":"2023-07-16T03:36:46.858735Z","shell.execute_reply":"2023-07-16T03:36:46.877525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from engine import train_one_epoch, evaluate\nimport utils\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-07-16T03:36:46.883592Z","iopub.execute_input":"2023-07-16T03:36:46.886103Z","iopub.status.idle":"2023-07-16T03:36:47.463208Z","shell.execute_reply.started":"2023-07-16T03:36:46.886056Z","shell.execute_reply":"2023-07-16T03:36:47.462187Z"},"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-16T03:36:47.464912Z","iopub.execute_input":"2023-07-16T03:36:47.46528Z","iopub.status.idle":"2023-07-16T03:36:47.491109Z","shell.execute_reply.started":"2023-07-16T03:36:47.465245Z","shell.execute_reply":"2023-07-16T03:36:47.490155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append(\"/opt/conda/lib/python3.10/site-packages/DCNv3-1.0-py3.10-linux-x86_64.egg\")\nsys.path.append(\"/kaggle/input/internimage-sourcecode\")\n\nfrom mmdet.apis import init_detector, inference_detector,show_result_pyplot, set_random_seed\nimport mmcv_custom  # noqa: F401,F403\nimport mmdet_custom  # noqa: F401,F403\nfrom segment_anything import SamPredictor, sam_model_registry\n\nfrom mmcv import Config\n\nconfig_file = '/kaggle/input/internimage-large-1536-dataset1/dataset1_config.py'\ncfg = Config.fromfile(config_file )\ncfg.data.test.pipeline[1].img_scale= [(1440, 1440)]\n\n#test cfg\ncfg.model.test_cfg.rcnn.max_per_img = 100\ncfg.model.test_cfg.rcnn.nms.iou_threshold=0.6\ncfg.model.test_cfg.rcnn.score_thr=0.05\ncfg.model.test_cfg.rcnn.mask_thr_binary=0.5\n\ncfg.seed = 42\nset_random_seed(42, deterministic=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-16T03:47:36.772176Z","iopub.execute_input":"2023-07-16T03:47:36.772578Z","iopub.status.idle":"2023-07-16T03:47:36.797245Z","shell.execute_reply.started":"2023-07-16T03:47:36.772546Z","shell.execute_reply":"2023-07-16T03:47:36.796223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_file = '/kaggle/input/internimage-large-1536-dataset1/best_bbox_mAP_epoch_10.pth'\nmodel = init_detector(cfg, checkpoint_file, device=device)  # or device='cuda:0'\nall_imgs = glob.glob('/kaggle/input/hubmap-hacking-the-human-vasculature/test/*.tif')","metadata":{"execution":{"iopub.status.busy":"2023-07-16T03:47:40.056077Z","iopub.execute_input":"2023-07-16T03:47:40.056607Z","iopub.status.idle":"2023-07-16T03:47:48.910145Z","shell.execute_reply.started":"2023-07-16T03:47:40.056575Z","shell.execute_reply":"2023-07-16T03:47:48.909109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nheights = []\nwidths = []\nprediction_strings = []\nsample = None\nimport mmcv\nimport matplotlib.pyplot as plt\nimport pycocotools\nimport pycocotools.mask\n\nfor img in all_imgs:\n    image = cv2.imread(img)\n    image = image[::-1]  # HWC to CHW, BGR to RGB\n    image = np.ascontiguousarray(image)\n\n    img_array = mmcv.imread(img,channel_order='rgb')\n    [h, w, c] = img_array.shape \n    pred = inference_detector(model,img)\n    print(\"Len pred: \", len(pred))\n    pred_string = ''\n    \n    pred_class = pred[0]\n    pred_mask = pred[1]\n\n    mask = 0\n    for i, classe in enumerate(pred_class):\n        if classe.shape != (0, 5):\n            print(i)\n            print(classe.shape) #5,5) (40,5) , (1,5)\n\n            if(i==0): #blood case \n                bbs = classe\n                #print(bbs)\n                sgs = pred_mask[i]\n                print(len(sgs))\n                m=0\n                validcount=0\n                for bb, sg in zip(bbs,sgs):\n                    box = bb[:4]\n                    cnf = bb[4] \n                    \n                    binary_mask = sg.astype(np.uint8)\n                    area = int(np.sum(binary_mask))\n                    if area < 60:\n                        continue\n#                     if area < 1000:\n#                         kernel = np.ones(shape=(3, 3), dtype=np.uint8)\n#                     elif area < 2000:\n#                         kernel = np.ones(shape=(3, 3), dtype=np.uint8)\n#                     elif area < 4000:\n#                         kernel = np.ones(shape=(5, 5), dtype=np.uint8)\n#                     elif area < 8000:\n#                         kernel = np.ones(shape=(6, 6), dtype=np.uint8)\n#                     elif area < 16000:\n#                         kernel = np.ones(shape=(7, 7), dtype=np.uint8)\n#                     elif area < 32000:\n#                         kernel = np.ones(shape=(8, 8), dtype=np.uint8)\n#                     else:\n#                         kernel = np.ones(shape=(9, 9), dtype=np.uint8)\n\n#                     binary_mask = cv2.dilate(np.copy(binary_mask), kernel, 2)\n                    binary_mask = binary_mask.astype(bool)   \n                    \n                    encoded = encode_binary_mask(binary_mask)\n                    if m==0:\n                        pred_string += f\"0 {cnf} {encoded.decode('utf-8')}\"\n                        m=m+1\n                    else:\n                        pred_string += f\" 0 {cnf} {encoded.decode('utf-8')}\"\n\n    \n    ids.append(os.path.basename(img).split('.')[0])\n    heights.append(h)\n    widths.append(w)\n    prediction_strings.append(pred_string)                    \n\nsubmission = 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-16T03:47:54.707266Z","iopub.execute_input":"2023-07-16T03:47:54.707637Z","iopub.status.idle":"2023-07-16T03:47:56.73646Z","shell.execute_reply.started":"2023-07-16T03:47:54.707607Z","shell.execute_reply":"2023-07-16T03:47:56.735134Z"},"trusted":true},"execution_count":null,"outputs":[]}]}