Advertisement
tuttelikz

tf_v4[validation] +

Jul 28th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.15 KB | None | 0 0
  1. import tensorflow as tf
  2. import h5py
  3. import numpy as np
  4. from sklearn.model_selection import train_test_split
  5.  
  6. with h5py.File('preprocessed_skin_img_orig.hdf5', 'r') as hf:
  7.             img_data = hf['preprocessed_skin_img_data'][:]
  8.             label_data = hf["preprocessed_skin_img_label"][:]
  9.  
  10. X_train, X_test, y_train, y_test = train_test_split(img_data, label_data, test_size=0.33, random_state=42)
  11. X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=0.33, random_state=42)
  12.  
  13. #img_data_arr_resh = np.reshape(img_data, [29839,34560])
  14. onehot_label_data_train = tf.one_hot(y_train, 2)
  15. onehot_label_data_valid = tf.one_hot(y_valid, 2)
  16. onehot_label_data_test = tf.one_hot(y_test, 2)
  17.  
  18. #label_data_arr_resh = np.reshape(label_data_as_array, [29839,1])
  19.  
  20. x = tf.placeholder(tf.float32, shape=[None,128,270,1])
  21. y_ = tf.placeholder(tf.float32, shape=[None, 2])
  22. #x_image = tf.reshape(x, [-1, 128, 270, 1])
  23.  
  24. def weight_variable(shape):
  25.   initial = tf.truncated_normal(shape, stddev=0.1)
  26.   return tf.Variable(initial)
  27.  
  28. def bias_variable(shape):
  29.   initial = tf.constant(0.1, shape=shape)
  30.   return tf.Variable(initial)
  31.  
  32. def conv2d(x, W):
  33.   return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
  34.  
  35. def max_pool_2x2(x):
  36.   return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
  37.                         strides=[1, 2, 2, 1], padding='SAME')
  38.  
  39. W_conv1 = weight_variable([5, 5, 1, 32])
  40. b_conv1 = bias_variable([32])
  41.  
  42. #x_image = tf.reshape(img_data, [-1, 128, 270, 1])
  43.  
  44. h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1)
  45. h_pool1 = max_pool_2x2(h_conv1)
  46.  
  47. W_conv2 = weight_variable([5, 5, 32, 64])
  48. b_conv2 = bias_variable([64])
  49.  
  50. h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
  51. h_pool2 = max_pool_2x2(h_conv2)
  52.  
  53. W_fc1 = weight_variable([32*68*64, 1024])
  54. b_fc1 = bias_variable([1024])
  55.  
  56. h_pool2_flat = tf.reshape(h_pool2, [-1, 32*68*64])
  57. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
  58.  
  59. keep_prob = tf.placeholder(tf.float32)
  60. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
  61.  
  62. W_fc2 = weight_variable([1024, 2])
  63. b_fc2 = bias_variable([2])
  64.  
  65. y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
  66.  
  67. cross_entropy = tf.reduce_mean(
  68.     tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
  69. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  70. correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
  71. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  72.  
  73. with tf.Session() as sess:
  74.     onehot_label_data_train = sess.run(onehot_label_data_train)
  75.     onehot_label_data_valid = sess.run(onehot_label_data_valid)
  76.     onehot_label_data_test = sess.run(onehot_label_data_test)
  77.     sess.run(tf.global_variables_initializer())
  78.     print('Training phase:\n') #print('Epoch %d'%(j))
  79.     for j in range(10):
  80.         batch_count = 0 #avg_train_acc = 0
  81.         tot_valid_acc = 0
  82.         for i in range(0, 13394, 128): #29839 215
  83.             train_step.run(feed_dict={x:X_train[i:i+128,:,:,:],y_:onehot_label_data_train[i:i+128,:],keep_prob: 0.5})
  84.         for k in range(0, 6597, 128): #29839 215
  85.             batch_count += 1
  86.             valid_accuracy = accuracy.eval(feed_dict={x:X_valid[k:k+128,:,:,:],y_:onehot_label_data_valid[k:k+128,:], keep_prob: 1.0})
  87.             tot_valid_acc = tot_valid_acc + valid_accuracy
  88.         avg_valid_acc = tot_valid_acc / batch_count
  89.         print('epoch %d, validation accuracy %g' % (j,avg_valid_acc))
  90.     print('Testing:\n') #for j in range(5): print('Epoch %d'%(j))
  91.     batch_count = 0
  92.     tot_test_acc = 0
  93.     for i in range(0, 9846, 128):
  94.         batch_count += 1
  95.         test_accuracy = accuracy.eval(feed_dict={x:X_test[i:i+128,:,:,:],y_:onehot_label_data_test[i:i+128,:], keep_prob: 1.0})
  96.         tot_test_acc = tot_test_acc + test_accuracy
  97.     avg_test_acc = tot_test_acc / batch_count
  98.     print('Test accuracy %g' % (avg_test_acc))
  99.  
  100.  
  101. '''
  102. Training phase:
  103.  
  104. epoch 0, validation accuracy 0.779632
  105. epoch 1, validation accuracy 0.892505
  106. epoch 2, validation accuracy 0.937702
  107. epoch 3, validation accuracy 0.960538
  108. epoch 4, validation accuracy 0.970403
  109. epoch 5, validation accuracy 0.985753
  110. epoch 6, validation accuracy 0.991136
  111. epoch 7, validation accuracy 0.995042
  112. epoch 8, validation accuracy 0.997897
  113. epoch 9, validation accuracy 0.998197
  114. Testing:
  115.  
  116. Test accuracy 0.998072
  117. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement