Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tensorflow as tf
- import numpy as np
- from read_input_data import RID
- data_reader = RID()
- #################
- # training data #
- #################
- trX = np.array(data_reader.get_data_dict_with_arrays()["data_train"]).astype(np.float32) / 255.0
- trY = np.array(data_reader.get_data_dict_with_arrays()["labels_train_coords"]).astype(np.float32)
- trY[:, 0] /= 800.0
- trY[:, 1] /= 600.0
- trY = np.array([trY[:, 0]]).reshape((21, 1))
- #############
- # test data #
- #############
- teX = np.array(data_reader.get_data_dict_with_arrays()["data_test"]).astype(np.float32) / 255.0
- teY = np.array(data_reader.get_data_dict_with_arrays()["labels_test_coords"]).astype(np.float32)
- teY[:, 0] /= 800.0
- teY[:, 1] /= 600.0
- teY = np.array([teY[:, 0]]).reshape((7, 1))
- ################
- # reshape data #
- ################
- trX = trX.reshape(-1, 600, 800, 1)
- teX = teX.reshape(-1, 600, 800, 1)
- ################################################################################
- # weight, bias, conv2d and max_pool methods for initialization and calculation #
- ################################################################################
- def weight_variable(shape):
- initial = tf.truncated_normal(shape, stddev=0.1)
- return tf.Variable(initial)
- def bias_variable(shape):
- initial = tf.constant(0.1, shape=shape)
- return tf.Variable(initial)
- def conv2d(x, W):
- return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
- def max_pool_2x2(x):
- return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
- strides=[1, 2, 2, 1], padding='SAME')
- ##############################
- # placeholder for data input #
- ##############################
- x = tf.placeholder("float", shape=[None, 600, 800, 1])
- y_train = tf.placeholder("float", shape=[None, 1])
- # reshape data
- x_image = tf.reshape(x, [-1, 600, 800, 1])
- ########################
- # network architecture #
- ########################
- # conv/maxpool layer
- W_conv1 = weight_variable([5, 5, 1, 8])
- b_conv1 = bias_variable([8])
- h_conv1 = tf.nn.relu((conv2d(x_image, W_conv1) + b_conv1))
- h_pool1 = max_pool_2x2(h_conv1)
- # conv/maxpool layer
- W_conv2 = weight_variable([5, 5, 8, 16])
- b_conv2 = bias_variable([16])
- h_conv2 = tf.nn.relu((conv2d(h_pool1, W_conv2) + b_conv2))
- h_pool2 = max_pool_2x2(h_conv2)
- # fully connected layer
- W_fc1 = weight_variable([150 * 200 * 16, 100])
- b_fc1 = bias_variable([100])
- h_pool2_flat = tf.reshape(h_pool2, [-1, 150 * 200 * 16])
- h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
- keep_prob = tf.placeholder("float")
- h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
- # fully connected softmax layer
- W_fc2 = weight_variable([100, 1])
- b_fc2 = bias_variable([1])
- y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
- ##################
- # train and eval #
- ##################
- cross_entropy = -tf.reduce_sum(y_train * tf.log(y_conv))
- # cross_entropy = tf.reduce_mean(-(y_train * tf.log(y_conv) + (1 - y_train) * tf.log(1 - y_conv)))
- # train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
- train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)
- # train_step = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cross_entropy)
- correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_train, 1))
- accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
- #################
- # session stuff #
- #################
- sess = tf.Session()
- init = tf.initialize_all_variables()
- sess.run(init)
- ###################
- # actual training #
- ###################
- for i in range(1, 1000 + 1):
- # if i % 5 == 0:
- # train_accuracy = sess.run(accuracy, feed_dict={
- # x: trX, y_train: trY, keep_prob: 1.0})
- # print("step {:>3}, training accuracy {}".format(i, train_accuracy))
- # training step
- sess.run(train_step, feed_dict={x: trX, y_train: trY, keep_prob: 0.5})
- #################
- # testing stuff #
- #################
- # testing picture 0
- test_out = sess.run(y_conv, feed_dict={x: [trX[0]],
- keep_prob: 1.0})
- print("out[0]: {:>5.5f} | out original[0]: {:>5.5f} | diff[0]: {:>5.5f}".format(test_out[0][0], trY[0][0], abs(
- test_out[0][0] - trY[0][0])))
- # testing picture 1
- test_out = sess.run(y_conv, feed_dict={x: [trX[1]],
- keep_prob: 1.0})
- print("out[1]: {:>5.5f} | out original[1]: {:>5.5f} | diff[1]: {:>5.5f}".format(test_out[0][0], trY[1][0], abs(
- test_out[0][0] - trY[1][0])))
- # testing picture 2
- test_out = sess.run(y_conv, feed_dict={x: [trX[2]],
- keep_prob: 1.0})
- print("out[2]: {:>5.5f} | out original[2]: {:>5.5f} | diff[2]: {:>5.5f}".format(test_out[0][0], trY[2][0], abs(
- test_out[0][0] - trY[2][0])))
- # testing picture 3
- test_out = sess.run(y_conv, feed_dict={x: [trX[3]],
- keep_prob: 1.0})
- print("out[3]: {:>5.5f} | out original[3]: {:>5.5f} | diff[3]: {:>5.5f}".format(test_out[0][0], trY[3][0], abs(
- test_out[0][0] - trY[3][0])))
- # testing random picture/array
- print(sess.run(y_conv, feed_dict={x: [np.random.rand(600, 800).reshape(600, 800, 1)],
- keep_prob: 1.0}))
- # testing array of zeros
- print(sess.run(y_conv, feed_dict={x: [np.zeros([600, 800, 1])],
- keep_prob: 1.0}))
- # testing array of ones
- print(sess.run(y_conv, feed_dict={x: [np.ones([600, 800, 1])],
- keep_prob: 1.0}))
- #################
- # test accuracy #
- #################
- print("\ntesting accuracy...")
- print("\ntest accuracy: %g" % sess.run(accuracy, feed_dict={
- x: teX, y_train: teY, keep_prob: 1.0}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement