{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"markdown","source":"## 1. Loading Libraries and Dataset"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom skimage.data import imread\nimport matplotlib.pyplot as plt\nimport os\nfrom keras.preprocessing.image import load_img\nfrom tqdm import tqdm_notebook\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb4d1d647a2f527dfe1747308b0b1f119d197932","collapsed":true},"cell_type":"code","source":"Train_Image_folder='../input/train/'\nTest_Image_folder='../input/test/'\nTrain_Image_name=os.listdir(path=Train_Image_folder)\nTest_Image_name=os.listdir(path=Test_Image_folder)\nTrain_Image_path=[]\nTrain_Mask_path=[]\nTrain_id=[]\nfor i in Train_Image_name:\n    path1=Train_Image_folder+i\n    id1=i.split(sep='.')[0]\n    Train_Image_path.append(path1)\n    Train_id.append(id1)\n \ndf_Train_path=pd.DataFrame({'ImageId':Train_id,'Train_Image_path':Train_Image_path})\nprint('Train Shape: ',df_Train_path.shape)\ndf_Train_path.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"f09790316396527c0e2c151b7d87063f724dab5e","collapsed":true},"cell_type":"code","source":"Test_Image_path=[]\nTest_id=[]\nfor i in Test_Image_name:\n    path=Test_Image_folder+i\n    id2=i.split(sep='.')[0]\n    Test_Image_path.append(path)\n    Test_id.append(id2)\ndf_Test_path=pd.DataFrame({'ImageId':Test_id,'Test_Image_path':Test_Image_path})\nprint('Test Shape: ',df_Test_path.shape)\ndf_Test_path.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5bcb30d48b946bc83bfab412ef1181a55c04a14","collapsed":true},"cell_type":"code","source":"masks = pd.read_csv('../input/train_ship_segmentations.csv')\nprint('Mask Shape: ',masks.shape)\nmasks.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5e5df0543a6162fb4068a338efcfaa21e21a3749"},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd1e5a797c5652c543abd8b80e33347cd40c801f","collapsed":true},"cell_type":"markdown","source":"## 2. Basic Visualization"},{"metadata":{"trusted":true,"_uuid":"b9384203310f0921250d4bd6f3e80ce5aaacb1d1"},"cell_type":"code","source":"#https://www.kaggle.com/inversion/run-length-decoding-quick-start\nImageId = '0005d01c8.jpg'\n\nimg = imread('../input/train/' + 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()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"9d2487fd25abc2bdedca58aff5d556c8fa3b5b73"},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}