{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":9988,"databundleVersionId":868324,"sourceType":"competition"}],"dockerImageVersionId":5671,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom skimage.data import imread\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-27T03:36:53.121572Z","iopub.execute_input":"2023-11-27T03:36:53.122168Z","iopub.status.idle":"2023-11-27T03:36:53.138776Z","shell.execute_reply.started":"2023-11-27T03:36:53.122067Z","shell.execute_reply":"2023-11-27T03:36:53.137452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = os.listdir('../input/train_v2')\nprint(len(train))\n\ntest = os.listdir('../input/test_v2')\nprint(len(test))","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2023-11-27T03:36:53.139925Z","iopub.execute_input":"2023-11-27T03:36:53.140276Z","iopub.status.idle":"2023-11-27T03:36:53.299066Z","shell.execute_reply.started":"2023-11-27T03:36:53.140144Z","shell.execute_reply":"2023-11-27T03:36:53.298279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/sample_submission_v2.csv')\nsubmission.head()","metadata":{"_uuid":"c6dbe55d09328048f5f7b98aedd8eaa8f68a3751","execution":{"iopub.status.busy":"2023-11-27T03:36:53.300317Z","iopub.execute_input":"2023-11-27T03:36:53.300550Z","iopub.status.idle":"2023-11-27T03:36:53.332295Z","shell.execute_reply.started":"2023-11-27T03:36:53.300515Z","shell.execute_reply":"2023-11-27T03:36:53.331404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef rle_decode(mask_rle, shape=(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\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(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T  # Needed to align to RLE direction","metadata":{"_uuid":"6122ccb9e58bfac6fa5e11c86121e78d9e5151b1","execution":{"iopub.status.busy":"2023-11-27T03:36:53.333857Z","iopub.execute_input":"2023-11-27T03:36:53.334313Z","iopub.status.idle":"2023-11-27T03:36:53.353312Z","shell.execute_reply.started":"2023-11-27T03:36:53.334238Z","shell.execute_reply":"2023-11-27T03:36:53.352342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = pd.read_csv('../input/train_ship_segmentations_v2.csv')\nmasks.head()","metadata":{"_uuid":"206104f888afa9c62a0bbcb45229f58111094f18","execution":{"iopub.status.busy":"2023-11-27T03:36:53.354782Z","iopub.execute_input":"2023-11-27T03:36:53.355124Z","iopub.status.idle":"2023-11-27T03:36:54.162392Z","shell.execute_reply.started":"2023-11-27T03:36:53.355070Z","shell.execute_reply":"2023-11-27T03:36:54.161375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId = '0005d01c8.jpg'\n\nimg = imread('../input/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)\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)\nplt.show()\n","metadata":{"_uuid":"63e18e8573dbb3fe1d3ff1d72b6dc756e067d43a","execution":{"iopub.status.busy":"2023-11-27T03:36:54.164024Z","iopub.execute_input":"2023-11-27T03:36:54.164417Z","iopub.status.idle":"2023-11-27T03:36:55.247086Z","shell.execute_reply.started":"2023-11-27T03:36:54.164355Z","shell.execute_reply":"2023-11-27T03:36:55.246072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"0a38d343b2654f87934a88524ebc14a5759e07cb","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]}]}