{"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)\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.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f1dc695a22cda506f3da78158c718486b79ad987"},"cell_type":"markdown","source":"<h2>Check train csv</h2>"},{"metadata":{"trusted":true,"_uuid":"4e8d69e92c4be00bd9bb9f2c5c955b5cec3e0778"},"cell_type":"code","source":"masks_train = pd.read_csv('../input/train_ship_segmentations_v2.csv')\nmasks_train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d83752bc52f52f484f70b090cf8fceb1f33028c0"},"cell_type":"code","source":"masks_train.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":true},"cell_type":"code","source":"#checking train files\nTRAIN=\"../input/train_v2\"\nfile_names=os.listdir(TRAIN)\nprint(\"Train files :\",len(file_names))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db205ec4493ee3c2188a6fc24d7f219b3de02776"},"cell_type":"code","source":"#checking test files\nTEST=\"../input/test_v2\"\ntest_file_names=os.listdir(TEST)\nprint(\"Test files :\",len(test_file_names))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e75d383870fd81104b8fe05d9d6f2e1b1ab46440"},"cell_type":"markdown","source":"<h2>Lets check a random train image</h2>"},{"metadata":{"trusted":true,"_uuid":"c02557d8893d9abb1193a47780cd003c07b426ba"},"cell_type":"code","source":"from PIL import Image\nImageId=file_names[25]\nim = Image.open(TRAIN+\"/\"+ImageId)\nim.size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e58e901f8b3e710518da31a77673912df7236565"},"cell_type":"code","source":"im_test = Image.open(TEST+\"/\"+test_file_names[5])\nim_test.size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"519f7fcd24ede9f088e496d191c231a4def76556"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\nplt.imshow(im)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d465c3b6f2fb8bc43d7b266e9cb141e80bc3686"},"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":"dca2d63ea6460b7b3310a2df7801000fc3a4a125"},"cell_type":"code","source":"img_masks = masks_train.loc[masks_train['ImageId'] == ImageId, 'EncodedPixels'].tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2db5ee9eff10778238972ba9d8ea1d26340401ee"},"cell_type":"code","source":"mask_img = np.zeros((768, 768))\nfor mask in img_masks:\n    mask_img += rle_decode(mask)\nplt.imshow(mask_img)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6bbc6c786e01fa5f97259e2d357f2151d57f4e1b"},"cell_type":"markdown","source":"<h2>Train image +Mask</h2>"},{"metadata":{"trusted":true,"_uuid":"7a57a85985caaace564423fcb85f11b48c5baaa7"},"cell_type":"code","source":"def show_img(im, figsize=None, ax=None, alpha=None):\n    if not ax: fig,ax = plt.subplots(figsize=figsize)\n    ax.imshow(im, alpha=alpha)\n    ax.set_axis_off()\n    return ax","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a21b1f1c1f4b442eb89ed3706ec6356fc90f77d"},"cell_type":"code","source":"def get_mask_img(ImageId):\n    img_masks = masks_train.loc[masks_train['ImageId'] == ImageId, 'EncodedPixels'].tolist()\n    mask_img = np.zeros((768, 768))\n    if len(img_masks)==1:\n        return mask_img\n    for mask in img_masks:\n        mask_img += rle_decode(mask)\n    return mask_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39525ca647b7e035efa3d60411a4352e1f438fce"},"cell_type":"code","source":"fig, axes = plt.subplots(4, 5, figsize=(18, 12))\nfor i,ax in enumerate(axes.flat):\n    imageid=file_names[i+100]\n    img=Image.open(TRAIN+\"/\"+imageid)\n    mask=get_mask_img(imageid)\n    ax = show_img(img, ax=ax)\n    show_img(mask, ax=ax, alpha=0.3)\nplt.tight_layout(0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"87191913e13c362b2d5df9a804df48947f807575"},"cell_type":"code","source":"#check for Nan\ncount=masks_train['EncodedPixels'].isnull().sum()\nprint(\"Null mask counts :\",count )\nprint(\"Train images contatining ships :\",len(masks_train)-count )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3e1093b9c10586f8a234d399dae312f77b252fd"},"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}