{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "408030a9-ff1a-570c-2f89-137369bdf830"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "from skimage.transform import resize\n",
        "import tensorflow as tf\n",
        "\n",
        "def imcrop_tosquare(img):\n",
        "    \"\"\"Make any image a square image.\n",
        "\n",
        "    Parameters\n",
        "    ----------\n",
        "    img : np.ndarray\n",
        "        Input image to crop, assumed at least 2d.\n",
        "\n",
        "    Returns\n",
        "    -------\n",
        "    crop : np.ndarray\n",
        "        Cropped image.\n",
        "    \"\"\"\n",
        "    if img.shape[0] > img.shape[1]:\n",
        "        extra = (img.shape[0] - img.shape[1])\n",
        "        if extra % 2 == 0:\n",
        "            crop = img[extra // 2:-extra // 2, :]\n",
        "        else:\n",
        "            crop = img[max(0, extra // 2 - 1):min(-1, -extra // 2), :]\n",
        "    elif img.shape[1] > img.shape[0]:\n",
        "        extra = (img.shape[1] - img.shape[0])\n",
        "        if extra % 2 == 0:\n",
        "            crop = img[:, extra // 2:-extra // 2]\n",
        "        else:\n",
        "            crop = img[:, max(0, extra // 2 - 1):min(-1, -extra // 2)]\n",
        "    else:\n",
        "        crop = img\n",
        "    return crop\n",
        "\n",
        "dirnameTrain = \"../input/train\"\n",
        "dirnameTest = \"../input/test1\"\n",
        "def getFiles(dirName):\n",
        "    return [os.path.join(dirName, fname) for fname in os.listdir(dirName)]\n",
        "\n",
        "# Make sure we have exactly 100 image files!\n",
        "filenames = getFiles(dirnameTrain)[:100]\n",
        "imgs = [plt.imread(fname)[..., :3] for fname in filenames]\n",
        "# Crop every image to a square\n",
        "imgs = [imcrop_tosquare(img_i) for img_i in imgs]\n",
        "# Then resize the square image to 100 x 100 pixels\n",
        "imgs = [resize(img_i, (100, 100)) for img_i in imgs]# Finally make our list of 3-D images a 4-D array with the first dimension the number of images:\n",
        "imgs = np.array(imgs).astype(np.float32)\n",
        "print(imgs.shape)\n",
        "sess = tf.InteractiveSession()\n",
        "mean_img_op = tf.reduce_mean(imgs,2)\n",
        "mean_img_4d = tf.reduce_mean(imgs, reduction_indices=0, keep_dims=True)\n",
        "subtraction = imgs - tf.reduce_mean(imgs, reduction_indices=0, keep_dims=True)\n",
        "std_img_op = tf.sqrt(tf.reduce_sum(subtraction * subtraction, reduction_indices=0))\n",
        "mean_img = sess.run(mean_img_op)\n",
        "std_img = sess.run(std_img_op)\n",
        "norm_imgs_op = (imgs - mean_img) / std_img\n",
        "norm_imgs = sess.run(norm_imgs_op)\n",
        "print(np.min(norm_imgs), np.max(norm_imgs))\n",
        "print(imgs.dtype)\n",
        "\n",
        "sess.close()"
      ]
    }
  ],
  "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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}