{"cells":[{"metadata":{"_uuid":"187612a37afe37e2f7fff8e3bd7b849346be32e8"},"cell_type":"markdown","source":"### This quick bit of code could help improve the quality of the masks.\n### Here, we ultimately want to alter the masks to differentiate each object, such that there will be a 255 pixel border around discrete objects. Blobs of, for example, cars, will be broken into individual cars.\n### Any pixel not completely surrounded by pixels of the same label gets marked as a boundary. Boundaries are 2 pixels thick."},{"metadata":{"_uuid":"5d5274bd2b30124297214bf7414e552f15f10d8b"},"cell_type":"markdown","source":"### The idea here is to build this process into a custom transformation in a dataloader for PyTorch, or iterate over all masks in the train / val directory as a preprocessing step"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","collapsed":true,"trusted":true},"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom numpy import copy\nfrom skimage.segmentation import find_boundaries\nfrom PIL import Image","execution_count":39,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Read in an example mask and view the critical part of it\nmsk = np.asarray(Image.open(\"../input/train_label/171206_033642600_Camera_5_instanceIds.png\"))\nplt.figure(figsize=(20,20))\nplt.imshow(msk[1600:1900:, 1000:2100:])","execution_count":40,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3a7e3ae996f2caa155f8b478e81b890f0260dcc2"},"cell_type":"code","source":"## use find_boundaries function; eyeball check to see how it did (which is remarkably well)\nboundaries = find_boundaries(copy(msk), mode = 'thick')\nplt.figure(figsize=(20,20))\nplt.imshow(boundaries[1600:1900:, 1000:2100:])","execution_count":41,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16b63a3ea9778b0c833eb2ded6e43f94e1f57c43"},"cell_type":"code","source":"# since the `boundaries` array is a boolean, it is simple to use it to set pixels to 255 on the original mask where the bool is True\nmsk_boundaries = copy(msk)\nmsk_boundaries[boundaries] = 255\n\nplt.figure(figsize=(20,20))\nplt.imshow(msk_boundaries[1600:1900:, 1000:2100:])","execution_count":42,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}