# coding: utf-8 """ Convolutional Neural Network (CNN) with MNIST """ import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # Training Data mnist = input_data.read_data_sets('data/', one_hot=True) trainimg = mnist.train.images trainlabel = mnist.train.labels testimg = mnist.test.images testlabel = mnist.test.labels # Define convolutional neural network architecture # Parameters learning_rate = 0.001 training_epochs = 5 batch_size = 100 display_step = 1 # Network n_input = 784 n_output = 10 weights = { # 3x3 filters of 1 depth(black and white) and 64 of them. 'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64], stddev=0.1)), 'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.1)), 'wd1': tf.Variable(tf.random_normal([7*7*128, 1024], stddev=0.1)), 'wd2': tf.Variable(tf.random_normal([1024, n_output], stddev=0.1)) } biases = { 'bc1': tf.Variable(tf.random_normal([64], stddev=0.1)), 'bc2': tf.Variable(tf.random_normal([128], stddev=0.1)), 'bd1': tf.Variable(tf.random_normal([1024], stddev=0.1)), 'bd2': tf.Variable(tf.random_normal([n_output], stddev=0.1)) } def conv_basic(_input, _w, _b, _keepratio): """Defines the conv+pool + fully connected layers arg: _input: the input _w: the filter size or weight _b: bias _keepratio: dropout probability return: out: a dictionary containing the output of each of the layers. """ # Input [batch,h,w,channel] _input_r = tf.reshape(_input, shape=[-1, 28, 28, 1]) # this is a useful node that allows you to see how variables are being changed # _input_r = tf.Print(_input_r, [tf.shape(_input_r)], message = "This is _input_r: ", summarize=4) # input_shape = tf.shape(_input_r) # Conv1 # SAME padding rounds up, meaning the partial windows are included. _conv1 = tf.nn.relu(tf.nn.bias_add( tf.nn.conv2d(_input_r, _w['wc1'], strides=[1, 1, 1, 1], padding='SAME') , _b['bc1'])) # Pool1 using a kernel of 2x2x1 where the F = 2 for width and height, and depth = input_depth 1, # in this case. S = 2 is chosen. A -1x14x14x1 will be outputted. _pool1 = tf.nn.max_pool(_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') _pool_dr1 = tf.nn.dropout(_pool1, _keepratio) # _pool_dr1 shape is [100,14,14,64] input_shape = tf.shape(_pool_dr1) # Conv2 _conv2 = tf.nn.relu(tf.nn.bias_add( tf.nn.conv2d(_pool_dr1, _w['wc2'], strides=[1, 1, 1, 1], padding='SAME') , _b['bc2'])) _pool2 = tf.nn.max_pool(_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') _pool_dr2 = tf.nn.dropout(_pool2, _keepratio) # Vectorize to match the FC weight's dim _dense1 = tf.reshape(_pool_dr2, [-1, _w['wd1'].get_shape().as_list()[0]]) # Fc1 _fc1 = tf.nn.relu(tf.add(tf.matmul(_dense1, _w['wd1']), _b['bd1'])) _fc_dr1 = tf.nn.dropout(_fc1, _keepratio) # Fc2 _out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2']) # Return everything out = { 'input_r': _input_r, 'input_shape': input_shape, 'conv1': _conv1, 'pool1': _pool1, 'pool1_dr1': _pool_dr1, 'conv2': _conv2, 'pool2': _pool2, 'pool_dr2': _pool_dr2, 'dense1': _dense1, 'fc1': _fc1, 'fc_dr1': _fc_dr1, 'out': _out } return out # tf Graph input x = tf.placeholder(tf.float32, [None, n_input]) y = tf.placeholder(tf.float32, [None, n_output]) keepratio = tf.placeholder(tf.float32) # Functions! conv_dict = conv_basic(x, weights, biases, keepratio) _pred = conv_dict['out'] re_input = conv_dict['input_shape'] # _pred contains the 'unscaled' log probabilities. or jsut output of last layer. # compute the mean of the prediction cost on the whole batch. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(_pred, y)) optm = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) _corr = tf.equal(tf.argmax(_pred,1), tf.argmax(y,1)) # Count corrects accr = tf.reduce_mean(tf.cast(_corr, tf.float32)) # Accuracy init = tf.initialize_all_variables() # Saver save_step = 1; saver = tf.train.Saver(max_to_keep=training_epochs) print ("Network Ready to Go!") do_train = 1 sess = tf.Session() sess.run(init) if do_train == 1: for epoch in range(training_epochs): avg_cost = 0. total_batch = int(mnist.train.num_examples/batch_size) # Loop over all batches for i in range(total_batch): batch_xs, batch_ys = mnist.train.next_batch(batch_size) # Feed training using batch data sess.run(optm, feed_dict={x: batch_xs, y: batch_ys, keepratio:0.7}) # Compute average loss avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keepratio:1.})/total_batch print(sess.run(re_input, feed_dict={x: batch_xs, keepratio:1.})) print("loop: ", i) # Display logs per epoch step if epoch % display_step == 0: print ("Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost)) train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys, keepratio:1.}) print (" Training accuracy: %.3f" % (train_acc)) test_acc = sess.run(accr, feed_dict={x: testimg, y: testlabel, keepratio:1.}) print (" Test accuracy: %.3f" % (test_acc)) # Save Net if epoch % save_step == 0: saver.save(sess, "nets/cnn_basic.ckpt-" + str(epoch)) print ("Optimization Finished.") # In[6]: # Restore net if do_train == 0: epoch = 4 saver.restore(sess, "nets/cnn_basic.ckpt-" + str(epoch)) # In[7]: # Compute test accuracy test_acc = sess.run(accr, feed_dict={x: testimg, y: testlabel, keepratio:1.}) print (" Test accuracy: %.3f" % (test_acc))