{"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":"# Intro\nWelcome to the [Sartorius - Cell Instance Segmentation[](http://)](https://www.kaggle.com/c/sartorius-cell-instance-segmentation/code) compedition\n![](https://storage.googleapis.com/kaggle-competitions/kaggle/30201/logos/header.png)\n<span style=\"color: royalblue;\">Please vote the notebook up if it helps you. Feel free to leave a comment above the notebook. Thank you. </span>","metadata":{}},{"cell_type":"markdown","source":"# Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:16.964018Z","iopub.execute_input":"2021-10-15T08:34:16.964407Z","iopub.status.idle":"2021-10-15T08:34:17.168407Z","shell.execute_reply.started":"2021-10-15T08:34:16.964312Z","shell.execute_reply":"2021-10-15T08:34:17.167603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Path","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/sartorius-cell-instance-segmentation/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.169938Z","iopub.execute_input":"2021-10-15T08:34:17.17018Z","iopub.status.idle":"2021-10-15T08:34:17.178438Z","shell.execute_reply.started":"2021-10-15T08:34:17.170142Z","shell.execute_reply":"2021-10-15T08:34:17.177554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.17961Z","iopub.execute_input":"2021-10-15T08:34:17.179996Z","iopub.status.idle":"2021-10-15T08:34:17.760671Z","shell.execute_reply.started":"2021-10-15T08:34:17.179967Z","shell.execute_reply":"2021-10-15T08:34:17.760111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview","metadata":{}},{"cell_type":"code","source":"print('Number of train samples: ', len(train_data.index))\nprint('Number of features: ', len(train_data.columns))","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.761633Z","iopub.execute_input":"2021-10-15T08:34:17.761994Z","iopub.status.idle":"2021-10-15T08:34:17.767694Z","shell.execute_reply.started":"2021-10-15T08:34:17.761953Z","shell.execute_reply":"2021-10-15T08:34:17.766794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* id: unique identifier for object\n* annotation: run length encoded pixels for the identified neuronal cell\n* width: source image width\n* height: source image height\n* cell_type: the cell line\n* plate_time: time plate was created\n* sample_date: date sample was created\n* sample_id: sample identifier\n* elapsed_timedelta: time since first image taken of sample","metadata":{}},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.769432Z","iopub.execute_input":"2021-10-15T08:34:17.769646Z","iopub.status.idle":"2021-10-15T08:34:17.792881Z","shell.execute_reply.started":"2021-10-15T08:34:17.769621Z","shell.execute_reply":"2021-10-15T08:34:17.792307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"markdown","source":"There are 606 images in the train data set:","metadata":{}},{"cell_type":"code","source":"len(os.listdir(path+'train/'))","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.793845Z","iopub.execute_input":"2021-10-15T08:34:17.794558Z","iopub.status.idle":"2021-10-15T08:34:17.857306Z","shell.execute_reply.started":"2021-10-15T08:34:17.794512Z","shell.execute_reply":"2021-10-15T08:34:17.856295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 3 cell types:","metadata":{}},{"cell_type":"code","source":"train_data['cell_type'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.858307Z","iopub.execute_input":"2021-10-15T08:34:17.858552Z","iopub.status.idle":"2021-10-15T08:34:17.874901Z","shell.execute_reply.started":"2021-10-15T08:34:17.858523Z","shell.execute_reply":"2021-10-15T08:34:17.874062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All images have the same shape:","metadata":{}},{"cell_type":"code","source":"train_data['height'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.876416Z","iopub.execute_input":"2021-10-15T08:34:17.876916Z","iopub.status.idle":"2021-10-15T08:34:17.887698Z","shell.execute_reply.started":"2021-10-15T08:34:17.876873Z","shell.execute_reply":"2021-10-15T08:34:17.887106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['width'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.888769Z","iopub.execute_input":"2021-10-15T08:34:17.889169Z","iopub.status.idle":"2021-10-15T08:34:17.901392Z","shell.execute_reply.started":"2021-10-15T08:34:17.889108Z","shell.execute_reply":"2021-10-15T08:34:17.900804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Setting\nAs we have seen, all the images have the same shape. So we can set a variable to fix the values:","metadata":{}},{"cell_type":"code","source":"shape = (520, 704)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.902366Z","iopub.execute_input":"2021-10-15T08:34:17.902791Z","iopub.status.idle":"2021-10-15T08:34:17.910083Z","shell.execute_reply.started":"2021-10-15T08:34:17.902741Z","shell.execute_reply":"2021-10-15T08:34:17.90939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Focus On Sample Id\nWe consider the first dataset of the train data:","metadata":{}},{"cell_type":"code","source":"row = 0\nid_ = train_data.loc[row, 'id']\nfile = id_+'.png'\nfile in os.listdir(path+'train/')","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.911383Z","iopub.execute_input":"2021-10-15T08:34:17.911924Z","iopub.status.idle":"2021-10-15T08:34:17.927304Z","shell.execute_reply.started":"2021-10-15T08:34:17.911882Z","shell.execute_reply":"2021-10-15T08:34:17.926302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 395 samples for the image id:","metadata":{}},{"cell_type":"code","source":"len(train_data[train_data['id']==id_])","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.928559Z","iopub.execute_input":"2021-10-15T08:34:17.928842Z","iopub.status.idle":"2021-10-15T08:34:17.942593Z","shell.execute_reply.started":"2021-10-15T08:34:17.928807Z","shell.execute_reply":"2021-10-15T08:34:17.94179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load Image and show shape:","metadata":{}},{"cell_type":"code","source":"img = cv2.imread(path+'train/'+file)\nprint('Image shape:', img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:17.943779Z","iopub.execute_input":"2021-10-15T08:34:17.944533Z","iopub.status.idle":"2021-10-15T08:34:17.988944Z","shell.execute_reply.started":"2021-10-15T08:34:17.944499Z","shell.execute_reply":"2021-10-15T08:34:17.988183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot Image:","metadata":{}},{"cell_type":"code","source":"fig, axs = plt.subplots(1, 1, figsize=(7, 7))\naxs.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\naxs.set_xticklabels([])\naxs.set_yticklabels([])\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-10-15T08:34:17.991801Z","iopub.execute_input":"2021-10-15T08:34:17.992354Z","iopub.status.idle":"2021-10-15T08:34:18.236379Z","shell.execute_reply.started":"2021-10-15T08:34:17.992317Z","shell.execute_reply":"2021-10-15T08:34:18.235708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Annotations:\nTo encode the masks we use the function of this examples: \n* https://www.kaggle.com/paulorzp/run-length-encode-and-decode\n* https://www.kaggle.com/inversion/run-length-decoding-quick-start","metadata":{"execution":{"iopub.status.busy":"2021-10-14T20:45:36.046641Z"}}},{"cell_type":"code","source":"def rle_decode(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:18.23745Z","iopub.execute_input":"2021-10-15T08:34:18.237761Z","iopub.status.idle":"2021-10-15T08:34:18.243556Z","shell.execute_reply.started":"2021-10-15T08:34:18.237733Z","shell.execute_reply":"2021-10-15T08:34:18.242988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We write all masks of the image into a list and the decode them with the function above:","metadata":{}},{"cell_type":"code","source":"img_masks = train_data.loc[train_data['id']==id_, 'annotation'].to_list()\nall_masks = np.zeros(shape)\nfor mask in img_masks:\n    all_masks += rle_decode(mask, shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:34:18.24444Z","iopub.execute_input":"2021-10-15T08:34:18.244788Z","iopub.status.idle":"2021-10-15T08:34:18.419048Z","shell.execute_reply.started":"2021-10-15T08:34:18.24476Z","shell.execute_reply":"2021-10-15T08:34:18.418445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We plot the original image, the masks and the image with the masks:","metadata":{}},{"cell_type":"code","source":"fig, axarr = plt.subplots(1, 3, figsize=(15, 40))\naxarr[0].axis('off')\naxarr[1].axis('off')\naxarr[2].axis('off')\naxarr[0].imshow(img)\naxarr[0].set_title('Original Image')\naxarr[1].imshow(all_masks)\naxarr[1].set_title('Masks')\naxarr[2].imshow(img)\naxarr[2].imshow(all_masks, alpha=0.4)\naxarr[2].set_title('Original Image And Masks')\nplt.tight_layout(h_pad=0.1, w_pad=0.1)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-10-15T08:34:18.419913Z","iopub.execute_input":"2021-10-15T08:34:18.420277Z","iopub.status.idle":"2021-10-15T08:34:18.920238Z","shell.execute_reply.started":"2021-10-15T08:34:18.420247Z","shell.execute_reply":"2021-10-15T08:34:18.919336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The next step is to encode the masks. Therefore we use the function of the recommended links above:","metadata":{}},{"cell_type":"code","source":"def rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\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-10-15T08:34:29.47867Z","iopub.execute_input":"2021-10-15T08:34:29.479116Z","iopub.status.idle":"2021-10-15T08:34:29.485611Z","shell.execute_reply.started":"2021-10-15T08:34:29.479068Z","shell.execute_reply":"2021-10-15T08:34:29.484901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rle_encode(all_masks)[:100]","metadata":{"execution":{"iopub.status.busy":"2021-10-15T08:50:05.978703Z","iopub.execute_input":"2021-10-15T08:50:05.97899Z","iopub.status.idle":"2021-10-15T08:50:05.999517Z","shell.execute_reply.started":"2021-10-15T08:50:05.978957Z","shell.execute_reply":"2021-10-15T08:50:05.99895Z"},"trusted":true},"execution_count":null,"outputs":[]}]}