{"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\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-15T20:59:27.238893Z","iopub.execute_input":"2021-10-15T20:59:27.239609Z","iopub.status.idle":"2021-10-15T20:59:27.244056Z","shell.execute_reply.started":"2021-10-15T20:59:27.239557Z","shell.execute_reply":"2021-10-15T20:59:27.243440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Train csv contains all individual masks. Goal of notebook is to combine all annotations into a single np array for training","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/sartorius-cell-instance-segmentation/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-15T20:59:28.407355Z","iopub.execute_input":"2021-10-15T20:59:28.408177Z","iopub.status.idle":"2021-10-15T20:59:28.944295Z","shell.execute_reply.started":"2021-10-15T20:59:28.408131Z","shell.execute_reply":"2021-10-15T20:59:28.943566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T20:59:30.534830Z","iopub.execute_input":"2021-10-15T20:59:30.535810Z","iopub.status.idle":"2021-10-15T20:59:30.577572Z","shell.execute_reply.started":"2021-10-15T20:59:30.535748Z","shell.execute_reply":"2021-10-15T20:59:30.576728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### All images are 520x704","metadata":{}},{"cell_type":"code","source":"height = 520\nwidth = 704","metadata":{"execution":{"iopub.status.busy":"2021-10-15T20:59:33.164352Z","iopub.execute_input":"2021-10-15T20:59:33.164622Z","iopub.status.idle":"2021-10-15T20:59:33.172239Z","shell.execute_reply.started":"2021-10-15T20:59:33.164594Z","shell.execute_reply":"2021-10-15T20:59:33.170989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make all Masks","metadata":{}},{"cell_type":"code","source":"#Go through all images\nfor file_id in df['id'].unique():\n    file_name = f'{file_id}_mask.npy'\n    test_mask = np.zeros((height*width))\n    \n    masks = df[df['id']==file_id]['annotation']\n    \n    #Making all masks value 1 in np array\n    for mask in masks:\n        pixel = []\n        length = []\n        for i, val in enumerate(mask.split()):\n            if i % 2 == 0:\n                pixel.append(int(val)-1)\n            else:\n                length.append(int(val))\n        for pixel, length in zip(pixel,length):\n            test_mask[pixel-1:pixel+length] = 1\n    \n    test_mask = test_mask.reshape((height,width))\n    \n    np.save(file_name,test_mask)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T21:00:13.502835Z","iopub.execute_input":"2021-10-15T21:00:13.503135Z","iopub.status.idle":"2021-10-15T21:00:22.108570Z","shell.execute_reply.started":"2021-10-15T21:00:13.503103Z","shell.execute_reply":"2021-10-15T21:00:22.107715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check Random Image and Mask","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport random\n\nroot = '../input/sartorius-cell-instance-segmentation/train/'\nimages = os.listdir(root)\nimages = [x.split('.')[0] for x in images]\nindex=random.randint(0, len(images))\n\nfile_id = images[index]\n\nimage = root+file_id+'.png'\nmask = np.load(file_id+'_mask.npy')\n\nplt.figure(figsize = (15,10))\nplt.title(file_id)\nimg = mpimg.imread(image)\nimgplot = plt.imshow(img, cmap='gray')\nplt.show()\n\nplt.figure(figsize = (15,10))\nplt.imshow(mask, cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T21:09:23.925818Z","iopub.execute_input":"2021-10-15T21:09:23.926471Z","iopub.status.idle":"2021-10-15T21:09:24.668152Z","shell.execute_reply.started":"2021-10-15T21:09:23.926419Z","shell.execute_reply":"2021-10-15T21:09:24.667092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}