tensorflow_dataset_api.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """ TensorFlow Dataset API.
  2. In this example, we will show how to load numpy array data into the new
  3. TensorFlow 'Dataset' API. The Dataset API implements an optimized data pipeline
  4. with queues, that make data processing and training faster (especially on GPU).
  5. Author: Aymeric Damien
  6. Project: https://github.com/aymericdamien/TensorFlow-Examples/
  7. """
  8. from __future__ import print_function
  9. import tensorflow as tf
  10. # Import MNIST data (Numpy format)
  11. from tensorflow.examples.tutorials.mnist import input_data
  12. mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
  13. # Parameters
  14. learning_rate = 0.001
  15. num_steps = 2000
  16. batch_size = 128
  17. display_step = 100
  18. # Network Parameters
  19. n_input = 784 # MNIST data input (img shape: 28*28)
  20. n_classes = 10 # MNIST total classes (0-9 digits)
  21. dropout = 0.75 # Dropout, probability to keep units
  22. sess = tf.Session()
  23. # Create a dataset tensor from the images and the labels
  24. dataset = tf.contrib.data.Dataset.from_tensor_slices(
  25. (mnist.train.images, mnist.train.labels))
  26. # Create batches of data
  27. dataset = dataset.batch(batch_size)
  28. # Create an iterator, to go over the dataset
  29. iterator = dataset.make_initializable_iterator()
  30. # It is better to use 2 placeholders, to avoid to load all data into memory,
  31. # and avoid the 2Gb restriction length of a tensor.
  32. _data = tf.placeholder(tf.float32, [None, n_input])
  33. _labels = tf.placeholder(tf.float32, [None, n_classes])
  34. # Initialize the iterator
  35. sess.run(iterator.initializer, feed_dict={_data: mnist.train.images,
  36. _labels: mnist.train.labels})
  37. # Neural Net Input
  38. X, Y = iterator.get_next()
  39. # -----------------------------------------------
  40. # THIS IS A CLASSIC CNN (see examples, section 3)
  41. # -----------------------------------------------
  42. # Note that a few elements have changed (usage of sess run).
  43. # Create model
  44. def conv_net(x, n_classes, dropout, reuse, is_training):
  45. # Define a scope for reusing the variables
  46. with tf.variable_scope('ConvNet', reuse=reuse):
  47. # MNIST data input is a 1-D vector of 784 features (28*28 pixels)
  48. # Reshape to match picture format [Height x Width x Channel]
  49. # Tensor input become 4-D: [Batch Size, Height, Width, Channel]
  50. x = tf.reshape(x, shape=[-1, 28, 28, 1])
  51. # Convolution Layer with 32 filters and a kernel size of 5
  52. conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
  53. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  54. conv1 = tf.layers.max_pooling2d(conv1, 2, 2)
  55. # Convolution Layer with 32 filters and a kernel size of 5
  56. conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
  57. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  58. conv2 = tf.layers.max_pooling2d(conv2, 2, 2)
  59. # Flatten the data to a 1-D vector for the fully connected layer
  60. fc1 = tf.contrib.layers.flatten(conv2)
  61. # Fully connected layer (in contrib folder for now)
  62. fc1 = tf.layers.dense(fc1, 1024)
  63. # Apply Dropout (if is_training is False, dropout is not applied)
  64. fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)
  65. # Output layer, class prediction
  66. out = tf.layers.dense(fc1, n_classes)
  67. # Because 'softmax_cross_entropy_with_logits' already apply softmax,
  68. # we only apply softmax to testing network
  69. out = tf.nn.softmax(out) if not is_training else out
  70. return out
  71. # Because Dropout have different behavior at training and prediction time, we
  72. # need to create 2 distinct computation graphs that share the same weights.
  73. # Create a graph for training
  74. logits_train = conv_net(X, n_classes, dropout, reuse=False, is_training=True)
  75. # Create another graph for testing that reuse the same weights, but has
  76. # different behavior for 'dropout' (not applied).
  77. logits_test = conv_net(X, n_classes, dropout, reuse=True, is_training=False)
  78. # Define loss and optimizer (with train logits, for dropout to take effect)
  79. loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
  80. logits=logits_train, labels=Y))
  81. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
  82. train_op = optimizer.minimize(loss_op)
  83. # Evaluate model (with test logits, for dropout to be disabled)
  84. correct_pred = tf.equal(tf.argmax(logits_test, 1), tf.argmax(Y, 1))
  85. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
  86. # Initialize the variables (i.e. assign their default value)
  87. init = tf.global_variables_initializer()
  88. # Run the initializer
  89. sess.run(init)
  90. # Training cycle
  91. for step in range(1, num_steps + 1):
  92. try:
  93. # Run optimization
  94. sess.run(train_op)
  95. except tf.errors.OutOfRangeError:
  96. # Reload the iterator when it reaches the end of the dataset
  97. sess.run(iterator.initializer,
  98. feed_dict={_data: mnist.train.images,
  99. _labels: mnist.train.labels})
  100. sess.run(train_op)
  101. if step % display_step == 0 or step == 1:
  102. # Calculate batch loss and accuracy
  103. # (note that this consume a new batch of data)
  104. loss, acc = sess.run([loss_op, accuracy])
  105. print("Step " + str(step) + ", Minibatch Loss= " + \
  106. "{:.4f}".format(loss) + ", Training Accuracy= " + \
  107. "{:.3f}".format(acc))
  108. print("Optimization Finished!")