{"cells":[{"metadata":{"_uuid":"1435c6f984ebeb8424606e34b66fe20a5b2211c7"},"cell_type":"markdown","source":"# Decode All Airbus Masks Function\n\nThe function I created below decodes all run length encoding segmentations for the airbus competition into a multi-dimensional array. Due to the volume of data I have not run to the end but have tested on batch. Anyone care to try it out? My first kernel contribution . Hope you like it."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bf878334ee8a61cfb4f92b1224650790ca7e67ea"},"cell_type":"code","source":"train_segs = r\"/kaggle/input/train_ship_segmentations_v2.csv\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa514d5a0ad34b0c7c0067f41d5b1eae11265489"},"cell_type":"code","source":"def rle_decode(path_to_segments, shape = (768,768)):\n    segs = pd.read_csv(path_to_segments, index_col = 0)\n    segNames = np.unique(segs.index)\n    allMasks = []\n    for segName in segNames:\n        masks = []\n        segData = segs.loc[segName].dropna()\n        if segData.shape[0] != 0:\n            if segData.shape[0] > 1:\n                obj = [np.array(each.replace(\" \", \", \").split(', '), dtype = 'int') for each in segData.EncodedPixels]\n            else:\n                obj = [np.array(segData.EncodedPixels.replace(\" \", \", \").split(', '), dtype = 'int')]\n            for eachRLE in obj:\n                rle_indexes = eachRLE.reshape((int(eachRLE.shape[0]/2), 2))\n                template = np.arange(0,shape[0]*shape[1],1).reshape(shape).T\n                for i, rle in enumerate(rle_indexes):\n                    runItems = np.arange(rle[0], rle[0] + rle[1])\n                    for runItem in runItems:\n                        template[template == runItem] = -1\n                masks.append((template == -1).astype('int'))\n        else:\n            masks.append(np.zeros((shape)))\n            \n        allMasks.append(np.array(masks).max(axis = 0))\n    return np.array(allMasks)\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db2a29b53fc03b5a818affa8c382159814de17bf"},"cell_type":"code","source":"#masks = rle_decode(train_segs)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}