Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. def load_data(data_directory):
  2. directories = [d for d in os.listdir(data_directory)
  3. if os.path.isdir(os.path.join(data_directory, d))]
  4. labels = []
  5. images = []
  6. for d in directories:
  7. label_directory = os.path.join(data_directory, d)
  8. file_names = [os.path.join(label_directory, f)
  9. for f in os.listdir(label_directory)
  10. if f.endswith(".ppm")]
  11. for f in file_names:
  12. images.append(skimage.data.imread(f))
  13. labels.append(int(d))
  14. return images, labels
  15.  
  16. import os
  17. import skimage
  18. from skimage import transform
  19. from skimage.color import rgb2gray
  20. import numpy as np
  21. import keras
  22. from keras import layers
  23. from keras.layers import Dense
  24. ROOT_PATH = "C://Users//Jay//AppData//Local//Programs//Python//Python37//Scriptcodes//BelgianSignals"
  25. train_data_directory = os.path.join(ROOT_PATH, "Training")
  26. test_data_directory = os.path.join(ROOT_PATH, "Testing")
  27.  
  28. images, labels = load_data(train_data_directory)
  29.  
  30.  
  31. # Print the `labels` dimensions
  32. print(np.array(labels))
  33.  
  34. # Print the number of `labels`'s elements
  35. print(np.array(labels).size)
  36.  
  37. # Count the number of labels
  38. print(len(set(np.array(labels))))
  39.  
  40. # Print the `images` dimensions
  41. print(np.array(images))
  42.  
  43. # Print the number of `images`'s elements
  44. print(np.array(images).size)
  45.  
  46. # Print the first instance of `images`
  47. np.array(images)[0]
  48.  
  49. images28 = [transform.resize(image, (28, 28)) for image in images]
  50.  
  51. images28 = np.array(images28)
  52.  
  53. images28 = rgb2gray(images28)
  54.  
  55. # Import `tensorflow`
  56. import tensorflow as tf
  57.  
  58. # Initialize placeholders
  59. x = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28])
  60. y = tf.placeholder(dtype = tf.int32, shape = [None])
  61.  
  62. # Flatten the input data
  63. images_flat = tf.keras.layers.flatten(x)
  64.  
  65. # Fully connected layer
  66. logits = tf.contrib.layers.dense(images_flat, 62, tf.nn.relu)
  67.  
  68. # Define a loss function
  69. loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y,
  70. logits = logits))
  71. # Define an optimizer
  72. train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
  73.  
  74. # Convert logits to label indexes
  75. correct_pred = tf.argmax(logits, 1)
  76.  
  77. # Define an accuracy metric
  78. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement