{"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 -q install 'git+https://github.com/facebookresearch/detectron2.git'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-24T19:09:52.067588Z","iopub.execute_input":"2021-11-24T19:09:52.068792Z","iopub.status.idle":"2021-11-24T19:10:04.911966Z","shell.execute_reply.started":"2021-11-24T19:09:52.068734Z","shell.execute_reply":"2021-11-24T19:10:04.911227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import detectron2\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.config import get_cfg\nfrom detectron2.data.datasets import register_coco_instances\nfrom detectron2.data import MetadataCatalog, DatasetCatalog\nfrom detectron2.utils.visualizer import Visualizer, ColorMode\nimport pycocotools.mask as mask_util\nfrom PIL import Image\nimport cv2\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport numpy as np\nfrom fastcore.all import *","metadata":{"execution":{"iopub.status.busy":"2021-11-24T19:10:15.405961Z","iopub.execute_input":"2021-11-24T19:10:15.406286Z","iopub.status.idle":"2021-11-24T19:10:16.687832Z","shell.execute_reply.started":"2021-11-24T19:10:15.406250Z","shell.execute_reply":"2021-11-24T19:10:16.686947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataDir=Path('../input/sartorius-cell-instance-segmentation/')\nregister_coco_instances('all',{},'../input/sartorius-cell-instance-segmentation-coco/annotations_all.json', dataDir)\nall_metadata = MetadataCatalog.get('all')\nall_dataset = DatasetCatalog.get('all')","metadata":{"execution":{"iopub.status.busy":"2021-11-24T19:10:18.557433Z","iopub.execute_input":"2021-11-24T19:10:18.557734Z","iopub.status.idle":"2021-11-24T19:10:23.744404Z","shell.execute_reply.started":"2021-11-24T19:10:18.557702Z","shell.execute_reply":"2021-11-24T19:10:23.743593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cache = {d['image_id']: d for d in all_dataset}\n\ndef vis(*image_ids, rect=None):\n    l = len(image_ids)\n    fig = plt.figure(figsize=(40, 12 * l))\n    if rect:\n        x, y, w, h = rect\n    for num, image_id in enumerate(image_ids):\n        d = cache[image_id]\n        name = d[\"file_name\"]\n        img = cv2.imread(name)\n        visualizer = Visualizer(img[:, :, ::-1], metadata=all_metadata, scale=1,instance_mode=ColorMode.SEGMENTATION)\n        for a in d['annotations']:\n            a['bbox'] = [-10,-10,0,0]\n        out = visualizer.draw_dataset_dict(d)\n        img2 = out.get_image()[:, :, ::-1]\n        \n        ax = fig.add_subplot(1 * l, 2, 2*num + 1)\n        if rect:\n            ax.add_patch(patches.Rectangle((x, y), w, h, linewidth=1, edgecolor='r', facecolor='none'))\n        plt.imshow(img2)\n        plt.title(d['image_id'])\n\n        ax = fig.add_subplot(1 * l, 2, 2*num + 2)\n        if rect:\n            ax.add_patch(patches.Rectangle((x, y), w, h, linewidth=1, edgecolor='r', facecolor='none'))\n        plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T19:10:30.670825Z","iopub.execute_input":"2021-11-24T19:10:30.671615Z","iopub.status.idle":"2021-11-24T19:10:30.682665Z","shell.execute_reply.started":"2021-11-24T19:10:30.671568Z","shell.execute_reply":"2021-11-24T19:10:30.681919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inconsistent annotations","metadata":{}},{"cell_type":"code","source":"vis('35fc12883459', 'eec79772cb99', 'e92c56871769')","metadata":{"execution":{"iopub.status.busy":"2021-11-24T19:10:34.671847Z","iopub.execute_input":"2021-11-24T19:10:34.672426Z","iopub.status.idle":"2021-11-24T19:10:38.270380Z","shell.execute_reply.started":"2021-11-24T19:10:34.672372Z","shell.execute_reply":"2021-11-24T19:10:38.268886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Atefacts example","metadata":{}},{"cell_type":"code","source":"vis('25559c20c6f3', '71d6e508abdc', rect=(300, 380, 80, 80))","metadata":{"execution":{"iopub.status.busy":"2021-11-24T19:10:48.230791Z","iopub.execute_input":"2021-11-24T19:10:48.231127Z","iopub.status.idle":"2021-11-24T19:10:50.702686Z","shell.execute_reply.started":"2021-11-24T19:10:48.231094Z","shell.execute_reply":"2021-11-24T19:10:50.701445Z"},"trusted":true},"execution_count":null,"outputs":[]}]}