Advertisement
Guest User

Untitled

a guest
Oct 28th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 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. #################################################### test
  39. test_size = 300
  40. img_size = 20*20*3
  41. #class Struct:
  42.  
  43. #test_images=[];
  44. #test_labels=[];
  45. #test_data= Struct(training_set=test_images, labels=test_labels)
  46.  
  47. test_images = np.empty(shape=(test_size,20,20,3))
  48. import glob
  49. i = 0
  50. for filename in glob.glob('D:/Minutia/PrincipleWrinkleMinutia/*.jpg'):
  51. image = misc.imread(filename)
  52. test_images[i] = image
  53. i+=1
  54. print(test_images[0].shape)
  55. 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,
  56. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  57. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
  58. test_labels = tf.one_hot(a,3)
  59. sess = tf.Session()
  60. sess.run(test_labels)
  61.  
  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.  
  74. import numpy as np
  75.  
  76. def next_batch(num, data, labels):
  77. '''
  78. Return a total of `num` random samples and labels.
  79. '''
  80. idx = np.arange(0 , len(data))
  81. np.random.shuffle(idx)
  82. idx = idx[:num]
  83. data_shuffle = [data[ i] for i in idx]
  84. labels_shuffle = [labels[ i] for i in idx]
  85.  
  86. return np.asarray(data_shuffle), np.asarray(labels_shuffle)
  87.  
  88. #Xtr, Ytr = np.arange(0, 10), np.arange(0, 100).reshape(10, 10)
  89.  
  90. #print(Xtr)
  91. #print(Ytr)
  92.  
  93. #Xtr, Ytr = next_batch(5, Xtr, Ytr)
  94. #print('\n5 random samples')
  95. #print(Xtr)
  96. #print(Ytr)
  97.  
  98. for _ in range(1000):
  99. batch_size=100
  100.  
  101. batch_xs, batch_ys = next_batch(100,training_images,training_labels)
  102.  
  103.  
  104. array_batch_xs= np.reshape(batch_xs, [-1, img_size ])
  105.  
  106.  
  107. sess.run(train_step, feed_dict={x: array_batch_xs, y: batch_ys})
  108. correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
  109. correct_prediction = tf.equal(training_images, training_labels)
  110. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float64))
  111. print(sess.run(accuracy, feed_dict={x: test_images, y_: test_labels}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement