{"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 ../input/detectron-05/whls/pycocotools-2.0.2/dist/pycocotools-2.0.2.tar --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/fvcore-0.1.5.post20211019/fvcore-0.1.5.post20211019 --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/antlr4-python3-runtime-4.8/antlr4-python3-runtime-4.8 --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/detectron2-0.5/detectron2 --no-index --find-links ../input/detectron-05/whls ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-17T21:13:27.138734Z","iopub.execute_input":"2023-04-17T21:13:27.139353Z","iopub.status.idle":"2023-04-17T21:16:42.390114Z","shell.execute_reply.started":"2023-04-17T21:13:27.139258Z","shell.execute_reply":"2023-04-17T21:16:42.389233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import detectron2\nimport torch\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.config import get_cfg\nfrom detectron2.data import MetadataCatalog, DatasetCatalog\nfrom detectron2.data.datasets import register_coco_instances\nimport os\n\nfrom PIL import Image\nimport numpy as np  \nimport pandas as pd \nimport cv2  \nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-04-17T21:18:17.316176Z","iopub.execute_input":"2023-04-17T21:18:17.316467Z","iopub.status.idle":"2023-04-17T21:18:17.322179Z","shell.execute_reply.started":"2023-04-17T21:18:17.316423Z","shell.execute_reply":"2023-04-17T21:18:17.321081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = get_cfg()\n\ncfg.INPUT.MASK_FORMAT='bitmask'\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml\"))\ncfg.DATASETS.TRAIN = (\"sartorius_cells_train\",)\ncfg.DATASETS.TEST = (\"sartorius_cells_val\",)\ncfg.DATALOADER.NUM_WORKERS = 2\ncfg.SOLVER.IMS_PER_BATCH = 2\ncfg.SOLVER.BASE_LR = 0.001 \ncfg.SOLVER.MAX_ITER = 3000    \ncfg.SOLVER.STEPS = []        \ncfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128   \ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 3  \ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = .35\ncfg.MODEL.ROI_HEADS.NMS_THRESH_TEST = 0.7","metadata":{"execution":{"iopub.status.busy":"2023-04-17T21:19:34.165723Z","iopub.execute_input":"2023-04-17T21:19:34.166028Z","iopub.status.idle":"2023-04-17T21:19:34.190252Z","shell.execute_reply.started":"2023-04-17T21:19:34.165994Z","shell.execute_reply":"2023-04-17T21:19:34.189361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids, masks=[],[]\ntest_names = os.listdir('../input/sartorius-cell-instance-segmentation/test')\n\ncfg = get_cfg()\nfrom detectron2.projects.point_rend import add_pointrend_config\nadd_pointrend_config(cfg)\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml\"))\ncfg.INPUT.MASK_FORMAT='bitmask'\ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 3 \ncfg.MODEL.WEIGHTS = '/kaggle/input/model-final/model_final_3_5it_lr1.pth'  \ncfg.TEST.DETECTIONS_PER_IMAGE = 1500\npredictor = DefaultPredictor(cfg)\nTHRESHOLDS = [.15, .35, .55] \nMIN_PIXELS = [75, 150, 75]","metadata":{"execution":{"iopub.status.busy":"2023-04-17T21:24:23.476631Z","iopub.execute_input":"2023-04-17T21:24:23.476959Z","iopub.status.idle":"2023-04-17T21:24:31.571261Z","shell.execute_reply.started":"2023-04-17T21:24:23.476924Z","shell.execute_reply":"2023-04-17T21:24:31.570450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# From https://www.kaggle.com/stainsby/fast-tested-rle\ndef rle_decode(mask_rle, shape=(520, 704)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape)  # Needed to align to RLE direction\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\ndef get_masks(fn, predictor):\n    im = cv2.imread(str(fn))\n    pred = predictor(im)\n    pred_class = torch.mode(pred['instances'].pred_classes)[0]\n    take = pred['instances'].scores >= THRESHOLDS[pred_class]\n    pred_masks = pred['instances'].pred_masks[take]\n    pred_masks = pred_masks.cpu().numpy()\n    res = []\n    used = np.zeros(im.shape[:2], dtype=int) \n    for mask in pred_masks:\n        mask = mask * (1-used)\n        if mask.sum() >= MIN_PIXELS[pred_class]: # skip predictions with small area\n            used += mask\n            res.append(rle_encode(mask))\n    return res","metadata":{"execution":{"iopub.status.busy":"2023-04-17T21:24:53.403319Z","iopub.execute_input":"2023-04-17T21:24:53.404139Z","iopub.status.idle":"2023-04-17T21:24:53.414459Z","shell.execute_reply.started":"2023-04-17T21:24:53.404100Z","shell.execute_reply":"2023-04-17T21:24:53.413734Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for fn in test_names:\n    print(fn[:-4])\n    file = os.path.join(\"../input/sartorius-cell-instance-segmentation/test\", fn)\n    encoded_masks = get_masks(file, predictor)\n    for enc in encoded_masks:\n        ids.append(fn[:-4])\n        masks.append(enc)\n        \ndf = pd.DataFrame({'id':ids, 'predicted':masks})\ndf.to_csv('submission.csv', index=False)\npd.read_csv('submission.csv').head()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T21:25:40.357512Z","iopub.execute_input":"2023-04-17T21:25:40.357945Z","iopub.status.idle":"2023-04-17T21:25:48.002331Z","shell.execute_reply.started":"2023-04-17T21:25:40.357901Z","shell.execute_reply":"2023-04-17T21:25:48.001646Z"},"trusted":true},"execution_count":null,"outputs":[]}]}