{"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 'git+https://github.com/facebookresearch/detectron2.git'","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-01-04T11:26:32.053746Z","iopub.execute_input":"2022-01-04T11:26:32.054079Z","iopub.status.idle":"2022-01-04T11:30:24.399710Z","shell.execute_reply.started":"2022-01-04T11:26:32.054036Z","shell.execute_reply":"2022-01-04T11:30:24.398651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install gdown ","metadata":{"execution":{"iopub.status.busy":"2022-01-04T11:23:42.654191Z","iopub.execute_input":"2022-01-04T11:23:42.654580Z","iopub.status.idle":"2022-01-04T11:24:08.696056Z","shell.execute_reply.started":"2022-01-04T11:23:42.654465Z","shell.execute_reply":"2022-01-04T11:24:08.694922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gdown --id 1-DhkgqWAd8jmrdFb4-AKjfGPQq2uGqwt","metadata":{"execution":{"iopub.status.busy":"2022-01-04T11:24:26.196362Z","iopub.execute_input":"2022-01-04T11:24:26.196696Z","iopub.status.idle":"2022-01-04T11:24:36.095205Z","shell.execute_reply.started":"2022-01-04T11:24:26.196664Z","shell.execute_reply":"2022-01-04T11:24:36.094058Z"},"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\n\nimport cv2\nimport pycocotools.mask as mask_util\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-01-04T11:30:24.402897Z","iopub.execute_input":"2022-01-04T11:30:24.403350Z","iopub.status.idle":"2022-01-04T11:30:25.253400Z","shell.execute_reply.started":"2022-01-04T11:30:24.403306Z","shell.execute_reply":"2022-01-04T11:30:25.252139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sartorius_vis import show_predictions, show_zoomed","metadata":{"execution":{"iopub.status.busy":"2022-01-04T11:30:25.255040Z","iopub.execute_input":"2022-01-04T11:30:25.255834Z","iopub.status.idle":"2022-01-04T11:30:25.274874Z","shell.execute_reply.started":"2022-01-04T11:30:25.255783Z","shell.execute_reply":"2022-01-04T11:30:25.273921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"register_coco_instances('sartorius_val',{},'../input/k-fold-crossvalidation-coco-dataset-generator/coco_cell_valid_fold4.json', \n                        '../input/sartorius-cell-instance-segmentation/')\n\nval_ds = DatasetCatalog.get('sartorius_val')","metadata":{"execution":{"iopub.status.busy":"2022-01-04T11:30:25.277532Z","iopub.execute_input":"2022-01-04T11:30:25.278565Z","iopub.status.idle":"2022-01-04T11:30:26.233907Z","shell.execute_reply.started":"2022-01-04T11:30:25.278508Z","shell.execute_reply":"2022-01-04T11:30:26.232809Z"},"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_masks, targ):\n   \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)\n\n\ndef score_all(model, ths, config):\n    cfg = get_cfg()\n    cfg.merge_from_file(model_zoo.get_config_file(config))\n    cfg.INPUT.MASK_FORMAT='bitmask'\n    cfg.MODEL.ROI_HEADS.NUM_CLASSES = 3 \n    cfg.MODEL.WEIGHTS = model\n    cfg.TEST.DETECTIONS_PER_IMAGE = 1000\n    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5\n    predictor = DefaultPredictor(cfg)\n\n    scores = []\n    \n    for item in val_ds:\n        im =  cv2.imread(item['file_name'])\n        output = predictor(im)\n        pred_classes = output['instances'].pred_classes.cpu().numpy().tolist()\n        pred_class = max(set(pred_classes), key=pred_classes.count)\n        take = output['instances'].scores >= ths[pred_class]\n        pred_masks = output['instances'].pred_masks[take]\n        pred_masks = pred_masks.cpu().numpy()\n        \n        sc = score(pred_masks, item)\n        scores.append(sc)\n        \n    return np.mean(scores)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-04T11:30:26.235715Z","iopub.execute_input":"2022-01-04T11:30:26.236277Z","iopub.status.idle":"2022-01-04T11:30:26.257015Z","shell.execute_reply.started":"2022-01-04T11:30:26.236229Z","shell.execute_reply":"2022-01-04T11:30:26.255928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finally calculate score across all validation files:","metadata":{}},{"cell_type":"code","source":"config1 = \"Misc/cascade_mask_rcnn_R_50_FPN_3x.yaml\"\nconfig2 = \"COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml\"","metadata":{"execution":{"iopub.status.busy":"2022-01-04T11:30:26.259293Z","iopub.execute_input":"2022-01-04T11:30:26.260068Z","iopub.status.idle":"2022-01-04T11:30:26.271994Z","shell.execute_reply.started":"2022-01-04T11:30:26.260018Z","shell.execute_reply":"2022-01-04T11:30:26.270742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_all('./model_best.pth', [0.19, 0.39, 0.59], config2 )","metadata":{"execution":{"iopub.status.busy":"2022-01-04T11:30:26.275714Z","iopub.execute_input":"2022-01-04T11:30:26.276038Z","iopub.status.idle":"2022-01-04T11:31:34.840690Z","shell.execute_reply.started":"2022-01-04T11:30:26.275996Z","shell.execute_reply":"2022-01-04T11:31:34.838461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install gdown ","metadata":{"execution":{"iopub.status.busy":"2022-01-04T11:26:27.047015Z","iopub.status.idle":"2022-01-04T11:26:27.047962Z","shell.execute_reply.started":"2022-01-04T11:26:27.047612Z","shell.execute_reply":"2022-01-04T11:26:27.047645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gdown --id 1-DhkgqWAd8jmrdFb4-AKjfGPQq2uGqwt","metadata":{"execution":{"iopub.status.busy":"2022-01-04T11:26:27.049965Z","iopub.status.idle":"2022-01-04T11:26:27.050561Z","shell.execute_reply.started":"2022-01-04T11:26:27.050249Z","shell.execute_reply":"2022-01-04T11:26:27.050280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gdown --id 1-C4Ri5TeH34pQrfOmEGDSPLSHzEYmawT","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:22:30.181559Z","iopub.execute_input":"2021-12-29T05:22:30.182497Z","iopub.status.idle":"2021-12-29T05:22:34.858568Z","shell.execute_reply.started":"2021-12-29T05:22:30.182452Z","shell.execute_reply":"2021-12-29T05:22:34.857674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_all('../input/best-transfer-100k/model_best.pth',  [0.15, 0.35, 0.67])","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:22:34.860002Z","iopub.execute_input":"2021-12-29T05:22:34.860564Z","iopub.status.idle":"2021-12-29T05:23:46.99729Z","shell.execute_reply.started":"2021-12-29T05:22:34.860522Z","shell.execute_reply":"2021-12-29T05:23:46.996602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_all('./model_best.pth',  [0.15, 0.35, 0.67])","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:26:19.522822Z","iopub.execute_input":"2021-12-29T05:26:19.52326Z","iopub.status.idle":"2021-12-29T05:26:44.574089Z","shell.execute_reply.started":"2021-12-29T05:26:19.523227Z","shell.execute_reply":"2021-12-29T05:26:44.573248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp  ../input/best-transfer-100k/model_best.pth ./model_x101.pth","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:33:50.898957Z","iopub.execute_input":"2021-12-29T05:33:50.899649Z","iopub.status.idle":"2021-12-29T05:33:52.316685Z","shell.execute_reply.started":"2021-12-29T05:33:50.899605Z","shell.execute_reply":"2021-12-29T05:33:52.315779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv ./model_best.pth ./model_cascade_pseudo.pth\n!mv ./model_x101.pth ./model_x101_pseudo.pth","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:33:52.394251Z","iopub.execute_input":"2021-12-29T05:33:52.394487Z","iopub.status.idle":"2021-12-29T05:33:53.820327Z","shell.execute_reply.started":"2021-12-29T05:33:52.39446Z","shell.execute_reply":"2021-12-29T05:33:53.818941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config1 = \"Misc/cascade_mask_rcnn_R_50_FPN_3x.yaml\"\nconfig2 = \"COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml\"","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:49:53.18308Z","iopub.execute_input":"2021-12-29T05:49:53.183751Z","iopub.status.idle":"2021-12-29T05:49:53.188456Z","shell.execute_reply.started":"2021-12-29T05:49:53.183715Z","shell.execute_reply":"2021-12-29T05:49:53.187583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = []","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=5/100; j=5/100 #; k=12/100\n\nfor num in range(16,25):\n    k=num/100\n    sc = score_all('./model_cascade_pseudo.pth',  [0.10+i, 0.30+j, 0.50+k])\n    scores.append([sc, [0.10+i, 0.30+j, 0.50+k]] )","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:42:02.933108Z","iopub.execute_input":"2021-12-29T05:42:02.933832Z","iopub.status.idle":"2021-12-29T05:45:48.952552Z","shell.execute_reply.started":"2021-12-29T05:42:02.933782Z","shell.execute_reply":"2021-12-29T05:45:48.951593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores.sort()\nscores","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:45:48.954309Z","iopub.execute_input":"2021-12-29T05:45:48.954551Z","iopub.status.idle":"2021-12-29T05:45:48.972927Z","shell.execute_reply.started":"2021-12-29T05:45:48.954516Z","shell.execute_reply":"2021-12-29T05:45:48.972174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#0.73","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores1= [] ","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:45:48.974235Z","iopub.execute_input":"2021-12-29T05:45:48.974899Z","iopub.status.idle":"2021-12-29T05:45:48.979292Z","shell.execute_reply.started":"2021-12-29T05:45:48.974862Z","shell.execute_reply":"2021-12-29T05:45:48.97768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=5/100; j=5/100 #; k=12/100\n\nfor num in range(10,25):\n    k=num/100\n    sc = score_all('./model_cascade.pth',  [0.10+i, 0.30+j, 0.50+k], config1)\n    scores1.append([sc, [0.10+i, 0.30+j, 0.50+k]] )","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:53:15.449889Z","iopub.execute_input":"2021-12-29T05:53:15.450542Z","iopub.status.idle":"2021-12-29T05:53:45.97051Z","shell.execute_reply.started":"2021-12-29T05:53:15.450502Z","shell.execute_reply":"2021-12-29T05:53:45.969191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores1.sort()\nscores1","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:53:49.818148Z","iopub.execute_input":"2021-12-29T05:53:49.818934Z","iopub.status.idle":"2021-12-29T05:53:49.825234Z","shell.execute_reply.started":"2021-12-29T05:53:49.818886Z","shell.execute_reply":"2021-12-29T05:53:49.824556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#0.62","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores2= [] ","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:54:19.311983Z","iopub.execute_input":"2021-12-29T05:54:19.312531Z","iopub.status.idle":"2021-12-29T05:54:19.316316Z","shell.execute_reply.started":"2021-12-29T05:54:19.312493Z","shell.execute_reply":"2021-12-29T05:54:19.315376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores2.sort()\nscores2[-5:]","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:58:38.270577Z","iopub.execute_input":"2021-12-29T05:58:38.270836Z","iopub.status.idle":"2021-12-29T05:58:38.276743Z","shell.execute_reply.started":"2021-12-29T05:58:38.270807Z","shell.execute_reply":"2021-12-29T05:58:38.275988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores3=[]","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:59:27.69071Z","iopub.execute_input":"2021-12-29T05:59:27.691404Z","iopub.status.idle":"2021-12-29T05:59:27.696165Z","shell.execute_reply.started":"2021-12-29T05:59:27.691348Z","shell.execute_reply":"2021-12-29T05:59:27.69527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=5/100; j=5/100 #; k=12/100\n\nfor num in range(10,25):\n    k=num/100\n    sc = score_all('./model_x101.pth',  [0.10+i, 0.30+j, 0.50+k], config2)\n    scores2.append([sc, [0.10+i, 0.30+j, 0.50+k]] )","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:59:29.005777Z","iopub.execute_input":"2021-12-29T05:59:29.006059Z","iopub.status.idle":"2021-12-29T05:59:33.049472Z","shell.execute_reply.started":"2021-12-29T05:59:29.00601Z","shell.execute_reply":"2021-12-29T05:59:33.046385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=5/100; j=5/100 #; k=12/100\n\nfor num in range(20,30):\n    k=num/100\n    sc = score_all('./model_x101_pseudo.pth',  [0.10+i, 0.30+j, 0.50+k], config2)\n    scores2.append([sc, [0.10+i, 0.30+j, 0.50+k]] )","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:56:38.592236Z","iopub.status.idle":"2021-12-29T05:56:38.592797Z","shell.execute_reply.started":"2021-12-29T05:56:38.592561Z","shell.execute_reply":"2021-12-29T05:56:38.592587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores3.sort()\nscores3[-5:]","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:56:38.593877Z","iopub.status.idle":"2021-12-29T05:56:38.594483Z","shell.execute_reply.started":"2021-12-29T05:56:38.594225Z","shell.execute_reply":"2021-12-29T05:56:38.594251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#0.67 or ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gdown --id 1-6kgnkZTilzbLO6BqoqhVZo5kXBzcpVe #mrcnn x101 f4 #lb = 0.307 std thss\n!mv model_best.pth model_x101.pth\n!gdown --id 1CVWavXTMyjdT8IoiAW4AuDoV1qiO8VaJ #cascade run0 f4 #lb = 0.31 std thss\n!mv model_best.pth model_cascade.pth\n'''!gdown --id 1-6_Etpic1uk49GVVF66w1jmxhShj1ALc #cascade run1 f4\n!mv model_best.pth model_best_cascade2.pth'''","metadata":{"execution":{"iopub.status.busy":"2021-12-29T05:34:17.709762Z","iopub.execute_input":"2021-12-29T05:34:17.710059Z","iopub.status.idle":"2021-12-29T05:34:30.936471Z","shell.execute_reply.started":"2021-12-29T05:34:17.71001Z","shell.execute_reply":"2021-12-29T05:34:30.935672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}