{"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 matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\n\nfrom skimage.io import imread\nfrom pathlib import Path\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-14T10:13:06.283483Z","iopub.execute_input":"2021-11-14T10:13:06.284021Z","iopub.status.idle":"2021-11-14T10:13:06.564375Z","shell.execute_reply.started":"2021-11-14T10:13:06.283910Z","shell.execute_reply":"2021-11-14T10:13:06.563200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataDir='../input/sartorius-cell-instance-segmentation'\ntraindf = pd.read_csv(dataDir+'/train.csv')\ntraindf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:06.568098Z","iopub.execute_input":"2021-11-14T10:13:06.569059Z","iopub.status.idle":"2021-11-14T10:13:07.276067Z","shell.execute_reply.started":"2021-11-14T10:13:06.569003Z","shell.execute_reply":"2021-11-14T10:13:07.274393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SHAPE = (520, 704)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:07.277220Z","iopub.execute_input":"2021-11-14T10:13:07.277449Z","iopub.status.idle":"2021-11-14T10:13:07.281943Z","shell.execute_reply.started":"2021-11-14T10:13:07.277422Z","shell.execute_reply":"2021-11-14T10:13:07.281009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(annotations, shape=SHAPE):\n    '''\n    returns binary masked map \n    '''\n    s = annotations.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[::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-11-14T10:13:07.284147Z","iopub.execute_input":"2021-11-14T10:13:07.284680Z","iopub.status.idle":"2021-11-14T10:13:07.295886Z","shell.execute_reply.started":"2021-11-14T10:13:07.284642Z","shell.execute_reply":"2021-11-14T10:13:07.295152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_images_from_folder(folderName, nameList):\n    image_list= []\n    for fileName in nameList:\n        img = imread(f\"../input/sartorius-cell-instance-segmentation/{folderName}/{fileName}.png\")\n        image_list.append(img)\n    return image_list","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:07.296966Z","iopub.execute_input":"2021-11-14T10:13:07.297481Z","iopub.status.idle":"2021-11-14T10:13:07.308228Z","shell.execute_reply.started":"2021-11-14T10:13:07.297445Z","shell.execute_reply":"2021-11-14T10:13:07.307351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs = np.empty((0,*SHAPE))\nmasked_imgs = np.empty((0,*SHAPE))","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:15:37.757618Z","iopub.execute_input":"2021-11-14T10:15:37.757965Z","iopub.status.idle":"2021-11-14T10:15:37.768661Z","shell.execute_reply.started":"2021-11-14T10:15:37.757911Z","shell.execute_reply":"2021-11-14T10:15:37.767217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IDS = traindf[\"id\"].unique()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:07.325745Z","iopub.execute_input":"2021-11-14T10:13:07.326344Z","iopub.status.idle":"2021-11-14T10:13:07.346199Z","shell.execute_reply.started":"2021-11-14T10:13:07.326305Z","shell.execute_reply":"2021-11-14T10:13:07.345203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs  = np.append(train_imgs,load_images_from_folder(\"train\", IDS),0)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:07.347694Z","iopub.execute_input":"2021-11-14T10:13:07.348018Z","iopub.status.idle":"2021-11-14T10:13:15.025773Z","shell.execute_reply.started":"2021-11-14T10:13:07.347956Z","shell.execute_reply":"2021-11-14T10:13:15.024635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"radomIMG = np.random.randint(606)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:15.027264Z","iopub.execute_input":"2021-11-14T10:13:15.027530Z","iopub.status.idle":"2021-11-14T10:13:15.031825Z","shell.execute_reply.started":"2021-11-14T10:13:15.027498Z","shell.execute_reply":"2021-11-14T10:13:15.031098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:15.033140Z","iopub.execute_input":"2021-11-14T10:13:15.033540Z","iopub.status.idle":"2021-11-14T10:13:15.048713Z","shell.execute_reply.started":"2021-11-14T10:13:15.033491Z","shell.execute_reply":"2021-11-14T10:13:15.047811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train_imgs[radomIMG], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:15.050022Z","iopub.execute_input":"2021-11-14T10:13:15.050819Z","iopub.status.idle":"2021-11-14T10:13:15.399873Z","shell.execute_reply.started":"2021-11-14T10:13:15.050784Z","shell.execute_reply":"2021-11-14T10:13:15.399096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageMaks=traindf[traindf['id']==IDS[radomIMG]]\nallMask = np.zeros(SHAPE)\nfor i in range(len(imageMaks['annotation'])):\n    allMask = allMask + rle_decode(imageMaks['annotation'].iloc[i])\nplt.imshow(allMask, cmap=\"Greens\") #allMask is binary[0-1] masked img","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:15.400885Z","iopub.execute_input":"2021-11-14T10:13:15.401183Z","iopub.status.idle":"2021-11-14T10:13:15.735898Z","shell.execute_reply.started":"2021-11-14T10:13:15.401149Z","shell.execute_reply":"2021-11-14T10:13:15.734747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masked_imgs","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:13:15.738687Z","iopub.execute_input":"2021-11-14T10:13:15.738953Z","iopub.status.idle":"2021-11-14T10:13:15.746055Z","shell.execute_reply.started":"2021-11-14T10:13:15.738908Z","shell.execute_reply":"2021-11-14T10:13:15.744816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for ID in IDS:\n    allMask = np.zeros(SHAPE)\n    imageMaks=traindf[traindf['id']==ID]\n    for i in range(len(imageMaks['annotation'])):\n        allMask = allMask + rle_decode(imageMaks['annotation'].iloc[i])\n    allMask = allMask.reshape(1,*SHAPE)\n    masked_imgs  = np.append(masked_imgs,allMask,0)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T10:16:54.877096Z","iopub.execute_input":"2021-11-14T10:16:54.877627Z","iopub.status.idle":"2021-11-14T10:21:11.140232Z","shell.execute_reply.started":"2021-11-14T10:16:54.877573Z","shell.execute_reply":"2021-11-14T10:21:11.139071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}