#coding:utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import time
#paras
n_classes = 10
# Training Parameters
learning_rate = 0.001
num_steps = 20000
batch_size = 128
display_step = 10
X = tf.placeholder(tf.float32,[None,28*28])
y = tf.placeholder(tf.float32,[None,n_classes])
#build vgg16 model
x = tf.reshape(X,[-1,28,28,1])
tf.summary.image('x',x)
#conv_1
with tf.name_scope('conv1_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,1,16],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(x,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[16],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
conv1_1 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv1_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,16,16],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(conv1_1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[16],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
conv1_2 = tf.nn.relu(out,name='scope')
#pool1
pool_1 = tf.nn.max_pool(conv1_2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool_1')
#conv_2
with tf.name_scope('conv2_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,16,32],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(pool_1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[32],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
conv2_1 = tf.nn.relu(out,name='scope')
with tf.name_scope('conv2_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,32,32],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(conv2_1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[32],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
conv2_2 = tf.nn.relu(out,name='scope')
#pool2
pool_2 = tf.nn.max_pool(conv2_2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool_1')
#conv_3
with tf.name_scope('conv3_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3, 3, 32, 64], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(pool_2, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), trainable=True, name='biases')
out = tf.nn.bias_add(conv, biases)
conv3_1 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv3_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 64], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(conv3_1, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), trainable=True, name='biases')
out = tf.nn.bias_add(conv, biases)
conv3_2 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv3_3') as scope:
kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 64], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(conv3_2, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), trainable=True, name='biases')
out = tf.nn.bias_add(conv, biases)
conv3_3 = tf.nn.relu(out, name='scope')
#pool_3
pool_3 = tf.nn.max_pool(conv3_3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool_3')
# conv_4
with tf.name_scope('conv4_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 128], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(pool_3, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32), trainable=True, name='biases')
out = tf.nn.bias_add(conv, biases)
conv4_1 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv4_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 128], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(conv4_1, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32), trainable=True, name='biases')
out = tf.nn.bias_add(conv, biases)
conv4_2 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv4_3') as scope:
kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 128], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(conv4_2, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32), trainable=True, name='biases')
out = tf.nn.bias_add(conv, biases)
conv4_3 = tf.nn.relu(out, name='scope')
# pool_4
pool_4 = tf.nn.max_pool(conv4_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool_4')
# conv_5
with tf.name_scope('conv5_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 256], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(pool_4, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name='biases')
out = tf.nn.bias_add(conv, biases)
conv5_1 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv5_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(conv5_1, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name='biases')
out = tf.nn.bias_add(conv, biases)
conv5_2 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv5_3') as scope:
kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(conv5_2, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name='biases')
out = tf.nn.bias_add(conv, biases)
conv5_3 = tf.nn.relu(out, name='scope')
# pool_5
pool_5 = tf.nn.max_pool(conv5_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool_5')
#fc1
with tf.name_scope('fc1') as scope:
shape = int(np.prod(pool_5.get_shape()[1:]))
fc1w = tf.Variable(tf.truncated_normal([shape,100],dtype=tf.float32,stddev=1e-1),name='weights')
fc1b = tf.Variable(tf.constant(1.0,shape=[100],dtype=tf.float32),trainable=True,name='biases')
pool5_flat = tf.reshape(pool_5,[-1,shape])
fc11 = tf.nn.bias_add(tf.matmul(pool5_flat,fc1w),fc1b)
fc1 = tf.nn.relu(fc11,name='scope')
#fc2
with tf.name_scope('fc2') as scope:
fc2w = tf.Variable(tf.truncated_normal([100,100],dtype=tf.float32,stddev=1e-1),name='weights')
fc2b = tf.Variable(tf.constant(1.0,shape=[100],dtype=tf.float32),trainable=True,name='biases')
fc21 = tf.nn.bias_add(tf.matmul(fc1,fc2w),fc2b)
fc2 = tf.nn.relu(fc21,name='scope')
#fc3
with tf.name_scope('fc3') as scope:
fc3w = tf.Variable(tf.truncated_normal([100, 10], dtype=tf.float32, stddev=1e-1), name='weights')
fc3b = tf.Variable(tf.constant(1.0, shape=[10], dtype=tf.float32), trainable=True, name='biases')
fc31 = tf.nn.bias_add(tf.matmul(fc2, fc3w), fc3b,name='scope')
mnist = input_data.read_data_sets('../data/',one_hot=True)
prediction = tf.nn.softmax(fc31)
#tensorboard事件保存地址
log_dir = './tensorboard/'
# Define loss and optimizer
with tf.name_scope('loss'):
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=fc31, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
#evaluate model
with tf.name_scope('accuracy'):
correct_pred = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))
#tensorboard
tf.summary.scalar('loss',loss_op)
tf.summary.scalar('accuracy',accuracy)
merged_summary_op = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter(log_dir,graph=tf.get_default_graph())
# initialize the variables
init = tf.global_variables_initializer()
# Start training
with tf.Session() as sess:
# Run the initializer
sess.run(init)
for step in range(1, num_steps+1):
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Run optimization op (backprop)
sess.run(train_op, feed_dict={X: batch_x, y: batch_y})
if step % display_step == 0 or step == 1:
# Calculate batch loss and accuracy
loss, acc,summary = sess.run([loss_op, accuracy,merged_summary_op], feed_dict={X: batch_x, y: batch_y})
summary_writer.add_summary(summary,step)
print("Step " + str(step) + ", Minibatch Loss= " + \
"{:.4f}".format(loss) + ", Training Accuracy= " + \
"{:.3f}".format(acc))
print("Optimization Finished!")
# Calculate accuracy for 256 MNIST test images
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={X: mnist.test.images[:256],
y: mnist.test.labels[:256]}))
print("run the tensorbord:tensorboard --logdir=./tensorboard")