def load(depth, width, height): ''' Loading datasets ''' # Training Set: 60,000 datapoints - Test Set: 10,000 datapoints print("[INFO] Downloading/fetching MNIST dataset") dataset = input_data.read_data_sets("../data", False) # Images # Each image is a vector of dimension 784 (1*28*28 - one channel, grayscale images) # Values are between 0 and 1, they are already normalized # Note: I won't use the valisation dataset to tune hyperparameters x_train = np.concatenate((dataset.train.images, dataset.validation.images), axis=0) x_test = dataset.test.images # Labels y_train = np.concatenate((dataset.train.labels, dataset.validation.labels), axis=0) y_test = dataset.test.labels x_train = x_train.reshape( (x_train.shape[0], depth, width, height)).astype('float32') x_test = x_test.reshape( (x_test.shape[0], depth, width, height)).astype('float32') return (x_train, x_test, y_train, y_test)
from sklearn.metrics import confusion_matrix from sklearn.metrics import precision_recall_fscore_support from lib import input_data from lib import graph from lib import model from lib import corrupt from keras import backend as K ### Keras Backend #print(K.backend()) #print(K.image_dim_ordering(), ": ", K.image_data_format()) ### Loading data # Training Set: 60,000 datapoints - Test Set: 10,000 datapoints print("[INFO] Downloading/fetching MNIST dataset") dataset = input_data.read_data_sets("../data", False) # Images # Each image is a vector of dimension 784 (1*28*28 - one channel, grayscale images) # Values are between 0 and 1, they are already normalized # Note: I won't use the valisation dataset to tune hyperparameters x_train = np.concatenate((dataset.train.images, dataset.validation.images), axis=0) x_test = dataset.test.images # Labels y_train = np.concatenate((dataset.train.labels, dataset.validation.labels), axis=0) y_test = dataset.test.labels ### Plot Sample images (4 - grayscale) graph.plot_sample(x_train) ### Image Definition # number of classes
# Import MNIST data import tensorflow as tf import lib.input_data as input_data from tensorflow.python.framework import ops from tensorflow.python.saved_model import builder, utils from tensorflow.python.saved_model import signature_constants from tensorflow.python.saved_model import signature_def_utils from tensorflow.python.saved_model import tag_constants mnist = input_data.read_data_sets("tmp/data/", one_hot=True) # Set parameters learning_rate = 0.01 training_iteration = 30 batch_size = 100 display_step = 2 # TF graph input x = tf.placeholder(tf.float32, shape=[None, 784], name="x") # mnist data image of shape 28*28=784 y = tf.placeholder(tf.float32, shape=[None, 10]) # 0-9 digits recognition => 10 classes # Create a model # Set model weights W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) # Construct a linear model model = tf.nn.softmax(tf.matmul(x, W) + b, name='y') # Softmax
def run_training(): data_sets = input_data.read_data_sets('MNIST_data', fake_data=False) # 下载数据 # 创建训练图像shape 和标记shape with tf.Graph().as_default(): images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, mnist.IMAGE_PIXELS)) # 输入图 labels_placeholder = tf.placeholder(tf.int32, shape=batch_size) # 标签图 print('images_placeholder 的维度为:', images_placeholder.shape) print('lables_placeholder 的维度为:', labels_placeholder.shape) # 第一层 with tf.name_scope('hidden1'): weight = tf.Variable(tf.truncated_normal( [mnist.IMAGE_PIXELS, mnist.HIDDEN1_UNITS], stddev=1.0 / math.sqrt(float(mnist.IMAGE_PIXELS))), name='weights') # 初始化随机 biases = tf.Variable(tf.zeros([mnist.HIDDEN1_UNITS]), name='biases') hidden1 = tf.nn.relu( tf.matmul(images_placeholder, weight) + biases) # 第一层的计算方法 print('hidden1 的维度为:', hidden1.shape) # 第二层 with tf.name_scope('hidden2'): weight = tf.Variable(tf.truncated_normal( [mnist.HIDDEN1_UNITS, mnist.HIDDEN2_UNITS], stddev=1.0 / math.sqrt(float(mnist.HIDDEN1_UNITS))), name='weight') biases = tf.Variable(tf.zeros([mnist.HIDDEN2_UNITS]), name='biases') hidden2 = tf.nn.relu(tf.matmul(hidden1, weight) + biases) print('hidden2 的维度为:', hidden2.shape) # 输出层 with tf.name_scope('softmax_linear'): weight = tf.Variable(tf.truncated_normal( [mnist.HIDDEN2_UNITS, mnist.NUM_CLASSES], stddev=1.0 / math.sqrt(float(mnist.HIDDEN2_UNITS))), name='weight') biases = tf.Variable(tf.zeros([mnist.NUM_CLASSES]), name='biases') logits = tf.matmul(hidden2, weight) + biases print('logits 的维度为:', logits.shape) labels = tf.to_int64(labels_placeholder) # 标签一致化 print('labels 维度:', labels.shape) loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) # 根据稀疏表示的label和输出层数据计算损失 http://blog.csdn.net/cnki_ok/article/details/70243960 print('loss 的维度为:', loss.shape) tf.summary.scalar('loss', loss) # 可视化 optimizer = tf.train.GradientDescentOptimizer( mnist.LEARNING_RATE) # 设置下降参数 global_step = tf.Variable(0, name='global_step', trainable=False) train_op = optimizer.minimize(loss, global_step=global_step) # 训练目标 correct = tf.nn.in_top_k(logits, labels_placeholder, 1) eval_correct = tf.reduce_sum(tf.cast(correct, tf.int32)) # 正确率获取图 summary = tf.summary.merge_all() init = tf.global_variables_initializer() saver = tf.train.Saver() sess = tf.Session() summary_writer = tf.summary.FileWriter(log_dir, sess.graph) sess.run(init) for step in range(2000): start_time = time.time() batch = data_sets.train.next_batch(batch_size, fake_data=False) _, loss_value = sess.run([train_op, loss], feed_dict={ images_placeholder: batch[0], labels_placeholder: batch[1] }) duration = time.time() - start_time if step % 100 == 0: print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration)) summary_str = sess.run(summary, feed_dict={ images_placeholder: batch[0], labels_placeholder: batch[1] }) summary_writer.add_summary(summary_str, step) summary_writer.flush() if (step + 1) % 1000 == 0 or (step + 1) == 2000: checkout_file = os.path.join(log_dir, 'model.ckpt') saver.save(sess, checkout_file, global_step=step) print('Training Data Eval:') do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.train) print('Validation Data Eval:') do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.validation) # Evaluate against the test set. print('Test Data Eval:') do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.test)
def run_training(): """Train MNIST for a number of steps.""" # Get the sets of images and labels for training, validation, and # test on MNIST. #data_sets = input_data.read_data_sets(FLAGS.input_data_dir, FLAGS.fake_data) data_sets = input_data.read_data_sets('MNIST_data', fake_data=False) # 下载数据 # Tell TensorFlow that the model will be built into the default Graph. with tf.Graph().as_default(): images_placeholder = tf.placeholder(tf.float32, shape=(FLAGS.batch_size, mnist.IMAGE_PIXELS)) labels_placeholder = tf.placeholder(tf.int32, shape=(FLAGS.batch_size)) with tf.name_scope('hidden1'): weights = tf.Variable( tf.truncated_normal([mnist.IMAGE_PIXELS, mnist.HIDDEN1_UNITS], stddev=1.0 / math.sqrt(float(mnist.IMAGE_PIXELS))), name='weights') biases = tf.Variable(tf.zeros([mnist.HIDDEN1_UNITS]), name='biases') hidden1 = tf.nn.relu(tf.matmul(images_placeholder, weights) + biases) # Hidden 2 with tf.name_scope('hidden2'): weights = tf.Variable( tf.truncated_normal([FLAGS.hidden1, mnist.HIDDEN2_UNITS], stddev=1.0 / math.sqrt(float(mnist.HIDDEN2_UNITS))), name='weights') biases = tf.Variable(tf.zeros([mnist.HIDDEN2_UNITS]), name='biases') hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases) # Linear with tf.name_scope('softmax_linear'): weights = tf.Variable( tf.truncated_normal([mnist.HIDDEN2_UNITS, mnist.NUM_CLASSES], stddev=1.0 / math.sqrt(float(mnist.HIDDEN2_UNITS))), name='weights') biases = tf.Variable(tf.zeros([mnist.NUM_CLASSES]), name='biases') logits = tf.matmul(hidden2, weights) + biases labels = tf.to_int64(labels_placeholder) loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) tf.summary.scalar('loss', loss) optimizer = tf.train.GradientDescentOptimizer(mnist.LEARNING_RATE) global_step = tf.Variable(0, name='global_step', trainable=False) train_op = optimizer.minimize(loss, global_step=global_step) correct = tf.nn.in_top_k(logits, labels_placeholder, 1) eval_correct = tf.reduce_sum(tf.cast(correct, tf.int32)) summary = tf.summary.merge_all() # Add the variable initializer Op. init = tf.global_variables_initializer() # Create a saver for writing training checkpoints. saver = tf.train.Saver() # Create a session for running Ops on the Graph. sess = tf.Session() # Instantiate a SummaryWriter to output summaries and the Graph. summary_writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph) # And then after everything is built: # Run the Op to initialize the variables. sess.run(init) # Start the training loop. for step in xrange(FLAGS.max_steps): start_time = time.time() # Fill a feed dictionary with the actual set of images and labels # for this particular training step. ''' feed_dict = fill_feed_dict(data_sets.train, images_placeholder, labels_placeholder) ''' batch = data_sets.train.next_batch(batch_size=100, fake_data=False) # Run one step of the model. The return values are the activations # from the `train_op` (which is discarded) and the `loss` Op. To # inspect the values of your Ops or variables, you may include them # in the list passed to sess.run() and the value tensors will be # returned in the tuple from the call. print('logits 的维度是', logits.shape) print('loss 的维度是', loss.shape) print('feed_dict im的维度是', images_placeholder.shape) print('feed_dict im的维度是', labels_placeholder.shape) _, loss_value = sess.run([train_op, loss], feed_dict={images_placeholder: batch[0], labels_placeholder: batch[1]}) duration = time.time() - start_time # Write the summaries and print an overview fairly often. if step % 100 == 0: # Print status to stdout. print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration)) # Update the events file. summary_str = sess.run(summary, feed_dict={images_placeholder: batch[0], labels_placeholder: batch[1]}) summary_writer.add_summary(summary_str, step) summary_writer.flush() # Save a checkpoint and evaluate the model periodically. if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps: checkpoint_file = os.path.join(FLAGS.log_dir, 'model.ckpt') saver.save(sess, checkpoint_file, global_step=step) # Evaluate against the training set. print('Training Data Eval:') do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.train) # Evaluate against the validation set. print('Validation Data Eval:') do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.validation) # Evaluate against the test set. print('Test Data Eval:') do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.test)