{"nbformat": 4, "cells": [{"execution_count": null, "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 tensorflow as tf\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."], "outputs": [], "metadata": {"_cell_guid": "375b90f0-2e8a-4398-892d-c2f242c95d5c", "_uuid": "a03cbec52d7db20d52841f6ec71c97fb993209c5"}, "cell_type": "code"}, {"execution_count": null, "source": ["# read the data from the cvs files\n", "df = pd.read_csv( '../input/test.csv' )\n", "data = df.as_matrix()"], "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code"}, {"execution_count": null, "source": ["# get a random image from the MNIST data\n", "id = np.random.randint(1000)\n", "image = data[id:id+1]\n", "\n", "# display the image\n", "plt.tick_params( axis='both', which='both', bottom='off', left='off', labelbottom='off', labelleft='off' )\n", "plt.imshow(image.reshape(28,28), cmap='gray')\n", "# append '_r' to cmap for reversed color map"], "outputs": [], "metadata": {}, "cell_type": "code"}, {"execution_count": null, "source": ["# normalize the data\n", "print(\"Min: %.2f Max: %.2f\" % (np.min(image), np.max(image)))\n", "image = np.multiply(image, 1.0/255)\n", "print(\"Min: %.2f Max: %.2f\" % (np.min(image), np.max(image)))"], "outputs": [], "metadata": {}, "cell_type": "code"}, {"source": ["<h2>Create tensorflow network (graph)</h2>"], "metadata": {}, "cell_type": "markdown"}, {"execution_count": null, "source": ["# we know that all the image are 28 by 28\n", "# this is the input placeholder to provide data to our network\n", "x = tf.placeholder(tf.float32, [None,28*28])"], "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code"}, {"execution_count": null, "source": ["# Reshape the image to comply with the input that a convolutional layer takes\n", "# batch size, width, height, features\n", "xshaped = tf.reshape(x, [-1,28,28,1])"], "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code"}, {"execution_count": null, "source": ["# create a session to run our graphs\n", "sess = tf.InteractiveSession()"], "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code"}, {"source": ["<h2>Create convolutional layer</h2>"], "metadata": {}, "cell_type": "markdown"}, {"execution_count": null, "source": ["# how many features do we want to extract from the image\n", "# each feature is an image with a filter applied to it\n", "features = 5\n", "# weights\n", "W = tf.Variable( tf.truncated_normal([5,5,1,features], stddev=0.1))\n", "# biases\n", "b = tf.Variable( tf.constant(0.1, shape=[features]) )\n", "# first 2 dimensions are patch size (5x5)\n", "#   filters are applied to a section of the image (denoted by the patch size)\n", "#   the section is then moved in the image by some value (denoted by a stride)\n", "# next is number of input channels (we are only using 1 'greyscale')\n", "#   this could also be the number of features coming from previous convolutional layer\n", "# next is number of output channels or features"], "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code"}, {"execution_count": null, "source": ["# initialize the variables\n", "sess.run(tf.global_variables_initializer())"], "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code"}, {"execution_count": null, "source": ["# create a convolutional layer\n", "# stride denotes how patch (defined above) is moved in the image\n", "# truncate the bottom of the output (<0 = 0) with relu (rectified linear unit)\n", "conv1 = tf.nn.relu(tf.nn.conv2d(xshaped, W, strides=[1,1,1,1], padding='SAME') + b)"], "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code"}, {"execution_count": null, "source": ["out = sess.run(conv1, feed_dict={x:image}) # this is convoluted image\n", "out.shape\n", "# last dimension is the number of output channels in W\n", "# (1,image width, image height, output channels / features)"], "outputs": [], "metadata": {}, "cell_type": "code"}, {"execution_count": null, "source": ["# Let see how all the features look like ...\n", "# split the array to images with different features\n", "img = np.dsplit(out[0], features)\n", "width = out.shape[1]\n", "height = out.shape[2]\n", "# display the different features as images\n", "plt.figure( figsize=( 14,10 ) )\n", "for i in range( features ):\n", "    plt.subplot(1, 5, i+1)\n", "    plt.tick_params( axis='both', which='both', bottom='off', left='off', labelbottom='off', labelleft='off' )\n", "    plt.imshow(img[i].reshape(width, height), cmap='gray')"], "outputs": [], "metadata": {}, "cell_type": "code"}, {"execution_count": null, "source": ["# Create a pooling layer\n", "#   a pooling layer is used to reduce the size of the inputs going forward in the network.\n", "#   with a convolutional layer the size has already risen by a factor of feature count.\n", "# this esssentially scales down all the images/features by a factor\n", "scale_factor = 2\n", "# add the pooling layer to the graph\n", "pool1 = tf.nn.max_pool( conv1, ksize=[1,scale_factor,scale_factor,1], strides=[1,scale_factor,scale_factor,1], padding='SAME' )"], "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code"}, {"execution_count": null, "source": ["# run the pooling layer\n", "out = sess.run(pool1, feed_dict={x:image})\n", "out.shape\n", "# the middle 2 dimensions have reduced by half"], "outputs": [], "metadata": {}, "cell_type": "code"}, {"execution_count": null, "source": ["# split the array to images with different features\n", "img = np.dsplit(out[0], features)\n", "width = out.shape[1]\n", "height = out.shape[2]\n", "# display the different features as images\n", "plt.figure( figsize=( 14,10 ) )\n", "for i in range( features ):\n", "    plt.subplot(1, 5, i+1)\n", "    plt.tick_params( axis='both', which='both', bottom='off', left='off', labelbottom='off', labelleft='off' )\n", "    plt.imshow(img[i].reshape(width, height), cmap='gray')"], "outputs": [], "metadata": {}, "cell_type": "code"}, {"execution_count": null, "source": ["# Most of the graphs attach another convolution and pooling layer\n", "# then the graph is attached to a fully connected layer"], "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code"}], "nbformat_minor": 1, "metadata": {"language_info": {"mimetype": "text/x-python", "pygments_lexer": "ipython3", "name": "python", "nbconvert_exporter": "python", "version": "3.6.3", "file_extension": ".py", "codemirror_mode": {"name": "ipython", "version": 3}}, "kernelspec": {"name": "python3", "language": "python", "display_name": "Python 3"}}}