Created
September 25, 2017 14:01
-
-
Save johnhany/7548480338a8961111df5d97059ceb25 to your computer and use it in GitHub Desktop.
A demo for MNIST LeNet model for TensorFlow, including exporting model and visualization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tensorflow as tf | |
LOGDIR = 'visualize/' | |
from tensorflow.examples.tutorials.mnist import input_data | |
mnist = input_data.read_data_sets("F:\\Datasets\\AorB\\mnist\\", one_hot=True) | |
def conv_layer(input, size_in, size_out, name="conv"): | |
with tf.name_scope(name): | |
w = tf.Variable(tf.truncated_normal([5, 5, size_in, size_out], stddev=0.1), name="W") | |
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B") | |
conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding="VALID") | |
act = tf.nn.relu(conv + b) | |
tf.summary.histogram("weights", w) | |
tf.summary.histogram("biases", b) | |
tf.summary.histogram("activations", act) | |
return tf.nn.max_pool(act, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") | |
def fc_layer(input, size_in, size_out, name="fc"): | |
with tf.name_scope(name): | |
w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name="W") | |
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B") | |
act = tf.nn.relu(tf.matmul(input, w) + b) | |
tf.summary.histogram("weights", w) | |
tf.summary.histogram("biases", b) | |
tf.summary.histogram("activations", act) | |
return act | |
def fc_output_layer(input, size_in, size_out, name="fco"): | |
with tf.name_scope(name): | |
w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name="W") | |
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B") | |
act = tf.nn.softmax(tf.matmul(input, w) + b) | |
tf.summary.histogram("weights", w) | |
tf.summary.histogram("biases", b) | |
tf.summary.histogram("activations", act) | |
return act | |
def main(): | |
tf.reset_default_graph() | |
sess = tf.Session() | |
# Setup placeholders, and reshape the data | |
x = tf.placeholder(tf.float32, shape=[None, 784], name="x") | |
x_image = tf.reshape(x, [-1, 28, 28, 1]) | |
tf.summary.image('input', x_image, 3) | |
y = tf.placeholder(tf.float32, shape=[None, 10], name="labels") | |
conv_1 = conv_layer(x_image, 1, 6, "conv1") | |
conv_2 = conv_layer(conv_1, 6, 12, "conv2") | |
flattened = tf.reshape(conv_2, [-1, 4 * 4 * 12]) | |
fc1 = fc_layer(flattened, 4 * 4 * 12, 1024, "fc1") | |
embedding_input = fc1 | |
embedding_size = 1024 | |
logits = fc_output_layer(fc1, 1024, 10, "fc2") | |
with tf.name_scope("loss"): | |
loss = tf.reduce_mean(tf.square(logits - y), name="loss") | |
tf.summary.scalar("loss", loss) | |
with tf.name_scope("train"): | |
train_step = tf.train.GradientDescentOptimizer(1.0).minimize(loss) | |
with tf.name_scope("accuracy"): | |
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1)) | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) | |
tf.summary.scalar("accuracy", accuracy) | |
summ = tf.summary.merge_all() | |
embedding = tf.Variable(tf.zeros([1024, embedding_size]), name="test_embedding") | |
assignment = embedding.assign(embedding_input) | |
saver = tf.train.Saver() | |
sess.run(tf.global_variables_initializer()) | |
writer = tf.summary.FileWriter(LOGDIR + "mnist-convnet") | |
writer.add_graph(sess.graph) | |
config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig() | |
embedding_config = config.embeddings.add() | |
embedding_config.tensor_name = embedding.name | |
embedding_config.sprite.image_path = LOGDIR + 'sprite_1024.png' | |
embedding_config.metadata_path = LOGDIR + 'labels_1024.tsv' | |
# Specify the width and height of a single thumbnail. | |
embedding_config.sprite.single_image_dim.extend([28, 28]) | |
tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer, config) | |
for i in range(2001): | |
batch = mnist.train.next_batch(100) | |
if i % 5 == 0: | |
[train_accuracy, s] = sess.run([accuracy, summ], feed_dict={x: batch[0], y: batch[1]}) | |
writer.add_summary(s, i) | |
if i % 200 == 0: | |
print("Step %d, train accuracy %g"%(i, train_accuracy)) | |
if i % 500 == 0: | |
sess.run(assignment, feed_dict={x: mnist.test.images[:1024], y: mnist.test.labels[:1024]}) | |
saver.save(sess, os.path.join(LOGDIR, "model.ckpt"), i) | |
sess.run(train_step, feed_dict={x: batch[0], y: batch[1]}) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment