{"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":"# LiveCell shsy5y COCO Dataset\nI've created a [COCO dataset](https://www.kaggle.com/monikabozhinova/sartorius-live-cell-shsy5y-coco) from the provided LiveCell shsy5y data that can be easily explored with pycocotools and loaded into detectron2.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"!pip install pycocotools\n!pip install 'git+https://github.com/facebookresearch/detectron2.git'","metadata":{"execution":{"iopub.status.busy":"2021-11-15T18:26:49.849336Z","iopub.execute_input":"2021-11-15T18:26:49.850107Z","iopub.status.idle":"2021-11-15T18:29:08.103638Z","shell.execute_reply.started":"2021-11-15T18:26:49.850009Z","shell.execute_reply":"2021-11-15T18:29:08.102582Z"},"scrolled":true,"_kg_hide-output":true,"_kg_hide-input":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport cv2\nimport json\nfrom pathlib import Path\nfrom pycocotools import _mask\nfrom pycocotools.coco import COCO\nfrom detectron2.data import MetadataCatalog, DatasetCatalog\nfrom detectron2.data.datasets import register_coco_instances\nfrom detectron2.utils.visualizer import Visualizer, ColorMode","metadata":{"execution":{"iopub.status.busy":"2021-11-15T18:42:15.457800Z","iopub.execute_input":"2021-11-15T18:42:15.458091Z","iopub.status.idle":"2021-11-15T18:42:15.463254Z","shell.execute_reply.started":"2021-11-15T18:42:15.458057Z","shell.execute_reply":"2021-11-15T18:42:15.462662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load LiveCell shsy5y COCO train and val datasets","metadata":{}},{"cell_type":"code","source":"# Load LiveCell shsy5y data\nlive_cell_imgs_dir = Path('../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_train_val_images/SHSY5Y')\n\nregister_coco_instances('sartorius_live_cell_train',{}, '../input/sartorius-live-cell-shsy5y-coco/annotations_train.json', live_cell_imgs_dir)\nregister_coco_instances('sartorius_live_cell_val',{},'../input/sartorius-live-cell-shsy5y-coco/annotations_val.json', live_cell_imgs_dir)\n\nlive_cell_train_meta = MetadataCatalog.get('sartorius_live_cell_train')\nlive_cell_train_ds = DatasetCatalog.get('sartorius_live_cell_train')\n\nlive_cell_val_meta = MetadataCatalog.get('sartorius_live_cell_val')\nlive_cell_val_ds = DatasetCatalog.get('sartorius_live_cell_val')","metadata":{"execution":{"iopub.status.busy":"2021-11-15T18:47:07.749050Z","iopub.execute_input":"2021-11-15T18:47:07.749486Z","iopub.status.idle":"2021-11-15T18:47:13.489904Z","shell.execute_reply.started":"2021-11-15T18:47:07.749438Z","shell.execute_reply":"2021-11-15T18:47:13.488921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display a sample file to check the LiveCell shsy5y train data is loaded correctly\ntrain_sample = live_cell_train_ds[0]\nimg = cv2.imread(train_sample['file_name'])\n\nvisualizer = Visualizer(img[:, :, ::-1], metadata=live_cell_train_meta)\nout = visualizer.draw_dataset_dict(train_sample)\n\n_,ax = plt.subplots(1, 2, figsize=(30, 30))\nax[0].imshow(img)\nax[1].imshow(out.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2021-11-15T18:47:13.491563Z","iopub.execute_input":"2021-11-15T18:47:13.491826Z","iopub.status.idle":"2021-11-15T18:47:15.668579Z","shell.execute_reply.started":"2021-11-15T18:47:13.491795Z","shell.execute_reply":"2021-11-15T18:47:15.667606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display a sample file to check the LiveCell shsy5y val data is loaded correctly\nval_sample = live_cell_val_ds[0]\nimg = cv2.imread(val_sample['file_name'])\n\nvisualizer = Visualizer(img[:, :, ::-1], metadata=live_cell_val_meta)\nout = visualizer.draw_dataset_dict(val_sample)\n\n_,ax = plt.subplots(1, 2, figsize=(30, 30))\nax[0].imshow(img)\nax[1].imshow(out.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2021-11-15T18:47:15.670464Z","iopub.execute_input":"2021-11-15T18:47:15.670882Z","iopub.status.idle":"2021-11-15T18:47:18.015090Z","shell.execute_reply.started":"2021-11-15T18:47:15.670834Z","shell.execute_reply":"2021-11-15T18:47:18.014047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How it was reformatted to load without error","metadata":{}},{"cell_type":"code","source":"# Read LiveCell shsy5y train and val data \nwith open('../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/annotations/LIVECell_single_cells/shsy5y/livecell_shsy5y_train.json') as f:\n  data_train = json.loads(f.read())\n\nwith open('../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/annotations/LIVECell_single_cells/shsy5y/livecell_shsy5y_val.json') as f:\n  data_val = json.loads(f.read())","metadata":{"execution":{"iopub.status.busy":"2021-11-15T18:43:05.939360Z","iopub.execute_input":"2021-11-15T18:43:05.939909Z","iopub.status.idle":"2021-11-15T18:43:11.782801Z","shell.execute_reply.started":"2021-11-15T18:43:05.939865Z","shell.execute_reply":"2021-11-15T18:43:11.781733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add categories\ncategories = [{'name':'shsy5y', 'id':1}, {'name':'astro', 'id':2}, {'name':'cort', 'id':3}]\n\ndata_train['categories'] = categories\ndata_val['categories'] = categories","metadata":{"execution":{"iopub.status.busy":"2021-11-15T18:43:11.784521Z","iopub.execute_input":"2021-11-15T18:43:11.784816Z","iopub.status.idle":"2021-11-15T18:43:11.790855Z","shell.execute_reply.started":"2021-11-15T18:43:11.784775Z","shell.execute_reply":"2021-11-15T18:43:11.789611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Format to COCO RLE format\ntrain_annotations = []\nfor key in data_train['annotations'].keys():\n  rle = _mask.frPoly(data_train['annotations'][key]['segmentation'],520,704)\n  data_train['annotations'][key]['segmentation'] = {'size': rle[0]['size'], 'counts':rle[0]['counts'].decode('utf-8')}\n  train_annotations.append(data_train['annotations'][key])\ndata_train['annotations'] = train_annotations\n\nval_annotations = []\nfor key in data_val['annotations'].keys():\n  rle = _mask.frPoly(data_val['annotations'][key]['segmentation'],520,704)\n  data_val['annotations'][key]['segmentation'] = {'size': rle[0]['size'], 'counts': rle[0]['counts'].decode('utf-8')}\n  val_annotations.append(data_val['annotations'][key])\ndata_val['annotations'] = val_annotations","metadata":{"execution":{"iopub.status.busy":"2021-11-15T18:43:11.792207Z","iopub.execute_input":"2021-11-15T18:43:11.792461Z","iopub.status.idle":"2021-11-15T18:43:16.887300Z","shell.execute_reply.started":"2021-11-15T18:43:11.792429Z","shell.execute_reply":"2021-11-15T18:43:16.886433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save formatted data to JSON\nwith open('annotations_train.json', 'w', encoding='utf-8') as f:\n  json.dump(data_train, f, ensure_ascii=True, indent=4)\n\nwith open('annotations_val.json', 'w', encoding='utf-8') as f:\n  json.dump(data_val, f, ensure_ascii=True, indent=4)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T18:44:11.126816Z","iopub.execute_input":"2021-11-15T18:44:11.127246Z","iopub.status.idle":"2021-11-15T18:44:20.735461Z","shell.execute_reply.started":"2021-11-15T18:44:11.127214Z","shell.execute_reply":"2021-11-15T18:44:20.734448Z"},"trusted":true},"execution_count":null,"outputs":[]}]}