convolutional_network.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """ Convolutional Neural Network.
  2. Build and train a convolutional neural network with TensorFlow.
  3. This example is using the MNIST database of handwritten digits
  4. (http://yann.lecun.com/exdb/mnist/)
  5. This example is using TensorFlow layers API, see 'convolutional_network_raw'
  6. example for a raw implementation with variables.
  7. Author: Aymeric Damien
  8. Project: https://github.com/aymericdamien/TensorFlow-Examples/
  9. """
  10. from __future__ import division, print_function, absolute_import
  11. # Import MNIST data
  12. from tensorflow.examples.tutorials.mnist import input_data
  13. mnist = input_data.read_data_sets("/tmp/data/", one_hot=False)
  14. import tensorflow as tf
  15. # Training Parameters
  16. learning_rate = 0.001
  17. num_steps = 2000
  18. batch_size = 128
  19. # Network Parameters
  20. num_input = 784 # MNIST data input (img shape: 28*28)
  21. num_classes = 10 # MNIST total classes (0-9 digits)
  22. dropout = 0.75 # Dropout, probability to keep units
  23. # Create the neural network
  24. def conv_net(x_dict, n_classes, dropout, reuse, is_training):
  25. # Define a scope for reusing the variables
  26. with tf.variable_scope('ConvNet', reuse=reuse):
  27. # TF Estimator input is a dict, in case of multiple inputs
  28. x = x_dict['images']
  29. # MNIST data input is a 1-D vector of 784 features (28*28 pixels)
  30. # Reshape to match picture format [Height x Width x Channel]
  31. # Tensor input become 4-D: [Batch Size, Height, Width, Channel]
  32. x = tf.reshape(x, shape=[-1, 28, 28, 1])
  33. # Convolution Layer with 32 filters and a kernel size of 5
  34. conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
  35. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  36. conv1 = tf.layers.max_pooling2d(conv1, 2, 2)
  37. # Convolution Layer with 64 filters and a kernel size of 3
  38. conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
  39. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  40. conv2 = tf.layers.max_pooling2d(conv2, 2, 2)
  41. # Flatten the data to a 1-D vector for the fully connected layer
  42. fc1 = tf.contrib.layers.flatten(conv2)
  43. # Fully connected layer (in tf contrib folder for now)
  44. fc1 = tf.layers.dense(fc1, 1024)
  45. # Apply Dropout (if is_training is False, dropout is not applied)
  46. fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)
  47. # Output layer, class prediction
  48. out = tf.layers.dense(fc1, n_classes)
  49. return out
  50. # Define the model function (following TF Estimator Template)
  51. def model_fn(features, labels, mode):
  52. # Build the neural network
  53. # Because Dropout have different behavior at training and prediction time, we
  54. # need to create 2 distinct computation graphs that still share the same weights.
  55. logits_train = conv_net(features, num_classes, dropout, reuse=False,
  56. is_training=True)
  57. logits_test = conv_net(features, num_classes, dropout, reuse=True,
  58. is_training=False)
  59. # Predictions
  60. pred_classes = tf.argmax(logits_test, axis=1)
  61. pred_probas = tf.nn.softmax(logits_test)
  62. # If prediction mode, early return
  63. if mode == tf.estimator.ModeKeys.PREDICT:
  64. return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)
  65. # Define loss and optimizer
  66. loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
  67. logits=logits_train, labels=tf.cast(labels, dtype=tf.int32)))
  68. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
  69. train_op = optimizer.minimize(loss_op,
  70. global_step=tf.train.get_global_step())
  71. # Evaluate the accuracy of the model
  72. acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)
  73. # TF Estimators requires to return a EstimatorSpec, that specify
  74. # the different ops for training, evaluating, ...
  75. estim_specs = tf.estimator.EstimatorSpec(
  76. mode=mode,
  77. predictions=pred_classes,
  78. loss=loss_op,
  79. train_op=train_op,
  80. eval_metric_ops={'accuracy': acc_op})
  81. return estim_specs
  82. # Build the Estimator
  83. model = tf.estimator.Estimator(model_fn)
  84. # Define the input function for training
  85. input_fn = tf.estimator.inputs.numpy_input_fn(
  86. x={'images': mnist.train.images}, y=mnist.train.labels,
  87. batch_size=batch_size, num_epochs=None, shuffle=True)
  88. # Train the Model
  89. model.train(input_fn, steps=num_steps)
  90. # Evaluate the Model
  91. # Define the input function for evaluating
  92. input_fn = tf.estimator.inputs.numpy_input_fn(
  93. x={'images': mnist.test.images}, y=mnist.test.labels,
  94. batch_size=batch_size, shuffle=False)
  95. # Use the Estimator 'evaluate' method
  96. e = model.evaluate(input_fn)
  97. print("Testing Accuracy:", e['accuracy'])