{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0c751ddd-e18f-1040-54b5-380ba5b641fb"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import gzip\n",
        "import json\n",
        "\n",
        "import PIL\n",
        "\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import skimage\n",
        "pd.set_option('max_columns', 50)\n",
        "pd.set_option('max_rows', 1000)\n",
        "\n",
        "import matplotlib\n",
        "import matplotlib.pyplot as plt\n",
        "matplotlib.style.use('ggplot')\n",
        "%matplotlib inline\n",
        "matplotlib.rcParams['figure.figsize'] = (8, 6)\n",
        "\n",
        "from pandas.io.parsers import read_csv\n",
        "from sklearn.utils import shuffle\n",
        "\n",
        "from IPython.core.display import display, HTML, Image"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8d536df5-53ff-d9d4-bbf4-e7efe47ea62e",
        "collapsed": true
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8fa4907a-a101-4a36-e0bf-20df45728ca8"
      },
      "outputs": [],
      "source": [
        "def img_to_array(img):\n",
        "    return np.array(img.getdata()).reshape(img.width, img.width, 3) / 255\n",
        "\n",
        "\n",
        "def trim(im):\n",
        "    \"\"\"trim black margin, http://stackoverflow.com/questions/10615901/trim-whitespace-using-pil\"\"\"\n",
        "    bg = PIL.Image.new(im.mode, im.size, im.getpixel((0,0)))\n",
        "    diff = PIL.ImageChops.difference(im, bg)\n",
        "    diff = PIL.ImageChops.add(diff, diff, 2.0, -20)\n",
        "    bbox = diff.getbbox()\n",
        "    if bbox:\n",
        "        return im.crop(bbox)\n",
        "\n",
        "\n",
        "def calc_thumbnail_size(img):\n",
        "    \"\"\"calculate thumbnail size with constant aspect ratio\"\"\"\n",
        "    width, length = img.size\n",
        "    ratio = width / length\n",
        "\n",
        "    # for some reason, if it's exactly 224, then thumnailed image is 223\n",
        "    dim = 224 + 1          # output dim\n",
        "    if ratio > 1:\n",
        "        size = (dim * ratio, dim)\n",
        "    else:\n",
        "        size = (dim, dim / ratio)\n",
        "#     print(size)\n",
        "    return size\n",
        "\n",
        "\n",
        "def calc_crop_coords(img):\n",
        "    \"\"\"crop to square of desired dimension size\"\"\"\n",
        "    dim = 224\n",
        "    width, length = img.size\n",
        "    left = 0\n",
        "    right = width\n",
        "    bottom = length\n",
        "    top = 0\n",
        "    if width > dim:\n",
        "        delta = (width - dim) / 2\n",
        "        left = delta\n",
        "        right = width - delta\n",
        "    if length > dim:\n",
        "        delta = (length - dim) / 2\n",
        "        top = delta\n",
        "        bottom = length - delta\n",
        "    return (left, top, right, bottom)\n",
        "\n",
        "\n",
        "def preprocess(img):\n",
        "    img = trim(img)\n",
        "    tsize = calc_thumbnail_size(img)\n",
        "    img.thumbnail(tsize)\n",
        "    crop_coords = calc_crop_coords(img)\n",
        "    img = img.crop(crop_coords)\n",
        "    return img"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7b14a297-1d06-bf68-a1d4-8c5a4bee60d2",
        "collapsed": true
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "7f17f045-021b-ecda-fc9a-e741ce09bd2a"
      },
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4bc10daf-d981-e085-6a34-5abcb52a4b3f"
      },
      "outputs": [],
      "source": [
        "df = pd.read_csv('../input/trainLabels.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "65cacf1e-28c5-21e4-ff0a-4dd1d25e7cfc"
      },
      "outputs": [],
      "source": [
        "df.level.value_counts().to_frame(name='count').T"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9c51dcdb-33b4-6bb2-b508-9379d9ee3937"
      },
      "outputs": [],
      "source": [
        "df = df.query('image in {0}'.format([_.replace('.jpeg', '') for _ in os.listdir('../input/')]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ef01239f-4547-010c-bdd0-24c512d7e36d"
      },
      "outputs": [],
      "source": [
        "data_dir = '../input/'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1c79539c-7294-da5b-1409-757c4501a2fa",
        "collapsed": true
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "beb44a14-7d22-d626-4ff2-2b3f2621cde6"
      },
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7434986d-de50-9e44-e13c-4fff899bf2c1"
      },
      "outputs": [],
      "source": [
        "PIL.__version__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "434038c3-aaa2-5d79-1e02-afdf9d6b2a6b"
      },
      "source": [
        "# Sample first"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3d76ef15-a65d-2b9d-5fd4-1597fca6ab61"
      },
      "outputs": [],
      "source": [
        "imgs_with_label = []\n",
        "n_samples = 5\n",
        "for i in range(5):\n",
        "    _vals = df.query('level == {0}'.format(i)).sample(n_samples).image.apply(\n",
        "        lambda v: (os.path.join(data_dir, v) + '.jpeg', i)).values.tolist()\n",
        "    imgs_with_label.extend(_vals)\n",
        "\n",
        "fig, axes = plt.subplots(5, 5, figsize=(16, 16))\n",
        "axes = axes.ravel()\n",
        "for k, (img, label) in enumerate(imgs_with_label):\n",
        "    im = PIL.Image.open(img)\n",
        "    im = preprocess(im)\n",
        "    ax = axes[k]\n",
        "    ax.imshow(img_to_array(im))\n",
        "    ax.set_xticklabels([])\n",
        "    ax.set_yticklabels([])\n",
        "    ax.grid(False)\n",
        "    if k % 5 == 0:\n",
        "        ax.set_ylabel('level = {0}'.format(label))\n",
        "    ax.set_title(os.path.basename(img))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "87ce62be-d9f0-52f6-ac5d-9baa9c7b8657"
      },
      "source": [
        "# Sample twice"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9ad3f46c-3066-aefc-8260-378a34a69010"
      },
      "outputs": [],
      "source": [
        "imgs_with_label = []\n",
        "n_samples = 5\n",
        "for i in range(5):\n",
        "    _vals = df.query('level == {0}'.format(i)).sample(n_samples).image.apply(\n",
        "        lambda v: (os.path.join(data_dir, v) + '.jpeg', i)).values.tolist()\n",
        "    imgs_with_label.extend(_vals)\n",
        "\n",
        "fig, axes = plt.subplots(5, 5, figsize=(16, 16))\n",
        "axes = axes.ravel()\n",
        "for k, (img, label) in enumerate(imgs_with_label):\n",
        "    im = PIL.Image.open(img)\n",
        "    im = preprocess(im)\n",
        "    ax = axes[k]\n",
        "    ax.imshow(img_to_array(im))\n",
        "    ax.set_xticklabels([])\n",
        "    ax.set_yticklabels([])\n",
        "    ax.grid(False)\n",
        "    if k % 5 == 0:\n",
        "        ax.set_ylabel('level = {0}'.format(label))\n",
        "    ax.set_title(os.path.basename(img))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "aabb6a7c-c52f-ffd9-6759-66f6d10b3a0a"
      },
      "source": [
        "# Sample a third time"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b9bc8316-638f-78f8-080c-b64bb5f6c006"
      },
      "outputs": [],
      "source": [
        "imgs_with_label = []\n",
        "n_samples = 5\n",
        "for i in range(5):\n",
        "    _vals = df.query('level == {0}'.format(i)).sample(n_samples).image.apply(\n",
        "        lambda v: (os.path.join(data_dir, v) + '.jpeg', i)).values.tolist()\n",
        "    imgs_with_label.extend(_vals)\n",
        "\n",
        "fig, axes = plt.subplots(5, 5, figsize=(16, 16))\n",
        "axes = axes.ravel()\n",
        "for k, (img, label) in enumerate(imgs_with_label):\n",
        "    im = PIL.Image.open(img)\n",
        "    im = preprocess(im)\n",
        "    ax = axes[k]\n",
        "    ax.imshow(img_to_array(im))\n",
        "    ax.set_xticklabels([])\n",
        "    ax.set_yticklabels([])\n",
        "    ax.grid(False)\n",
        "    if k % 5 == 0:\n",
        "        ax.set_ylabel('level = {0}'.format(label))\n",
        "    ax.set_title(os.path.basename(img))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "06811067-a619-2f60-4786-f2f73bcd3830"
      },
      "outputs": [],
      "source": ""
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}