|
| 1 | +from __future__ import absolute_import, unicode_literals |
| 2 | +import input_data |
| 3 | +import tensorflow as tf |
| 4 | +import shutil |
| 5 | +import os.path |
| 6 | + |
| 7 | +export_dir = './tmp/expert-export' |
| 8 | + |
| 9 | +if os.path.exists(export_dir): |
| 10 | + shutil.rmtree(export_dir) |
| 11 | + |
| 12 | +def weight_variable(shape): |
| 13 | + initial = tf.truncated_normal(shape, stddev=0.1) |
| 14 | + return tf.Variable(initial) |
| 15 | + |
| 16 | +def bias_variable(shape): |
| 17 | + initial = tf.constant(0.1, shape=shape) |
| 18 | + return tf.Variable(initial) |
| 19 | + |
| 20 | +def conv2d(x, W): |
| 21 | + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') |
| 22 | + |
| 23 | +def max_pool_2x2(x): |
| 24 | + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], |
| 25 | + strides=[1, 2, 2, 1], padding='SAME') |
| 26 | + |
| 27 | +mnist = input_data.read_data_sets("data/", one_hot=True) |
| 28 | + |
| 29 | +g = tf.Graph() |
| 30 | +with g.as_default(): |
| 31 | + x = tf.placeholder("float", shape=[None, 784]) |
| 32 | + y_ = tf.placeholder("float", shape=[None, 10]) |
| 33 | + |
| 34 | + W_conv1 = weight_variable([5, 5, 1, 32]) |
| 35 | + b_conv1 = bias_variable([32]) |
| 36 | + x_image = tf.reshape(x, [-1, 28, 28, 1]) |
| 37 | + h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) |
| 38 | + h_pool1 = max_pool_2x2(h_conv1) |
| 39 | + |
| 40 | + W_conv2 = weight_variable([5, 5, 32, 64]) |
| 41 | + b_conv2 = bias_variable([64]) |
| 42 | + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) |
| 43 | + h_pool2 = max_pool_2x2(h_conv2) |
| 44 | + |
| 45 | + W_fc1 = weight_variable([7 * 7 * 64, 1024]) |
| 46 | + b_fc1 = bias_variable([1024]) |
| 47 | + h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) |
| 48 | + h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) |
| 49 | + |
| 50 | + keep_prob = tf.placeholder("float") |
| 51 | + h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) |
| 52 | + |
| 53 | + W_fc2 = weight_variable([1024, 10]) |
| 54 | + b_fc2 = bias_variable([10]) |
| 55 | + |
| 56 | + y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) |
| 57 | + |
| 58 | + cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) |
| 59 | + train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) |
| 60 | + correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) |
| 61 | + accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) |
| 62 | + |
| 63 | + sess = tf.Session() |
| 64 | + sess.run(tf.initialize_all_variables()) |
| 65 | + |
| 66 | + for i in range(20000): |
| 67 | + batch = mnist.train.next_batch(50) |
| 68 | + if i % 100 == 0: |
| 69 | + train_accuracy = accuracy.eval( |
| 70 | + {x: batch[0], y_: batch[1], keep_prob: 1.0}, sess) |
| 71 | + print "step %d, training accuracy %g" % (i, train_accuracy) |
| 72 | + train_step.run( |
| 73 | + {x: batch[0], y_: batch[1], keep_prob: 0.5}, sess) |
| 74 | + |
| 75 | + print "test accuracy %g" % accuracy.eval( |
| 76 | + {x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}, sess) |
| 77 | + |
| 78 | +# Store variable |
| 79 | +_W_conv1 = W_conv1.eval(sess) |
| 80 | +_b_conv1 = b_conv1.eval(sess) |
| 81 | +_W_conv2 = W_conv2.eval(sess) |
| 82 | +_b_conv2 = b_conv2.eval(sess) |
| 83 | +_W_fc1 = W_fc1.eval(sess) |
| 84 | +_b_fc1 = b_fc1.eval(sess) |
| 85 | +_W_fc2 = W_fc2.eval(sess) |
| 86 | +_b_fc2 = b_fc2.eval(sess) |
| 87 | + |
| 88 | +sess.close() |
| 89 | + |
| 90 | +# Create new graph for exporting |
| 91 | +g_2 = tf.Graph() |
| 92 | +with g_2.as_default(): |
| 93 | + x_2 = tf.placeholder("float", shape=[None, 784], name="input") |
| 94 | + |
| 95 | + W_conv1_2 = tf.constant(_W_conv1, name="constant_W_conv1") |
| 96 | + b_conv1_2 = tf.constant(_b_conv1, name="constant_b_conv1") |
| 97 | + x_image_2 = tf.reshape(x_2, [-1, 28, 28, 1]) |
| 98 | + h_conv1_2 = tf.nn.relu(conv2d(x_image_2, W_conv1_2) + b_conv1_2) |
| 99 | + h_pool1_2 = max_pool_2x2(h_conv1_2) |
| 100 | + |
| 101 | + W_conv2_2 = tf.constant(_W_conv2, name="constant_W_conv2") |
| 102 | + b_conv2_2 = tf.constant(_b_conv2, name="constant_b_conv2") |
| 103 | + h_conv2_2 = tf.nn.relu(conv2d(h_pool1_2, W_conv2_2) + b_conv2_2) |
| 104 | + h_pool2_2 = max_pool_2x2(h_conv2_2) |
| 105 | + |
| 106 | + W_fc1_2 = tf.constant(_W_fc1, name="constant_W_fc1") |
| 107 | + b_fc1_2 = tf.constant(_b_fc1, name="constant_b_fc1") |
| 108 | + h_pool2_flat_2 = tf.reshape(h_pool2_2, [-1, 7 * 7 * 64]) |
| 109 | + h_fc1_2 = tf.nn.relu(tf.matmul(h_pool2_flat_2, W_fc1_2) + b_fc1_2) |
| 110 | + |
| 111 | + W_fc2_2 = tf.constant(_W_fc2, name="constant_W_fc2") |
| 112 | + b_fc2_2 = tf.constant(_b_fc2, name="constant_b_fc2") |
| 113 | + |
| 114 | + # DropOut is skipped for exported graph. |
| 115 | + |
| 116 | + y_conv_2 = tf.nn.softmax(tf.matmul(h_fc1_2, W_fc2_2) + b_fc2_2, name="output") |
| 117 | + |
| 118 | + sess_2 = tf.Session() |
| 119 | + init_2 = tf.initialize_all_variables(); |
| 120 | + sess_2.run(init_2) |
| 121 | + |
| 122 | + graph_def = g_2.as_graph_def() |
| 123 | + tf.train.write_graph(graph_def, export_dir, 'expert-graph.pb', as_text=False) |
| 124 | + |
| 125 | + # Test trained model |
| 126 | + y__2 = tf.placeholder("float", [None, 10]) |
| 127 | + correct_prediction_2 = tf.equal(tf.argmax(y_conv_2, 1), tf.argmax(y__2, 1)) |
| 128 | + accuracy_2 = tf.reduce_mean(tf.cast(correct_prediction_2, "float")) |
| 129 | + |
| 130 | + print "check accuracy %g" % accuracy_2.eval( |
| 131 | + {x_2: mnist.test.images, y__2: mnist.test.labels}, sess_2) |
0 commit comments