Advertisement
Guest User

Untitled

a guest
Oct 26th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. try:
  5. from scipy import misc
  6. except ImportError:
  7. !pip install scipy
  8. from scipy import misc
  9.  
  10. ############################################ train
  11. training_size = 300
  12. img_size = 20*20*3
  13.  
  14. #class Struct:
  15. # "A structure that can have any fields defined."
  16. # def __init__(self, **entries): self.__dict__.update(entries)
  17. #training_images=[];
  18. #training_labels=[];
  19. #training_data= Struct(training_set=training_images, labels=training_labels)
  20. training_images = np.empty(shape=(training_size,20,20,3))
  21. import glob
  22. i = 0
  23. for filename in glob.glob('D:/Minutia/PrincipleWrinkleMinutia/*.jpg'):
  24. image = misc.imread(filename)
  25. training_images[i] = image
  26. i+=1
  27. print(training_images[0].shape)
  28.  
  29. a= [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  30. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  31. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
  32. training_labels = tf.one_hot(a,3)
  33. sess = tf.Session()
  34. sess.run(training_labels)
  35.  
  36.  
  37. #################################################### test
  38. test_size = 300
  39. img_size = 20*20*3
  40. #class Struct:
  41.  
  42. #test_images=[];
  43. #test_labels=[];
  44. #test_data= Struct(training_set=test_images, labels=test_labels)
  45.  
  46. test_images = np.empty(shape=(test_size,20,20,3))
  47. import glob
  48. i = 0
  49. for filename in glob.glob('D:/Minutia/PrincipleWrinkleMinutia/*.jpg'):
  50. image = misc.imread(filename)
  51. test_images[i] = image
  52. i+=1
  53. print(test_images[0].shape)
  54. a= [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  55. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  56. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
  57. test_labels = tf.one_hot(a,3)
  58. sess = tf.Session()
  59. sess.run(test_labels)
  60.  
  61. x = tf.placeholder(tf.float64, [None, img_size])
  62. W = tf.Variable(tf.zeros([img_size, 3], dtype=tf.float64))
  63. b = tf.Variable(tf.zeros([3], dtype=tf.float64))
  64. y = tf.nn.softmax(tf.matmul(x, W) + b)
  65. y_ = tf.placeholder(tf.float64, [None, 3])
  66. cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
  67. train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
  68. sess = tf.InteractiveSession()
  69. tf.global_variables_initializer().run()
  70.  
  71. import numpy as np
  72.  
  73. def next_batch(num, data, labels):
  74. '''
  75. Return a total of `num` random samples and labels.
  76. '''
  77. idx = np.arange(0 , len(data))
  78. np.random.shuffle(idx)
  79. idx = idx[:num]
  80. data_shuffle = [data[ i] for i in idx]
  81. labels_shuffle = [labels[ i] for i in idx]
  82.  
  83. return np.asarray(data_shuffle), np.asarray(labels_shuffle)
  84.  
  85. #Xtr, Ytr = np.arange(0, 10), np.arange(0, 100).reshape(10, 10)
  86.  
  87. #print(Xtr)
  88. #print(Ytr)
  89.  
  90. #Xtr, Ytr = next_batch(5, Xtr, Ytr)
  91. #print('\n5 random samples')
  92. #print(Xtr)
  93. #print(Ytr)
  94.  
  95. for _ in range(1000):
  96. batch_xs, batch_ys = next_batch(100,training_images,training_labels)
  97. sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
  98. correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
  99. correct_prediction = tf.equal(training_images, training_labels)
  100. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float64))
  101. print(sess.run(accuracy, feed_dict={x: test_images, y_: test_labels}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement