Advertisement
Guest User

Untitled

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