fork download
  1. from __future__ import absolute_import
  2. from __future__ import division
  3. from __future__ import print_function
  4. # inference
  5. import argparse
  6. import sys
  7. import tempfile
  8.  
  9. #from tensorflow.examples.tutorials.mnist import input_data
  10.  
  11. import tensorflow as tf
  12.  
  13. FLAGS = None
  14.  
  15.  
  16. def deepnn(x):
  17. """deepnn builds the graph for a deep net for classifying digits.
  18. Args:
  19. x: an input tensor with the dimensions (N_examples, 784), where 784 is the
  20. number of pixels in a standard MNIST image.
  21. Returns:
  22. A tuple (y, keep_prob). y is a tensor of shape (N_examples, 10), with values
  23. equal to the logits of classifying the digit into one of 10 classes (the
  24. digits 0-9). keep_prob is a scalar placeholder for the probability of
  25. dropout.
  26. """
  27. # Reshape to use within a convolutional neural net.
  28. # Last dimension is for "features" - there is only one here, since images are
  29. # grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc.
  30. with tf.name_scope('reshape'):
  31. x_image = tf.reshape(x, [-1, 28, 28, 1])
  32.  
  33. # First convolutional layer - maps one grayscale image to 32 feature maps.
  34. with tf.name_scope('conv1'):
  35. W_conv1 = weight_variable([5, 5, 1, 32])
  36. b_conv1 = bias_variable([32])
  37. h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
  38.  
  39. # Pooling layer - downsamples by 2X.
  40. with tf.name_scope('pool1'):
  41. h_pool1 = max_pool_2x2(h_conv1)
  42.  
  43. # Second convolutional layer -- maps 32 feature maps to 64.
  44. with tf.name_scope('conv2'):
  45. W_conv2 = weight_variable([5, 5, 32, 64])
  46. b_conv2 = bias_variable([64])
  47. h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
  48.  
  49. # Second pooling layer.
  50. with tf.name_scope('pool2'):
  51. h_pool2 = max_pool_2x2(h_conv2)
  52.  
  53. # Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image
  54. # is down to 7x7x64 feature maps -- maps this to 1024 features.
  55. with tf.name_scope('fc1'):
  56. W_fc1 = weight_variable([7 * 7 * 64, 1024])
  57. b_fc1 = bias_variable([1024])
  58.  
  59. h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
  60. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
  61.  
  62. #-
  63. # Dropout - controls the complexity of the model, prevents co-adaptation of
  64. # features.
  65. #with tf.name_scope('dropout'):
  66. # keep_prob = tf.placeholder(tf.float32)
  67. # h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
  68.  
  69. # Map the 1024 features to 10 classes, one for each digit
  70. with tf.name_scope('fc2'):
  71. W_fc2 = weight_variable([1024, 10])
  72. b_fc2 = bias_variable([10])
  73.  
  74. # y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
  75. #return y_conv, keep_prob
  76. y_conv = tf.matmul(h_fc1, W_fc2) + b_fc2
  77. return y_conv
  78.  
  79.  
  80. def conv2d(x, W):
  81. """conv2d returns a 2d convolution layer with full stride."""
  82. return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
  83.  
  84.  
  85. def max_pool_2x2(x):
  86. """max_pool_2x2 downsamples a feature map by 2X."""
  87. return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
  88. strides=[1, 2, 2, 1], padding='SAME')
  89.  
  90.  
  91. def weight_variable(shape):
  92. """weight_variable generates a weight variable of a given shape."""
  93. initial = tf.truncated_normal(shape, stddev=0.1)
  94. return tf.Variable(initial)
  95.  
  96.  
  97. def bias_variable(shape):
  98. """bias_variable generates a bias variable of a given shape."""
  99. initial = tf.constant(0.1, shape=shape)
  100. return tf.Variable(initial)
  101.  
  102.  
  103. def main(_):
  104. #-
  105. # Import data
  106. #mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
  107.  
  108. # Create the model
  109. x = tf.placeholder(tf.float32, [None, 784],name="input")
  110.  
  111. #-
  112. # Define loss and optimizer
  113. #y_ = tf.placeholder(tf.float32, [None, 10])
  114.  
  115. #-
  116. # Build the graph for the deep net
  117. #y_conv, keep_prob = deepnn(x)
  118. # No longer need keep_prob since removing dropout layers.
  119. y_conv = deepnn(x)
  120. output = tf.nn.softmax(y_conv, name='output')
  121. # with tf.name_scope('loss'):
  122.  
  123. # train_writer.add_graph(tf.get_default_graph())
  124. saver = tf.train.Saver(tf.global_variables())
  125. #saver = tf.train.Saver()
  126.  
  127. with tf.Session() as sess:
  128. sess.run(tf.global_variables_initializer())
  129. sess.run(tf.local_variables_initializer())
  130. # read the previously saved network.
  131. saver.restore(sess, '.' + '/mnist_model')
  132. # save the version of the network ready that can be compiled for NCS
  133. saver.save(sess, '.' + '/mnist_inference')
  134. #for i in range(3000):
  135.  
  136. #save_path = saver.save(sess, graph_location + "/mnist_model")
  137.  
  138. if __name__ == '__main__':
  139. parser = argparse.ArgumentParser()
  140. parser.add_argument('--data_dir', type=str,
  141. default='/tmp/tensorflow/mnist/input_data',
  142. help='Directory for storing input data')
  143. FLAGS, unparsed = parser.parse_known_args()
  144. tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
Runtime error #stdin #stdout #stderr 0.03s 119808KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 11, in <module>
    import tensorflow as tf
ImportError: No module named tensorflow