Advertisement
Guest User

Untitled

a guest
Nov 16th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1.  
  2.  
  3.  
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. try:
  7. from scipy import misc
  8. except ImportError:
  9. !pip install scipy
  10. from scipy import misc
  11.  
  12. import tensorflow as tf
  13. import numpy as np
  14. import imageio
  15. import matplotlib.pyplot as plt
  16. try:
  17. from scipy import misc
  18. except ImportError:
  19. !pip install scipy
  20. from scipy import misc
  21.  
  22.  
  23. training_size = 265
  24. col_row = 400
  25. img_size = 400*400
  26. training_data = np.empty(shape=(training_size, 400*400))
  27.  
  28. import glob
  29. i = 0
  30. for filename in glob.glob('C:/Mah/TensorPalms/Train/*.jpg'):
  31. image = imageio.imread(filename)
  32. print(image.shape)
  33. training_data[i] = image.reshape(-1)
  34. i+=1
  35.  
  36.  
  37. #label
  38.  
  39.  
  40. a= [0,0,0,0,0,
  41. 1,1,1,1,1,
  42. 2,2,2,2,2,
  43. 3,3,3,3,3,
  44. 4,4,4,4,4,
  45. 5,5,5,5,5,
  46. 6,6,6,6,6,
  47. 7,7,7,7,7,
  48. 8,8,8,8,8,
  49. 9,9,9,9,9,
  50. 10,10,10,10,10,
  51. 11,11,11,11,11,
  52. 12,12,12,12,12,
  53. 13,13,13,13,13,
  54. 14,14,14,14,14,
  55. 15,15,15,15,15,
  56. 16,16,16,16,16,
  57. 17,17,17,17,17,
  58. 18,18,18,18,18,
  59. 19,19,19,19,19,
  60. 20,20,20,20,20,
  61. 21,21,21,21,21,
  62. 22,22,22,22,22,
  63. 23,23,23,23,23,
  64. 24,24,24,24,24,
  65. 25,25,25,25,25,
  66. 26,26,26,26,26,
  67. 27,27,27,27,27,
  68. 28,28,28,28,28,
  69. 29,29,29,29,29,
  70. 30,30,30,30,30,
  71. 31,31,31,31,31,
  72. 32,32,32,32,32,
  73. 33,33,33,33,33,
  74. 34,34,34,34,34,
  75. 35,35,35,35,35,
  76. 36,36,36,36,36,
  77. 37,37,37,37,37,
  78. 38,38,38,38,38,
  79. 39,39,39,39,39,
  80. 40,40,40,40,40,
  81. 41,41,41,41,41,
  82. 42,42,42,42,42,
  83. 43,43,43,43,43,
  84. 44,44,44,44,44,
  85. 45,45,45,45,45,
  86. 46,46,46,46,46,
  87. 47,47,47,47,47,
  88. 48,48,48,48,48,
  89. 49,49,49,49,49,
  90. 50,50,50,50,50,
  91. 51,51,51,51,51,
  92. 52,52,52,52,52,]
  93.  
  94.  
  95.  
  96. b = tf.one_hot(a,53)
  97. sess = tf.Session()
  98. sess.run(b)
  99.  
  100. print(b.shape)
  101.  
  102. from sklearn.preprocessing import OneHotEncoder
  103. training_label = OneHotEncoder(sparse=False).fit_transform(np.asarray(a).reshape(-1, 1))
  104. print(training_label)
  105.  
  106.  
  107.  
  108. #test
  109. import tensorflow as tf
  110. import numpy as np
  111. import matplotlib.pyplot as plt
  112. try:
  113. from scipy import misc
  114. except ImportError:
  115. !pip install scipy
  116. from scipy import misc
  117.  
  118. test_size = 159
  119. img_size = 400*400
  120.  
  121. test_images = np.empty(shape=(test_size,400*400))
  122. import glob
  123. i = 0
  124. for filename in glob.glob('C:/Mah/TensorPalms/Test/*.jpg'):
  125. image = imageio.imread(filename)
  126. print(image.shape)
  127. test_images[i] = image.reshape(-1)
  128. i+=1
  129.  
  130.  
  131. c= [0,0,0,
  132. 1,1,1,
  133. 2,2,2,
  134. 3,3,3,
  135. 4,4,4,
  136. 5,5,5,
  137. 6,6,6,
  138. 7,7,7,
  139. 8,8,8,
  140. 9,9,9,
  141. 10,10,10,
  142. 11,11,11,
  143. 12,12,12,
  144. 13,13,13,
  145. 14,14,14,
  146. 15,15,15,
  147. 16,16,16,
  148. 17,17,17,
  149. 18,18,18,
  150. 19,19,19,
  151. 20,20,20,
  152. 21,21,21,
  153. 22,22,22,
  154. 23,23,23,
  155. 24,24,24,
  156. 25,25,25,
  157. 26,26,26,
  158. 27,27,27,
  159. 28,28,28,
  160. 29,29,29,
  161. 30,30,30,
  162. 31,31,31,
  163. 32,32,32,
  164. 33,33,33,
  165. 34,34,34,
  166. 35,35,35,
  167. 36,36,36,
  168. 37,37,37,
  169. 38,38,38,
  170. 39,39,39,
  171. 40,40,40,
  172. 41,41,41,
  173. 42,42,42,
  174. 43,43,43,
  175. 44,44,44,
  176. 45,45,45,
  177. 46,46,46,
  178. 47,47,47,
  179. 48,48,48,
  180. 49,49,49,
  181. 50,50,50,
  182. 51,51,51,
  183. 52,52,52]
  184.  
  185.  
  186. test_labels = tf.one_hot(c,53)
  187. sess = tf.Session()
  188. sess.run(test_labels)
  189.  
  190.  
  191.  
  192.  
  193. from sklearn.preprocessing import OneHotEncoder
  194. test_label = OneHotEncoder(sparse=False).fit_transform(np.asarray(c).reshape(-1, 1))
  195. print(test_label)
  196.  
  197.  
  198. import tensorflow as tf
  199. sess = tf.InteractiveSession()
  200.  
  201.  
  202.  
  203.  
  204. def weight_variable(shape):
  205. initial = tf.truncated_normal(shape, stddev=0.1)
  206. return tf.Variable(initial)
  207.  
  208. def bias_variable(shape):
  209. initial = tf.constant(0.1, shape=shape)
  210. return tf.Variable(initial)
  211.  
  212.  
  213.  
  214. def conv2d(x, W):
  215. return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
  216.  
  217. def max_pool_2x2(x):
  218. return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
  219. strides=[1, 2, 2, 1], padding='SAME')
  220.  
  221.  
  222.  
  223. # Input layer
  224.  
  225. x = tf.placeholder(tf.float32, [None, img_size])
  226. y_ = tf.placeholder(tf.float32, [None, 53])
  227. x_image = tf.reshape(x, [-1, 400, 400, 1])
  228.  
  229. #First Convolutional Layer,Convolutional layer 1
  230.  
  231.  
  232. W_conv1 = weight_variable([5, 5, 1, 32])
  233. b_conv1 = bias_variable([32])
  234. x_image = tf.reshape(x, [-1, 400, 400, 1])
  235.  
  236. h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
  237. h_pool1 = max_pool_2x2(h_conv1)
  238.  
  239. #Second Convolutional Layer,Convolutional layer 2
  240.  
  241. W_conv2 = weight_variable([5, 5, 32, 64])
  242. b_conv2 = bias_variable([64])
  243.  
  244. h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
  245. h_pool2 = max_pool_2x2(h_conv2)
  246.  
  247.  
  248. #Densely Connected Layer
  249.  
  250. W_fc1 = weight_variable([7 * 7 * 64, 1024])
  251. b_fc1 = bias_variable([1024 ])
  252.  
  253. h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
  254. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
  255.  
  256.  
  257. #Dropout
  258.  
  259. keep_prob = tf.placeholder(tf.float32)
  260. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
  261.  
  262. #Readout Layer,Fully connected layer (Output layer)
  263.  
  264. W_fc2 = weight_variable([1024, 53])
  265. b_fc2 = bias_variable([53])
  266.  
  267. y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
  268.  
  269.  
  270.  
  271. import numpy as np
  272.  
  273. def next_batch(num, data, labels):
  274. '''
  275. Return a total of `num` random samples and labels.
  276. '''
  277. idx = np.arange(0 , len(data))
  278. np.random.shuffle(idx)
  279. idx = idx[:num]
  280. data_shuffle = [data[ i] for i in idx]
  281. labels_shuffle = [labels[ i] for i in idx]
  282.  
  283. return np.asarray(data_shuffle), np.asarray(labels_shuffle)
  284.  
  285.  
  286.  
  287.  
  288. cross_entropy = tf.reduce_mean(
  289. tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
  290. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  291. correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
  292. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  293.  
  294. with tf.Session() as sess:
  295. sess.run(tf.global_variables_initializer())
  296. for i in range(20000):
  297. batch_xs, batch_ys = next_batch(50,training_data,training_label)
  298. array_batch_xs = np.reshape(batch_xs, [-1, img_size ])
  299.  
  300. if i % 100 == 0:
  301. train_accuracy = accuracy.eval(feed_dict={
  302. x: array_batch_xs, y_: batch_ys, keep_prob: 1.0})
  303. print('step %d, training accuracy %g' % (i, train_accuracy))
  304. train_step.run(feed_dict={x: array_batch_xs, y_: batch_ys, keep_prob: 0.5})
  305.  
  306. print('test accuracy %g' % accuracy.eval(feed_dict={
  307. x: test_images, y_: test_label, keep_prob: 1.0}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement