{"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":"# For mmdet 2.26\n\n\nIt's a terrible experience for me to switch from mmdet 2.x to 3.0. \n\nIt takes me 1-2 days, and in the end, I give up and go back to mmdet 2.x.\" :(\n\nhttps://mmdetection.readthedocs.io/en/latest/migration/config_migration.html\n\n\n## Reference\n\nhttps://www.kaggle.com/code/fnands/a-quick-yolov7-baseline-inference\n\nhttps://www.kaggle.com/code/andtaichi/hubmap-mmdet-ver3-0-0-infer\n\nhttps://www.kaggle.com/code/isps737/mmdetection-train-infer-baseline\n\nhttps://www.kaggle.com/awsaf49/sartorius-mmdetection-infer\n\n\n\nmmdet issue\n\nhttps://www.kaggle.com/competitions/hubmap-hacking-the-human-vasculature/discussion/414513\n\n\n\ntrain/valid split\n\nhttps://www.kaggle.com/code/benihime91/hubmap-2023-create-coco-annotations\n\n\nmodel info \n\ncascade_mask_rcnn_x101_64x4d_fpn_20e_coco\n\n+------------+-------+--------------+-------+----------+-------+\n\n| category   | AP    | category     | AP    | category | AP    |\n\n+------------+-------+--------------+-------+----------+-------+\n\n| glomerulus | 0.548 | blood_vessel | 0.295 | unsure   | 0.001 |\n\n+------------+-------+--------------+-------+----------+-------+\n\n2023-06-29 05:04:30,302 - mmdet - INFO - Epoch(val) [18][422] \n\nsegm_mAP: 0.2810, segm_mAP_50: 0.4160, segm_mAP_75: 0.2860, segm_mAP_s: 0.0900, segm_mAP_m: 0.1930, \n\nsegm_mAP_l: 0.3600, segm_mAP1000: 0.2810, segm_mAP_copypaste: 0.281 0.416 0.286 0.090 0.193 0.360\n\n\n\n\n","metadata":{}},{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\ntorch.__version__  #torch 2.0\n#!nvidia-smi   #CUDA Version: 11.4\n! ls /usr/local","metadata":{"execution":{"iopub.status.busy":"2023-07-09T08:22:37.795183Z","iopub.execute_input":"2023-07-09T08:22:37.796727Z","iopub.status.idle":"2023-07-09T08:22:38.758084Z","shell.execute_reply.started":"2023-07-09T08:22:37.796678Z","shell.execute_reply":"2023-07-09T08:22:38.756951Z"},"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# # Install mmcv and mmdet packages\n# #3.0\n# #!pip install mmcv mmdet --no-index --find-links /kaggle/input/mmdetection/ -q\n# #os.chdir(\"/kaggle/working\")\n# #ytt 2140\n# !pip install '/kaggle/input/mmdetectionv2140/addict-2.4.0-py3-none-any.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/yapf-0.31.0-py2.py3-none-any.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/terminal-0.4.0-py3-none-any.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/terminaltables-3.1.0-py3-none-any.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/mmcv_full-1_3_8-cu110-torch1_7_0/mmcv_full-1.3.8-cp37-cp37m-manylinux1_x86_64.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/pycocotools-2.0.2/pycocotools-2.0.2' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/mmpycocotools-12.0.3/mmpycocotools-12.0.3' --no-deps\n\n# !rm -rf mmdetection\n\n# !cp -r /kaggle/input/mmdetectionv2140/mmdetection-2.14.0 /kaggle/working/\n# !mv /kaggle/working/mmdetection-2.14.0 /kaggle/working/mmdetection\n# %cd /kaggle/working/mmdetection\n# !pip install -e .\n\n\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#ytt\n#!pip install /kaggle/input/2023-hhp-mmdet/mmcv_full-1.7.0-cp310-cp310-manylinux1_x86_64_cu113.whl\n!pip install /kaggle/input/mmdet3-wheels/mmcv_full-1.7.1-cp310-cp310-linux_x86_64.whl\n#!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/mmcv_full-1.7.0-cp37-cp37m-linux_x86_64.whl\n#!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/pycocotools-2.0.6-cp37-cp37m-linux_x86_64.whl\n#!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/mmpycocotools-12.0.3-cp37-cp37m-linux_x86_64.whl\n\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\n","metadata":{"execution":{"iopub.status.busy":"2023-07-09T08:22:38.760410Z","iopub.execute_input":"2023-07-09T08:22:38.760824Z","iopub.status.idle":"2023-07-09T08:27:21.061124Z","shell.execute_reply.started":"2023-07-09T08:22:38.760770Z","shell.execute_reply":"2023-07-09T08:27:21.059952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-07-09T08:36:03.628619Z","iopub.execute_input":"2023-07-09T08:36:03.629008Z","iopub.status.idle":"2023-07-09T08:36:03.633957Z","shell.execute_reply.started":"2023-07-09T08:36:03.628976Z","shell.execute_reply":"2023-07-09T08:36:03.632831Z"},"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-09T09:11:44.814060Z","iopub.execute_input":"2023-07-09T09:11:44.814479Z","iopub.status.idle":"2023-07-09T09:11:44.825818Z","shell.execute_reply.started":"2023-07-09T09:11:44.814446Z","shell.execute_reply":"2023-07-09T09:11:44.824793Z"},"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-09T08:27:21.083458Z","iopub.execute_input":"2023-07-09T08:27:21.083826Z","iopub.status.idle":"2023-07-09T08:27:21.094842Z","shell.execute_reply.started":"2023-07-09T08:27:21.083793Z","shell.execute_reply":"2023-07-09T08:27:21.093718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import torchvision\n# import torchvision\n# from torchvision.models.detection.faster_rcnn import FastRCNNPredictor\n# from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor\n\n# def 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-09T08:27:21.096157Z","iopub.execute_input":"2023-07-09T08:27:21.096579Z","iopub.status.idle":"2023-07-09T08:27:21.106715Z","shell.execute_reply.started":"2023-07-09T08:27:21.096548Z","shell.execute_reply":"2023-07-09T08:27:21.105830Z"},"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-09T08:27:21.108204Z","iopub.execute_input":"2023-07-09T08:27:21.108581Z","iopub.status.idle":"2023-07-09T08:27:21.366669Z","shell.execute_reply.started":"2023-07-09T08:27:21.108551Z","shell.execute_reply":"2023-07-09T08:27:21.365788Z"},"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-09T08:27:21.367913Z","iopub.execute_input":"2023-07-09T08:27:21.368235Z","iopub.status.idle":"2023-07-09T08:27:21.394057Z","shell.execute_reply.started":"2023-07-09T08:27:21.368205Z","shell.execute_reply":"2023-07-09T08:27:21.393097Z"},"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-09T08:27:21.395402Z","iopub.execute_input":"2023-07-09T08:27:21.395739Z","iopub.status.idle":"2023-07-09T08:27:21.428844Z","shell.execute_reply.started":"2023-07-09T08:27:21.395709Z","shell.execute_reply":"2023-07-09T08:27:21.427964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import mmdet, mmcv, mmengine\n#from mmengine.config import Config\n#from mmengine.runner import Runner\n#from mmdet.utils import register_all_modules\n#from mmdet.apis import init_detector, inference_detector\n#from mmengine.visualization import Visualizer\n\nfrom mmdet.apis import init_detector, inference_detector,show_result_pyplot, set_random_seed\n\n#print(mmdet.__version__)\n#print(mmcv.__version__)\n#print(mmengine.__version__)\n\n\n# # #check file her\n\nfrom mmcv import Config\n\n# config_file = '/kaggle/input/2023-hhp-mmd-model/job43_cascade_mask_rcnn_x101_64x4d_fpn_20e_coco_miniou4_1280-1600_b295.py'\n\nconfig_file = '/kaggle/input/hubmap-dataset/cascade_mask_rcnn_r50_fpn_20e_coco.py'\n\ncfg = Config.fromfile(config_file )\n\n\n\ncfg.data.test.pipeline[1].img_scale= [(512, 512)]#train 1280-1600 , inf 1280-4000 -> 1440,2520,3600#\n\n#test cfg\n#cfg.model.test_cfg.rcnn.max_per_img = 1000\n#cfg.model.test_cfg.rcnn.nms.iou_threshold=0.3\n#cfg.model.test_cfg.rcnn.mask_thr_binary=0.45\n\ncfg.seed = 0\nset_random_seed(0, deterministic=False)\n#print(f'Config:\\n{cfg.pretty_text}')","metadata":{"execution":{"iopub.status.busy":"2023-07-09T08:27:21.432931Z","iopub.execute_input":"2023-07-09T08:27:21.433287Z","iopub.status.idle":"2023-07-09T08:27:25.104321Z","shell.execute_reply.started":"2023-07-09T08:27:21.433260Z","shell.execute_reply":"2023-07-09T08:27:25.103339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checkpoint_file = '/kaggle/input/2023-hhp-mmd-model/job43_cascade_mask_rcnn_x101_64x4d_fpn_20e_coco_miniou4_1280-1600_epoch_18_b295.pth'\ncheckpoint_file = '/kaggle/input/hubmap-dataset/epoch_11.pth'\n\n#model = init_detector(config_file, checkpoint_file, device=device)  # or device='cuda:0'\nmodel = init_detector(cfg, checkpoint_file, device=device)  # or device='cuda:0'\n# model = get_model_instance_segmentation(num_classes=2)\n# model.to(device)\n# model.load_state_dict(torch.load('/kaggle/input/hubmap-train/fold_0_epoch5.pth'))\n# model.eval()\n# print()","metadata":{"execution":{"iopub.status.busy":"2023-07-09T08:27:25.105895Z","iopub.execute_input":"2023-07-09T08:27:25.106249Z","iopub.status.idle":"2023-07-09T08:27:33.330480Z","shell.execute_reply.started":"2023-07-09T08:27:25.106216Z","shell.execute_reply":"2023-07-09T08:27:33.329479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-09T08:28:45.920181Z","iopub.execute_input":"2023-07-09T08:28:45.920565Z","iopub.status.idle":"2023-07-09T08:28:45.928685Z","shell.execute_reply.started":"2023-07-09T08:28:45.920535Z","shell.execute_reply":"2023-07-09T08:28:45.926971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nheights = []\nwidths = []\nprediction_strings = []","metadata":{"execution":{"iopub.status.busy":"2023-07-09T08:28:46.127699Z","iopub.execute_input":"2023-07-09T08:28:46.128583Z","iopub.status.idle":"2023-07-09T08:28:46.134720Z","shell.execute_reply.started":"2023-07-09T08:28:46.128540Z","shell.execute_reply":"2023-07-09T08:28:46.133744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = None\nimport mmcv\n\n\n#confidence_thresholds = {0: 0.5, 1: 0.5, 2: 0.8}\n\nfor img in all_imgs:\n    img_array = mmcv.imread(img,channel_order='rgb')\n    [h, w, c] = img_array.shape \n    pred = inference_detector(model,img)\n    pred_string = ''\n    #print(pred.pred_instances)\n    #print (len(pred)) #2\n    print(pred)\n    \n    pred_class = pred[0]\n#     print(len(pred_class)) #2 class\n#     print(\"pred_class[0].shape:\", pred_class[0].shape) # (48, 5)\n#     print(\"pred_class[1].shape:\", pred_class[1].shape) # (15, 5)\n\n#     print(\"pred_class[0].shape:\", pred_class[0].shape) #(5,5) #class 0 with 5 item\n#     print(pred_class[1].shape) #(40,5)#class 1 with 40 item\n#     print(pred_class[2].shape) #(1,5) #class 2 with 1 item\n    #print(pred.pred_instances)\n    \n    pred_mask = pred[1]\n#     print(\"pred_mask:\", len(pred_mask)) # 2\n#     print(\"len(pred[1][0]):\", len(pred[1][0])) # 48\n#     print(\"len(pred[1][1]):\", len(pred[1][1])) # 15\n\n#     print(len(pred_mask))#3\n#     print(len(pred[1][0])) #5\n#     print(len(pred[1][1])) #40\n#     print(len(pred[1][2]))#1\n\n    for i, classes in enumerate(pred_class):\n        if classes.shape != (0, 5):\n            print(\"i: \", i)\n            print(\"classes.shape: \", classes.shape) #5,5) (40,5) , (1,5)\n\n            if(i==1): #blood case \n                bbs = classes\n#                 print(\"bbs: \", bbs)\n                sgs = pred_mask[i]\n                print(\"len(sgs):\", 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                    #print(cnf)\n                    # print(sg.shape)#512,512\n                    if cnf < 0.6:\n                        continue\n                    #dilation ------\n                    #print(type(sg))\n                    #print(sg)\n                    sg = sg.astype(np.uint8)\n                    kernel = np.ones(shape=(3, 3), dtype=np.uint8)\n                    binary_mask = cv2.dilate(sg, kernel, 3)    \n                    binary_mask = binary_mask.astype(bool)                    \n                    #-------------------------\n                    print(\"binary_mask: \", binary_mask)\n                    encoded = encode_binary_mask(binary_mask)\n                    print(\"encoded: \", encoded)\n                    if m==0: # 把第一个的string前面不加空格\n                        pred_string += f\"0 {cnf} {encoded.decode('utf-8')}\"\n                        print(\"pred_string:\", pred_string)\n                        m=1\n                    else:\n                        pred_string += f\" 0 {cnf} {encoded.decode('utf-8')}\"\n                        print(\"pred_string:\", pred_string)\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","metadata":{"execution":{"iopub.status.busy":"2023-07-09T11:35:03.772768Z","iopub.execute_input":"2023-07-09T11:35:03.773227Z","iopub.status.idle":"2023-07-09T11:35:04.308229Z","shell.execute_reply.started":"2023-07-09T11:35:03.773189Z","shell.execute_reply":"2023-07-09T11:35:04.306166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# colors = [ 'Set1', 'Set3'] \n# legend = {0: 'blood_vessel',1: 'glomerulus'} \n# from skimage import io\n# import matplotlib.patches as mpatches\n# fig, axs = plt.subplots(1, 2, figsize=(10, 5))\n# I = io.imread(str(all_imgs[0]))\n# axs[0].imshow(I)\n# axs[0].set_title('Image')\n# axs[1].imshow(I)\n# pred = inference_detector(model,img)\n# previous_masks = []\n# for i, mask in enumerate(pred[0]):\n#     print(pred[0])\n#     # Filter-out low-scoring results.\n#     score = pred.pred_instances[\"scores\"][i].cpu().item()\n#     label = pred.pred_instances[\"labels\"][i].cpu().item()\n#     if score > min_score_dict[label]:\n#         mk = mask.cpu().numpy()\n#         # Keep only highly likely pixels\n#         binary_mask = mk > mask_threshold_dict[label]\n#         binary_mask = remove_overlapping_pixels(binary_mask, previous_masks)\n#         previous_masks.append(binary_mask)\n#         color = colors[label]\n#         mask = np.ma.masked_where(mk == 0, mk)\n#         axs[1].imshow(mask, cmap=color, alpha=0.8)\n#         axs[1].set_title('Predicted Masks')\n#         # Add score text on each segment\n#         y, x = np.where(mk > 0)\n#         text_x, text_y = np.min(x), np.min(y)\n#         axs[1].text(text_x, text_y, f\"{score:.2f}\", color='white', fontsize=8)\n#         handles = []\n#         for cl in legend:\n#             color = colors[cl]\n#             handles.append(mpatches.Patch(color=plt.colormaps.get_cmap(color)(0)))\n#         axs[1].legend(handles, legend.values(), bbox_to_anchor=(1.05, 1), loc='upper left')","metadata":{"execution":{"iopub.status.busy":"2023-07-09T07:33:06.149991Z","iopub.execute_input":"2023-07-09T07:33:06.150663Z","iopub.status.idle":"2023-07-09T07:33:07.110695Z","shell.execute_reply.started":"2023-07-09T07:33:06.150627Z","shell.execute_reply":"2023-07-09T07:33:07.106492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ids = []\n# heights = []\n# widths = []\n# prediction_strings = []\n\n# for img in all_imgs:\n#     img_array = mmcv.imread(img,channel_order='rgb')\n#     [h, w, c] = img_array.shape \n#     pred = inference_detector(model,img)\n#     print (pred[1]) \n#     previous_masks = []\n#     masks_use = []\n#     labels_use = []\n#     pred_string=\"\"\n#     for i, mask in enumerate(pred.pred_instances[\"masks\"]):\n#         # Filter-out low-scoring results.\n#         score = pred.pred_instances[\"scores\"][i].cpu().item()\n#         print(score)\n#         label = pred.pred_instances[\"labels\"][i].cpu().item()\n# #         if score > min_score_dict[label]:\n# #             mask = mask.cpu().numpy()\n# #             # Keep only highly likely pixels\n# #             binary_mask = mask > mask_threshold_dict[label]\n# #             binary_mask = remove_overlapping_pixels(binary_mask, previous_masks)\n# #             masks_use.append(binary_mask)\n# #             labels_use.append(label)\n# #             previous_masks.append(binary_mask)\n# #             encoded = encode_binary_mask(binary_mask)\n# #             #if label != 0: continue\n# #             if i == 0:\n# #                 pred_string += f\"{int(label)} {score} {encoded.decode('utf-8')}\"\n# #             else:\n# #                 pred_string += f\" {int(label)} {score} {encoded.decode('utf-8')}\"      \n# #             #print(pred_classes[i])\n# #     ids.append(str(img).split('.')[0].split('/')[-1])\n# #     heights.append(h)\n# #     widths.append(w)\n# #     prediction_strings.append(pred_string)","metadata":{"execution":{"iopub.status.busy":"2023-07-09T07:31:32.621094Z","iopub.execute_input":"2023-07-09T07:31:32.621814Z","iopub.status.idle":"2023-07-09T07:31:32.627927Z","shell.execute_reply.started":"2023-07-09T07:31:32.621779Z","shell.execute_reply":"2023-07-09T07:31:32.626691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(prediction_strings)","metadata":{"execution":{"iopub.status.busy":"2023-07-09T07:31:32.629267Z","iopub.execute_input":"2023-07-09T07:31:32.629676Z","iopub.status.idle":"2023-07-09T07:31:32.639723Z","shell.execute_reply.started":"2023-07-09T07:31:32.629635Z","shell.execute_reply":"2023-07-09T07:31:32.638744Z"},"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-09T07:31:32.641084Z","iopub.execute_input":"2023-07-09T07:31:32.641446Z","iopub.status.idle":"2023-07-09T07:31:32.676935Z","shell.execute_reply.started":"2023-07-09T07:31:32.641414Z","shell.execute_reply":"2023-07-09T07:31:32.676060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf mmdetection\n!rm -rf packages","metadata":{"execution":{"iopub.status.busy":"2023-07-09T07:31:32.678307Z","iopub.execute_input":"2023-07-09T07:31:32.678635Z","iopub.status.idle":"2023-07-09T07:31:34.714954Z","shell.execute_reply.started":"2023-07-09T07:31:32.678605Z","shell.execute_reply":"2023-07-09T07:31:34.713652Z"},"trusted":true},"execution_count":null,"outputs":[]}]}