├── .gitignore ├── README.md ├── LICENSE ├── eval.py ├── train.py ├── train_and_eval.py ├── mnist_cnn.py └── mnist_cnn.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .floydexpt 2 | .floydignore 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FloydHub quick start project 2 | [Website](http://www.floydhub.com) • [Docs](https://docs.floydhub.com) • [Forum](https://forum.floydhub.com) • [Twitter](https://twitter.com/floydhub_) • [We're Hiring](https://angel.co/floydhub) • [Write for FloydHub ✏️](https://blog.floydhub.com/write-for-floydhub) 3 | 4 | This is the example model training project used in FloydHub's [quick start guide](http://docs.floydhub.com/getstarted/quick_start). 5 | 6 | We will train a simple convolutional neural network (CNN) model to recognize handwritten digits using Tensorflow and the MNIST dataset. For more details on the data and the model, please refer to the [TensorFlow tutorial](https://www.tensorflow.org/get_started/mnist/pros). 7 | 8 | ### Credits 9 | 10 | The example code used here is derived from [aymericdamien/TensorFlow-Examples](https://github.com/aymericdamien/TensorFlow-Examples) 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 FloydHub 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /eval.py: -------------------------------------------------------------------------------- 1 | ''' 2 | A Convolutional Network implementation example using TensorFlow library. 3 | This example is using the MNIST database of handwritten digits 4 | (http://yann.lecun.com/exdb/mnist/) 5 | 6 | Author: Aymeric Damien 7 | Project: https://github.com/aymericdamien/TensorFlow-Examples/ 8 | ''' 9 | 10 | from __future__ import print_function 11 | 12 | import tensorflow as tf 13 | 14 | # Import MNIST data 15 | from tensorflow.examples.tutorials.mnist import input_data 16 | mnist = input_data.read_data_sets("/mnist", one_hot=True) 17 | 18 | # Launch the graph 19 | with tf.Session() as sess: 20 | tf.saved_model.loader.load(sess, ["EVALUATING"], "/model/cnn_model") 21 | # Retrieve placeholder tensor by name 22 | graph = tf.get_default_graph() 23 | x = graph.get_tensor_by_name("x:0") 24 | y = graph.get_tensor_by_name("y:0") 25 | accuracy = graph.get_tensor_by_name("accuracy:0") 26 | keep_prob = graph.get_tensor_by_name("keep_prob:0") 27 | 28 | # Calculate accuracy for 256 mnist test images 29 | print("Testing Accuracy:", \ 30 | sess.run(accuracy, feed_dict={x: mnist.test.images[:256], 31 | y: mnist.test.labels[:256], 32 | keep_prob: 1.})) 33 | -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | ''' 2 | A Convolutional Network implementation example using TensorFlow library. 3 | This example is using the MNIST database of handwritten digits 4 | (http://yann.lecun.com/exdb/mnist/) 5 | 6 | Author: Aymeric Damien 7 | Project: https://github.com/aymericdamien/TensorFlow-Examples/ 8 | ''' 9 | 10 | from __future__ import print_function 11 | 12 | import tensorflow as tf 13 | 14 | # Import MNIST data 15 | from tensorflow.examples.tutorials.mnist import input_data 16 | mnist = input_data.read_data_sets("/mnist", one_hot=True) 17 | 18 | # Parameters 19 | learning_rate = 0.001 20 | training_iters = 200000 21 | batch_size = 128 22 | display_step = 10 23 | 24 | # Network Parameters 25 | n_input = 784 # MNIST data input (img shape: 28*28) 26 | n_classes = 10 # MNIST total classes (0-9 digits) 27 | dropout = 0.75 # Dropout, probability to keep units 28 | 29 | # tf Graph input 30 | x = tf.placeholder(tf.float32, [None, n_input], name="x") 31 | y = tf.placeholder(tf.float32, [None, n_classes], name="y") 32 | keep_prob = tf.placeholder(tf.float32, name="keep_prob") #dropout (keep probability) 33 | 34 | 35 | # Create some wrappers for simplicity 36 | def conv2d(x, W, b, strides=1): 37 | # Conv2D wrapper, with bias and relu activation 38 | x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') 39 | x = tf.nn.bias_add(x, b) 40 | return tf.nn.relu(x) 41 | 42 | 43 | def maxpool2d(x, k=2): 44 | # MaxPool2D wrapper 45 | return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], 46 | padding='SAME') 47 | 48 | 49 | # Create model 50 | def conv_net(x, weights, biases, dropout): 51 | # Reshape input picture 52 | x = tf.reshape(x, shape=[-1, 28, 28, 1]) 53 | 54 | # Convolution Layer 55 | conv1 = conv2d(x, weights['wc1'], biases['bc1']) 56 | # Max Pooling (down-sampling) 57 | conv1 = maxpool2d(conv1, k=2) 58 | 59 | # Convolution Layer 60 | conv2 = conv2d(conv1, weights['wc2'], biases['bc2']) 61 | # Max Pooling (down-sampling) 62 | conv2 = maxpool2d(conv2, k=2) 63 | 64 | # Fully connected layer 65 | # Reshape conv2 output to fit fully connected layer input 66 | fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]]) 67 | fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) 68 | fc1 = tf.nn.relu(fc1) 69 | # Apply Dropout 70 | fc1 = tf.nn.dropout(fc1, dropout) 71 | 72 | # Output, class prediction 73 | out = tf.add(tf.matmul(fc1, weights['out']), biases['out']) 74 | return out 75 | 76 | # Store layers weight & bias 77 | weights = { 78 | # 5x5 conv, 1 input, 32 outputs 79 | 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), 80 | # 5x5 conv, 32 inputs, 64 outputs 81 | 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), 82 | # fully connected, 7*7*64 inputs, 1024 outputs 83 | 'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])), 84 | # 1024 inputs, 10 outputs (class prediction) 85 | 'out': tf.Variable(tf.random_normal([1024, n_classes])) 86 | } 87 | 88 | biases = { 89 | 'bc1': tf.Variable(tf.random_normal([32])), 90 | 'bc2': tf.Variable(tf.random_normal([64])), 91 | 'bd1': tf.Variable(tf.random_normal([1024])), 92 | 'out': tf.Variable(tf.random_normal([n_classes])) 93 | } 94 | 95 | # Construct model 96 | pred = conv_net(x, weights, biases, keep_prob) 97 | 98 | # Define loss and optimizer 99 | cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) 100 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 101 | 102 | # Evaluate model 103 | correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) 104 | accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name="accuracy") 105 | 106 | # Initializing the variables 107 | init = tf.global_variables_initializer() 108 | builder = tf.saved_model.builder.SavedModelBuilder("cnn_model") 109 | 110 | # Launch the graph 111 | with tf.Session() as sess: 112 | sess.run(init) 113 | step = 1 114 | # Keep training until reach max iterations 115 | while step * batch_size < training_iters: 116 | batch_x, batch_y = mnist.train.next_batch(batch_size) 117 | # Run optimization op (backprop) 118 | sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, 119 | keep_prob: dropout}) 120 | if step % display_step == 0: 121 | # Calculate batch loss and accuracy 122 | loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x, 123 | y: batch_y, 124 | keep_prob: 1.}) 125 | print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \ 126 | "{:.6f}".format(loss) + ", Training Accuracy= " + \ 127 | "{:.5f}".format(acc)) 128 | step += 1 129 | print("Optimization Finished!") 130 | 131 | builder.add_meta_graph_and_variables(sess, ["EVALUATING"]) 132 | builder.save() 133 | -------------------------------------------------------------------------------- /train_and_eval.py: -------------------------------------------------------------------------------- 1 | ''' 2 | A Convolutional Network implementation example using TensorFlow library. 3 | This example is using the MNIST database of handwritten digits 4 | (http://yann.lecun.com/exdb/mnist/) 5 | Author: Aymeric Damien 6 | Project: https://github.com/aymericdamien/TensorFlow-Examples/ 7 | ''' 8 | 9 | from __future__ import print_function 10 | 11 | import tensorflow as tf 12 | 13 | # Import MNIST data 14 | from tensorflow.examples.tutorials.mnist import input_data 15 | mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) 16 | 17 | # Parameters 18 | learning_rate = 0.001 19 | training_iters = 200000 20 | batch_size = 128 21 | display_step = 10 22 | 23 | # Network Parameters 24 | n_input = 784 # MNIST data input (img shape: 28*28) 25 | n_classes = 10 # MNIST total classes (0-9 digits) 26 | dropout = 0.75 # Dropout, probability to keep units 27 | 28 | # tf Graph input 29 | x = tf.placeholder(tf.float32, [None, n_input]) 30 | y = tf.placeholder(tf.float32, [None, n_classes]) 31 | keep_prob = tf.placeholder(tf.float32) #dropout (keep probability) 32 | 33 | 34 | # Create some wrappers for simplicity 35 | def conv2d(x, W, b, strides=1): 36 | # Conv2D wrapper, with bias and relu activation 37 | x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') 38 | x = tf.nn.bias_add(x, b) 39 | return tf.nn.relu(x) 40 | 41 | 42 | def maxpool2d(x, k=2): 43 | # MaxPool2D wrapper 44 | return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], 45 | padding='SAME') 46 | 47 | 48 | # Create model 49 | def conv_net(x, weights, biases, dropout): 50 | # Reshape input picture 51 | x = tf.reshape(x, shape=[-1, 28, 28, 1]) 52 | 53 | # Convolution Layer 54 | conv1 = conv2d(x, weights['wc1'], biases['bc1']) 55 | # Max Pooling (down-sampling) 56 | conv1 = maxpool2d(conv1, k=2) 57 | 58 | # Convolution Layer 59 | conv2 = conv2d(conv1, weights['wc2'], biases['bc2']) 60 | # Max Pooling (down-sampling) 61 | conv2 = maxpool2d(conv2, k=2) 62 | 63 | # Fully connected layer 64 | # Reshape conv2 output to fit fully connected layer input 65 | fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]]) 66 | fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) 67 | fc1 = tf.nn.relu(fc1) 68 | # Apply Dropout 69 | fc1 = tf.nn.dropout(fc1, dropout) 70 | 71 | # Output, class prediction 72 | out = tf.add(tf.matmul(fc1, weights['out']), biases['out']) 73 | return out 74 | 75 | # Store layers weight & bias 76 | weights = { 77 | # 5x5 conv, 1 input, 32 outputs 78 | 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), 79 | # 5x5 conv, 32 inputs, 64 outputs 80 | 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), 81 | # fully connected, 7*7*64 inputs, 1024 outputs 82 | 'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])), 83 | # 1024 inputs, 10 outputs (class prediction) 84 | 'out': tf.Variable(tf.random_normal([1024, n_classes])) 85 | } 86 | 87 | biases = { 88 | 'bc1': tf.Variable(tf.random_normal([32])), 89 | 'bc2': tf.Variable(tf.random_normal([64])), 90 | 'bd1': tf.Variable(tf.random_normal([1024])), 91 | 'out': tf.Variable(tf.random_normal([n_classes])) 92 | } 93 | 94 | # Construct model 95 | pred = conv_net(x, weights, biases, keep_prob) 96 | 97 | # Define loss and optimizer 98 | cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) 99 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 100 | 101 | # Evaluate model 102 | correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) 103 | accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) 104 | 105 | # Initializing the variables 106 | init = tf.global_variables_initializer() 107 | 108 | # Launch the graph 109 | with tf.Session() as sess: 110 | sess.run(init) 111 | step = 1 112 | # Keep training until reach max iterations 113 | while step * batch_size < training_iters: 114 | batch_x, batch_y = mnist.train.next_batch(batch_size) 115 | # Run optimization op (backprop) 116 | sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, 117 | keep_prob: dropout}) 118 | if step % display_step == 0: 119 | # Calculate batch loss and accuracy 120 | loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x, 121 | y: batch_y, 122 | keep_prob: 1.}) 123 | print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \ 124 | "{:.6f}".format(loss) + ", Training Accuracy= " + \ 125 | "{:.5f}".format(acc)) 126 | step += 1 127 | print("Optimization Finished!") 128 | 129 | # Calculate accuracy for 256 mnist test images 130 | print("Testing Accuracy:", \ 131 | sess.run(accuracy, feed_dict={x: mnist.test.images[:256], 132 | y: mnist.test.labels[:256], 133 | keep_prob: 1.})) 134 | -------------------------------------------------------------------------------- /mnist_cnn.py: -------------------------------------------------------------------------------- 1 | ''' 2 | A Convolutional Network implementation example using TensorFlow library. 3 | This example is using the MNIST database of handwritten digits 4 | (http://yann.lecun.com/exdb/mnist/) 5 | Author: Aymeric Damien 6 | Project: https://github.com/aymericdamien/TensorFlow-Examples/ 7 | ''' 8 | 9 | from __future__ import print_function 10 | import sys 11 | import time 12 | 13 | import tensorflow as tf 14 | 15 | # patch older version of tensorflow to use new download mirror 16 | if tf.__version__.startswith('0.12') or tf.__version__.startswith('1.0'): 17 | import tensorflow.contrib.learn.python.learn.datasets.mnist 18 | tensorflow.contrib.learn.datasets.mnist.SOURCE_URL = 'https://storage.googleapis.com/cvdf-datasets/mnist/' 19 | # Import MNIST data 20 | from tensorflow.examples.tutorials.mnist import input_data 21 | mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) 22 | 23 | # Parameters 24 | learning_rate = 0.001 25 | training_iters = 200000 26 | batch_size = 128 27 | display_step = 10 28 | 29 | # Network Parameters 30 | n_input = 784 # MNIST data input (img shape: 28*28) 31 | n_classes = 10 # MNIST total classes (0-9 digits) 32 | dropout = 0.75 # Dropout, probability to keep units 33 | 34 | # tf Graph input 35 | x = tf.placeholder(tf.float32, [None, n_input]) 36 | y = tf.placeholder(tf.float32, [None, n_classes]) 37 | keep_prob = tf.placeholder(tf.float32) # dropout (keep probability) 38 | 39 | 40 | # Create some wrappers for simplicity 41 | def conv2d(x, W, b, strides=1): 42 | # Conv2D wrapper, with bias and relu activation 43 | x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') 44 | x = tf.nn.bias_add(x, b) 45 | return tf.nn.relu(x) 46 | 47 | 48 | def maxpool2d(x, k=2): 49 | # MaxPool2D wrapper 50 | return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], 51 | padding='SAME') 52 | 53 | 54 | # Create model 55 | def conv_net(x, weights, biases, dropout): 56 | # Reshape input picture 57 | x = tf.reshape(x, shape=[-1, 28, 28, 1]) 58 | 59 | # Convolution Layer 60 | conv1 = conv2d(x, weights['wc1'], biases['bc1']) 61 | # Max Pooling (down-sampling) 62 | conv1 = maxpool2d(conv1, k=2) 63 | 64 | # Convolution Layer 65 | conv2 = conv2d(conv1, weights['wc2'], biases['bc2']) 66 | # Max Pooling (down-sampling) 67 | conv2 = maxpool2d(conv2, k=2) 68 | 69 | # Fully connected layer 70 | # Reshape conv2 output to fit fully connected layer input 71 | fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]]) 72 | fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) 73 | fc1 = tf.nn.relu(fc1) 74 | # Apply Dropout 75 | fc1 = tf.nn.dropout(fc1, dropout) 76 | 77 | # Output, class prediction 78 | out = tf.add(tf.matmul(fc1, weights['out']), biases['out']) 79 | return out 80 | 81 | # Store layers weight & bias 82 | weights = { 83 | # 5x5 conv, 1 input, 32 outputs 84 | 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), 85 | # 5x5 conv, 32 inputs, 64 outputs 86 | 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), 87 | # fully connected, 7*7*64 inputs, 1024 outputs 88 | 'wd1': tf.Variable(tf.random_normal([7 * 7 * 64, 1024])), 89 | # 1024 inputs, 10 outputs (class prediction) 90 | 'out': tf.Variable(tf.random_normal([1024, n_classes])) 91 | } 92 | 93 | biases = { 94 | 'bc1': tf.Variable(tf.random_normal([32])), 95 | 'bc2': tf.Variable(tf.random_normal([64])), 96 | 'bd1': tf.Variable(tf.random_normal([1024])), 97 | 'out': tf.Variable(tf.random_normal([n_classes])) 98 | } 99 | 100 | # Construct model 101 | pred = conv_net(x, weights, biases, keep_prob) 102 | 103 | # Define loss and optimizer 104 | cost = tf.reduce_mean( 105 | tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) 106 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 107 | 108 | # Evaluate model 109 | correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) 110 | accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) 111 | 112 | # Initializing the variables 113 | init = tf.global_variables_initializer() 114 | 115 | # Launch the graph 116 | with tf.Session() as sess: 117 | sess.run(init) 118 | step = 1 119 | # Keep training until reach max iterations 120 | time1 = time.time() 121 | while step * batch_size < training_iters: 122 | batch_x, batch_y = mnist.train.next_batch(batch_size) 123 | # Run optimization op (backprop) 124 | sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, 125 | keep_prob: dropout}) 126 | if step % display_step == 0: 127 | # Calculate batch loss and accuracy 128 | loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x, 129 | y: batch_y, 130 | keep_prob: 1.}) 131 | print("Iter " + str(step * batch_size) + ", Minibatch Loss= " + 132 | "{:.6f}".format(loss) + ", Training Accuracy= " + 133 | "{:.5f}".format(acc)) 134 | sys.stdout.flush() 135 | if acc >= 0.95: 136 | break 137 | step += 1 138 | time2 = time.time() 139 | print("Optimization Finished!") 140 | print("Time elapsed: ", time2 - time1) 141 | 142 | # Calculate accuracy for 256 mnist test images 143 | print("Testing Accuracy:", 144 | sess.run(accuracy, 145 | feed_dict={x: mnist.test.images[:256], 146 | y: mnist.test.labels[:256], 147 | keep_prob: 1.})) 148 | -------------------------------------------------------------------------------- /mnist_cnn.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "'''\n", 12 | "A Convolutional Network implementation example using TensorFlow library.\n", 13 | "This example is using the MNIST database of handwritten digits\n", 14 | "(http://yann.lecun.com/exdb/mnist/)\n", 15 | "\n", 16 | "Author: Aymeric Damien\n", 17 | "Project: https://github.com/aymericdamien/TensorFlow-Examples/\n", 18 | "'''" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [], 28 | "source": [ 29 | "import tensorflow as tf\n", 30 | "\n", 31 | "# Import MNIST data\n", 32 | "from tensorflow.examples.tutorials.mnist import input_data\n", 33 | "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": { 40 | "collapsed": true 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "# Parameters\n", 45 | "learning_rate = 0.001\n", 46 | "training_iters = 200000\n", 47 | "batch_size = 128\n", 48 | "display_step = 10\n", 49 | "\n", 50 | "# Network Parameters\n", 51 | "n_input = 784 # MNIST data input (img shape: 28*28)\n", 52 | "n_classes = 10 # MNIST total classes (0-9 digits)\n", 53 | "dropout = 0.75 # Dropout, probability to keep units\n", 54 | "\n", 55 | "# tf Graph input\n", 56 | "x = tf.placeholder(tf.float32, [None, n_input])\n", 57 | "y = tf.placeholder(tf.float32, [None, n_classes])\n", 58 | "keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 3, 64 | "metadata": { 65 | "collapsed": true 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "# Create some wrappers for simplicity\n", 70 | "def conv2d(x, W, b, strides=1):\n", 71 | " # Conv2D wrapper, with bias and relu activation\n", 72 | " x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')\n", 73 | " x = tf.nn.bias_add(x, b)\n", 74 | " return tf.nn.relu(x)\n", 75 | "\n", 76 | "\n", 77 | "def maxpool2d(x, k=2):\n", 78 | " # MaxPool2D wrapper\n", 79 | " return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],\n", 80 | " padding='SAME')\n", 81 | "\n", 82 | "\n", 83 | "# Create model\n", 84 | "def conv_net(x, weights, biases, dropout):\n", 85 | " # Reshape input picture\n", 86 | " x = tf.reshape(x, shape=[-1, 28, 28, 1])\n", 87 | "\n", 88 | " # Convolution Layer\n", 89 | " conv1 = conv2d(x, weights['wc1'], biases['bc1'])\n", 90 | " # Max Pooling (down-sampling)\n", 91 | " conv1 = maxpool2d(conv1, k=2)\n", 92 | "\n", 93 | " # Convolution Layer\n", 94 | " conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])\n", 95 | " # Max Pooling (down-sampling)\n", 96 | " conv2 = maxpool2d(conv2, k=2)\n", 97 | "\n", 98 | " # Fully connected layer\n", 99 | " # Reshape conv2 output to fit fully connected layer input\n", 100 | " fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])\n", 101 | " fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])\n", 102 | " fc1 = tf.nn.relu(fc1)\n", 103 | " # Apply Dropout\n", 104 | " fc1 = tf.nn.dropout(fc1, dropout)\n", 105 | "\n", 106 | " # Output, class prediction\n", 107 | " out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])\n", 108 | " return out" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 4, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "# Store layers weight & bias\n", 120 | "weights = {\n", 121 | " # 5x5 conv, 1 input, 32 outputs\n", 122 | " 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),\n", 123 | " # 5x5 conv, 32 inputs, 64 outputs\n", 124 | " 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),\n", 125 | " # fully connected, 7*7*64 inputs, 1024 outputs\n", 126 | " 'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),\n", 127 | " # 1024 inputs, 10 outputs (class prediction)\n", 128 | " 'out': tf.Variable(tf.random_normal([1024, n_classes]))\n", 129 | "}\n", 130 | "\n", 131 | "biases = {\n", 132 | " 'bc1': tf.Variable(tf.random_normal([32])),\n", 133 | " 'bc2': tf.Variable(tf.random_normal([64])),\n", 134 | " 'bd1': tf.Variable(tf.random_normal([1024])),\n", 135 | " 'out': tf.Variable(tf.random_normal([n_classes]))\n", 136 | "}\n", 137 | "\n", 138 | "# Construct model\n", 139 | "pred = conv_net(x, weights, biases, keep_prob)\n", 140 | "\n", 141 | "# Define loss and optimizer\n", 142 | "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))\n", 143 | "optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)\n", 144 | "\n", 145 | "# Evaluate model\n", 146 | "correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n", 147 | "accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))\n", 148 | "\n", 149 | "# Initializing the variables\n", 150 | "init = tf.global_variables_initializer()" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 5, 156 | "metadata": { 157 | "collapsed": false 158 | }, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "Iter 1280, Minibatch Loss= 26574.855469, Training Accuracy= 0.25781\n", 165 | "Iter 2560, Minibatch Loss= 11454.494141, Training Accuracy= 0.49219\n", 166 | "Iter 3840, Minibatch Loss= 10070.515625, Training Accuracy= 0.55469\n", 167 | "Iter 5120, Minibatch Loss= 4008.586426, Training Accuracy= 0.78125\n", 168 | "Iter 6400, Minibatch Loss= 3148.004639, Training Accuracy= 0.80469\n", 169 | "Iter 7680, Minibatch Loss= 6740.440430, Training Accuracy= 0.71875\n", 170 | "Iter 8960, Minibatch Loss= 4103.991699, Training Accuracy= 0.80469\n", 171 | "Iter 10240, Minibatch Loss= 2631.275391, Training Accuracy= 0.85938\n", 172 | "Iter 11520, Minibatch Loss= 1428.798828, Training Accuracy= 0.91406\n", 173 | "Iter 12800, Minibatch Loss= 3909.772705, Training Accuracy= 0.78906\n", 174 | "Iter 14080, Minibatch Loss= 1423.095947, Training Accuracy= 0.88281\n", 175 | "Iter 15360, Minibatch Loss= 1524.569824, Training Accuracy= 0.89062\n", 176 | "Iter 16640, Minibatch Loss= 2234.539795, Training Accuracy= 0.86719\n", 177 | "Iter 17920, Minibatch Loss= 933.932800, Training Accuracy= 0.90625\n", 178 | "Iter 19200, Minibatch Loss= 2039.046021, Training Accuracy= 0.89062\n", 179 | "Iter 20480, Minibatch Loss= 674.179932, Training Accuracy= 0.95312\n", 180 | "Iter 21760, Minibatch Loss= 3778.958984, Training Accuracy= 0.82812\n", 181 | "Iter 23040, Minibatch Loss= 1038.217773, Training Accuracy= 0.91406\n", 182 | "Iter 24320, Minibatch Loss= 1689.513672, Training Accuracy= 0.89062\n", 183 | "Iter 25600, Minibatch Loss= 1800.954956, Training Accuracy= 0.85938\n", 184 | "Iter 26880, Minibatch Loss= 1086.292847, Training Accuracy= 0.90625\n", 185 | "Iter 28160, Minibatch Loss= 656.042847, Training Accuracy= 0.94531\n", 186 | "Iter 29440, Minibatch Loss= 1210.589844, Training Accuracy= 0.91406\n", 187 | "Iter 30720, Minibatch Loss= 1099.606323, Training Accuracy= 0.90625\n", 188 | "Iter 32000, Minibatch Loss= 1073.128174, Training Accuracy= 0.92969\n", 189 | "Iter 33280, Minibatch Loss= 518.844543, Training Accuracy= 0.95312\n", 190 | "Iter 34560, Minibatch Loss= 540.856689, Training Accuracy= 0.92188\n", 191 | "Iter 35840, Minibatch Loss= 353.990906, Training Accuracy= 0.97656\n", 192 | "Iter 37120, Minibatch Loss= 1488.962891, Training Accuracy= 0.91406\n", 193 | "Iter 38400, Minibatch Loss= 231.191864, Training Accuracy= 0.98438\n", 194 | "Iter 39680, Minibatch Loss= 171.154480, Training Accuracy= 0.98438\n", 195 | "Iter 40960, Minibatch Loss= 2092.023682, Training Accuracy= 0.90625\n", 196 | "Iter 42240, Minibatch Loss= 480.594299, Training Accuracy= 0.95312\n", 197 | "Iter 43520, Minibatch Loss= 504.128143, Training Accuracy= 0.96875\n", 198 | "Iter 44800, Minibatch Loss= 143.534485, Training Accuracy= 0.97656\n", 199 | "Iter 46080, Minibatch Loss= 325.875580, Training Accuracy= 0.96094\n", 200 | "Iter 47360, Minibatch Loss= 602.813049, Training Accuracy= 0.91406\n", 201 | "Iter 48640, Minibatch Loss= 794.595093, Training Accuracy= 0.94531\n", 202 | "Iter 49920, Minibatch Loss= 415.539032, Training Accuracy= 0.95312\n", 203 | "Iter 51200, Minibatch Loss= 146.016022, Training Accuracy= 0.96094\n", 204 | "Iter 52480, Minibatch Loss= 294.180786, Training Accuracy= 0.94531\n", 205 | "Iter 53760, Minibatch Loss= 50.955730, Training Accuracy= 0.99219\n", 206 | "Iter 55040, Minibatch Loss= 1026.607056, Training Accuracy= 0.92188\n", 207 | "Iter 56320, Minibatch Loss= 283.756134, Training Accuracy= 0.96875\n", 208 | "Iter 57600, Minibatch Loss= 691.538208, Training Accuracy= 0.95312\n", 209 | "Iter 58880, Minibatch Loss= 491.075073, Training Accuracy= 0.96094\n", 210 | "Iter 60160, Minibatch Loss= 571.951660, Training Accuracy= 0.95312\n", 211 | "Iter 61440, Minibatch Loss= 284.041168, Training Accuracy= 0.97656\n", 212 | "Iter 62720, Minibatch Loss= 1041.941528, Training Accuracy= 0.92969\n", 213 | "Iter 64000, Minibatch Loss= 664.833923, Training Accuracy= 0.93750\n", 214 | "Iter 65280, Minibatch Loss= 1582.112793, Training Accuracy= 0.88281\n", 215 | "Iter 66560, Minibatch Loss= 783.135376, Training Accuracy= 0.94531\n", 216 | "Iter 67840, Minibatch Loss= 245.942398, Training Accuracy= 0.96094\n", 217 | "Iter 69120, Minibatch Loss= 752.858948, Training Accuracy= 0.96875\n", 218 | "Iter 70400, Minibatch Loss= 623.243286, Training Accuracy= 0.94531\n", 219 | "Iter 71680, Minibatch Loss= 846.498230, Training Accuracy= 0.93750\n", 220 | "Iter 72960, Minibatch Loss= 586.516479, Training Accuracy= 0.95312\n", 221 | "Iter 74240, Minibatch Loss= 92.774963, Training Accuracy= 0.98438\n", 222 | "Iter 75520, Minibatch Loss= 644.039612, Training Accuracy= 0.95312\n", 223 | "Iter 76800, Minibatch Loss= 693.247681, Training Accuracy= 0.96094\n", 224 | "Iter 78080, Minibatch Loss= 466.491882, Training Accuracy= 0.96094\n", 225 | "Iter 79360, Minibatch Loss= 964.212341, Training Accuracy= 0.93750\n", 226 | "Iter 80640, Minibatch Loss= 230.451904, Training Accuracy= 0.97656\n", 227 | "Iter 81920, Minibatch Loss= 280.434570, Training Accuracy= 0.95312\n", 228 | "Iter 83200, Minibatch Loss= 213.208252, Training Accuracy= 0.97656\n", 229 | "Iter 84480, Minibatch Loss= 774.836060, Training Accuracy= 0.94531\n", 230 | "Iter 85760, Minibatch Loss= 164.687729, Training Accuracy= 0.96094\n", 231 | "Iter 87040, Minibatch Loss= 419.967407, Training Accuracy= 0.96875\n", 232 | "Iter 88320, Minibatch Loss= 160.920151, Training Accuracy= 0.96875\n", 233 | "Iter 89600, Minibatch Loss= 586.063599, Training Accuracy= 0.96094\n", 234 | "Iter 90880, Minibatch Loss= 345.598145, Training Accuracy= 0.96875\n", 235 | "Iter 92160, Minibatch Loss= 931.361145, Training Accuracy= 0.92188\n", 236 | "Iter 93440, Minibatch Loss= 170.107117, Training Accuracy= 0.97656\n", 237 | "Iter 94720, Minibatch Loss= 497.162750, Training Accuracy= 0.93750\n", 238 | "Iter 96000, Minibatch Loss= 906.600464, Training Accuracy= 0.94531\n", 239 | "Iter 97280, Minibatch Loss= 303.382202, Training Accuracy= 0.92969\n", 240 | "Iter 98560, Minibatch Loss= 509.161652, Training Accuracy= 0.97656\n", 241 | "Iter 99840, Minibatch Loss= 359.561981, Training Accuracy= 0.97656\n", 242 | "Iter 101120, Minibatch Loss= 136.516541, Training Accuracy= 0.97656\n", 243 | "Iter 102400, Minibatch Loss= 517.199341, Training Accuracy= 0.96875\n", 244 | "Iter 103680, Minibatch Loss= 487.793335, Training Accuracy= 0.95312\n", 245 | "Iter 104960, Minibatch Loss= 407.351929, Training Accuracy= 0.96094\n", 246 | "Iter 106240, Minibatch Loss= 70.495193, Training Accuracy= 0.98438\n", 247 | "Iter 107520, Minibatch Loss= 344.783508, Training Accuracy= 0.96094\n", 248 | "Iter 108800, Minibatch Loss= 242.682465, Training Accuracy= 0.95312\n", 249 | "Iter 110080, Minibatch Loss= 169.181458, Training Accuracy= 0.96094\n", 250 | "Iter 111360, Minibatch Loss= 152.638245, Training Accuracy= 0.98438\n", 251 | "Iter 112640, Minibatch Loss= 170.795868, Training Accuracy= 0.96875\n", 252 | "Iter 113920, Minibatch Loss= 133.262726, Training Accuracy= 0.98438\n", 253 | "Iter 115200, Minibatch Loss= 296.063293, Training Accuracy= 0.95312\n", 254 | "Iter 116480, Minibatch Loss= 254.247543, Training Accuracy= 0.96094\n", 255 | "Iter 117760, Minibatch Loss= 506.795715, Training Accuracy= 0.94531\n", 256 | "Iter 119040, Minibatch Loss= 446.006897, Training Accuracy= 0.96094\n", 257 | "Iter 120320, Minibatch Loss= 149.467377, Training Accuracy= 0.97656\n", 258 | "Iter 121600, Minibatch Loss= 52.783600, Training Accuracy= 0.98438\n", 259 | "Iter 122880, Minibatch Loss= 49.041794, Training Accuracy= 0.98438\n", 260 | "Iter 124160, Minibatch Loss= 184.371246, Training Accuracy= 0.97656\n", 261 | "Iter 125440, Minibatch Loss= 129.838501, Training Accuracy= 0.97656\n", 262 | "Iter 126720, Minibatch Loss= 288.006531, Training Accuracy= 0.96875\n", 263 | "Iter 128000, Minibatch Loss= 187.284653, Training Accuracy= 0.97656\n", 264 | "Iter 129280, Minibatch Loss= 197.969955, Training Accuracy= 0.96875\n", 265 | "Iter 130560, Minibatch Loss= 299.969818, Training Accuracy= 0.96875\n", 266 | "Iter 131840, Minibatch Loss= 537.602173, Training Accuracy= 0.96094\n", 267 | "Iter 133120, Minibatch Loss= 4.519302, Training Accuracy= 0.99219\n", 268 | "Iter 134400, Minibatch Loss= 133.264191, Training Accuracy= 0.97656\n", 269 | "Iter 135680, Minibatch Loss= 89.662292, Training Accuracy= 0.97656\n", 270 | "Iter 136960, Minibatch Loss= 107.774078, Training Accuracy= 0.96875\n", 271 | "Iter 138240, Minibatch Loss= 335.904572, Training Accuracy= 0.96094\n", 272 | "Iter 139520, Minibatch Loss= 457.494568, Training Accuracy= 0.96094\n", 273 | "Iter 140800, Minibatch Loss= 259.131531, Training Accuracy= 0.95312\n", 274 | "Iter 142080, Minibatch Loss= 152.205383, Training Accuracy= 0.96094\n", 275 | "Iter 143360, Minibatch Loss= 252.535828, Training Accuracy= 0.95312\n", 276 | "Iter 144640, Minibatch Loss= 109.477585, Training Accuracy= 0.96875\n", 277 | "Iter 145920, Minibatch Loss= 24.468613, Training Accuracy= 0.99219\n", 278 | "Iter 147200, Minibatch Loss= 51.722107, Training Accuracy= 0.97656\n", 279 | "Iter 148480, Minibatch Loss= 69.715233, Training Accuracy= 0.97656\n", 280 | "Iter 149760, Minibatch Loss= 405.289246, Training Accuracy= 0.92969\n", 281 | "Iter 151040, Minibatch Loss= 282.976379, Training Accuracy= 0.95312\n", 282 | "Iter 152320, Minibatch Loss= 134.991119, Training Accuracy= 0.97656\n", 283 | "Iter 153600, Minibatch Loss= 491.618103, Training Accuracy= 0.92188\n", 284 | "Iter 154880, Minibatch Loss= 154.299988, Training Accuracy= 0.99219\n", 285 | "Iter 156160, Minibatch Loss= 79.480019, Training Accuracy= 0.96875\n", 286 | "Iter 157440, Minibatch Loss= 68.093750, Training Accuracy= 0.99219\n", 287 | "Iter 158720, Minibatch Loss= 459.739685, Training Accuracy= 0.92188\n", 288 | "Iter 160000, Minibatch Loss= 168.076843, Training Accuracy= 0.94531\n", 289 | "Iter 161280, Minibatch Loss= 256.141846, Training Accuracy= 0.97656\n", 290 | "Iter 162560, Minibatch Loss= 236.400391, Training Accuracy= 0.94531\n", 291 | "Iter 163840, Minibatch Loss= 177.011261, Training Accuracy= 0.96875\n", 292 | "Iter 165120, Minibatch Loss= 48.583298, Training Accuracy= 0.97656\n", 293 | "Iter 166400, Minibatch Loss= 413.800293, Training Accuracy= 0.96094\n", 294 | "Iter 167680, Minibatch Loss= 209.587387, Training Accuracy= 0.96875\n", 295 | "Iter 168960, Minibatch Loss= 239.407318, Training Accuracy= 0.98438\n", 296 | "Iter 170240, Minibatch Loss= 183.567017, Training Accuracy= 0.96875\n", 297 | "Iter 171520, Minibatch Loss= 87.937515, Training Accuracy= 0.96875\n", 298 | "Iter 172800, Minibatch Loss= 203.777039, Training Accuracy= 0.98438\n", 299 | "Iter 174080, Minibatch Loss= 566.378052, Training Accuracy= 0.94531\n", 300 | "Iter 175360, Minibatch Loss= 325.170898, Training Accuracy= 0.95312\n", 301 | "Iter 176640, Minibatch Loss= 300.142212, Training Accuracy= 0.97656\n", 302 | "Iter 177920, Minibatch Loss= 205.370193, Training Accuracy= 0.95312\n", 303 | "Iter 179200, Minibatch Loss= 5.594437, Training Accuracy= 0.99219\n", 304 | "Iter 180480, Minibatch Loss= 110.732109, Training Accuracy= 0.98438\n", 305 | "Iter 181760, Minibatch Loss= 33.320297, Training Accuracy= 0.99219\n", 306 | "Iter 183040, Minibatch Loss= 6.885544, Training Accuracy= 0.99219\n", 307 | "Iter 184320, Minibatch Loss= 221.144806, Training Accuracy= 0.96875\n", 308 | "Iter 185600, Minibatch Loss= 365.337372, Training Accuracy= 0.94531\n", 309 | "Iter 186880, Minibatch Loss= 186.558258, Training Accuracy= 0.96094\n", 310 | "Iter 188160, Minibatch Loss= 149.720322, Training Accuracy= 0.98438\n", 311 | "Iter 189440, Minibatch Loss= 105.281998, Training Accuracy= 0.97656\n", 312 | "Iter 190720, Minibatch Loss= 289.980011, Training Accuracy= 0.96094\n", 313 | "Iter 192000, Minibatch Loss= 214.382278, Training Accuracy= 0.96094\n", 314 | "Iter 193280, Minibatch Loss= 461.044312, Training Accuracy= 0.93750\n", 315 | "Iter 194560, Minibatch Loss= 138.653076, Training Accuracy= 0.98438\n", 316 | "Iter 195840, Minibatch Loss= 112.004883, Training Accuracy= 0.98438\n", 317 | "Iter 197120, Minibatch Loss= 212.691467, Training Accuracy= 0.97656\n", 318 | "Iter 198400, Minibatch Loss= 57.642502, Training Accuracy= 0.97656\n", 319 | "Iter 199680, Minibatch Loss= 80.503563, Training Accuracy= 0.96875\n", 320 | "Optimization Finished!\n", 321 | "Testing Accuracy: 0.984375\n" 322 | ] 323 | } 324 | ], 325 | "source": [ 326 | "# Launch the graph\n", 327 | "with tf.Session() as sess:\n", 328 | " sess.run(init)\n", 329 | " step = 1\n", 330 | " # Keep training until reach max iterations\n", 331 | " while step * batch_size < training_iters:\n", 332 | " batch_x, batch_y = mnist.train.next_batch(batch_size)\n", 333 | " # Run optimization op (backprop)\n", 334 | " sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,\n", 335 | " keep_prob: dropout})\n", 336 | " if step % display_step == 0:\n", 337 | " # Calculate batch loss and accuracy\n", 338 | " loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,\n", 339 | " y: batch_y,\n", 340 | " keep_prob: 1.})\n", 341 | " print(\"Iter \" + str(step*batch_size) + \", Minibatch Loss= \" + \\\n", 342 | " \"{:.6f}\".format(loss) + \", Training Accuracy= \" + \\\n", 343 | " \"{:.5f}\".format(acc))\n", 344 | " step += 1\n", 345 | " print(\"Optimization Finished!\")\n", 346 | "\n", 347 | " # Calculate accuracy for 256 mnist test images\n", 348 | " print(\"Testing Accuracy:\", \\\n", 349 | " sess.run(accuracy, feed_dict={x: mnist.test.images[:256],\n", 350 | " y: mnist.test.labels[:256],\n", 351 | " keep_prob: 1.}))" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": null, 357 | "metadata": { 358 | "collapsed": true 359 | }, 360 | "outputs": [], 361 | "source": [] 362 | } 363 | ], 364 | "metadata": { 365 | "kernelspec": { 366 | "display_name": "Python 2", 367 | "language": "python", 368 | "name": "python2" 369 | }, 370 | "language_info": { 371 | "codemirror_mode": { 372 | "name": "ipython", 373 | "version": 2 374 | }, 375 | "file_extension": ".py", 376 | "mimetype": "text/x-python", 377 | "name": "python", 378 | "nbconvert_exporter": "python", 379 | "pygments_lexer": "ipython2", 380 | "version": "2.7.13" 381 | } 382 | }, 383 | "nbformat": 4, 384 | "nbformat_minor": 0 385 | } 386 | --------------------------------------------------------------------------------