{"cells":[{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom skimage.io import imread\n\nimport os\nprint(os.listdir('../input'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c6ee03fffba3df35e4665fb7c739eb3d55f1c06"},"cell_type":"code","source":"segmentations = pd.read_csv('../input/train_ship_segmentations.csv')\nsegmentations.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"606415a7ae086d6de9e312da3ea15ce79e4d19e6"},"cell_type":"code","source":"print('Number of images')\nprint(f'- containing ships     : {len(segmentations[segmentations.EncodedPixels.isna() == False].ImageId.unique())}')\nprint(f'- not containing ships : {len(segmentations[segmentations.EncodedPixels.isna()].ImageId.unique())}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3f818b33eb979408c3aa8dee773123c17fc4901"},"cell_type":"code","source":"image_ids = segmentations[segmentations.EncodedPixels.isna() == False].sample(6, random_state=123).ImageId\nimage_ids","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"fe75596d35c1afd9241d7714ed8ade52ca0edfe8"},"cell_type":"code","source":"image_id = image_ids.iat[0]\nimg = imread(f'../input/train/{image_id}')\nprint(f'Image id   : {image_id}')\nprint(f'Image shape: {img.shape}')\n\nplt.axis('off')\nplt.imshow(img)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9f37c66959ef975e821b36d5a31560995ad3397"},"cell_type":"code","source":"def rle_decode(encoded_pixels, shape):\n    s = encoded_pixels.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[::2], s[1::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    for start, end in zip(starts, ends):\n        img[start:end] = 1\n    return img.reshape(shape).T","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00062a8ae46e4106e966dc99f8e84204a2f6ad4b","scrolled":false},"cell_type":"code","source":"fig, axes = plt.subplots(3, 4, figsize=(16, 12))\n\nfor i, image_id in enumerate(image_ids):\n    img = imread(f'../input/train/{image_id}')\n    mask_shape = img.shape[:-1]\n    mask = np.zeros(mask_shape)\n    \n    encoded_pixels_list = segmentations[segmentations.ImageId == image_id].EncodedPixels.tolist()\n    for encoded_pixels in encoded_pixels_list:\n        mask += rle_decode(encoded_pixels, mask_shape)\n        \n    row = i // 2\n    col = i * 2 % 4\n    axes[row][col].axis('off')\n    axes[row][col+1].axis('off')\n    axes[row][col].imshow(img)\n    axes[row][col+1].imshow(mask)\n    \nplt.tight_layout(h_pad=0, w_pad=0)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8f5810213c6f991dd099bad16e1cf095ef4e117"},"cell_type":"code","source":"image_ids = segmentations[segmentations.EncodedPixels.isna()].sample(8, random_state=123).ImageId\nimage_ids","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b660195e540ecb3ad623ad2a73d76e7b7caac6c"},"cell_type":"code","source":"fig, axes = plt.subplots(2, 4, figsize=(16, 8))\n\nfor i, image_id in enumerate(image_ids):\n    img = imread(f'../input/train/{image_id}')\n    \n    row = i // 4\n    col = i % 4\n        \n    axes[row][col].axis('off')\n    axes[row][col].imshow(img)\n    \nplt.tight_layout(h_pad=0, w_pad=0)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a29baa1d92cec080f0f85de007f4bb380565ba28"},"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}