{"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 cv2\nimport pandas as pd\nimport numpy as np\nimport os\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nimport h5py","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-25T02:02:08.943656Z","iopub.execute_input":"2021-10-25T02:02:08.943939Z","iopub.status.idle":"2021-10-25T02:02:09.158922Z","shell.execute_reply.started":"2021-10-25T02:02:08.943910Z","shell.execute_reply":"2021-10-25T02:02:09.157806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class config:\n    DIRECTORY_PATH = \"../input/sartorius-cell-instance-segmentation\"\n    TRAIN_CSV = DIRECTORY_PATH + \"/train.csv\"\n    TRAIN_PATH = DIRECTORY_PATH + \"/train\"\n    TEST_PATH = DIRECTORY_PATH + \"/test\"\n    TRAIN_SEMI_SUPERVISED_PATH = DIRECTORY_PATH + \"/train_semi_supervised\"","metadata":{"execution":{"iopub.status.busy":"2021-10-25T01:26:34.356016Z","iopub.execute_input":"2021-10-25T01:26:34.356378Z","iopub.status.idle":"2021-10-25T01:26:34.361439Z","shell.execute_reply.started":"2021-10-25T01:26:34.356344Z","shell.execute_reply":"2021-10-25T01:26:34.360244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getImagePaths(path):\n    \"\"\"\n    Function to Combine Directory Path with individual Image Paths\n    \n    parameters: path(string) - Path of directory\n    returns: image_names(string) - Full Image Path\n    \"\"\"\n    image_names = []\n    for dirname, _, filenames in os.walk(path):\n        for filename in tqdm(filenames):\n            fullpath = os.path.join(dirname, filename)\n            image_names.append(fullpath)\n    return image_names","metadata":{"execution":{"iopub.status.busy":"2021-10-25T01:26:34.363145Z","iopub.execute_input":"2021-10-25T01:26:34.363827Z","iopub.status.idle":"2021-10-25T01:26:34.375293Z","shell.execute_reply.started":"2021-10-25T01:26:34.363775Z","shell.execute_reply":"2021-10-25T01:26:34.374557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get complete image paths for train and test datasets\ntrain_images_path = getImagePaths(config.TRAIN_PATH)\ntest_images_path = getImagePaths(config.TEST_PATH)\ntrain_semi_supervised_path = getImagePaths(config.TRAIN_SEMI_SUPERVISED_PATH)","metadata":{"execution":{"iopub.status.busy":"2021-10-25T01:26:34.376502Z","iopub.execute_input":"2021-10-25T01:26:34.376910Z","iopub.status.idle":"2021-10-25T01:26:34.996339Z","shell.execute_reply.started":"2021-10-25T01:26:34.376879Z","shell.execute_reply":"2021-10-25T01:26:34.995753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(config.TRAIN_CSV)","metadata":{"execution":{"iopub.status.busy":"2021-10-25T01:26:34.998101Z","iopub.execute_input":"2021-10-25T01:26:34.998358Z","iopub.status.idle":"2021-10-25T01:26:35.595231Z","shell.execute_reply.started":"2021-10-25T01:26:34.998329Z","shell.execute_reply":"2021-10-25T01:26:35.594583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height, width, channels) of array to return \n    color: color for the mask\n    Returns numpy array (mask)\n\n    '''\n    s = mask_rle.split()\n    \n    starts = list(map(lambda x: int(x) - 1, s[0::2]))\n    lengths = list(map(int, s[1::2]))\n    ends = [x + y for x, y in zip(starts, lengths)]\n    \n    img = np.zeros((704 * 520, 1), dtype=np.float32)\n            \n    for start, end in zip(starts, ends):\n        img[start : end] = 1\n    \n    return img.reshape((520, 704))","metadata":{"execution":{"iopub.status.busy":"2021-10-25T01:26:35.596369Z","iopub.execute_input":"2021-10-25T01:26:35.597118Z","iopub.status.idle":"2021-10-25T01:26:35.607169Z","shell.execute_reply.started":"2021-10-25T01:26:35.597074Z","shell.execute_reply":"2021-10-25T01:26:35.606276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def store(image_id):\n    annos = df_train[(df_train.id == image_id)]['annotation'].tolist()\n    image = cv2.imread(f\"../input/sartorius-cell-instance-segmentation/train/{image_id}.png\")\n    mask = np.zeros((520, 704))\n    for anno in annos:\n        mask += rle_decode(anno)\n    f = h5py.File(f'{image_id}.hdf5', 'w')\n    f.create_dataset('image', data=image)\n    f.create_dataset('mask', data=mask)\n    f.close()","metadata":{"execution":{"iopub.status.busy":"2021-10-25T02:06:56.897435Z","iopub.execute_input":"2021-10-25T02:06:56.897828Z","iopub.status.idle":"2021-10-25T02:06:56.905132Z","shell.execute_reply.started":"2021-10-25T02:06:56.897791Z","shell.execute_reply":"2021-10-25T02:06:56.904009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = df_train.id.unique()\nstore(ids[0])\nhf = h5py.File('./0030fd0e6378.hdf5', 'r')\nimage = np.array(hf.get('image'))\nmask = np.array(hf.get('mask'))\nhf.close()","metadata":{"execution":{"iopub.status.busy":"2021-10-25T02:06:59.363672Z","iopub.execute_input":"2021-10-25T02:06:59.364605Z","iopub.status.idle":"2021-10-25T02:06:59.597187Z","shell.execute_reply.started":"2021-10-25T02:06:59.364548Z","shell.execute_reply":"2021-10-25T02:06:59.595783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2021-10-25T02:08:03.248239Z","iopub.execute_input":"2021-10-25T02:08:03.248758Z","iopub.status.idle":"2021-10-25T02:08:03.560187Z","shell.execute_reply.started":"2021-10-25T02:08:03.248725Z","shell.execute_reply":"2021-10-25T02:08:03.559494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask)","metadata":{"execution":{"iopub.status.busy":"2021-10-25T02:08:04.253315Z","iopub.execute_input":"2021-10-25T02:08:04.253838Z","iopub.status.idle":"2021-10-25T02:08:04.550682Z","shell.execute_reply.started":"2021-10-25T02:08:04.253799Z","shell.execute_reply":"2021-10-25T02:08:04.549592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = df_train.id.unique()\nfor i in ids:\n    store(i)\nhf = h5py.File('./0030fd0e6378.hdf5', 'r')\nimage = np.array(hf.get('image'))\nmask = np.array(hf.get('mask'))\nhf.close()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dct = {'shsy5y': 1, 'astro': 2, 'cort': 3}\ndf_train['type'] = df_train.cell_type.apply(lambda x: dct[x])","metadata":{"execution":{"iopub.status.busy":"2021-10-25T01:58:20.959599Z","iopub.execute_input":"2021-10-25T01:58:20.959909Z","iopub.status.idle":"2021-10-25T01:58:21.012595Z","shell.execute_reply.started":"2021-10-25T01:58:20.959876Z","shell.execute_reply":"2021-10-25T01:58:21.011758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.groupby('id')['type'].mean().unique()","metadata":{"execution":{"iopub.status.busy":"2021-10-25T01:58:52.097374Z","iopub.execute_input":"2021-10-25T01:58:52.097700Z","iopub.status.idle":"2021-10-25T01:58:52.116590Z","shell.execute_reply.started":"2021-10-25T01:58:52.097662Z","shell.execute_reply":"2021-10-25T01:58:52.115036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}