{"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\nimport imageio\nimport matplotlib.pyplot as plt\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-16T11:30:00.659138Z","iopub.execute_input":"2021-11-16T11:30:00.659568Z","iopub.status.idle":"2021-11-16T11:30:10.052489Z","shell.execute_reply.started":"2021-11-16T11:30:00.659471Z","shell.execute_reply":"2021-11-16T11:30:10.051244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Import Data**","metadata":{}},{"cell_type":"code","source":"# Load data\nmasks = pd.read_csv(r\"../input/airbus-ship-detection/train_ship_segmentations_v2.csv\")\nprint(f\"Dataframe with masks looks \\n{masks.head(10)}\\n\\n\")\n\n\nmasks['ships'] = masks['EncodedPixels'].map(lambda c_row: 1 if isinstance(c_row, str) else 0)\nunique_img_ids = masks.groupby('ImageId').agg({'ships': 'sum'}).reset_index()\nunique_img_ids['is_ship'] = unique_img_ids['ships'].map(lambda x: 1.0 if x>0 else 0.0)\n\nprint(f\"Count of images with/withot ships \\n{unique_img_ids['is_ship'].value_counts()}\\n\\n\")\nprint(f\"Count of images with number (0, 1, 2 etc.) of ships \\n{unique_img_ids['ships'].value_counts()}\\n\\n\")\n","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:35:29.861549Z","iopub.execute_input":"2021-11-16T11:35:29.861887Z","iopub.status.idle":"2021-11-16T11:35:31.043816Z","shell.execute_reply.started":"2021-11-16T11:35:29.861855Z","shell.execute_reply":"2021-11-16T11:35:31.04274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualizing masks over original image**","metadata":{}},{"cell_type":"code","source":"# Function to encode mask\ndef rle_decode(mask_rle, IMG_SIZE = (768, 768)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return\n    Returns numpy array, 1 - mask, 0 - background\n    ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(IMG_SIZE[0]*IMG_SIZE[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(IMG_SIZE).T","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:32:39.823264Z","iopub.execute_input":"2021-11-16T11:32:39.823608Z","iopub.status.idle":"2021-11-16T11:32:39.831426Z","shell.execute_reply.started":"2021-11-16T11:32:39.823569Z","shell.execute_reply":"2021-11-16T11:32:39.830301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Here you can choose how much ships do you want to see on an image - variable 'num_ships'\nnum_ships = 5\nprint(f\"ImageId's with {num_ships} ships on it \\n{unique_img_ids.loc[unique_img_ids.ships == num_ships, 'ImageId'].head(5)}\")","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:30:35.183401Z","iopub.execute_input":"2021-11-16T11:30:35.184562Z","iopub.status.idle":"2021-11-16T11:30:35.196452Z","shell.execute_reply.started":"2021-11-16T11:30:35.184507Z","shell.execute_reply":"2021-11-16T11:30:35.195118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy ImageId from output over cell and assign it's value to ImageId\nImageId = '0123b84ee.jpg'\n\nimg = imageio.imread('/kaggle/input/airbus-ship-detection/train_v2/' + ImageId)\nimg_masks = masks.loc[masks['ImageId'] == ImageId, 'EncodedPixels'].tolist()\n\n# Take the individual ship masks and create a single mask array for all ships\nall_masks = np.zeros((768, 768))\nfor mask in img_masks:\n    all_masks += rle_decode(mask, (768, 768))\n    \n\nfig, axarr = plt.subplots(1, 3, figsize=(15, 40))\naxarr[0].axis('off')\naxarr[1].axis('off')\naxarr[2].axis('off')\naxarr[0].imshow(img)\naxarr[1].imshow(all_masks)\naxarr[2].imshow(img)\naxarr[2].imshow(all_masks, alpha=0.4)\nplt.tight_layout(h_pad=0.1, w_pad=0.1) # to adjust automatically axis to subplot area\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:32:42.153379Z","iopub.execute_input":"2021-11-16T11:32:42.153668Z","iopub.status.idle":"2021-11-16T11:32:43.202363Z","shell.execute_reply.started":"2021-11-16T11:32:42.153638Z","shell.execute_reply":"2021-11-16T11:32:43.201454Z"},"trusted":true},"execution_count":null,"outputs":[]}]}