{"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":"!pip install -q /kaggle/input/detectron2-wheel/detectron2/detectron2-0.6-cp310-cp310-linux_x86_64.whl --no-index --find-links=/kaggle/input/detectron2-wheel/detectron2","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:07:01.808244Z","iopub.execute_input":"2023-07-06T10:07:01.808532Z","iopub.status.idle":"2023-07-06T10:07:19.414102Z","shell.execute_reply.started":"2023-07-06T10:07:01.808506Z","shell.execute_reply":"2023-07-06T10:07:19.412796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch, torchvision\nimport tqdm as tqdm\nimport detectron2\nfrom detectron2.utils.logger import setup_logger\nimport os\nimport numpy as np\nimport json\nimport cv2\nfrom detectron2.structures import BoxMode","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:07:19.420378Z","iopub.execute_input":"2023-07-06T10:07:19.422873Z","iopub.status.idle":"2023-07-06T10:07:23.381454Z","shell.execute_reply.started":"2023-07-06T10:07:19.422830Z","shell.execute_reply":"2023-07-06T10:07:23.380423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '/kaggle/input/hubmap-hacking-the-human-vasculature'\nannote_dir = f'{base_dir}/polygons.jsonl'\nimages_dir = f'{base_dir}/train' \ntest_images_dir = f'{base_dir}/test' ","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:07:23.382776Z","iopub.execute_input":"2023-07-06T10:07:23.384478Z","iopub.status.idle":"2023-07-06T10:07:23.390877Z","shell.execute_reply.started":"2023-07-06T10:07:23.384436Z","shell.execute_reply":"2023-07-06T10:07:23.389356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_class_dicts(img_dir,json_file='/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl'):\n   # classes = ['blood_vessel', 'glomerulus', 'unsure']\n    dataset_dicts = []\n    with open(json_file,'r') as f:\n        json_list = list(f)    \n    tiles_dicts = []\n    for i,json_str in enumerate(json_list):\n        tiles_dicts.append(json.loads(json_str))\n    for idx, v in tqdm.tqdm(enumerate(tiles_dicts)):\n        record = {}\n        filename = os.path.join(img_dir, v[\"id\"]+\".tif\")\n        height, width = cv2.imread(filename).shape[:2]\n        record[\"file_name\"] = filename\n        record[\"image_id\"] = idx\n        record[\"height\"] = height\n        record[\"width\"] = width\n        annos = v[\"annotations\"]\n        objs = []\n        for anno in annos:\n            label = anno['type']\n            if label==\"blood_vessel\":\n                px = [a[0] for a in anno['coordinates'][0]]\n                py = [a[1] for a in anno['coordinates'][0]]\n                poly = [(x+0.5, y+0.5) for x, y in zip(px, py)]\n                poly = [p for x in poly for p in x]\n                obj = {\n                    \"bbox\": [np.min(px), np.min(py), np.max(px), np.max(py)],\n                    \"bbox_mode\": BoxMode.XYXY_ABS,\n                    \"segmentation\": [poly],\n                    \"category_id\": 0\n                }\n                objs.append(obj)\n        if len(obj)>0:\n            record[\"annotations\"] = objs\n            dataset_dicts.append(record)\n    return dataset_dicts","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:07:23.393710Z","iopub.execute_input":"2023-07-06T10:07:23.394160Z","iopub.status.idle":"2023-07-06T10:07:23.408391Z","shell.execute_reply.started":"2023-07-06T10:07:23.394127Z","shell.execute_reply":"2023-07-06T10:07:23.407137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dir = '/kaggle/input/hubmap-hacking-the-human-vasculature/train'\npath_json = '/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl'","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:07:23.410049Z","iopub.execute_input":"2023-07-06T10:07:23.410531Z","iopub.status.idle":"2023-07-06T10:07:23.421970Z","shell.execute_reply.started":"2023-07-06T10:07:23.410499Z","shell.execute_reply":"2023-07-06T10:07:23.420783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.data import DatasetCatalog, MetadataCatalog\nDatasetCatalog.register(\"hubmap\", lambda d=img_dir: get_class_dicts(d))\nMetadataCatalog.get(\"hubmap\").set(thing_classes=['blood_vessel'])","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:07:23.424442Z","iopub.execute_input":"2023-07-06T10:07:23.425241Z","iopub.status.idle":"2023-07-06T10:07:23.512599Z","shell.execute_reply.started":"2023-07-06T10:07:23.425193Z","shell.execute_reply":"2023-07-06T10:07:23.511597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.utils.visualizer import Visualizer\nfrom detectron2.utils.visualizer import ColorMode\nimport random\nimport matplotlib.pyplot as plt\ndataset_dicts = get_class_dicts(img_dir)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:07:53.513653Z","iopub.execute_input":"2023-07-06T10:07:53.514022Z","iopub.status.idle":"2023-07-06T10:08:40.107029Z","shell.execute_reply.started":"2023-07-06T10:07:53.513993Z","shell.execute_reply":"2023-07-06T10:08:40.105932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,20))\nfor i,d in enumerate(random.sample(dataset_dicts, 6)):\n    img = cv2.imread(d[\"file_name\"])\n    visualizer = Visualizer(img[:, :, ::-1], metadata=MetadataCatalog.get(\"hubmap\"), scale=0.5)\n    vis = visualizer.draw_dataset_dict(d)\n    plt.subplot(2,3,i+1)\n    plt.imshow(vis.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:09:08.241976Z","iopub.execute_input":"2023-07-06T10:09:08.242393Z","iopub.status.idle":"2023-07-06T10:09:13.065867Z","shell.execute_reply.started":"2023-07-06T10:09:08.242361Z","shell.execute_reply":"2023-07-06T10:09:13.064499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.engine import DefaultTrainer\nfrom detectron2.config import get_cfg\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor\ncfg = get_cfg()\ncfg.OUTPUT_DIR = \"/kaggle/working\"\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml\"))","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:09:30.043489Z","iopub.execute_input":"2023-07-06T10:09:30.044196Z","iopub.status.idle":"2023-07-06T10:09:30.152867Z","shell.execute_reply.started":"2023-07-06T10:09:30.044160Z","shell.execute_reply":"2023-07-06T10:09:30.151835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.DATASETS.TRAIN = (\"hubmap\",)     # our training dataset\ncfg.DATASETS.TEST = ()\ncfg.DATALOADER.NUM_WORKERS = 2\ncfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128  \ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 1 # 1\ncfg.SOLVER.IMS_PER_BATCH = 2 \ncfg.INPUT.MASK_FORMAT='bitmask'\ncfg.SOLVER.MAX_ITER = 2000#Maximum of iterations 1\ncfg.SOLVER.BASE_LR = 0.0001\ntrainer = DefaultTrainer(cfg)\n#trainer.resume_or_load(resume=True)\ntrainer.train()","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:12:48.412934Z","iopub.execute_input":"2023-07-06T10:12:48.413346Z","iopub.status.idle":"2023-07-06T10:21:28.367259Z","shell.execute_reply.started":"2023-07-06T10:12:48.413310Z","shell.execute_reply":"2023-07-06T10:21:28.365644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, \"model_final.pth\")\ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.3 # set the testing threshold for this model\ncfg.DATASETS.TEST = (\"hubmap\", )\npredictor = DefaultPredictor(cfg)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:25:17.326892Z","iopub.execute_input":"2023-07-06T10:25:17.327301Z","iopub.status.idle":"2023-07-06T10:25:18.367924Z","shell.execute_reply.started":"2023-07-06T10:25:17.327269Z","shell.execute_reply":"2023-07-06T10:25:18.366829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.utils.visualizer import ColorMode\nplt.figure(figsize=(30,20))\nfor i,d in enumerate(dataset_dicts): \n    if i>=10:\n        break\n    im = cv2.imread(d[\"file_name\"])\n    outputs = predictor(im)\n    v = Visualizer(im[:, :, ::-1],\n                   metadata=MetadataCatalog.get(\"hubmap\"), \n                   scale=1, \n                   instance_mode=ColorMode.IMAGE_BW   # remove the colors of unsegmented pixels\n    )\n    v = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\n    plt.subplot(2,5,i+1)\n    plt.imshow(v.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2023-07-06T10:25:18.630970Z","iopub.execute_input":"2023-07-06T10:25:18.631385Z","iopub.status.idle":"2023-07-06T10:25:26.862950Z","shell.execute_reply.started":"2023-07-06T10:25:18.631350Z","shell.execute_reply":"2023-07-06T10:25:26.861930Z"},"trusted":true},"execution_count":null,"outputs":[]}]}