Advertisement
Guest User

Untitled

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