{"cells":[{"metadata":{},"cell_type":"markdown","source":"__Early EDA - Plotting masks__\n\nThis notebook is my take on the mask plotting. It is based on a few of the most upvoted notebooks already in the competitions, but I tried commenting a little bit more the code I wrote."},{"metadata":{"_uuid":"6f428777-db73-4ccb-8f1e-fbe550d11238","_cell_guid":"3fd6f85e-0ef2-40a2-8d2c-5b9bbab905aa","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.pyplot import imshow\nimport seaborn as sns\n\nfrom PIL import Image\n\nimport os\n\n%matplotlib inline\n\nprint(os.listdir('../input/'))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4033e23a-6f73-42e9-b0c6-bbc3592c6aae","_cell_guid":"e1e950e2-ce3e-414e-8c89-9398196fb5b2","trusted":true},"cell_type":"code","source":"train_labels = pd.read_csv('../input/understanding_cloud_organization/train.csv')\ntrain_labels.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"281e6a84-e373-408a-9ceb-5b3b2083c95b","_cell_guid":"9d83a401-045b-4526-b7db-f04a7d23c055","trusted":true},"cell_type":"code","source":"len(train_labels)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0bf4d0b3-1514-45dc-8047-828fc71b9d9b","_cell_guid":"dd3ed108-dd28-4791-8289-97cd013c2588","trusted":true},"cell_type":"code","source":"# Seperating image and label:\ntrain_labels['label'] = train_labels['Image_Label'].apply(lambda x: x.split('_')[1])\ntrain_labels['image'] = train_labels['Image_Label'].apply(lambda x: x.split('_')[0])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6eae56db-2184-4e45-a744-238058d79aec","_cell_guid":"b7ef6000-d1dc-473f-8308-05a808f84ccb","trusted":true},"cell_type":"code","source":"# Changind all the NaNs by -1\ntrain_labels['EncodedPixels'] = train_labels['EncodedPixels'].fillna(-1)\n\n# Remvoing all the -1 and only keeping the ones with actual Encoded Pixels\ntrain_no_nans = train_labels[train_labels.EncodedPixels != -1]\ntrain_no_nans.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8eac6d83-c6dc-4ac9-8983-e63ae8a73a5f","_cell_guid":"33fb38c0-27d0-42ad-a059-fc492023fa38","trusted":true},"cell_type":"code","source":"labels = ['Fish', 'Flower', 'Gravel', 'Sugar']\nnb_labels = []\nfor item in labels:\n    nb_label = train_no_nans['label'].str.count(item)\n    nb_labels.append(nb_label[nb_label == 1].count())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"935a3fd8-2db2-467a-9083-2e3dcce90d28","_cell_guid":"3f917bf0-1dba-42d2-8489-691752f9bc94","trusted":true},"cell_type":"code","source":"# Number of image containing each label\n\ntrain_no_nans['label'].value_counts().plot(kind='bar')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fb3bd6d5-90fa-41ae-8cd6-9f1d678aa9ce","_cell_guid":"63b21422-1fc8-49ce-b83f-e2ddf6cab299","trusted":true},"cell_type":"code","source":"images = train_no_nans['image'].unique()\nimages[:10]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"20e4561c-5bf3-4eb2-94a2-559166903fe4","_cell_guid":"87577117-7673-436f-a8f2-c0e7434a525c","trusted":true},"cell_type":"code","source":"# Printing the first few images and there labels\n\nfor img_nb in range(2):\n    print(train_no_nans.loc[train_labels['image'] == images[img_nb]]['label'])\n    img = plt.imread('../input/understanding_cloud_organization/train_images/' + images[img_nb])\n    plt.imshow(img)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The mask creation function works in 2 ways:\n- either the mask is a rectangle\n- either it's a complex shape\n\nRectangles are much faster to create, hence having a seperate function to do so. It just allows to gain a bit of time on the creation of the masks. I also outputed how many of the labels are rectangles, and how many are more complex, this does provide some interesting additonal information for the analysis of the masks in the trains et, but also on how to create the segmentations."},{"metadata":{"trusted":true},"cell_type":"code","source":"# Creating masks\n\ndef making_mask(nb_img_to_mask):\n    all_masks = []\n    complex_shapes = 0\n    rectangles = 0\n    row=0\n    for index in range(nb_img_to_mask):\n        #print('\\nfor image: ', index)\n        # Creating a 4d array (1d for each label)\n        mask = np.zeros((1400, 2100, 4))\n        \n        # Select 4 rows:\n        labels = train_labels.iloc[row:row+4]\n        band=0\n        for label in labels.values:\n            \n            # If there is a mask for a given label\n            if label[1] != -1:\n                \n                list_pixel = label[1].split()\n                # Create an mask the size of the image, with only zeros\n                mask_label = np.zeros((1400,2100))\n                # Store position of 1st pixel & length of string\n                positions = list_pixel[::2]\n                length = list_pixel[1::2]\n                \n                # If the length is always the same (we'll assume then there's only a rectangle)\n                unique_values = np.unique(length)\n                \n                if len(unique_values)==1:\n                    #print('rectangle shape')\n                    rectangles+=1\n                    #We make a rectangle starting from the top left, so 1st pixel in postions\n                    start = int(positions[0])\n                    start_column = start//1400\n                    start_row = start%1400\n                    end = int(length[0])\n                    # Create the masks that starts on top left, width nb elements in length[] &\n                    # length nb elements in positions\n                    mask_label[start_row:start_row+end,start_column:start_column+len(positions)] = 1\n                    \n                else:\n                    #print('complex shape')\n                    complex_shapes+=1\n                    for pos, le in zip(positions, length):\n                        start = int(pos)\n                        start_column = start//1400\n                        start_row = start%1400\n                        end = int(le)\n                        mask_label[start_row:start_row+end,start_column] = 1\n                \n                mask[:,:,band] = mask_label\n            band+=1\n        all_masks.append(mask)\n        index +=1\n        row +=4\n        \n    return all_masks, complex_shapes, rectangles","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"masks_images, nb_complex, nb_rectangles = making_mask(100)\nprint(f'There were:\\n{nb_complex} complex shapes ({np.round(nb_complex*100/(nb_complex+nb_rectangles),2)}%)\\n{nb_rectangles} rectangles ({np.round(nb_rectangles*100/(nb_complex+nb_rectangles),2)}%)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_mask_on_img(mask, img_nb):\n    label_list = ['Fish', 'Flower', 'Gravel', 'Sugar']\n    \n    img = plt.imread('../input/understanding_cloud_organization/train_images/' + images[img_nb])\n    plt.figure(figsize=[30, 10])\n    for label in range(4):\n        if 1 in mask[:,:,label]:\n            print(label_list[label])\n            plt.subplot(1,4,label+1)\n            plt.imshow(img)\n            plt.imshow(mask[:,:,label], alpha=0.3, label=label_list[label])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_mask_on_img(masks_images[0], 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_mask_on_img(masks_images[1], 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_mask_on_img(masks_images[2], 2)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}