{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport cv2\nimport glob\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\n\n!ls ../input/understanding_cloud_organization/","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"seed = 1234\nnp.random.seed(seed)\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_path = Path(\"../input/understanding_cloud_organization/\")\ntrain = pd.read_csv(data_path / \"train.csv\")\nsub = pd.read_csv(data_path / \"sample_submission.csv\")\n\nprint(\"Number of training samples: \", len(train))\nprint(\"Number of test samples: \", len(sub))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def rle_decode(mask_rle, shape=(1400, 2100)):\n    '''\n    mask_rle: run-length as string formatted (start length)\n    shape: (height, width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\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, order='F')  # Needed to align to RLE direction","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Let's clear up some mess\ntrain_images_path = \"../input/understanding_cloud_organization/train_images/\"\nvals = train[\"Image_Label\"].str.split(\"_\", expand=True)\ntrain[\"image\"] = vals[0]\ntrain[\"label\"] = vals[1]\ntrain[\"image_path\"] = train_images_path + train[\"image\"]\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# How many null values are there?\nprint(\"Number of null values in the data\")\ntrain.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"That is very strange. Out of `22K` samples, `~11K` don't have encoded pixel values? Interesting!"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Unique labels\ntrain[\"label\"].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Distribution of labels\ntrain[\"label\"].value_counts().plot(kind=\"bar\", figsize=(15, 5))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Maybe I am not sleeping very well. Let me know if you find anything wrong in this analysis. I am unable to digest the fact that all labels have same number of samples "},{"metadata":{"trusted":true},"cell_type":"code","source":"# Drop the null values for now\ntrain_clean = train.dropna().reset_index(drop=True)\ntrain_clean.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_indices = []\nto_select = 4\nfor label in train_clean['label'].unique():\n    label_indices = np.random.choice(train_clean.index[train_clean[\"label\"]==label], size=to_select)\n    sample_indices += label_indices.tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from skimage.io import imread","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f,ax = plt.subplots(4,4, figsize=(20,10))\nfor i, idx in enumerate(sample_indices):\n    img = imread(train_clean.iloc[idx][\"image_path\"])\n    mask_rle = train_clean.iloc[idx][\"EncodedPixels\"]\n    mask = rle_decode(mask_rle)\n    label = train_clean.iloc[idx][\"label\"]\n    \n    ax[i//4, i%4].imshow(img)\n    ax[i//4, i%4].imshow(mask, alpha=0.5, cmap='gray')\n    ax[i//4, i%4].set_title(label)\n    ax[i//4, i%4].axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}