Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.83 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. from read_input_data import RID
  4.  
  5. data_reader = RID()
  6.  
  7.  
  8. #################
  9. # training data #
  10. #################
  11. trX = np.array(data_reader.get_data_dict_with_arrays()["data_train"]).astype(np.float32) / 255.0
  12.  
  13. trY = np.array(data_reader.get_data_dict_with_arrays()["labels_train_coords"]).astype(np.float32)
  14. trY[:, 0] /= 800.0
  15. trY[:, 1] /= 600.0
  16. trY = np.array([trY[:, 0]]).reshape((21, 1))
  17.  
  18.  
  19. #############
  20. # test data #
  21. #############
  22. teX = np.array(data_reader.get_data_dict_with_arrays()["data_test"]).astype(np.float32) / 255.0
  23.  
  24. teY = np.array(data_reader.get_data_dict_with_arrays()["labels_test_coords"]).astype(np.float32)
  25. teY[:, 0] /= 800.0
  26. teY[:, 1] /= 600.0
  27. teY = np.array([teY[:, 0]]).reshape((7, 1))
  28.  
  29.  
  30. ################
  31. # reshape data #
  32. ################
  33. trX = trX.reshape(-1, 600, 800, 1)
  34. teX = teX.reshape(-1, 600, 800, 1)
  35.  
  36.  
  37. ################################################################################
  38. # weight, bias, conv2d and max_pool methods for initialization and calculation #
  39. ################################################################################
  40. def weight_variable(shape):
  41.     initial = tf.truncated_normal(shape, stddev=0.1)
  42.     return tf.Variable(initial)
  43.  
  44.  
  45. def bias_variable(shape):
  46.     initial = tf.constant(0.1, shape=shape)
  47.     return tf.Variable(initial)
  48.  
  49.  
  50. def conv2d(x, W):
  51.     return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
  52.  
  53.  
  54. def max_pool_2x2(x):
  55.     return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
  56.                           strides=[1, 2, 2, 1], padding='SAME')
  57.  
  58.  
  59. ##############################
  60. # placeholder for data input #
  61. ##############################
  62. x = tf.placeholder("float", shape=[None, 600, 800, 1])
  63. y_train = tf.placeholder("float", shape=[None, 1])
  64.  
  65. # reshape data
  66. x_image = tf.reshape(x, [-1, 600, 800, 1])
  67.  
  68.  
  69. ########################
  70. # network architecture #
  71. ########################
  72.  
  73. # conv/maxpool layer
  74. W_conv1 = weight_variable([5, 5, 1, 8])
  75. b_conv1 = bias_variable([8])
  76.  
  77. h_conv1 = tf.nn.relu((conv2d(x_image, W_conv1) + b_conv1))
  78. h_pool1 = max_pool_2x2(h_conv1)
  79.  
  80.  
  81. # conv/maxpool layer
  82. W_conv2 = weight_variable([5, 5, 8, 16])
  83. b_conv2 = bias_variable([16])
  84.  
  85. h_conv2 = tf.nn.relu((conv2d(h_pool1, W_conv2) + b_conv2))
  86. h_pool2 = max_pool_2x2(h_conv2)
  87.  
  88.  
  89. # fully connected layer
  90. W_fc1 = weight_variable([150 * 200 * 16, 100])
  91. b_fc1 = bias_variable([100])
  92.  
  93. h_pool2_flat = tf.reshape(h_pool2, [-1, 150 * 200 * 16])
  94. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
  95.  
  96. keep_prob = tf.placeholder("float")
  97. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
  98.  
  99.  
  100. # fully connected softmax layer
  101. W_fc2 = weight_variable([100, 1])
  102. b_fc2 = bias_variable([1])
  103.  
  104. y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
  105.  
  106.  
  107. ##################
  108. # train and eval #
  109. ##################
  110. cross_entropy = -tf.reduce_sum(y_train * tf.log(y_conv))
  111. # cross_entropy = tf.reduce_mean(-(y_train * tf.log(y_conv) + (1 - y_train) * tf.log(1 - y_conv)))
  112.  
  113. # train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
  114. train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)
  115. # train_step = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cross_entropy)
  116.  
  117. correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_train, 1))
  118.  
  119. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
  120.  
  121.  
  122. #################
  123. # session stuff #
  124. #################
  125. sess = tf.Session()
  126. init = tf.initialize_all_variables()
  127. sess.run(init)
  128.  
  129.  
  130.  
  131. ###################
  132. # actual training #
  133. ###################
  134. for i in range(1, 1000 + 1):
  135.     # if i % 5 == 0:
  136.     #     train_accuracy = sess.run(accuracy, feed_dict={
  137.     #         x: trX, y_train: trY, keep_prob: 1.0})
  138.     #     print("step {:>3}, training accuracy {}".format(i, train_accuracy))
  139.  
  140.     # training step
  141.     sess.run(train_step, feed_dict={x: trX, y_train: trY, keep_prob: 0.5})
  142.  
  143.  
  144.     #################
  145.     # testing stuff #
  146.     #################
  147.  
  148.     # testing picture 0
  149.     test_out = sess.run(y_conv, feed_dict={x: [trX[0]],
  150.                                          keep_prob: 1.0})
  151.     print("out[0]: {:>5.5f}  |  out original[0]: {:>5.5f}  |  diff[0]: {:>5.5f}".format(test_out[0][0], trY[0][0], abs(
  152.         test_out[0][0] - trY[0][0])))
  153.  
  154.     # testing picture 1
  155.     test_out = sess.run(y_conv, feed_dict={x: [trX[1]],
  156.                                          keep_prob: 1.0})
  157.     print("out[1]: {:>5.5f}  |  out original[1]: {:>5.5f}  |  diff[1]: {:>5.5f}".format(test_out[0][0], trY[1][0], abs(
  158.         test_out[0][0] - trY[1][0])))
  159.  
  160.     # testing picture 2
  161.     test_out = sess.run(y_conv, feed_dict={x: [trX[2]],
  162.                                          keep_prob: 1.0})
  163.     print("out[2]: {:>5.5f}  |  out original[2]: {:>5.5f}  |  diff[2]: {:>5.5f}".format(test_out[0][0], trY[2][0], abs(
  164.         test_out[0][0] - trY[2][0])))
  165.  
  166.     # testing picture 3
  167.     test_out = sess.run(y_conv, feed_dict={x: [trX[3]],
  168.                                          keep_prob: 1.0})
  169.     print("out[3]: {:>5.5f}  |  out original[3]: {:>5.5f}  |  diff[3]: {:>5.5f}".format(test_out[0][0], trY[3][0], abs(
  170.         test_out[0][0] - trY[3][0])))
  171.  
  172.     # testing random picture/array
  173.     print(sess.run(y_conv, feed_dict={x: [np.random.rand(600, 800).reshape(600, 800, 1)],
  174.                                          keep_prob: 1.0}))
  175.  
  176.     # testing array of zeros
  177.     print(sess.run(y_conv, feed_dict={x: [np.zeros([600, 800, 1])],
  178.                                          keep_prob: 1.0}))
  179.  
  180.     # testing array of ones
  181.     print(sess.run(y_conv, feed_dict={x: [np.ones([600, 800, 1])],
  182.                                          keep_prob: 1.0}))
  183.  
  184.  
  185. #################
  186. # test accuracy #
  187. #################
  188. print("\ntesting accuracy...")
  189. print("\ntest accuracy: %g" % sess.run(accuracy, feed_dict={
  190.     x: teX, y_train: teY, keep_prob: 1.0}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement