{"nbformat": 4, "metadata": {"language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "mimetype": "text/x-python", "file_extension": ".py", "name": "python", "version": "3.6.4", "pygments_lexer": "ipython3", "nbconvert_exporter": "python"}, "kernelspec": {"language": "python", "name": "python3", "display_name": "Python 3"}}, "nbformat_minor": 1, "cells": [{"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", "\n", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "import cv2\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\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", "\n", "from subprocess import check_output\n", "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n", "\n", "# Any results you write to the current directory are saved as output."], "metadata": {"_cell_guid": "834c2eea-497e-4998-8176-8d254f92ec28", "_uuid": "36f35e765b12118bea0806067c1a30a4e87d8549", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["train_labels = pd.read_csv(\"../input/stage1_train_labels.csv\")"], "metadata": {"_cell_guid": "821f3ea6-0195-422e-8e69-49d3bd5ab0ad", "_uuid": "5838cb79cf2388c4fc6ddc6687b7e6b0560b7a58", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["train_labels.head()"], "metadata": {"_cell_guid": "1ceb97a7-22be-4418-bbbc-d9d739bb93a2", "_uuid": "6cf9607037a9349dbcd796ed4c591d178b2125fb", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["# number of unique ImageIds\n", "images = train_labels['ImageId'].unique()\n", "print(len(images))"], "metadata": {"_cell_guid": "f521905c-447d-488f-bede-300b8ba46f48", "_uuid": "4114e8f74451f97174b5307384cecac102220fa7", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["def img_by_row(row):\n", "    image_id = train_labels['ImageId'].iloc[row]\n", "    image = \"../input/stage1_train/{}/images/{}.png\".format(image_id,image_id)\n", "    image = cv2.imread(image)\n", "    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", "    return image\n", "\n", "def img_by_id(image_id):\n", "    image = \"../input/stage1_train/{}/images/{}.png\".format(image_id,image_id)\n", "    image = cv2.imread(image)\n", "    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", "    return image"], "metadata": {"_cell_guid": "6c9fa905-c8c3-493e-8512-8c7188e061bb", "_uuid": "b7068f93fac74b60a5f450bfff722c9a2e6470f5", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["def random_train_images(df):\n", "    random_images = np.random.permutation(df['ImageId'])[:36]\n", "    \n", "    f, axarr = plt.subplots(6,6,figsize=(16,11))\n", "    for row in range(6):\n", "        for col in range(6):\n", "            image_id = random_images[row*6 + col]\n", "            image = img_by_id(image_id)\n", "            ax = axarr[row,col]\n", "            ax.axis('off')\n", "            ax.set_title(str(image.shape))\n", "            ax.imshow(image)"], "metadata": {"_cell_guid": "3245ed21-35ed-4294-b60a-1afe47988ef0", "_uuid": "d9ed861386f37d648c265ab22fe55f8dd80add73", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["# display a random set of images with image sizes\n", "random_train_images(train_labels)"], "metadata": {"_cell_guid": "ccdb6803-1cb0-4a66-84b0-59cb25723e48", "_uuid": "92ac3e8027920e014bfaa37af48f67fa88c95454", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["def mask_by_id(image_id):\n", "    df = train_labels[train_labels['ImageId'] == image_id]\n", "    filled_pixels = []\n", "    for img in range(len(df)):\n", "        encoded = list(map(int,df['EncodedPixels'].iloc[img].split()))\n", "        for i in range(0,len(encoded),2):\n", "            filled_pixels.append(encoded[i])\n", "            for j in range(1,encoded[i+1] + 1):\n", "                filled_pixels.append(encoded[i] + j)\n", "    image = img_by_id(image_id)\n", "    h = image.shape[0]\n", "    w = image.shape[1]\n", "    mask = np.zeros((h*w,1))\n", "    for px in filled_pixels:\n", "        mask[px - 2] = 1\n", "    mask = mask.reshape((h,w),order='F')\n", "    return mask"], "metadata": {"_cell_guid": "6b8525a0-2c1e-4ada-8655-08e45fa01696", "_uuid": "78708e493d6e82f1f98f97d51852c00f990724c2", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["def display_image_masks(image_id):\n", "    image = img_by_id(image_id)\n", "    mask = mask_by_id(image_id)\n", "    f = plt.figure(figsize=(10,20))\n", "    ax1 = f.add_subplot(121)\n", "    ax1.axis('off')\n", "    ax1.imshow(image)\n", "    ax1.set_title(\"original\")\n", "    ax2 = f.add_subplot(122)\n", "    ax2.axis('off')\n", "    ax2.imshow(mask)\n", "    ax2.set_title(\"masks\")"], "metadata": {"_cell_guid": "51a22b50-b8a8-485c-a3d0-3b73492d47d2", "_uuid": "fa5c12cfba0255dd3c4fa3346433d0bb465afe40", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["r = np.random.randint(0,len(images))\n", "print(\"ImageId:\",images[r])\n", "display_image_masks(images[r])"], "metadata": {"_cell_guid": "fda12d26-07d6-4f4c-aec3-928ac23e21f5", "_uuid": "d29ae9d4e330ada22ad49f434bb8f55a792560ce", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": [], "metadata": {"_cell_guid": "460e1845-33d4-4357-b2e2-4497463c3740", "_uuid": "3815b133663d09dc7532b287e049deeda20ac8d3", "collapsed": true}, "cell_type": "code", "outputs": [], "execution_count": null}]}