{"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 pycocotools","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:34.389429Z","iopub.execute_input":"2021-12-25T21:19:34.390400Z","iopub.status.idle":"2021-12-25T21:19:34.412899Z","shell.execute_reply.started":"2021-12-25T21:19:34.390255Z","shell.execute_reply":"2021-12-25T21:19:34.411896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nimport torchvision\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nfrom PIL import Image, ImageEnhance\nimport albumentations as A\nfrom skimage import measure\nimport json\nfrom matplotlib.path import Path","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-25T21:19:34.415297Z","iopub.execute_input":"2021-12-25T21:19:34.415729Z","iopub.status.idle":"2021-12-25T21:19:38.802126Z","shell.execute_reply.started":"2021-12-25T21:19:34.415679Z","shell.execute_reply":"2021-12-25T21:19:38.800928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_path = '../input/sartorius-cell-instance-segmentation/train'\ntrain_csv_path = '../input/sartorius-cell-instance-segmentation/train.csv'\nshs_json_path = '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/annotations/LIVECell_single_cells/shsy5y/livecell_shsy5y_train.json'\nshs_img_path = '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_train_val_images/SHSY5Y'","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:38.804109Z","iopub.execute_input":"2021-12-25T21:19:38.804460Z","iopub.status.idle":"2021-12-25T21:19:38.810158Z","shell.execute_reply.started":"2021-12-25T21:19:38.804416Z","shell.execute_reply":"2021-12-25T21:19:38.809219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(train_csv_path)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:38.813686Z","iopub.execute_input":"2021-12-25T21:19:38.814184Z","iopub.status.idle":"2021-12-25T21:19:39.578002Z","shell.execute_reply.started":"2021-12-25T21:19:38.814138Z","shell.execute_reply":"2021-12-25T21:19:39.577279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell_types   = {}\nfor id,row in train_df.groupby(['id']):\n    cell_type = row.cell_type.unique()[0]\n    if not cell_type in cell_types:\n        cell_types[cell_type]=[]\n    cell_types[cell_type].append(id)\n \nstats = {}\nfor cell_type,ids in cell_types.items():\n    stats[cell_type] = len(ids)\nfig = plt.figure(figsize=(5,5))\nax  = fig.subplots()\nax.bar(stats.keys(),stats.values())\nax.set_title('Cell Types')","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:39.579294Z","iopub.execute_input":"2021-12-25T21:19:39.579716Z","iopub.status.idle":"2021-12-25T21:19:39.918569Z","shell.execute_reply.started":"2021-12-25T21:19:39.579684Z","shell.execute_reply":"2021-12-25T21:19:39.917422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(shs_json_path, 'r') as f :\n    file = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:39.920134Z","iopub.execute_input":"2021-12-25T21:19:39.920504Z","iopub.status.idle":"2021-12-25T21:19:48.218803Z","shell.execute_reply.started":"2021-12-25T21:19:39.920463Z","shell.execute_reply":"2021-12-25T21:19:48.217590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_img_id = []\nnew_img_name = []\nshs_images = file['images']\nfor i in range(0, len(file['images']), 3) :\n    new_img_id.append(shs_images[i]['id']) \n    new_img_name.append(shs_images[i]['file_name'])","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.220389Z","iopub.execute_input":"2021-12-25T21:19:48.220640Z","iopub.status.idle":"2021-12-25T21:19:48.226460Z","shell.execute_reply.started":"2021-12-25T21:19:48.220611Z","shell.execute_reply":"2021-12-25T21:19:48.225582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_data = pd.DataFrame(columns = ['id', 'annotation'])","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.227636Z","iopub.execute_input":"2021-12-25T21:19:48.227907Z","iopub.status.idle":"2021-12-25T21:19:48.240232Z","shell.execute_reply.started":"2021-12-25T21:19:48.227877Z","shell.execute_reply":"2021-12-25T21:19:48.239420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# annotations = file['annotations']\n# for value in annotations.values() :\n#     if value['image_id'] in new_img_id :\n#         new_data.loc[len(new_data.index)] = [value['image_id'], value['segmentation']] \n        ","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.241421Z","iopub.execute_input":"2021-12-25T21:19:48.242417Z","iopub.status.idle":"2021-12-25T21:19:48.253262Z","shell.execute_reply.started":"2021-12-25T21:19:48.242365Z","shell.execute_reply":"2021-12-25T21:19:48.252473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_data['annotation'] = new_data['annotation'].apply(lambda x : [x])","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.256130Z","iopub.execute_input":"2021-12-25T21:19:48.256637Z","iopub.status.idle":"2021-12-25T21:19:48.266997Z","shell.execute_reply.started":"2021-12-25T21:19:48.256591Z","shell.execute_reply":"2021-12-25T21:19:48.266245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.268300Z","iopub.execute_input":"2021-12-25T21:19:48.268714Z","iopub.status.idle":"2021-12-25T21:19:48.279706Z","shell.execute_reply.started":"2021-12-25T21:19:48.268682Z","shell.execute_reply":"2021-12-25T21:19:48.278852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def rle_encode(img):\n#     \"\"\" TBD\n\n#     Args:\n#         img (np.array): \n#             - 1 indicating mask\n#             - 0 indicating background\n\n#     Returns: \n#         run length as string formated\n#     \"\"\"\n\n#     pixels = img.flatten()\n#     pixels = np.concatenate([[0], pixels, [0]])\n#     runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n#     runs[1::2] -= runs[::2]\n#     return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.281581Z","iopub.execute_input":"2021-12-25T21:19:48.281975Z","iopub.status.idle":"2021-12-25T21:19:48.292593Z","shell.execute_reply.started":"2021-12-25T21:19:48.281936Z","shell.execute_reply":"2021-12-25T21:19:48.291840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def annotation_transformation(annotations) :\n#     seg_list = []\n#     for img_mask in annotations :\n\n#         x = img_mask[0::2]\n#         y = img_mask[1::2]\n\n#         arr = [(x, y) for (x, y) in zip(y,x)]\n#         vertices = np.asarray(arr)\n#         path = Path(vertices)\n#         xmin, ymin, xmax, ymax = np.asarray(path.get_extents(), dtype=int).ravel()\n#         x, y = np.mgrid[:520, :704]\n\n#         # mesh grid to a list of points\n#         points = np.vstack((x.ravel(), y.ravel())).T\n\n#         # select points included in the path\n#         mask = path.contains_points(points)\n#         path_points = points[np.where(mask)]\n\n#         # reshape mask for display\n#         img_mask = mask.reshape(x.shape)\n#         img_mask = img_mask.astype(np.int)\n#         # ENCODED MASK\n#         encoded_img_mask = rle_encode(img_mask)\n#         seg_list.append(encoded_img_mask)\n#     return seg_list","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.294010Z","iopub.execute_input":"2021-12-25T21:19:48.294263Z","iopub.status.idle":"2021-12-25T21:19:48.308839Z","shell.execute_reply.started":"2021-12-25T21:19:48.294223Z","shell.execute_reply":"2021-12-25T21:19:48.308126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# annotations = []\n# for index, row in new_data.iterrows() :\n#     annotations.append(annotation_transformation(row['annotation']))","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.310395Z","iopub.execute_input":"2021-12-25T21:19:48.311261Z","iopub.status.idle":"2021-12-25T21:19:48.321219Z","shell.execute_reply.started":"2021-12-25T21:19:48.311215Z","shell.execute_reply":"2021-12-25T21:19:48.320513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(annotations)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.322422Z","iopub.execute_input":"2021-12-25T21:19:48.322658Z","iopub.status.idle":"2021-12-25T21:19:48.332665Z","shell.execute_reply.started":"2021-12-25T21:19:48.322622Z","shell.execute_reply":"2021-12-25T21:19:48.331879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_data['annotation'] = annotations","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.333879Z","iopub.execute_input":"2021-12-25T21:19:48.334229Z","iopub.status.idle":"2021-12-25T21:19:48.344814Z","shell.execute_reply.started":"2021-12-25T21:19:48.334201Z","shell.execute_reply":"2021-12-25T21:19:48.344195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_data['annotation'] = new_data['annotation'].apply(lambda x : x[0])\n# new_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.345939Z","iopub.execute_input":"2021-12-25T21:19:48.346684Z","iopub.status.idle":"2021-12-25T21:19:48.357968Z","shell.execute_reply.started":"2021-12-25T21:19:48.346623Z","shell.execute_reply":"2021-12-25T21:19:48.356863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_data['width'] = 704\n# new_data['height'] = 520\n# new_data['cell_type'] = 'shsy5y'\n# new_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.359898Z","iopub.execute_input":"2021-12-25T21:19:48.360494Z","iopub.status.idle":"2021-12-25T21:19:48.371854Z","shell.execute_reply.started":"2021-12-25T21:19:48.360441Z","shell.execute_reply":"2021-12-25T21:19:48.371099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.373074Z","iopub.execute_input":"2021-12-25T21:19:48.373904Z","iopub.status.idle":"2021-12-25T21:19:48.383114Z","shell.execute_reply.started":"2021-12-25T21:19:48.373861Z","shell.execute_reply":"2021-12-25T21:19:48.382308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_data.to_csv('shs_train_df.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.384426Z","iopub.execute_input":"2021-12-25T21:19:48.384785Z","iopub.status.idle":"2021-12-25T21:19:48.395798Z","shell.execute_reply.started":"2021-12-25T21:19:48.384744Z","shell.execute_reply":"2021-12-25T21:19:48.394801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.makedirs('./new_shs_images', exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.397127Z","iopub.execute_input":"2021-12-25T21:19:48.397641Z","iopub.status.idle":"2021-12-25T21:19:48.407924Z","shell.execute_reply.started":"2021-12-25T21:19:48.397545Z","shell.execute_reply":"2021-12-25T21:19:48.406950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_train_val_images/SHSY5Y'\nfor i in range(len(new_img_name)) :\n    img = Image.open(os.path.join(img_path, new_img_name[i]))\n    rgb_img = img.convert('RGB')\n    rgb_img.save('./new_shs_images/' + str(new_img_id[i]) + '.png')","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:19:48.410095Z","iopub.execute_input":"2021-12-25T21:19:48.410401Z","iopub.status.idle":"2021-12-25T21:20:12.900564Z","shell.execute_reply.started":"2021-12-25T21:19:48.410369Z","shell.execute_reply":"2021-12-25T21:20:12.899186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = os.listdir('./new_shs_images')\nlen(x)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:20:12.902595Z","iopub.execute_input":"2021-12-25T21:20:12.902950Z","iopub.status.idle":"2021-12-25T21:20:12.912475Z","shell.execute_reply.started":"2021-12-25T21:20:12.902903Z","shell.execute_reply":"2021-12-25T21:20:12.911623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('./new_shs_images/1553886.png')\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T21:20:12.913985Z","iopub.execute_input":"2021-12-25T21:20:12.914538Z","iopub.status.idle":"2021-12-25T21:20:13.236163Z","shell.execute_reply.started":"2021-12-25T21:20:12.914475Z","shell.execute_reply":"2021-12-25T21:20:13.234541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}