Advertisement
Guest User

Untitled

a guest
Nov 12th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 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. #train
  9. import tensorflow as tf
  10. import numpy as np
  11. import matplotlib.pyplot as plt
  12. try:
  13. from scipy import misc
  14. except ImportError:
  15. !pip install scipy
  16. from scipy import misc
  17.  
  18. training_size = 265
  19. img_size = 400*400
  20. training_data = np.empty(shape=(training_size, 400*400))
  21.  
  22. import glob
  23. i = 0
  24. for filename in glob.glob('D:/TensorPalms/Train/*.jpg'):
  25. image = misc.imread(filename)
  26. print(image.shape)
  27. training_data[i] = image.reshape(-1)
  28. i+=1
  29.  
  30.  
  31. #label
  32.  
  33.  
  34. a= [0,0,0,0,0,
  35. 1,1,1,1,1,
  36. 2,2,2,2,2,
  37. 3,3,3,3,3,
  38. 4,4,4,4,4,
  39. 5,5,5,5,5,
  40. 6,6,6,6,6,
  41. 7,7,7,7,7,
  42. 8,8,8,8,8,
  43. 9,9,9,9,9,
  44. 10,10,10,10,10,
  45. 11,11,11,11,11,
  46. 12,12,12,12,12,
  47. 13,13,13,13,13,
  48. 14,14,14,14,14,
  49. 15,15,15,15,15,
  50. 16,16,16,16,16,
  51. 17,17,17,17,17,
  52. 18,18,18,18,18,
  53. 19,19,19,19,19,
  54. 20,20,20,20,20,
  55. 21,21,21,21,21,
  56. 22,22,22,22,22,
  57. 23,23,23,23,23,
  58. 24,24,24,24,24,
  59. 25,25,25,25,25,
  60. 26,26,26,26,26,
  61. 27,27,27,27,27,
  62. 28,28,28,28,28,
  63. 29,29,29,29,29,
  64. 30,30,30,30,30,
  65. 31,31,31,31,31,
  66. 32,32,32,32,32,
  67. 33,33,33,33,33,
  68. 34,34,34,34,34,
  69. 35,35,35,35,35,
  70. 36,36,36,36,36,
  71. 37,37,37,37,37,
  72. 38,38,38,38,38,
  73. 39,39,39,39,39,
  74. 40,40,40,40,40,
  75. 41,41,41,41,41,
  76. 42,42,42,42,42,
  77. 43,43,43,43,43,
  78. 44,44,44,44,44,
  79. 45,45,45,45,45,
  80. 46,46,46,46,46,
  81. 47,47,47,47,47,
  82. 48,48,48,48,48,
  83. 49,49,49,49,49,
  84. 50,50,50,50,50,
  85. 51,51,51,51,51,
  86. 52,52,52,52,52,]
  87.  
  88.  
  89.  
  90. b = tf.one_hot(a,53)
  91. sess = tf.Session()
  92. sess.run(b)
  93.  
  94. print(b.shape)
  95.  
  96.  
  97. #test
  98. import tensorflow as tf
  99. test_size = 159
  100. img_size = 400*400
  101.  
  102. test_images = np.empty(shape=(test_size,400*400))
  103. import glob
  104. i = 0
  105. for filename in glob.glob('D:/TensorPalms/Test*.jpg'):
  106. image = misc.imread(filename)
  107. test_images[i] = image
  108. i+=1
  109. print(test_images[0].shape)
  110.  
  111. c= [0,0,0,
  112. 1,1,1,
  113. 2,2,2,
  114. 3,3,3,
  115. 4,4,4,
  116. 5,5,5,
  117. 6,6,6,
  118. 7,7,7,
  119. 8,8,8,
  120. 9,9,9,
  121. 10,10,10,
  122. 11,11,11,
  123. 12,12,12,
  124. 13,13,13,
  125. 14,14,14,
  126. 15,15,15,
  127. 16,16,16,
  128. 17,17,17,
  129. 18,18,18,
  130. 19,19,19,
  131. 20,20,20,
  132. 21,21,21,
  133. 22,22,22,
  134. 23,23,23,
  135. 24,24,24,
  136. 25,25,25,
  137. 26,26,26,
  138. 27,27,27,
  139. 28,28,28,
  140. 29,29,29,
  141. 30,30,30,
  142. 31,31,31,
  143. 32,32,32,
  144. 33,33,33,
  145. 34,34,34,
  146. 35,35,35,
  147. 36,36,36,
  148. 37,37,37,
  149. 38,38,38,
  150. 39,39,39,
  151. 40,40,40,
  152. 41,41,41,
  153. 42,42,42,
  154. 43,43,43,
  155. 44,44,44,
  156. 45,45,45,
  157. 46,46,46,
  158. 47,47,47,
  159. 48,48,48,
  160. 49,49,49,
  161. 50,50,50,
  162. 51,51,51,
  163. 52,52,52]
  164.  
  165.  
  166. test_labels = tf.one_hot(c,53)
  167. sess = tf.Session()
  168. sess.run(test_labels)
  169.  
  170.  
  171. import tensorflow as tf
  172. tf.reset_default_graph()
  173. from __future__ import division, print_function, absolute_import
  174.  
  175. import tflearn
  176. from tflearn.layers.core import input_data, dropout, fully_connected
  177. from tflearn.layers.conv import conv_2d, max_pool_2d
  178. from tflearn.layers.normalization import local_response_normalization
  179. from tflearn.layers.estimator import regression
  180.  
  181.  
  182. network = input_data(shape=[None, 400, 400,3])
  183. network = conv_2d(network, 96, 11, strides=4, activation='relu')
  184. network = max_pool_2d(network, 3, strides=2)
  185. network = local_response_normalization(network)
  186. network = conv_2d(network, 256, 5, activation='relu')
  187. network = max_pool_2d(network, 3, strides=2)
  188. network = local_response_normalization(network)
  189. network = conv_2d(network, 384, 3, activation='relu')
  190. network = conv_2d(network, 384, 3, activation='relu')
  191. network = conv_2d(network, 256, 3, activation='relu')
  192. network = max_pool_2d(network, 3, strides=2)
  193. network = local_response_normalization(network)
  194. network = fully_connected(network, 4096, activation='tanh')
  195. network = dropout(network, 0.5)
  196. network = fully_connected(network, 4096, activation='tanh')
  197. network = dropout(network, 0.5)
  198. network = fully_connected(network, 53, activation='softmax')
  199. network = regression(network, optimizer='momentum',
  200. loss='categorical_crossentropy',
  201. learning_rate=0.001)
  202. model = tflearn.DNN(network, checkpoint_path='model_alexnet',
  203. max_checkpoints=1, tensorboard_verbose=2)
  204.  
  205. model.fit(training_data, b, n_epoch=1000, validation_set=(test_images, test_labels),
  206. shuffle=True, show_metric=True, batch_size=64, snapshot_step=200,
  207. snapshot_epoch=False, run_id='alexnet_test')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement