{"nbformat_minor": 1, "cells": [{"cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "e47d04c9-d976-49e2-83f2-6fa6854cce53", "_uuid": "7c8fbce05cb31280b61e3915edb0e6d5aced80ad"}, "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 random\n", "import skimage.io\n", "import matplotlib.pyplot as plt\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."]}, {"cell_type": "markdown", "source": ["**Next section reads a random subset of 16 images from the training data set and shows them along with their nuclei masks. Odd rows in the display are the images and Even rows are the masks for the previous row of images.**"], "metadata": {"_cell_guid": "3953def3-5939-4084-8e06-0f7c5b028542", "_uuid": "f3e0b0d7a7af7c0e22f7c46f0b4c54189fcf3e11"}}, {"cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "58d4a240-48e6-4996-a0d8-8a2c38c7dd61", "_uuid": "c02d0a22cd8bf8c63f83c03fbd41212db536b141"}, "source": ["def read_image_labels(image_id):\n", "    # most of the content in this function is taken from 'Example Metric Implementation' kernel \n", "    # by 'William Cukierski'\n", "    image_file = \"../input/stage1_train/{}/images/{}.png\".format(image_id,image_id)\n", "    mask_file = \"../input/stage1_train/{}/masks/*.png\".format(image_id)\n", "    image = skimage.io.imread(image_file)\n", "    masks = skimage.io.imread_collection(mask_file).concatenate()    \n", "    height, width, _ = image.shape\n", "    num_masks = masks.shape[0]\n", "    labels = np.zeros((height, width), np.uint16)\n", "    for index in range(0, num_masks):\n", "        labels[masks[index] > 0] = index + 1\n", "    return image, labels\n", "\n", "def plot_images_masks(image_ids):\n", "    plt.close('all')\n", "    fig, ax = plt.subplots(nrows=8,ncols=4, figsize=(50,50))\n", "    for ax_index, image_id in enumerate(image_ids):\n", "        image, labels = read_image_labels(image_id)\n", "        img_row, img_col, mask_row, mask_col = int(ax_index/4)*2, ax_index%4, int(ax_index/4)*2 + 1, ax_index%4\n", "        ax[img_row][img_col].imshow(image)\n", "        ax[mask_row][mask_col].imshow(labels)\n", "\n", "image_ids = check_output([\"ls\", \"../input/stage1_train/\"]).decode(\"utf8\").split()\n", "print(\"Total Images in Training set: {}\".format(len(image_ids)))\n", "random_image_ids = random.sample(image_ids, 16)\n", "print(\"Randomly Selected Images: {}, their IDs: {}\".format(len(random_image_ids), random_image_ids))\n", "plot_images_masks(random_image_ids)\n", "    "]}, {"cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "9b32c7ba-9c7c-458a-bf04-0b6259f37cde", "_uuid": "3071e7e780f9a9a17ce0b49b4a662557d99a5049"}, "source": []}], "nbformat": 4, "metadata": {"kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}, "language_info": {"name": "python", "mimetype": "text/x-python", "pygments_lexer": "ipython3", "file_extension": ".py", "codemirror_mode": {"name": "ipython", "version": 3}, "version": "3.6.4", "nbconvert_exporter": "python"}}}