{"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":"%config Completer.use_jedi = False","metadata":{"papermill":{"duration":0.039128,"end_time":"2021-12-26T02:37:21.551714","exception":false,"start_time":"2021-12-26T02:37:21.512586","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-26T03:16:54.586788Z","iopub.execute_input":"2021-12-26T03:16:54.587124Z","iopub.status.idle":"2021-12-26T03:16:54.603053Z","shell.execute_reply.started":"2021-12-26T03:16:54.587091Z","shell.execute_reply":"2021-12-26T03:16:54.602425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -Uqqq pycocotools","metadata":{"papermill":{"duration":20.26228,"end_time":"2021-12-26T02:37:41.825999","exception":false,"start_time":"2021-12-26T02:37:21.563719","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-26T03:16:34.342249Z","iopub.execute_input":"2021-12-26T03:16:34.342548Z","iopub.status.idle":"2021-12-26T03:16:54.584757Z","shell.execute_reply.started":"2021-12-26T03:16:34.342514Z","shell.execute_reply":"2021-12-26T03:16:54.583799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"papermill":{"duration":0.018315,"end_time":"2021-12-26T02:37:41.855726","exception":false,"start_time":"2021-12-26T02:37:41.837411","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-26T03:16:57.696536Z","iopub.execute_input":"2021-12-26T03:16:57.696813Z","iopub.status.idle":"2021-12-26T03:16:57.701998Z","shell.execute_reply.started":"2021-12-26T03:16:57.696785Z","shell.execute_reply":"2021-12-26T03:16:57.701019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/original-traindata-plus150neuro/result.csv')\ndf.head()","metadata":{"papermill":{"duration":1.165472,"end_time":"2021-12-26T02:37:43.032405","exception":false,"start_time":"2021-12-26T02:37:41.866933","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-26T03:16:57.962720Z","iopub.execute_input":"2021-12-26T03:16:57.962987Z","iopub.status.idle":"2021-12-26T03:16:59.334822Z","shell.execute_reply.started":"2021-12-26T03:16:57.962960Z","shell.execute_reply":"2021-12-26T03:16:59.333964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Based on: https://www.kaggle.com/eigrad/convert-rle-to-bounding-box-x0-y0-x1-y1\ndef 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":{"papermill":{"duration":0.02589,"end_time":"2021-12-26T02:37:43.072965","exception":false,"start_time":"2021-12-26T02:37:43.047075","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-26T03:17:00.136280Z","iopub.execute_input":"2021-12-26T03:17:00.136563Z","iopub.status.idle":"2021-12-26T03:17:00.145237Z","shell.execute_reply.started":"2021-12-26T03:17:00.136535Z","shell.execute_reply":"2021-12-26T03:17:00.144270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nfrom pycocotools import mask as maskUtils\nfrom joblib import Parallel, delayed\n\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        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': c_rle,\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    ## Building the header\n    cat_ids = {name:id+1 for id, name in enumerate(df.cell_type.unique())}    \n    cats =[{'name':name, 'id':id} for name,id in cat_ids.items()]\n    images = [{'id':id, 'width':row.width, 'height':row.height, 'file_name':f'../input/sartorius-power2/data/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 tqdm(df.iterrows(), total = len(df)))\n        \n    return {'categories':cats, 'images':images, 'annotations':annotations}","metadata":{"papermill":{"duration":0.186472,"end_time":"2021-12-26T02:37:43.272214","exception":false,"start_time":"2021-12-26T02:37:43.085742","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-26T03:17:02.079039Z","iopub.execute_input":"2021-12-26T03:17:02.079327Z","iopub.status.idle":"2021-12-26T03:17:02.245339Z","shell.execute_reply.started":"2021-12-26T03:17:02.079292Z","shell.execute_reply":"2021-12-26T03:17:02.244524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json,itertools\nroot_train = coco_structure(df)\n#root_train = coco_structure(val_df)","metadata":{"papermill":{"duration":113.042378,"end_time":"2021-12-26T02:39:36.327837","exception":false,"start_time":"2021-12-26T02:37:43.285459","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-26T03:17:02.369219Z","iopub.execute_input":"2021-12-26T03:17:02.369524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('annotations_train.json', 'w', encoding='utf-8') as f:\n    json.dump(root_train, f, ensure_ascii=True, indent=4)","metadata":{"execution":{"iopub.execute_input":"2021-12-26T02:39:36.357229Z","iopub.status.busy":"2021-12-26T02:39:36.356188Z","iopub.status.idle":"2021-12-26T02:39:41.658464Z","shell.execute_reply":"2021-12-26T02:39:41.657755Z","shell.execute_reply.started":"2021-12-26T02:31:26.969879Z"},"papermill":{"duration":5.318059,"end_time":"2021-12-26T02:39:41.658614","exception":false,"start_time":"2021-12-26T02:39:36.340555","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pycocotools.coco import COCO\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\nfrom PIL import Image","metadata":{"execution":{"iopub.execute_input":"2021-12-26T02:39:41.690543Z","iopub.status.busy":"2021-12-26T02:39:41.689805Z","iopub.status.idle":"2021-12-26T02:39:41.692877Z","shell.execute_reply":"2021-12-26T02:39:41.693315Z","shell.execute_reply.started":"2021-12-26T02:31:32.464143Z"},"papermill":{"duration":0.020847,"end_time":"2021-12-26T02:39:41.693501","exception":false,"start_time":"2021-12-26T02:39:41.672654","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataDir=Path('../input/image-filters/data')\nannFile = Path('./annotations_train.json')\ncoco = COCO(annFile)\nimgIds = coco.getImgIds()","metadata":{"execution":{"iopub.execute_input":"2021-12-26T02:39:41.749248Z","iopub.status.busy":"2021-12-26T02:39:41.748514Z","iopub.status.idle":"2021-12-26T02:39:44.045928Z","shell.execute_reply":"2021-12-26T02:39:44.046807Z","shell.execute_reply.started":"2021-12-26T02:31:32.820742Z"},"papermill":{"duration":2.3157,"end_time":"2021-12-26T02:39:44.047027","exception":false,"start_time":"2021-12-26T02:39:41.731327","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.execute_input":"2021-12-26T02:35:02.018463Z","iopub.status.busy":"2021-12-26T02:35:02.018115Z","iopub.status.idle":"2021-12-26T02:35:02.025758Z","shell.execute_reply":"2021-12-26T02:35:02.024873Z","shell.execute_reply.started":"2021-12-26T02:35:02.018423Z"},"papermill":{"duration":0.013009,"end_time":"2021-12-26T02:39:44.073271","exception":false,"start_time":"2021-12-26T02:39:44.060262","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.012757,"end_time":"2021-12-26T02:39:44.099337","exception":false,"start_time":"2021-12-26T02:39:44.086580","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}