{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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)\nimport cv2 as cv\nimport matplotlib.pyplot as plt\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.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"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\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d6b03072e7daed5bfb098fe5fb664965c0953189"},"cell_type":"code","source":"def get_mask(img_id, df):\n    shape = (768,768)\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    masks = df.loc[img_id]['EncodedPixels']\n    if(type(masks) == float): return img.reshape(shape)\n    if(type(masks) == str): masks = [masks]\n    for mask in masks:\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            img[start:start+length] = 1\n    return img.reshape(shape).T","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d9d5b71e577a1e37832600629e338224cc0d8ac"},"cell_type":"code","source":"def get_ship_size(px):\n    shape = (768,768)\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    s = px.split()\n    for i in range(len(s)//2):\n        start = int(s[2*i]) - 1\n        length = int(s[2*i+1])\n        img[start:start+length] = 1\n    return img.reshape(shape).T.sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2d7a46ef7eabda41a903d09f961e74f7b153ebe"},"cell_type":"code","source":"train_df = pd.read_csv('../input/train_ship_segmentations_v2.csv')\ntrain_df.set_index('ImageId', inplace=True)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c7cb18e19d04a3561a2b1e31d0977a05e0ffe9a"},"cell_type":"code","source":"no_ships_df = train_df[train_df['EncodedPixels'].isna()]\nprint(len(no_ships_df))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2464dc0fc2344ca7966004b681ebd81a7a06578e"},"cell_type":"code","source":"# Sample image with ships\nships_df = train_df[~train_df['EncodedPixels'].isna()]\nprint(len(ships_df))\nships_df.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8196f5923fd753f30b24c08a4fbdf995bc5cb60b"},"cell_type":"markdown","source":"# Count of ships in images"},{"metadata":{"trusted":true,"_uuid":"f24bdc568a2203d38d9685ab3aec85b977f43f2a"},"cell_type":"code","source":"ship_count = ships_df.groupby([ships_df.index]).size()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e7b10f348c571e57bb6e896a30ae106de7201a0"},"cell_type":"code","source":"plt.hist(ship_count, bins=range(15))\nplt.xticks(range(20))\nplt.title('Number of ships per image')\nplt.ylabel('Number of images in train set')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5affabec6140a4ef4234a4b9c3aaf389ec91e745"},"cell_type":"markdown","source":"# Distribution of ship sizes"},{"metadata":{"trusted":true,"_uuid":"c894ae3abe0e281708dc6655fc0a1ad68fae4f38"},"cell_type":"code","source":"ships_df['size'] = ships_df.loc[:, 'EncodedPixels'].apply(get_ship_size)\nships_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59e2d8bb442ba892b62a18c20dde6623504e359e"},"cell_type":"code","source":"print('Max ship size',np.max(ships_df['size']))\nprint('Min ship size', np.min(ships_df['size']))\n\nplt.hist(ships_df['size'], bins=[1,10,100,1000,10000,25000])\n# plt.xticks(range(20))\nplt.title('Ship size (pixels) per image')\nplt.ylabel('Number of ships in train set')\nplt.xlabel('Ship size (pixels)')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7cd975b77dfe2811464e4ebd08eca0467ad22814"},"cell_type":"code","source":"def plot_ship_masks(sample_imgids):\n    fig, ax = plt.subplots(2, 5, sharex='col', sharey='row')\n    fig.set_size_inches(20, 10)\n    fig.tight_layout()\n    for i, imgid in enumerate(sample_imgids):\n        col = i % 5\n        row = i // 5\n\n        img = cv.imread('../input/train_v2/{}'.format(imgid))\n        img = cv.cvtColor(img, cv.COLOR_BGR2RGB)\n        mask = get_mask(imgid, ships_df)\n        plot = ax[row, col]\n        plot.set_title(imgid)\n        plot.axis('off')\n        plot.imshow(img)\n        \n        plot = ax[row+1, col]\n        plot.axis('off')\n        plot.imshow(mask)\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"aad038c0a0e6f6128385da12b798d1984d1a8adc"},"cell_type":"code","source":"# sample_imgids = list(set(ships_df.index))[:5]\n\nsample_imgids = list(ship_count[ship_count < 2].index)[:5]\nplot_ship_masks(sample_imgids)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c13ec1146ba694425630da82fb6285c34c36e1b4"},"cell_type":"code","source":"sample_imgids = list(ship_count[(ship_count > 1) & (ship_count < 6)].index)[:5]\nplot_ship_masks(sample_imgids)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"64456f68671a139d34ddf23f036916fce5073a51"},"cell_type":"code","source":"sample_imgids = list(ship_count[(ship_count > 6)].index)[:5]\nplot_ship_masks(sample_imgids)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a746b9f83686809578f75775d1819805a04fe1b6"},"cell_type":"code","source":"img_id = '01541263e.jpg'\nimg = cv.imread('../input/train_v2/{}'.format(img_id))\nimg = cv.cvtColor(img, cv.COLOR_BGR2RGB)\nplt.figure(figsize=(20,20))\nplt.imshow(img)\n\nplt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"25ddeb6db7d1aa2107470ae25f3a1031790529f1"},"cell_type":"code","source":"# Empty images\nsample = train_df[train_df['EncodedPixels'].isna()].sample(10)\n\nfig, ax = plt.subplots(2, 5, sharex='col', sharey='row')\nfig.set_size_inches(20, 10)\nfig.tight_layout()\nfor i, imgid in enumerate(sample.index):\n    col = i % 5\n    row = i // 5\n    \n    img = cv.imread('../input/train_v2/{}'.format(imgid))\n    img = cv.cvtColor(img, cv.COLOR_BGR2RGB)\n    plot = ax[row, col]\n    plot.axis('off')\n    plot.imshow(img)\n    ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"40108e560517dc7ca3b41f0a65718bb1faa1d5da"},"cell_type":"markdown","source":"# Large ships"},{"metadata":{"trusted":true,"_uuid":"1db4c724378a74975c318301ea2a66e7ec9c9c6a"},"cell_type":"code","source":"# sample_imgids = list(set(ships_df.index))[:5]\n\nsample_imgids = list(ships_df[ships_df['size'] > 23000].index)[:5]\nplot_ship_masks(sample_imgids)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"548076fe1c2cee0a0a1a27bddf3af1f4fcc371fc"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ee74a028598f87866799b9f718720d30695bbb5"},"cell_type":"code","source":"","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}