{"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":"### A quick example on displaying detailed predictions from a model and calculating validation score\nhttps://www.kaggle.com/slawekbiel/sartorius-vis/ script is used for visualisation. You need to do File->Add utility script to use it.","metadata":{}},{"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":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-01T15:27:06.518213Z","iopub.execute_input":"2021-12-01T15:27:06.518497Z","iopub.status.idle":"2021-12-01T15:31:27.862049Z","shell.execute_reply.started":"2021-12-01T15:27:06.518418Z","shell.execute_reply":"2021-12-01T15:31:27.860724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\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 DatasetCatalog\nimport cv2\nimport pycocotools.mask as mask_util\nimport numpy as np\n","metadata":{"execution":{"iopub.status.busy":"2021-12-01T15:33:11.689978Z","iopub.execute_input":"2021-12-01T15:33:11.690262Z","iopub.status.idle":"2021-12-01T15:33:11.695971Z","shell.execute_reply.started":"2021-12-01T15:33:11.690231Z","shell.execute_reply":"2021-12-01T15:33:11.694908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sartorius_vis import show_predictions, show_zoomed","metadata":{"execution":{"iopub.status.busy":"2021-12-01T16:09:42.185276Z","iopub.execute_input":"2021-12-01T16:09:42.186006Z","iopub.status.idle":"2021-12-01T16:09:42.818895Z","shell.execute_reply.started":"2021-12-01T16:09:42.185878Z","shell.execute_reply":"2021-12-01T16:09:42.817802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = get_cfg()\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml\"))\ncfg.INPUT.MASK_FORMAT='bitmask'\ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 3 \ncfg.MODEL.WEIGHTS = '../input/sartorius-models/model_2696.pth'  \ncfg.TEST.DETECTIONS_PER_IMAGE = 1000\ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5\npredictor = DefaultPredictor(cfg)\n\nregister_coco_instances('sartorius_val',{},'../input/sartorius-cell-instance-segmentation-coco/annotations_val.json', \n                        '../input/sartorius-cell-instance-segmentation/')\n\nval_ds = DatasetCatalog.get('sartorius_val')","metadata":{"execution":{"iopub.status.busy":"2021-12-01T15:33:46.325707Z","iopub.execute_input":"2021-12-01T15:33:46.326244Z","iopub.status.idle":"2021-12-01T15:33:54.189883Z","shell.execute_reply.started":"2021-12-01T15:33:46.326190Z","shell.execute_reply":"2021-12-01T15:33:54.188831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Helper functions\ndef read_target_masks(item):\n    enc_targs = list(map(lambda x:x['segmentation'], item['annotations']))\n    #enc_targs = mask_util.frPyObjects(enc_targs, 520, 704)\n    tars = torch.tensor(mask_util.decode(enc_targs)).cuda()\n    return tars.permute(2,0,1).bool()\n\ndef get_preds_and_tars(item):\n    im = cv2.imread(item['file_name'])\n    pred = predictor(im) \n    pred_masks = pred['instances'].pred_masks\n    tars = read_target_masks(item)\n    return item['file_name'], pred_masks, tars\n\n\ndef precision_at(threshold, iou):\n    matches = iou > threshold\n    true_positives = np.sum(matches, axis=1) == 1  # Correct objects\n    false_positives = np.sum(matches, axis=0) == 0  # Missed objects\n    false_negatives = np.sum(matches, axis=1) == 0  # Extra objects\n    return np.sum(true_positives), np.sum(false_positives), np.sum(false_negatives)\n\ndef score(pred, targ):\n    pred_masks = pred['instances'].pred_masks.cpu().numpy()\n    enc_preds = [mask_util.encode(np.asarray(p, order='F')) for p in pred_masks]\n    enc_targs = list(map(lambda x:x['segmentation'], targ['annotations']))\n    ious = mask_util.iou(enc_preds, enc_targs, [0]*len(enc_targs))\n    prec = []\n    for t in np.arange(0.5, 1.0, 0.05):\n        tp, fp, fn = precision_at(t, ious)\n        p = tp / (tp + fp + fn)\n        prec.append(p)\n    return np.mean(prec)\ndef score_all():\n    scores = []\n    for item in val_ds:\n        im =  cv2.imread(item['file_name'])\n        pred = predictor(im)       \n        \n        sc = score(pred, item)\n        scores.append(sc)\n        \n    return np.mean(scores)","metadata":{"execution":{"iopub.status.busy":"2021-12-01T15:43:13.820433Z","iopub.execute_input":"2021-12-01T15:43:13.820718Z","iopub.status.idle":"2021-12-01T15:43:13.826796Z","shell.execute_reply.started":"2021-12-01T15:43:13.820688Z","shell.execute_reply":"2021-12-01T15:43:13.825509Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show a sample cell with the score breakdwn","metadata":{}},{"cell_type":"code","source":"show_predictions(*get_preds_and_tars(val_ds[0]))","metadata":{"execution":{"iopub.status.busy":"2021-12-01T15:58:08.477086Z","iopub.execute_input":"2021-12-01T15:58:08.477416Z","iopub.status.idle":"2021-12-01T15:58:17.842405Z","shell.execute_reply.started":"2021-12-01T15:58:08.477383Z","shell.execute_reply":"2021-12-01T15:58:17.841541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Lets zoom in to better view all the individual predicted cells","metadata":{}},{"cell_type":"code","source":"show_zoomed(*get_preds_and_tars(val_ds[0]))","metadata":{"execution":{"iopub.status.busy":"2021-12-01T15:51:56.165253Z","iopub.execute_input":"2021-12-01T15:51:56.165550Z","iopub.status.idle":"2021-12-01T15:52:14.637850Z","shell.execute_reply.started":"2021-12-01T15:51:56.165518Z","shell.execute_reply":"2021-12-01T15:52:14.636684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Do the same to another file","metadata":{}},{"cell_type":"code","source":"show_predictions(*get_preds_and_tars(val_ds[1]))","metadata":{"execution":{"iopub.status.busy":"2021-12-01T15:53:28.499732Z","iopub.execute_input":"2021-12-01T15:53:28.500484Z","iopub.status.idle":"2021-12-01T15:53:31.142563Z","shell.execute_reply.started":"2021-12-01T15:53:28.500442Z","shell.execute_reply":"2021-12-01T15:53:31.141711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_zoomed(*get_preds_and_tars(val_ds[1]))","metadata":{"execution":{"iopub.status.busy":"2021-12-01T15:53:42.958935Z","iopub.execute_input":"2021-12-01T15:53:42.959248Z","iopub.status.idle":"2021-12-01T15:53:50.488316Z","shell.execute_reply.started":"2021-12-01T15:53:42.959216Z","shell.execute_reply":"2021-12-01T15:53:50.487354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finally calculate score across all validation files:","metadata":{}},{"cell_type":"code","source":"score_all()","metadata":{"execution":{"iopub.status.busy":"2021-11-05T20:52:48.345032Z","iopub.execute_input":"2021-11-05T20:52:48.345585Z","iopub.status.idle":"2021-11-05T20:53:07.971469Z","shell.execute_reply.started":"2021-11-05T20:52:48.345545Z","shell.execute_reply":"2021-11-05T20:53:07.970665Z"},"trusted":true},"execution_count":null,"outputs":[]}]}