{"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":{"execution":{"iopub.status.busy":"2022-12-28T10:11:12.948223Z","iopub.execute_input":"2022-12-28T10:11:12.948776Z","iopub.status.idle":"2022-12-28T10:14:25.540451Z","shell.execute_reply.started":"2022-12-28T10:11:12.948643Z","shell.execute_reply":"2022-12-28T10:14:25.538999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\nimport os\nimport pandas as pd\nimport numpy as np\nimport pycocotools.mask as mask_util\nimport detectron2\nfrom pathlib import Path\nimport random,cv2,os\nimport matplotlib.pyplot as plt\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor,DefaultTrainer\nfrom detectron2.config import get_cfg\nfrom detectron2.utils.visualizer import Visualizer,ColorMode\nfrom detectron2.data import MetadataCatalog,DatasetCatalog\nfrom detectron2.utils.logger import setup_logger\nfrom detectron2.data.datasets import register_coco_instances\nfrom detectron2.evaluation.evaluator import DatasetEvaluator\nfrom detectron2.engine import BestCheckpointer\nfrom detectron2.checkpoint import DetectionCheckpointer\nsetup_logger()\nimport torch\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-28T10:14:25.543071Z","iopub.execute_input":"2022-12-28T10:14:25.543792Z","iopub.status.idle":"2022-12-28T10:14:27.818968Z","shell.execute_reply.started":"2022-12-28T10:14:25.543748Z","shell.execute_reply":"2022-12-28T10:14:27.817663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Annotation was taken from the notebook :https://www.kaggle.com/code/slawekbiel/positive-score-with-detectron-1-3-input-data/notebook ","metadata":{}},{"cell_type":"code","source":"class CFG:\n    wfold = 4\n    data_folder = \"../input/sartorius-cell-instance-segmentation/\"\n    anno_folder = \"/kaggle/input/annotationcellsegmentation/result/\"\n    model_arch = 'mask_rcnn_R_50_FPN_3x.yaml'\n    nof_iters = 10000\n    seed = 45","metadata":{"execution":{"iopub.status.busy":"2022-12-28T10:14:27.820829Z","iopub.execute_input":"2022-12-28T10:14:27.821494Z","iopub.status.idle":"2022-12-28T10:14:27.830007Z","shell.execute_reply.started":"2022-12-28T10:14:27.821450Z","shell.execute_reply":"2022-12-28T10:14:27.828502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    \nseed_everything(CFG.seed)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T10:14:27.833511Z","iopub.execute_input":"2022-12-28T10:14:27.834332Z","iopub.status.idle":"2022-12-28T10:14:27.845425Z","shell.execute_reply.started":"2022-12-28T10:14:27.834290Z","shell.execute_reply":"2022-12-28T10:14:27.844406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Metrics implementation is taken from here:- https://www.kaggle.com/theoviel/competition-metric-map-iou","metadata":{}},{"cell_type":"code","source":"def 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))\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\nclass MAPIOUEvaluator(DatasetEvaluator):\n    def __init__(self, dataset_name):\n        dataset_dicts = DatasetCatalog.get(dataset_name)\n        self.annotations_cache = {item['image_id']:item['annotations'] for item in dataset_dicts}\n            \n    def reset(self):\n        self.scores = []\n\n    def process(self, inputs, outputs):\n        for inp, out in zip(inputs, outputs):\n            if len(out['instances']) == 0:\n                self.scores.append(0)    \n            else:\n                targ = self.annotations_cache[inp['image_id']]\n                self.scores.append(score(out, targ))\n\n    def evaluate(self):\n        return {\"MaP IoU\": np.mean(self.scores)}\n\nclass Trainer(DefaultTrainer):\n    @classmethod\n    def build_evaluator(cls, cfg, dataset_name, output_folder=None):\n        return MAPIOUEvaluator(dataset_name)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T10:14:27.846934Z","iopub.execute_input":"2022-12-28T10:14:27.847736Z","iopub.status.idle":"2022-12-28T10:14:27.862789Z","shell.execute_reply.started":"2022-12-28T10:14:27.847692Z","shell.execute_reply":"2022-12-28T10:14:27.861737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Trainer(DefaultTrainer):\n    @classmethod\n    def build_evaluator(cls, cfg, dataset_name, output_folder=None):\n        return MAPIOUEvaluator(dataset_name)\n\n    def build_hooks(self):\n\n        # copy of cfg\n        cfg = self.cfg.clone()\n\n        # build the original model hooks\n        hooks = super().build_hooks()\n\n        # add the best checkpointer hook\n        hooks.insert(-1, BestCheckpointer(cfg.TEST.EVAL_PERIOD, \n                                         DetectionCheckpointer(self.model, cfg.OUTPUT_DIR),\n                                         \"MaP IoU\",\n                                         \"max\",\n                                         ))\n        return hooks","metadata":{"execution":{"iopub.status.busy":"2022-12-28T10:15:29.857838Z","iopub.execute_input":"2022-12-28T10:15:29.858269Z","iopub.status.idle":"2022-12-28T10:15:29.866104Z","shell.execute_reply.started":"2022-12-28T10:15:29.858240Z","shell.execute_reply":"2022-12-28T10:15:29.865026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Setup training / validation split for this fold","metadata":{}},{"cell_type":"code","source":"dataDir = Path(CFG.data_folder)\nregister_coco_instances('sartorius_train',{}, CFG.anno_folder + 'sartorius_train/annotations_train_f'+str(CFG.wfold)+'.json', dataDir)\nregister_coco_instances('sartorius_val',{}, CFG.anno_folder + 'sartorius_valid/annotations_valid_f'+str(CFG.wfold)+'.json', dataDir)\nmetadata = MetadataCatalog.get('sartorius_train')\ntrain_ds = DatasetCatalog.get('sartorius_train')","metadata":{"execution":{"iopub.status.busy":"2022-12-28T10:15:41.167529Z","iopub.execute_input":"2022-12-28T10:15:41.168147Z","iopub.status.idle":"2022-12-28T10:15:41.198308Z","shell.execute_reply.started":"2022-12-28T10:15:41.168113Z","shell.execute_reply":"2022-12-28T10:15:41.196734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = get_cfg()\ncfg.INPUT.MASK_FORMAT='bitmask'\ncfg.merge_from_file(model_zoo.get_config_file('COCO-InstanceSegmentation/' + CFG.model_arch))\ncfg.DATASETS.TRAIN = (\"sartorius_train\",)\ncfg.DATASETS.TEST = (\"sartorius_val\",)\ncfg.DATALOADER.NUM_WORKERS = 2\ncfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url('COCO-InstanceSegmentation/' + CFG.model_arch) \ncfg.SOLVER.IMS_PER_BATCH = 2\ncfg.SOLVER.BASE_LR = 0.001\ncfg.SOLVER.MAX_ITER = CFG.nof_iters    \ncfg.SOLVER.STEPS = []        \ncfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 512    \ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 3  \ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = .4\ncfg.TEST.EVAL_PERIOD = len(DatasetCatalog.get('sartorius_train')) // cfg.SOLVER.IMS_PER_BATCH  ","metadata":{"execution":{"iopub.status.busy":"2022-12-28T10:15:59.729375Z","iopub.execute_input":"2022-12-28T10:15:59.729765Z","iopub.status.idle":"2022-12-28T10:16:02.548578Z","shell.execute_reply.started":"2022-12-28T10:15:59.729733Z","shell.execute_reply":"2022-12-28T10:16:02.547616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)\ntrainer = Trainer(cfg) \ntrainer.resume_or_load(resume=False)\ntrainer.train()","metadata":{"execution":{"iopub.status.busy":"2022-12-28T10:16:05.513646Z","iopub.execute_input":"2022-12-28T10:16:05.514109Z","iopub.status.idle":"2022-12-28T10:16:28.299123Z","shell.execute_reply.started":"2022-12-28T10:16:05.514072Z","shell.execute_reply":"2022-12-28T10:16:28.296594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}