{"cells":[{"metadata":{"_uuid":"aa65d7e0166a4b347e5402e8295469ade43dea1f"},"cell_type":"markdown","source":"### Overview\nSometimes mask processing may result in mask overlap and the corresponding error when the prediction is submitted. This kernel removes overlapping pixels."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"INPUT = '../input/unet34-submission-tta-0-699-new-public-lb/submission.csv'\nOUTPUT = 'submission.csv'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bcb2b8c0235dca9941598bf5010b4172a6db01af"},"cell_type":"code","source":"def get_mask(img_id, df, shape = (768,768)):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    px = df.loc[img_id]['EncodedPixels']\n    if(type(px) == float): return None\n    elif(type(px) == str): px = [px]\n    count = 1\n    for mask in px:\n        if(type(mask) == float):\n            if len(px) == 1: return None\n            else: continue\n        s = mask.split()\n        for i in range(len(s)//2):\n            start = int(s[2*i]) - 1\n            length = int(s[2*i+1])\n            #keep previous prediction for overlapping pixels\n            img[start:start+length] = count*(img[start:start+length] == 0)\n        count+=1\n    return img.reshape(shape).T\n\ndef decode_mask(mask, shape=(768, 768)):\n    pixels = mask.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    if(len(runs) == 0): return np.nan\n    runs[runs > shape[0]*shape[1]] = shape[0]*shape[1]\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\ndef set_masks(mask):\n    n = mask.max()\n    result = []\n    for i in range(1,n+1):\n        result.append(decode_mask(mask == i))\n    return result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"28dcade50eda56b9fa5fb61420da37d63f49fe38"},"cell_type":"code","source":"pred_df = pd.read_csv(INPUT).set_index('ImageId')\npred_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6be13e5d157bad9bb8b33d493a31338fa41a99c5"},"cell_type":"code","source":"names = list(set(pred_df.index))\nship_list_dict = []\nfor name in tqdm(names):\n    mask = get_mask(name, pred_df)\n    if (not isinstance(mask, np.ndarray) and mask == None) \\\n      or mask.sum() == 0:# or name in test_names_nothing:\n        ship_list_dict.append({'ImageId':name,'EncodedPixels':np.nan})\n    else:\n        encodings = set_masks(mask)\n        if(len(encodings) == 0):\n            ship_list_dict.append({'ImageId':name,'EncodedPixels':np.nan})\n            continue\n        \n        buf =[]\n        for e in encodings:\n            if e == e: buf.append(e)\n        encodings = buf\n        if len(encodings) == 0 : encodings = [np.nan]\n        for encoding in encodings:\n            ship_list_dict.append({'ImageId':name,'EncodedPixels':encoding})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1dba7443cd8646fcd2fb22d4d69e5bf84c4b61d3"},"cell_type":"code","source":"pred_df_cor = pd.DataFrame(ship_list_dict)\npred_df_cor.to_csv(OUTPUT, index=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2f7135392518cf7f516029d678ff7482238dbd5f"},"cell_type":"markdown","source":"Check that everything is correct (taken from  https://www.kaggle.com/c/airbus-ship-detection/discussion/64252)"},{"metadata":{"trusted":true,"_uuid":"554aac473f6205c92be5ff9b131634010bea4f5b"},"cell_type":"code","source":"gr = pred_df_cor.groupby(\"ImageId\")[\"EncodedPixels\"].apply(lambda x: x.isnull().any() and len(x) > 1)  \nprint(gr.value_counts())  # should all be false  \nprint(gr[gr])  # these images have predictions and nan rows ","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}