{"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":{},"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-06-29T08:36:57.794278Z","iopub.execute_input":"2023-06-29T08:36:57.794633Z","iopub.status.idle":"2023-06-29T08:36:58.858818Z","shell.execute_reply.started":"2023-06-29T08:36:57.794607Z","shell.execute_reply":"2023-06-29T08:36:58.857117Z"},"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-06-29T08:37:06.512883Z","iopub.execute_input":"2023-06-29T08:37:06.513294Z","iopub.status.idle":"2023-06-29T08:41:45.396773Z","shell.execute_reply.started":"2023-06-29T08:37:06.513261Z","shell.execute_reply":"2023-06-29T08:41:45.39559Z"},"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-06-29T08:42:53.051678Z","iopub.execute_input":"2023-06-29T08:42:53.052102Z","iopub.status.idle":"2023-06-29T08:42:53.069201Z","shell.execute_reply.started":"2023-06-29T08:42:53.052065Z","shell.execute_reply":"2023-06-29T08:42:53.068063Z"},"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-06-29T08:42:55.650282Z","iopub.execute_input":"2023-06-29T08:42:55.650648Z","iopub.status.idle":"2023-06-29T08:42:55.661135Z","shell.execute_reply.started":"2023-06-29T08:42:55.65062Z","shell.execute_reply":"2023-06-29T08:42:55.660185Z"},"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-06-28T12:02:21.738082Z","iopub.execute_input":"2023-06-28T12:02:21.738937Z","iopub.status.idle":"2023-06-28T12:02:21.744142Z","shell.execute_reply.started":"2023-06-28T12:02:21.738896Z","shell.execute_reply":"2023-06-28T12:02:21.74318Z"},"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-06-29T08:42:58.968374Z","iopub.execute_input":"2023-06-29T08:42:58.968747Z","iopub.status.idle":"2023-06-29T08:42:59.225462Z","shell.execute_reply.started":"2023-06-29T08:42:58.968716Z","shell.execute_reply":"2023-06-29T08:42:59.224572Z"},"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-06-29T08:43:00.656244Z","iopub.execute_input":"2023-06-29T08:43:00.656589Z","iopub.status.idle":"2023-06-29T08:43:00.685909Z","shell.execute_reply.started":"2023-06-29T08:43:00.656561Z","shell.execute_reply":"2023-06-29T08:43:00.685062Z"},"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-06-29T08:43:02.660719Z","iopub.execute_input":"2023-06-29T08:43:02.661213Z","iopub.status.idle":"2023-06-29T08:43:02.703374Z","shell.execute_reply.started":"2023-06-29T08:43:02.661172Z","shell.execute_reply":"2023-06-29T08:43:02.701884Z"},"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\nconfig_file = '/kaggle/input/2023-hhp-mmd-model/job43_cascade_mask_rcnn_x101_64x4d_fpn_20e_coco_miniou4_1280-1600_b295.py'\n\ncfg = Config.fromfile(config_file )\n\n\n\ncfg.data.test.pipeline[1].img_scale= [(1440, 1440)]#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-06-29T08:43:05.559761Z","iopub.execute_input":"2023-06-29T08:43:05.5602Z","iopub.status.idle":"2023-06-29T08:43:10.447388Z","shell.execute_reply.started":"2023-06-29T08:43:05.56016Z","shell.execute_reply":"2023-06-29T08:43:10.44638Z"},"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'\n\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-06-29T08:43:49.742387Z","iopub.execute_input":"2023-06-29T08:43:49.742769Z","iopub.status.idle":"2023-06-29T08:44:04.978098Z","shell.execute_reply.started":"2023-06-29T08:43:49.742738Z","shell.execute_reply":"2023-06-29T08:44:04.97716Z"},"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-06-29T08:44:15.977956Z","iopub.execute_input":"2023-06-29T08:44:15.978318Z","iopub.status.idle":"2023-06-29T08:44:15.987388Z","shell.execute_reply.started":"2023-06-29T08:44:15.978287Z","shell.execute_reply":"2023-06-29T08:44:15.986485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nheights = []\nwidths = []\nprediction_strings = []","metadata":{"execution":{"iopub.status.busy":"2023-06-29T08:44:18.122595Z","iopub.execute_input":"2023-06-29T08:44:18.122936Z","iopub.status.idle":"2023-06-29T08:44:18.128581Z","shell.execute_reply.started":"2023-06-29T08:44:18.122907Z","shell.execute_reply":"2023-06-29T08:44:18.127534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = None\nimport mmcv\n\n\nconfidence_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)) #3 class\n#     print(pred_class)\n#     print(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(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, 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==1): #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                    #print(cnf)\n                    #print(sg.shape)#512,512\n                    \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                    \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","metadata":{"execution":{"iopub.status.busy":"2023-06-29T08:47:25.231813Z","iopub.execute_input":"2023-06-29T08:47:25.232236Z","iopub.status.idle":"2023-06-29T08:47:25.751384Z","shell.execute_reply.started":"2023-06-29T08:47:25.232205Z","shell.execute_reply":"2023-06-29T08:47:25.750447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(prediction_strings)","metadata":{"execution":{"iopub.status.busy":"2023-06-28T11:42:04.774136Z","iopub.execute_input":"2023-06-28T11:42:04.775021Z","iopub.status.idle":"2023-06-28T11:42:04.780624Z","shell.execute_reply.started":"2023-06-28T11:42:04.774976Z","shell.execute_reply":"2023-06-28T11:42:04.77965Z"},"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-06-28T12:03:01.116361Z","iopub.execute_input":"2023-06-28T12:03:01.116706Z","iopub.status.idle":"2023-06-28T12:03:01.135201Z","shell.execute_reply.started":"2023-06-28T12:03:01.116678Z","shell.execute_reply":"2023-06-28T12:03:01.13431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf mmdetection\n!rm -rf packages","metadata":{"execution":{"iopub.status.busy":"2023-06-28T12:03:15.040206Z","iopub.execute_input":"2023-06-28T12:03:15.040598Z","iopub.status.idle":"2023-06-28T12:03:17.100622Z","shell.execute_reply.started":"2023-06-28T12:03:15.040563Z","shell.execute_reply":"2023-06-28T12:03:17.099305Z"},"trusted":true},"execution_count":null,"outputs":[]}]}