{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Sources\n- https://www.kaggle.com/code/ammarnassanalhajali/sartorius-segmentation-detectron2-training\n- https://www.kaggle.com/code/slawekbiel/positive-score-with-detectron-2-3-training\n- https://www.kaggle.com/code/markunys/sartorius-transfer-learning-train-with-livecell\n- https://eidos-ai.medium.com/training-on-detectron2-with-a-validation-set-and-plot-loss-on-it-to-avoid-overfitting-6449418fbf4e","metadata":{}},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport math\nimport gc\nimport copy\nimport os\nimport cv2\nimport collections\nfrom pympler.asizeof import asizeof\nfrom PIL import Image\nfrom IPython.display import display\nfrom sklearn.model_selection import train_test_split\nimport torch\nimport torchvision.models as models\nimport torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:54:21.339630Z","iopub.execute_input":"2023-09-09T10:54:21.340018Z","iopub.status.idle":"2023-09-09T10:54:33.303559Z","shell.execute_reply.started":"2023-09-09T10:54:21.339985Z","shell.execute_reply":"2023-09-09T10:54:33.302595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Setup","metadata":{}},{"cell_type":"code","source":"live_cell_anno_dir = \"/kaggle/input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/annotations\"\nlive_cell_img_dir = \"/kaggle/input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images\"\ntrian_dir = \"/kaggle/input/sartorius-cell-instance-segmentation/train\"\ntest_dir = \"/kaggle/input/sartorius-cell-instance-segmentation/test\"\ntrain_csv_dir = \"/kaggle/input/sartorius-cell-instance-segmentation/train.csv\"\nsubmission_csv_dir = \"/kaggle/input/sartorius-cell-instance-segmentation/sample_submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:54:33.305585Z","iopub.execute_input":"2023-09-09T10:54:33.306283Z","iopub.status.idle":"2023-09-09T10:54:33.312100Z","shell.execute_reply.started":"2023-09-09T10:54:33.306247Z","shell.execute_reply":"2023-09-09T10:54:33.311084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(train_csv_dir)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:54:33.313347Z","iopub.execute_input":"2023-09-09T10:54:33.314075Z","iopub.status.idle":"2023-09-09T10:54:33.978265Z","shell.execute_reply.started":"2023-09-09T10:54:33.314042Z","shell.execute_reply":"2023-09-09T10:54:33.977299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = list(train_data.id.unique())\nclass_labels = list(train_data.cell_type.unique())\nclass_to_number = {label:idx for idx, label in enumerate(class_labels)}\nprint(\"Class Dictionary:\", class_to_number)\nprint(f\"Number of Images: {len(ids)}\")","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:54:33.981022Z","iopub.execute_input":"2023-09-09T10:54:33.981387Z","iopub.status.idle":"2023-09-09T10:54:34.008326Z","shell.execute_reply.started":"2023-09-09T10:54:33.981352Z","shell.execute_reply":"2023-09-09T10:54:34.007456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train_data)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:54:34.009589Z","iopub.execute_input":"2023-09-09T10:54:34.009929Z","iopub.status.idle":"2023-09-09T10:54:34.036447Z","shell.execute_reply.started":"2023-09-09T10:54:34.009895Z","shell.execute_reply":"2023-09-09T10:54:34.035587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split into Train - Validation - Test","metadata":{}},{"cell_type":"code","source":"test_ratio = 0.009\nvalidation_ratio = 0.035\n\n\nimage_groups = train_data.groupby('id')\n\n# Split images into training and testing sets\ntrain_image_ids, test_image_ids = train_test_split(train_data['id'].unique(), test_size=test_ratio,\n                                                   stratify=train_data.groupby('id')['cell_type'].first()\n                                                   ,random_state=42)\n\n# Collect instance information for training and testing sets\ntrain_instances = image_groups.filter(lambda x: x['id'].iloc[0] in train_image_ids)\ntest_instances = image_groups.filter(lambda x: x['id'].iloc[0] in test_image_ids)\n\n\n# Split images into training and testing sets\ntrain_image_ids, val_image_ids = train_test_split(train_instances['id'].unique(), test_size=validation_ratio,\n                                                  stratify=train_instances.groupby('id')['cell_type'].first(),\n                                                  random_state=42)\n\n# Collect instance information for training and testing sets\ntrain_instances = image_groups.filter(lambda x: x['id'].iloc[0] in train_image_ids)\nval_instances = image_groups.filter(lambda x: x['id'].iloc[0] in val_image_ids)\n\n# Now you have the training and testing instance information ready to use for modeling\n# print(\"Train Instances:\\n\", train_instances)\n# print(\"Test Instances:\\n\", test_instances)\nprint(train_instances['id'].unique().shape)\nprint(test_instances['id'].unique().shape)\nprint(val_instances['id'].unique().shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:54:34.038322Z","iopub.execute_input":"2023-09-09T10:54:34.038980Z","iopub.status.idle":"2023-09-09T10:54:34.393080Z","shell.execute_reply.started":"2023-09-09T10:54:34.038947Z","shell.execute_reply":"2023-09-09T10:54:34.391194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"!python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:54:34.394647Z","iopub.execute_input":"2023-09-09T10:54:34.394993Z","iopub.status.idle":"2023-09-09T10:57:44.866954Z","shell.execute_reply.started":"2023-09-09T10:54:34.394960Z","shell.execute_reply":"2023-09-09T10:57:44.865764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch, detectron2\n!nvcc --version\nTORCH_VERSION = \".\".join(torch.__version__.split(\".\")[:2])\nCUDA_VERSION = torch.__version__.split(\"+\")[-1]\nprint(\"torch: \", TORCH_VERSION, \"; cuda: \", CUDA_VERSION)\nprint(\"detectron2:\", detectron2.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:57:44.869848Z","iopub.execute_input":"2023-09-09T10:57:44.870224Z","iopub.status.idle":"2023-09-09T10:57:45.831648Z","shell.execute_reply.started":"2023-09-09T10:57:44.870182Z","shell.execute_reply":"2023-09-09T10:57:45.830298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle2mask(rle, img_w, img_h):\n    \n    ## transforming the string into an array of shape (2, N)\n    array = np.fromiter(rle.split(), dtype = np.uint)\n    array = array.reshape((-1,2)).T\n    array[0] = array[0] - 1\n    \n    ## decompressing the rle encoding (ie, turning [3, 1, 10, 2] into [3, 4, 10, 11, 12])\n    # for faster mask construction\n    starts, lenghts = array\n    mask_decompressed = np.concatenate([np.arange(s, s + l, dtype = np.uint) for s, l in zip(starts, lenghts)])\n\n    ## Building the binary mask\n    msk_img = np.zeros(img_w * img_h, dtype = np.uint8)\n    msk_img[mask_decompressed] = 1\n    msk_img = msk_img.reshape((img_h, img_w))\n    msk_img = np.asfortranarray(msk_img) ## This is important so pycocotools can handle this object\n    \n    return msk_img","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:57:45.834472Z","iopub.execute_input":"2023-09-09T10:57:45.835456Z","iopub.status.idle":"2023-09-09T10:57:45.843810Z","shell.execute_reply.started":"2023-09-09T10:57:45.835391Z","shell.execute_reply":"2023-09-09T10:57:45.842801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\ndef polygonFromMask(maskedArr):\n\n    contours, _ = cv2.findContours(maskedArr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n\n    segmentation = []\n    for contour in contours:\n        # Valid polygons have >= 6 coordinates (3 points)\n        if contour.size >= 6:\n            segmentation.append(contour.flatten().tolist())\n    RLEs = maskUtils.frPyObjects(segmentation, maskedArr.shape[0], maskedArr.shape[1])\n    RLE = maskUtils.merge(RLEs)\n    # RLE = mask.encode(np.asfortranarray(maskedArr))\n    area = maskUtils.area(RLE)\n    [x, y, w, h] = cv2.boundingRect(maskedArr)\n\n    return segmentation[0] #, [x, y, w, h], area","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:57:45.848505Z","iopub.execute_input":"2023-09-09T10:57:45.848780Z","iopub.status.idle":"2023-09-09T10:57:45.861740Z","shell.execute_reply.started":"2023-09-09T10:57:45.848755Z","shell.execute_reply":"2023-09-09T10:57:45.860790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.structures import polygons_to_bitmask\ndef polygon_to_rle(polygon, shape=(520, 704)):\n    #print(polygon)\n    mask = polygons_to_bitmask([np.asarray(polygon) + 0.25], shape[0], shape[1])\n\n    rle = mask_util.encode(np.asfortranarray(mask))\n    return rle","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:57:45.865032Z","iopub.execute_input":"2023-09-09T10:57:45.865301Z","iopub.status.idle":"2023-09-09T10:57:45.966583Z","shell.execute_reply.started":"2023-09-09T10:57:45.865277Z","shell.execute_reply":"2023-09-09T10:57:45.965644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pycocotools import mask as maskUtils\nfrom joblib import Parallel, delayed\ndef annotate(idx, row, cat_ids):\n    mask = rle2mask(row['annotation'], row['width'], row['height']) # Binary mask\n    c_rle = maskUtils.encode(mask) # Encoding it back to rle (coco format)\n    polygons = polygonFromMask(mask)\n    c_rle['counts'] = c_rle['counts'].decode('utf-8') # converting from binary to utf-8\n    area = maskUtils.area(c_rle).item() # calculating the area\n    bbox = maskUtils.toBbox(c_rle).astype(int).tolist() # calculating the bboxes\n    annotation = {\n        'segmentation': [polygons],\n        'bbox': bbox,\n        'area': area,\n        'image_id': row['id'],\n        'category_id': cat_ids[row['cell_type']],\n        'iscrowd': 0,\n        'id': idx\n    }\n    return annotation\n\ndef coco_structure(df, workers=4):\n\n    cats = [{'name': 'shsy5y', 'id': 1},\n            {'name': 'astro', 'id': 2},\n            {'name': 'cort', 'id': 3}]\n    cat_ids = {\"shsy5y\": 1, \"astro\": 2, \"cort\": 3}\n\n    images = [{'id': id, 'width': row.width, 'height': row.height, 'file_name': f'train/{id}.png'} for id, row in df.groupby('id').agg('first').iterrows()]\n\n    # Building the annotations\n    annotations = Parallel(n_jobs=workers)(delayed(annotate)(idx, row, cat_ids) for idx, row in df.iterrows())\n\n    return {'categories': cats, 'images': images, 'annotations': annotations}\n","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:57:45.968121Z","iopub.execute_input":"2023-09-09T10:57:45.968501Z","iopub.status.idle":"2023-09-09T10:57:45.980467Z","shell.execute_reply.started":"2023-09-09T10:57:45.968465Z","shell.execute_reply":"2023-09-09T10:57:45.979480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_train = coco_structure(train_instances)\nroot_test = coco_structure(test_instances)\nroot_val = coco_structure(val_instances)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T10:57:45.981869Z","iopub.execute_input":"2023-09-09T10:57:45.982454Z","iopub.status.idle":"2023-09-09T11:01:33.377729Z","shell.execute_reply.started":"2023-09-09T10:57:45.982404Z","shell.execute_reply":"2023-09-09T11:01:33.376650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_train['categories']","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:33.379296Z","iopub.execute_input":"2023-09-09T11:01:33.379674Z","iopub.status.idle":"2023-09-09T11:01:33.389541Z","shell.execute_reply.started":"2023-09-09T11:01:33.379637Z","shell.execute_reply":"2023-09-09T11:01:33.388587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_val['categories']","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:33.390879Z","iopub.execute_input":"2023-09-09T11:01:33.391640Z","iopub.status.idle":"2023-09-09T11:01:33.401327Z","shell.execute_reply.started":"2023-09-09T11:01:33.391606Z","shell.execute_reply":"2023-09-09T11:01:33.400380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_test['categories']","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:33.402553Z","iopub.execute_input":"2023-09-09T11:01:33.402954Z","iopub.status.idle":"2023-09-09T11:01:33.417962Z","shell.execute_reply.started":"2023-09-09T11:01:33.402921Z","shell.execute_reply":"2023-09-09T11:01:33.416865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nwith open('annotations_train.json', 'w', encoding='utf-8') as f:\n    json.dump(root_train, f, ensure_ascii=True, indent=4)\nwith open('annotations_val.json', 'w', encoding='utf-8') as f:\n    json.dump(root_val, f, ensure_ascii=True, indent=4)\nwith open('annotations_test.json', 'w', encoding='utf-8') as f:\n    json.dump(root_test, f, ensure_ascii=True, indent=4)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:33.419552Z","iopub.execute_input":"2023-09-09T11:01:33.419966Z","iopub.status.idle":"2023-09-09T11:01:41.779899Z","shell.execute_reply.started":"2023-09-09T11:01:33.419934Z","shell.execute_reply":"2023-09-09T11:01:41.778871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import detectron2\nfrom pathlib import Path\nimport random, cv2, os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pycocotools.mask as mask_util\n# import some common detectron2 utilities\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.data.datasets import register_coco_instances\nfrom detectron2.utils.logger import setup_logger\nfrom detectron2.evaluation.evaluator import DatasetEvaluator\nsetup_logger()\n\ndataDir=Path('../input/sartorius-cell-instance-segmentation/')\ndataDir2=Path('../input/livecell-dataset/LIVECell_dataset_2021/images')\nDatasetCatalog.clear()\nMetadataCatalog.clear()\nregister_coco_instances('mytrain',{}, 'annotations_train.json', dataDir)\nregister_coco_instances('myval',{},'annotations_val.json', dataDir)\nregister_coco_instances('mytest',{},'annotations_test.json', dataDir)\nregister_coco_instances('sartorius_train',{}, '../input/livecell-dataset/livecell_annotations_train.json', dataDir2)\nregister_coco_instances('sartorius_val',{},'../input/livecell-dataset/livecell_annotations_val.json', dataDir2)\nregister_coco_instances('sartorius_test',{}, '../input/livecell-dataset/livecell_annotations_test.json', dataDir2)\nmetadata = MetadataCatalog.get('mytrain')\ntrain_ds = DatasetCatalog.get('mytrain')\nval_metadata = MetadataCatalog.get('myval')\nval_ds = DatasetCatalog.get('myval')","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:41.781344Z","iopub.execute_input":"2023-09-09T11:01:41.781921Z","iopub.status.idle":"2023-09-09T11:01:45.171148Z","shell.execute_reply.started":"2023-09-09T11:01:41.781886Z","shell.execute_reply":"2023-09-09T11:01:45.170091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(metadata)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:45.172529Z","iopub.execute_input":"2023-09-09T11:01:45.172879Z","iopub.status.idle":"2023-09-09T11:01:45.180557Z","shell.execute_reply.started":"2023-09-09T11:01:45.172843Z","shell.execute_reply":"2023-09-09T11:01:45.178235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(val_metadata)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:45.181916Z","iopub.execute_input":"2023-09-09T11:01:45.182257Z","iopub.status.idle":"2023-09-09T11:01:45.191197Z","shell.execute_reply.started":"2023-09-09T11:01:45.182223Z","shell.execute_reply":"2023-09-09T11:01:45.190046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = train_ds[5]\nimg = cv2.imread(d[\"file_name\"])\nvisualizer = Visualizer(img[:, :, ::-1], metadata=metadata)\nout = visualizer.draw_dataset_dict(d)\nplt.figure(figsize = (20,15))\nplt.imshow(out.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:45.192724Z","iopub.execute_input":"2023-09-09T11:01:45.193057Z","iopub.status.idle":"2023-09-09T11:01:46.710521Z","shell.execute_reply.started":"2023-09-09T11:01:45.193025Z","shell.execute_reply":"2023-09-09T11:01:46.709306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.engine.hooks import HookBase\nfrom detectron2.evaluation import inference_context\nfrom detectron2.utils.logger import log_every_n_seconds\nfrom detectron2.data import DatasetMapper, build_detection_test_loader\nimport detectron2.utils.comm as comm\nimport torch\nimport time\nimport logging\nimport datetime\n\nclass LossEvalHook(HookBase):\n    def __init__(self, eval_period, model, data_loader):\n        self._model = model\n        self._period = eval_period\n        self._data_loader = data_loader\n    \n    def _do_loss_eval(self):\n        total = len(self._data_loader)\n        num_warmup = min(5, total - 1)\n            \n        start_time = time.perf_counter()\n        total_compute_time = 0\n        losses = []\n        for idx, inputs in enumerate(self._data_loader):            \n            if idx == num_warmup:\n                start_time = time.perf_counter()\n                total_compute_time = 0\n            start_compute_time = time.perf_counter()\n            if torch.cuda.is_available():\n                torch.cuda.synchronize()\n            total_compute_time += time.perf_counter() - start_compute_time\n            iters_after_start = idx + 1 - num_warmup * int(idx >= num_warmup)\n            seconds_per_img = total_compute_time / iters_after_start\n            if idx >= num_warmup * 2 or seconds_per_img > 5:\n                total_seconds_per_img = (time.perf_counter() - start_time) / iters_after_start\n                eta = datetime.timedelta(seconds=int(total_seconds_per_img * (total - idx - 1)))\n                log_every_n_seconds(\n                    logging.INFO,\n                    \"Loss on Validation  done {}/{}. {:.4f} s / img. ETA={}\".format(\n                        idx + 1, total, seconds_per_img, str(eta)\n                    ),\n                    n=5,\n                )\n            loss_batch = self._get_loss(inputs)\n            losses.append(loss_batch)\n        mean_loss = np.mean(losses)\n        self.trainer.storage.put_scalar('validation_loss', mean_loss)\n        comm.synchronize()\n\n        return losses\n            \n    def _get_loss(self, data):\n        # How loss is calculated on train_loop \n        metrics_dict = self._model(data)\n        metrics_dict = {\n            k: v.detach().cpu().item() if isinstance(v, torch.Tensor) else float(v)\n            for k, v in metrics_dict.items()\n        }\n        total_losses_reduced = sum(loss for loss in metrics_dict.values())\n        return total_losses_reduced\n        \n        \n    def after_step(self):\n        next_iter = self.trainer.iter + 1\n        is_final = next_iter == self.trainer.max_iter\n        if is_final or (self._period > 0 and next_iter % self._period == 0):\n            self._do_loss_eval()\n        self.trainer.storage.put_scalars(timetest=12)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:46.711919Z","iopub.execute_input":"2023-09-09T11:01:46.712330Z","iopub.status.idle":"2023-09-09T11:01:46.734431Z","shell.execute_reply.started":"2023-09-09T11:01:46.712297Z","shell.execute_reply":"2023-09-09T11:01:46.733229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.evaluation.evaluator import DatasetEvaluator\nfrom detectron2.data import build_detection_train_loader\nfrom detectron2.data import build_detection_test_loader\nfrom detectron2.data import detection_utils as utils\nimport pycocotools.mask as mask_util\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))\n    enc_targs = [polygon_to_rle(enc_targ[0]) for enc_targ in enc_targs]\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        self.images_id = []\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            self.images_id.append(inp['image_id'])\n\n    def evaluate(self):\n        return {\"MaP IoU\": np.mean(self.scores)}\n\nclass Trainer(DefaultTrainer):\n    @classmethod\n    def build_train_loader(cls, cfg):\n        return build_detection_train_loader(cfg, mapper=DatasetMapper(cfg, is_train=True, augmentations=[]))\n    @classmethod\n    def build_test_loader(cls, cfg, dataset_name):\n        return build_detection_test_loader(cfg, dataset_name,mapper=DatasetMapper(cfg, is_train=False, augmentations=[]))\n    @classmethod\n    def build_evaluator(cls, cfg, dataset_name, output_folder=None):\n#         print(f\"************{dataset_name}**********************\")\n        return MAPIOUEvaluator(dataset_name)\n    \n    def build_hooks(self):\n        hooks = super().build_hooks()\n        hooks.insert(-1,LossEvalHook(\n            20,\n            self.model,\n            build_detection_test_loader(\n                self.cfg,\n                self.cfg.DATASETS.TEST[0],\n                DatasetMapper(self.cfg,True, augmentations=[])\n            )\n        ))\n        return hooks","metadata":{"execution":{"iopub.status.busy":"2023-09-09T13:21:19.924771Z","iopub.execute_input":"2023-09-09T13:21:19.925168Z","iopub.status.idle":"2023-09-09T13:21:19.947402Z","shell.execute_reply.started":"2023-09-09T13:21:19.925132Z","shell.execute_reply":"2023-09-09T13:21:19.946062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.data import build_detection_train_loader\nimport detectron2.data.transforms as T\ndef custom_mapper(dataset_dict):\n    dataset_dict = copy.deepcopy(dataset_dict)\n    image = utils.read_image(dataset_dict[\"file_name\"], format=\"BGR\")\n    transform_list = [\n            T.RandomBrightness(0.9, 1.1),\n            T.RandomContrast(0.9, 1.1),\n            T.RandomSaturation(0.9, 1.1),\n            T.RandomLighting(0.9),\n            T.RandomFlip(prob=0.5, horizontal=False, vertical=True),\n            T.RandomFlip(prob=0.5, horizontal=True, vertical=False),\n    ]\n    image, transforms = T.apply_transform_gens(transform_list, image)\n    dataset_dict[\"image\"] = torch.as_tensor(image.transpose(2, 0, 1).astype(\"float32\"))\n\n    annos = [\n        utils.transform_instance_annotations(obj, transforms, image.shape[:2])\n        for obj in dataset_dict.pop(\"annotations\")\n        if obj.get(\"iscrowd\", 0) == 0\n    ]\n    instances = utils.annotations_to_instances(annos, image.shape[:2])\n    dataset_dict[\"instances\"] = utils.filter_empty_instances(instances)\n    return dataset_dict\nclass AugTrainer(DefaultTrainer):\n    @classmethod\n    def build_train_loader(cls, cfg):\n        return build_detection_train_loader(cfg, mapper=custom_mapper)\n    @classmethod\n    def build_test_loader(cls, cfg, dataset_name):\n        return build_detection_test_loader(cfg, dataset_name,mapper=DatasetMapper(cfg, is_train=False, augmentations=[]))\n    @classmethod\n    def build_evaluator(cls, cfg, dataset_name, output_folder=None):\n#         print(f\"************{dataset_name}**********************\")\n        return MAPIOUEvaluator(dataset_name)\n    def build_hooks(self):\n        hooks = super().build_hooks()\n        hooks.insert(-1,LossEvalHook(\n            20,\n            self.model,\n            build_detection_test_loader(\n                self.cfg,\n                self.cfg.DATASETS.TEST[0],\n                DatasetMapper(self.cfg,True)\n            )\n        ))\n        return hooks","metadata":{"execution":{"iopub.status.busy":"2023-09-09T14:16:23.010250Z","iopub.execute_input":"2023-09-09T14:16:23.010619Z","iopub.status.idle":"2023-09-09T14:16:23.026838Z","shell.execute_reply.started":"2023-09-09T14:16:23.010586Z","shell.execute_reply":"2023-09-09T14:16:23.025435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def setup_cfg(s : str, num_itr : int = 3000, lr = 0.001, num_classes = 3, \n              sets = None, chk = None, batch_size = 1, batch_size_per_image = 512):\n    cfg = get_cfg()\n    cfg.OUTPUT_DIR = s\n    cfg.merge_from_file(model_zoo.get_config_file(f\"COCO-InstanceSegmentation/{s}\"))\n    if sets is None:\n        cfg.DATASETS.TRAIN = (\"mytrain\",)\n        cfg.DATASETS.TEST = (\"myval\",)\n    else:\n        cfg.DATASETS.TRAIN = (sets[0],sets[1], sets[2]) # train with all the data\n        cfg.DATASETS.TEST = ()\n        cfg.INPUT.MIN_SIZE_TRAIN = (440, 480, 520, 580, 620)\n    cfg.TEST.EVAL_PERIOD = 150 # half epoch\n    cfg.INPUT.MASK_FORMAT='bitmask'\n    cfg.MODEL.DEVICE = \"cuda\"\n    cfg.MODEL.BACKBONE.FREEZE_AT = 5\n    cfg.DATALOADER.NUM_WORKERS = 2  # Set according to your system\n    check = chk if chk is not None else model_zoo.get_checkpoint_url(f\"COCO-InstanceSegmentation/{s}\")\n    cfg.MODEL.WEIGHTS = check\n    cfg.SOLVER.IMS_PER_BATCH = batch_size\n    cfg.SOLVER.BASE_LR = lr\n    cfg.SOLVER.CHECKPOINT_PERIOD = 1000\n    cfg.SOLVER.MAX_ITER = num_itr\n    cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = batch_size_per_image\n    cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes\n    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.4\n    cfg.MODEL.ROI_HEADS.NMS_THRESH_TEST = 0.8\n    cfg.TEST.DETECTIONS_PER_IMAGE = 1000\n    cfg.MODEL.RPN.POST_NMS_TOPK_TEST = 1000\n\n    return cfg","metadata":{"execution":{"iopub.status.busy":"2023-09-09T13:22:31.008918Z","iopub.execute_input":"2023-09-09T13:22:31.009341Z","iopub.status.idle":"2023-09-09T13:22:31.023569Z","shell.execute_reply.started":"2023-09-09T13:22:31.009300Z","shell.execute_reply":"2023-09-09T13:22:31.022566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\ndef train(cfg, aug = False):\n    directory_path = cfg.OUTPUT_DIR\n    if os.path.exists(directory_path):\n        # Delete the directory and its contents\n        shutil.rmtree(directory_path)\n    # Create the directory\n    os.makedirs(directory_path)\n    trainer = Trainer(cfg) if not aug else AugTrainer(cfg)\n    trainer.resume_or_load(resume=False)\n    trainer.train()\n    return trainer","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:46.793509Z","iopub.execute_input":"2023-09-09T11:01:46.794129Z","iopub.status.idle":"2023-09-09T11:01:46.808372Z","shell.execute_reply.started":"2023-09-09T11:01:46.794096Z","shell.execute_reply":"2023-09-09T11:01:46.807702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_train_result(s : str):\n    metrics_df = pd.read_json(f\"./{s}/metrics.json\", orient=\"records\", lines=True)\n    mdf = metrics_df.sort_values(\"iteration\")\n    plt.figure(figsize=(20, 20))\n\n    plt.subplot(3, 3, 1)  # 2 rows, 2 columns, subplot 1\n    mdf1 = mdf[~mdf[\"total_loss\"].isna()]\n    plt.plot(mdf1[\"iteration\"], mdf1[\"total_loss\"])\n    plt.title('total Loss')\n\n    plt.subplot(3, 3, 2)  # 2 rows, 2 columns, subplot 2\n    if \"validation_loss\" in mdf:\n        mdf1 = mdf[~mdf[\"validation_loss\"].isna()]\n        plt.plot(mdf1[\"iteration\"], mdf1[\"validation_loss\"])\n        plt.title('Val Loss')\n\n    plt.subplot(3, 3, 3)  # 2 rows, 2 columns, subplot 3\n    if \"MaP IoU\" in mdf:\n        mdf1 = mdf[~mdf[\"MaP IoU\"].isna()]\n        plt.plot(mdf1[\"iteration\"], mdf1[\"MaP IoU\"])\n        plt.title('mAP')\n\n    plt.subplot(3, 3, 4)  # 2 rows, 2 columns, subplot 3\n    mdf1 = mdf[~mdf[\"loss_cls\"].isna()]\n    plt.plot(mdf1[\"iteration\"], mdf1[\"loss_cls\"])\n    plt.title('loss_cls')\n\n    plt.subplot(3, 3, 5)  # 2 rows, 2 columns, subplot 3\n    mdf1 = mdf[~mdf[\"loss_rpn_cls\"].isna()]\n    plt.plot(mdf1[\"iteration\"], mdf1[\"loss_rpn_cls\"])\n    plt.title('loss_rpn_cls')\n\n    plt.subplot(3, 3, 6)  # 2 rows, 2 columns, subplot 3\n    mdf1 = mdf[~mdf[\"loss_rpn_loc\"].isna()]\n    plt.plot(mdf1[\"iteration\"], mdf1[\"loss_rpn_loc\"])\n    plt.title('loss_rpn_loc')\n\n    plt.subplot(3, 3, 7)  # 2 rows, 2 columns, subplot 3\n    mdf1 = mdf[~mdf[\"loss_box_reg\"].isna()]\n    plt.plot(mdf1[\"iteration\"], mdf1[\"loss_box_reg\"])\n    plt.title('loss_box_reg')\n\n    plt.subplot(3, 3, 8)  # 2 rows, 2 columns, subplot 3\n    mdf1 = mdf[~mdf[\"loss_mask\"].isna()]\n    plt.plot(mdf1[\"iteration\"], mdf1[\"loss_mask\"])\n    plt.title('loss_mask')\n\n    plt.tight_layout()  # To improve spacing between subplots\n\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:46.809852Z","iopub.execute_input":"2023-09-09T11:01:46.810466Z","iopub.status.idle":"2023-09-09T11:01:46.824442Z","shell.execute_reply.started":"2023-09-09T11:01:46.810432Z","shell.execute_reply":"2023-09-09T11:01:46.823399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.evaluation import inference_on_dataset\ndef evaluate(cfg, t):\n    # Build the data loader for evaluation dataset\n    data_loader = build_detection_test_loader(cfg, \"myval\")\n    evaluator = MAPIOUEvaluator(\"myval\")\n    # Perform inference and evaluation\n    results = inference_on_dataset(t.model, data_loader, evaluator)\n    print(results)\n    return evaluator","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:46.825580Z","iopub.execute_input":"2023-09-09T11:01:46.827824Z","iopub.status.idle":"2023-09-09T11:01:46.839267Z","shell.execute_reply.started":"2023-09-09T11:01:46.827798Z","shell.execute_reply":"2023-09-09T11:01:46.838559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pycocotools.coco import COCO\ncoco = COCO(\"annotations_val.json\")\ndef get_type(s : str):\n    # Replace this with the image ID you're interested in\n    image_id_to_find = s\n    # Get the annotation IDs for the specified image ID\n    annotation_ids = coco.getAnnIds(imgIds=[image_id_to_find], iscrowd=None)\n\n    # Get the first annotation (assuming there's only one) associated with the image\n    annotation_info = coco.loadAnns(annotation_ids)\n    category_id = annotation_info[0]['category_id']\n\n    # Look up the category information using the category ID\n    category_info = coco.loadCats(category_id)[0]\n\n    # Get the category name\n    category_name = category_info['name']\n\n    return category_name, annotation_info","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:46.840611Z","iopub.execute_input":"2023-09-09T11:01:46.841213Z","iopub.status.idle":"2023-09-09T11:01:46.915042Z","shell.execute_reply.started":"2023-09-09T11:01:46.841180Z","shell.execute_reply":"2023-09-09T11:01:46.914080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\ndef visualize_images_by_ids(ids, cfg, predictor):\n    outs = []\n    # Step 1: Filter dataset_dicts by specified IDs\n    id_to_dict = {d[\"image_id\"]: d for d in val_ds}\n    selected_samples = [id_to_dict[id] for id in ids if id in id_to_dict]\n\n    # Step 2: Visualize predictions and targets for selected samples\n    for d in selected_samples:\n        im = cv2.imread(d[\"file_name\"])\n        outputs = predictor(im)\n        v = Visualizer(im[:, :, ::-1], metadata=metadata)\n        out_pred = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\n        visualizer = Visualizer(im[:, :, ::-1], metadata=metadata)\n        out_target = visualizer.draw_dataset_dict(d)\n        outs.append(out_pred)\n        outs.append(out_target)\n\n    _, axs = plt.subplots(len(outs) // 2, 2, figsize=(40, 45))\n    for ax, out in zip(axs.reshape(-1), outs):\n        ax.imshow(out.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:46.916624Z","iopub.execute_input":"2023-09-09T11:01:46.916967Z","iopub.status.idle":"2023-09-09T11:01:46.928890Z","shell.execute_reply.started":"2023-09-09T11:01:46.916934Z","shell.execute_reply":"2023-09-09T11:01:46.927877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### worst images","metadata":{}},{"cell_type":"code","source":"def plot_worst(evaluator, cfg):\n    image_mAP_pairs = list(zip(evaluator.images_id, evaluator.scores))\n\n    # Sort the list by mAP scores in ascending order\n    sorted_images = sorted(image_mAP_pairs, key=lambda x: x[1])\n    # Print the image IDs and corresponding mAP scores for the lowest mAP images\n    num_lowest_images = 3  # You can adjust this number as needed\n    selected_ids = []\n    for img_id, mAP_score in sorted_images[:num_lowest_images]:\n        cell_type, annot = get_type(img_id)\n        print(f\"Image ID: {img_id}, mAP: {mAP_score}, type : {cell_type}\")\n        selected_ids.append(img_id)\n\n    predictor = DefaultPredictor(cfg)\n    dataset_dicts = val_ds\n    visualize_images_by_ids(selected_ids, cfg, predictor)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:46.930469Z","iopub.execute_input":"2023-09-09T11:01:46.931348Z","iopub.status.idle":"2023-09-09T11:01:46.939190Z","shell.execute_reply.started":"2023-09-09T11:01:46.931313Z","shell.execute_reply":"2023-09-09T11:01:46.938094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### best images","metadata":{}},{"cell_type":"code","source":"def plot_best(evaluator, cfg):\n    image_mAP_pairs = list(zip(evaluator.images_id, evaluator.scores))\n\n    # Sort the list by mAP scores in ascending order\n    sorted_images = sorted(image_mAP_pairs, key=lambda x: x[1])\n    num_lowest_images = 3  \n    selected_ids = []\n    for img_id, mAP_score in sorted_images[-num_lowest_images:][::-1]:\n        cell_type, annot = get_type(img_id)\n        print(f\"Image ID: {img_id}, mAP: {mAP_score}, type : {cell_type}\")\n        selected_ids.append(img_id)\n\n    predictor = DefaultPredictor(cfg)\n    dataset_dicts = val_ds\n    visualize_images_by_ids(selected_ids, cfg, predictor)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:01:46.940451Z","iopub.execute_input":"2023-09-09T11:01:46.941036Z","iopub.status.idle":"2023-09-09T11:01:46.955344Z","shell.execute_reply.started":"2023-09-09T11:01:46.940994Z","shell.execute_reply":"2023-09-09T11:01:46.954244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Leanring with LiveCell","metadata":{}},{"cell_type":"code","source":"cfg_RX101 = setup_cfg(\"mask_rcnn_X_101_32x8d_FPN_3x.yaml\",num_itr = 5387*3, num_classes = 8, \n                      sets = [\"sartorius_train\", \"sartorius_test\", \"sartorius_val\"], batch_size_per_image = 64) # 3 epochs","metadata":{"execution":{"iopub.status.busy":"2023-09-08T21:03:11.736284Z","iopub.execute_input":"2023-09-08T21:03:11.737066Z","iopub.status.idle":"2023-09-08T21:03:11.767750Z","shell.execute_reply.started":"2023-09-08T21:03:11.737026Z","shell.execute_reply":"2023-09-08T21:03:11.766664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -r transfer","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = train(cfg_RX101)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_train_result(cfg_RX101.OUTPUT_DIR)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T23:55:24.860416Z","iopub.execute_input":"2023-09-08T23:55:24.861473Z","iopub.status.idle":"2023-09-08T23:55:27.407026Z","shell.execute_reply.started":"2023-09-08T23:55:24.861431Z","shell.execute_reply":"2023-09-08T23:55:27.404547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"markdown","source":"### using data augmentation","metadata":{}},{"cell_type":"code","source":"# use the wieghts if the model produced from the step of tranfer learning\ncfg_train = setup_cfg(\"mask_rcnn_X_101_32x8d_FPN_3x.yaml\",num_itr = 3000, num_classes = 3, \n                      batch_size_per_image = 128, batch_size = 2, chk = \"/kaggle/working/transfer/model_final.pth\") # ~= 12 epochs","metadata":{"execution":{"iopub.status.busy":"2023-09-09T11:15:39.656816Z","iopub.execute_input":"2023-09-09T11:15:39.657215Z","iopub.status.idle":"2023-09-09T11:15:39.683041Z","shell.execute_reply.started":"2023-09-09T11:15:39.657182Z","shell.execute_reply":"2023-09-09T11:15:39.682115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = train(cfg_train, aug = True) # use data augmentation","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_train_result(cfg_train.OUTPUT_DIR)\ncfg_train.MODEL.WEIGHTS = f\"{cfg_train.OUTPUT_DIR}/model_final.pth\"","metadata":{"execution":{"iopub.status.busy":"2023-09-09T12:18:52.567841Z","iopub.execute_input":"2023-09-09T12:18:52.568228Z","iopub.status.idle":"2023-09-09T12:18:54.698234Z","shell.execute_reply.started":"2023-09-09T12:18:52.568194Z","shell.execute_reply":"2023-09-09T12:18:54.697368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate on test set\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.data import build_detection_test_loader\nfrom detectron2.data.datasets import register_coco_instances\nfrom detectron2.evaluation import COCOEvaluator, inference_on_dataset\n\npredictor = DefaultPredictor(cfg_train)\n\n#Call the COCO Evaluator function and pass the Validation Dataset\nevaluator = COCOEvaluator(\"mytest\", cfg_train, False, output_dir=\"./output/\")\nval_loader = build_detection_test_loader(cfg_train, \"mytest\")\n\n#Use the created predicted model in the previous step\ninference_on_dataset(predictor.model, val_loader, evaluator)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T12:18:59.438770Z","iopub.execute_input":"2023-09-09T12:18:59.439166Z","iopub.status.idle":"2023-09-09T12:19:03.591273Z","shell.execute_reply.started":"2023-09-09T12:18:59.439132Z","shell.execute_reply":"2023-09-09T12:19:03.590122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"e_X = evaluate(cfg_train, t)","metadata":{"_kg_hide-input":false,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2023-09-09T12:19:21.443712Z","iopub.execute_input":"2023-09-09T12:19:21.446625Z","iopub.status.idle":"2023-09-09T12:19:29.550674Z","shell.execute_reply.started":"2023-09-09T12:19:21.446577Z","shell.execute_reply":"2023-09-09T12:19:29.549495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_worst(e_X, cfg_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T12:19:29.552988Z","iopub.execute_input":"2023-09-09T12:19:29.553359Z","iopub.status.idle":"2023-09-09T12:19:43.973902Z","shell.execute_reply.started":"2023-09-09T12:19:29.553320Z","shell.execute_reply":"2023-09-09T12:19:43.971546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_best(e_X, cfg_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T12:19:46.540037Z","iopub.execute_input":"2023-09-09T12:19:46.540479Z","iopub.status.idle":"2023-09-09T12:19:57.436017Z","shell.execute_reply.started":"2023-09-09T12:19:46.540435Z","shell.execute_reply":"2023-09-09T12:19:57.434509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### with no data augmentation","metadata":{}},{"cell_type":"code","source":"# use the wieghts if the model produced from the step of tranfer learning\ncfg_train = setup_cfg(\"mask_rcnn_X_101_32x8d_FPN_3x.yaml\",num_itr = 3000, num_classes = 3, \n                      batch_size_per_image = 128, batch_size = 2, chk = \"/kaggle/working/transfer/model_final.pth\") # ~= 12 epochs\ncfg_train.OUTPUT_DIR = \"no_aug\"","metadata":{"execution":{"iopub.status.busy":"2023-09-09T13:22:58.791380Z","iopub.execute_input":"2023-09-09T13:22:58.791834Z","iopub.status.idle":"2023-09-09T13:22:58.819087Z","shell.execute_reply.started":"2023-09-09T13:22:58.791800Z","shell.execute_reply":"2023-09-09T13:22:58.818064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = train(cfg_train) # no data augmentation","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_train_result(cfg_train.OUTPUT_DIR)\ncfg_train.MODEL.WEIGHTS = f\"{cfg_train.OUTPUT_DIR}/model_final.pth\"","metadata":{"execution":{"iopub.status.busy":"2023-09-09T14:19:05.431166Z","iopub.execute_input":"2023-09-09T14:19:05.431619Z","iopub.status.idle":"2023-09-09T14:19:07.585265Z","shell.execute_reply.started":"2023-09-09T14:19:05.431579Z","shell.execute_reply":"2023-09-09T14:19:07.584080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate on test set\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.data import build_detection_test_loader\nfrom detectron2.data.datasets import register_coco_instances\nfrom detectron2.evaluation import COCOEvaluator, inference_on_dataset\n\npredictor = DefaultPredictor(cfg_train)\n\n#Call the COCO Evaluator function and pass the Validation Dataset\nevaluator = COCOEvaluator(\"mytest\", cfg_train, False, output_dir=\"./output_no_aug/\")\nval_loader = build_detection_test_loader(cfg_train, \"mytest\")\n\n#Use the created predicted model in the previous step\ninference_on_dataset(predictor.model, val_loader, evaluator)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T14:19:11.841943Z","iopub.execute_input":"2023-09-09T14:19:11.842524Z","iopub.status.idle":"2023-09-09T14:19:16.264315Z","shell.execute_reply.started":"2023-09-09T14:19:11.842479Z","shell.execute_reply":"2023-09-09T14:19:16.263102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"e_X = evaluate(cfg_train, t)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T14:19:39.932169Z","iopub.execute_input":"2023-09-09T14:19:39.932625Z","iopub.status.idle":"2023-09-09T14:19:47.754494Z","shell.execute_reply.started":"2023-09-09T14:19:39.932586Z","shell.execute_reply":"2023-09-09T14:19:47.753154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_worst(e_X, cfg_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T14:19:51.429898Z","iopub.execute_input":"2023-09-09T14:19:51.430670Z","iopub.status.idle":"2023-09-09T14:20:03.897652Z","shell.execute_reply.started":"2023-09-09T14:19:51.430622Z","shell.execute_reply":"2023-09-09T14:20:03.895997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_best(e_X, cfg_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T14:20:07.430489Z","iopub.execute_input":"2023-09-09T14:20:07.430881Z","iopub.status.idle":"2023-09-09T14:20:18.300171Z","shell.execute_reply.started":"2023-09-09T14:20:07.430849Z","shell.execute_reply":"2023-09-09T14:20:18.298562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}