Guest User

Untitled

a guest
Mar 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. from __future__ import absolute_import
  2. from __future__ import division
  3. from __future__ import print_function
  4.  
  5. # Imports
  6. import numpy as np
  7. import tensorflow as tf
  8.  
  9.  
  10. def cnn_model_fn(features, labels, mode):
  11. """Model function for CNN."""
  12. # Input Layer
  13. # array[0] = batch size = Size of the subset of examples to use when performing gradient descent during training.
  14. # array[1] = width of images
  15. # array[2] = height of images
  16. # array[3] = channels = Number of color channels in the example images (1 for grey, 3 for rgb)
  17. input_layer = tf.reshape(features, [-1, 40, 40, 1])
  18.  
  19. # Convolutional Layer #1
  20. conv1 = tf.layers.conv2d(
  21. inputs=input_layer,
  22. filters=32,
  23. kernel_size=[5, 5],
  24. # To specify that the output tensor should have the same width and height values as the input tensor
  25. # value can be "same" ou "valid"
  26. padding="same",
  27. activation=tf.nn.relu)
  28.  
  29. # Pooling Layer #1
  30. pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
  31.  
  32. # Convolutional Layer #2 and Pooling Layer #2
  33. conv2 = tf.layers.conv2d(
  34. inputs=pool1,
  35. filters=64,
  36. kernel_size=[5, 5],
  37. padding="same",
  38. activation=tf.nn.relu)
  39. pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
  40.  
  41. # Dense Layer
  42. # pool2 width * pool2 height * pool2 channels
  43. pool2_shape = pool2.get_shape().as_list()
  44. pool2_flat = tf.reshape(pool2, [-1, pool2_shape[1] * pool2_shape[2] * pool2_shape[3]])
  45. dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
  46. dropout = tf.layers.dropout(
  47. inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)
  48.  
  49. # Logits Layer
  50. logits = tf.layers.dense(inputs=dropout, units=1)
  51.  
  52. print("================")
  53. print(logits)
  54. print("================")
  55. print("================")
  56. print(labels)
  57. print("================")
  58.  
  59. predictions = {
  60. # Generate predictions (for PREDICT and EVAL mode)
  61. "classes": tf.argmax(input=logits, axis=1),
  62. # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
  63. # `logging_hook`.
  64. "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
  65. }
  66.  
  67. if mode == tf.estimator.ModeKeys.PREDICT:
  68. return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
  69.  
  70. # Calculate Loss (for both TRAIN and EVAL modes)
  71. loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
  72.  
  73. # Configure the Training Op (for TRAIN mode)
  74. if mode == tf.estimator.ModeKeys.TRAIN:
  75. optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
  76. train_op = optimizer.minimize(
  77. loss=loss,
  78. global_step=tf.train.get_global_step())
  79. return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
  80.  
  81. # Add evaluation metrics (for EVAL mode)
  82. eval_metric_ops = {
  83. "accuracy": tf.metrics.accuracy(
  84. labels=labels, predictions=predictions["classes"])}
  85. return tf.estimator.EstimatorSpec(
  86. mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
  87.  
  88.  
  89. def train_input_fn_custom(filenames_array, labels_array, batch_size):
  90. # Reads an image from a file, decodes it into a dense tensor, and resizes it to a fixed shape.
  91. def _parse_function(filename, label):
  92. image_string = tf.read_file(filename)
  93. image_decoded = tf.image.decode_png(image_string)
  94. image_resized = tf.image.resize_images(image_decoded, [40, 40])
  95. return image_resized, label
  96.  
  97. filenames = tf.constant(filenames_array)
  98. labels = tf.constant(labels_array)
  99.  
  100. # Convert the inputs to a Dataset
  101. dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
  102.  
  103. # Convert filenames to a decoded image
  104. dataset = dataset.map(_parse_function)
  105.  
  106. # Shuffle, repeat, and batch the examples.
  107. dataset = dataset.shuffle(1000).repeat().batch(batch_size)
  108.  
  109. # Build the Iterator, and return the read end of the pipeline.
  110. return dataset.make_one_shot_iterator().get_next()
  111.  
  112.  
  113. def train_label_custom():
  114. return [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
  115.  
  116.  
  117. def eval_input_fn_custom(filenames_array, labels_array, batch_size):
  118. # Reads an image from a file, decodes it into a dense tensor, and resizes it to a fixed shape.
  119. def _parse_function(filename, label):
  120. image_string = tf.read_file(filename)
  121. image_decoded = tf.image.decode_png(image_string)
  122. image_resized = tf.image.resize_images(image_decoded, [40, 40])
  123. return image_resized, label
  124.  
  125. filenames = tf.constant(filenames_array)
  126. labels = tf.constant(labels_array)
  127.  
  128. # Convert the inputs to a Dataset
  129. dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
  130.  
  131. # Convert filenames to a decoded image
  132. dataset = dataset.map(_parse_function)
  133.  
  134. # Shuffle, repeat, and batch the examples.
  135. dataset = dataset.batch(batch_size)
  136.  
  137. # Build the Iterator, and return the read end of the pipeline.
  138. return dataset.make_one_shot_iterator().get_next()
  139.  
  140.  
  141. def eval_label_custom():
  142. return [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
  143.  
  144.  
  145. def main(self):
  146. tf.logging.set_verbosity(tf.logging.INFO)
  147.  
  148. filenames_train = ['blackcorner-data/1.png', 'blackcorner-data/2.png', 'blackcorner-data/3.png',
  149. 'blackcorner-data/4.png', 'blackcorner-data/n1.png', 'blackcorner-data/n2.png',
  150. 'blackcorner-data/n3.png', 'blackcorner-data/n4.png',
  151. 'blackcorner-data/11.png', 'blackcorner-data/21.png', 'blackcorner-data/31.png',
  152. 'blackcorner-data/41.png', 'blackcorner-data/n11.png', 'blackcorner-data/n21.png',
  153. 'blackcorner-data/n31.png', 'blackcorner-data/n41.png'
  154. ]
  155.  
  156. filenames_eval = ['blackcorner-data-eval/1.png', 'blackcorner-data-eval/2.png', 'blackcorner-data-eval/3.png',
  157. 'blackcorner-data-eval/4.png', 'blackcorner-data-eval/n1.png', 'blackcorner-data-eval/n2.png',
  158. 'blackcorner-data-eval/n3.png', 'blackcorner-data-eval/n4.png',
  159. 'blackcorner-data-eval/11.png', 'blackcorner-data-eval/21.png', 'blackcorner-data-eval/31.png',
  160. 'blackcorner-data-eval/41.png', 'blackcorner-data-eval/n11.png', 'blackcorner-data-eval/n21.png',
  161. 'blackcorner-data-eval/n31.png', 'blackcorner-data-eval/n41.png'
  162. ]
  163.  
  164. # Create the Estimator
  165. mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/test_convnet_model")
  166.  
  167. # Set up logging for predictions
  168. tensors_to_log = {"probabilities": "softmax_tensor"}
  169. logging_hook = tf.train.LoggingTensorHook(
  170. tensors=tensors_to_log, every_n_iter=50)
  171.  
  172. # Train the model
  173. cust_train_input_fn = lambda: train_input_fn_custom(
  174. filenames_array=filenames_train, labels_array=train_label_custom(), batch_size=1)
  175. mnist_classifier.train(
  176. input_fn=cust_train_input_fn,
  177. steps=20000,
  178. hooks=[logging_hook])
  179.  
  180. # Evaluate the model and print results
  181. # eval_input_fn = tf.estimator.inputs.numpy_input_fn(
  182. # x={"x": eval_data},
  183. # y=eval_labels,
  184. # num_epochs=1,
  185. # shuffle=False)
  186. # eval_results = mnist_classifier.evaluate(input_fn=cust_eval_input_fn)
  187. # print("======================")
  188. # print("Eval results : ")
  189. # print(eval_results)
  190.  
  191.  
  192. if __name__ == "__main__":
  193. tf.app.run()
Add Comment
Please, Sign In to add comment