{"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":"import numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport os\nimport sys\nimport tifffile\nimport imageio\nimport glob\nimport shutil\nimport glob\nimport shutil\nimport time\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:31:13.549052Z","iopub.execute_input":"2022-06-17T16:31:13.549856Z","iopub.status.idle":"2022-06-17T16:31:13.82238Z","shell.execute_reply.started":"2022-06-17T16:31:13.54978Z","shell.execute_reply":"2022-06-17T16:31:13.821578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pycocotools","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:32:29.841019Z","iopub.execute_input":"2022-06-17T16:32:29.841534Z","iopub.status.idle":"2022-06-17T16:33:21.509775Z","shell.execute_reply.started":"2022-06-17T16:32:29.841495Z","shell.execute_reply":"2022-06-17T16:33:21.508132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original Image Dimensions\nHEIGHT = 520\nWIDTH = 704\nSHAPE = (HEIGHT, WIDTH)","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:31:23.102872Z","iopub.execute_input":"2022-06-17T16:31:23.10325Z","iopub.status.idle":"2022-06-17T16:31:23.108276Z","shell.execute_reply.started":"2022-06-17T16:31:23.103219Z","shell.execute_reply":"2022-06-17T16:31:23.107113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rles_to_mask(encs, shape=SHAPE):\n    \"\"\"\n    Decodes a rle.\n\n    Args:\n        encs (list of str): Rles for each class.\n        shape (tuple [2]): Mask size.\n\n    Returns:\n        np array [shape]: Mask.\n    \"\"\"\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint)\n    if type(encs)==float:\n        return img\n    for m, enc in enumerate(encs):\n        if isinstance(enc, np.float) and np.isnan(enc):\n            continue\n        enc_split = enc.split()\n        for i in range(len(enc_split) // 2):\n            start = int(enc_split[2 * i]) - 1\n            length = int(enc_split[2 * i + 1])\n            img[start: start + length] = 1 + m\n    return img.reshape(shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:31:33.545576Z","iopub.execute_input":"2022-06-17T16:31:33.546017Z","iopub.status.idle":"2022-06-17T16:31:33.555671Z","shell.execute_reply.started":"2022-06-17T16:31:33.545983Z","shell.execute_reply":"2022-06-17T16:31:33.554533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## <font color='emperal'/> **Prepare LIVECell data**","metadata":{}},{"cell_type":"code","source":"import sys\nsys.path.append('../input/sartorius-helper-package')\nfrom livecell_dataset import LiveCell_Dataset","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:34:21.020165Z","iopub.execute_input":"2022-06-17T16:34:21.020666Z","iopub.status.idle":"2022-06-17T16:34:21.038583Z","shell.execute_reply.started":"2022-06-17T16:34:21.020606Z","shell.execute_reply":"2022-06-17T16:34:21.037797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define path constances\nSHSY5Y_LIVECELL_TRAIN_IMAGE_FOLDER = '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_train_val_images/SHSY5Y'\nSHSY5Y_LIVECELL_TRAIN_ANNOTATION_PATH = '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/annotations/LIVECell_single_cells/shsy5y/livecell_shsy5y_train.json'\nSHSY5Y_LIVECELL_VAL_ANNOTATION_PATH = '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/annotations/LIVECell_single_cells/shsy5y/livecell_shsy5y_val.json'","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:34:34.417501Z","iopub.execute_input":"2022-06-17T16:34:34.417918Z","iopub.status.idle":"2022-06-17T16:34:34.423681Z","shell.execute_reply.started":"2022-06-17T16:34:34.417883Z","shell.execute_reply":"2022-06-17T16:34:34.422537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir ./train ./val","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:34:44.142859Z","iopub.execute_input":"2022-06-17T16:34:44.144073Z","iopub.status.idle":"2022-06-17T16:34:44.907504Z","shell.execute_reply.started":"2022-06-17T16:34:44.144017Z","shell.execute_reply":"2022-06-17T16:34:44.906255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"livecell_dataset = LiveCell_Dataset(image_folder=SHSY5Y_LIVECELL_TRAIN_IMAGE_FOLDER, \n                                    train_annotation_path=SHSY5Y_LIVECELL_TRAIN_ANNOTATION_PATH,\n                                    val_annotation_path=SHSY5Y_LIVECELL_VAL_ANNOTATION_PATH,\n                                    shape_target=SHAPE,\n                                    shape=SHAPE)\n\n# Convert polygon mask to rle\nlivecell_dataset.convert_polygon_to_rle()\n\n# Save images to working dir\nlivecell_dataset.save_image_to_working_dir('train', pad_img=False)\nlivecell_dataset.save_image_to_working_dir('val', pad_img=False)\n\n# Get LiveCell DataFrame\nlivecell_train_df = livecell_dataset.create_livecell_dataframe(img_group='train')\nlivecell_val_df = livecell_dataset.create_livecell_dataframe(img_group='val')","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:34:52.91683Z","iopub.execute_input":"2022-06-17T16:34:52.917248Z","iopub.status.idle":"2022-06-17T16:40:54.878037Z","shell.execute_reply.started":"2022-06-17T16:34:52.917206Z","shell.execute_reply":"2022-06-17T16:40:54.877024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r -q livecell_train.zip train\n!zip -r -q livecell_val.zip val","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:46:34.187856Z","iopub.execute_input":"2022-06-17T16:46:34.18833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#livecell_train_df = livecell_train_df.groupby('id')['annotation'].agg(list).reset_index()\n#livecell_val_df = livecell_val_df.groupby('id')['annotation'].agg(list).reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:41:26.458166Z","iopub.execute_input":"2022-06-17T16:41:26.458597Z","iopub.status.idle":"2022-06-17T16:41:26.567366Z","shell.execute_reply.started":"2022-06-17T16:41:26.458563Z","shell.execute_reply":"2022-06-17T16:41:26.565903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"livecell_train_df","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:41:36.424302Z","iopub.execute_input":"2022-06-17T16:41:36.424912Z","iopub.status.idle":"2022-06-17T16:41:36.468398Z","shell.execute_reply.started":"2022-06-17T16:41:36.424878Z","shell.execute_reply":"2022-06-17T16:41:36.467603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"livecell_train_df.to_csv('./livecell_train.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:42:37.494443Z","iopub.execute_input":"2022-06-17T16:42:37.494847Z","iopub.status.idle":"2022-06-17T16:42:39.627075Z","shell.execute_reply.started":"2022-06-17T16:42:37.494815Z","shell.execute_reply":"2022-06-17T16:42:39.625998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"livecell_val_df","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:41:47.135675Z","iopub.execute_input":"2022-06-17T16:41:47.13605Z","iopub.status.idle":"2022-06-17T16:41:47.166076Z","shell.execute_reply.started":"2022-06-17T16:41:47.13602Z","shell.execute_reply":"2022-06-17T16:41:47.164872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"livecell_val_df.to_csv('./livecell_val.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-17T16:43:31.55556Z","iopub.execute_input":"2022-06-17T16:43:31.555951Z","iopub.status.idle":"2022-06-17T16:43:31.934977Z","shell.execute_reply.started":"2022-06-17T16:43:31.55592Z","shell.execute_reply":"2022-06-17T16:43:31.933697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}