build_an_image_dataset.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. """ Build an Image Dataset in TensorFlow.
  2. For this example, you need to make your own set of images (JPEG).
  3. We will show 2 different ways to build that dataset:
  4. - From a root folder, that will have a sub-folder containing images for each class
  5. ```
  6. ROOT_FOLDER
  7. |-------- SUBFOLDER (CLASS 0)
  8. | |
  9. | | ----- image1.jpg
  10. | | ----- image2.jpg
  11. | | ----- etc...
  12. |
  13. |-------- SUBFOLDER (CLASS 1)
  14. | |
  15. | | ----- image1.jpg
  16. | | ----- image2.jpg
  17. | | ----- etc...
  18. ```
  19. - From a plain text file, that will list all images with their class ID:
  20. ```
  21. /path/to/image/1.jpg CLASS_ID
  22. /path/to/image/2.jpg CLASS_ID
  23. /path/to/image/3.jpg CLASS_ID
  24. /path/to/image/4.jpg CLASS_ID
  25. etc...
  26. ```
  27. Below, there are some parameters that you need to change (Marked 'CHANGE HERE'),
  28. such as the dataset path.
  29. Author: Aymeric Damien
  30. Project: https://github.com/aymericdamien/TensorFlow-Examples/
  31. """
  32. from __future__ import print_function
  33. import tensorflow as tf
  34. import os
  35. # Dataset Parameters - CHANGE HERE
  36. MODE = 'folder' # or 'file', if you choose a plain text file (see above).
  37. DATASET_PATH = '/path/to/dataset/' # the dataset file or root folder path.
  38. # Image Parameters
  39. N_CLASSES = 2 # CHANGE HERE, total number of classes
  40. IMG_HEIGHT = 64 # CHANGE HERE, the image height to be resized to
  41. IMG_WIDTH = 64 # CHANGE HERE, the image width to be resized to
  42. CHANNELS = 3 # The 3 color channels, change to 1 if grayscale
  43. # Reading the dataset
  44. # 2 modes: 'file' or 'folder'
  45. def read_images(dataset_path, mode, batch_size):
  46. imagepaths, labels = list(), list()
  47. if mode == 'file':
  48. # Read dataset file
  49. data = open(dataset_path, 'r').read().splitlines()
  50. for d in data:
  51. imagepaths.append(d.split(' ')[0])
  52. labels.append(int(d.split(' ')[1]))
  53. elif mode == 'folder':
  54. # An ID will be affected to each sub-folders by alphabetical order
  55. label = 0
  56. # List the directory
  57. try: # Python 2
  58. classes = sorted(os.walk(dataset_path).next()[1])
  59. except Exception: # Python 3
  60. classes = sorted(os.walk(dataset_path).__next__()[1])
  61. # List each sub-directory (the classes)
  62. for c in classes:
  63. c_dir = os.path.join(dataset_path, c)
  64. try: # Python 2
  65. walk = os.walk(c_dir).next()
  66. except Exception: # Python 3
  67. walk = os.walk(c_dir).__next__()
  68. # Add each image to the training set
  69. for sample in walk[2]:
  70. # Only keeps jpeg images
  71. if sample.endswith('.jpg') or sample.endswith('.jpeg'):
  72. imagepaths.append(os.path.join(c_dir, sample))
  73. labels.append(label)
  74. label += 1
  75. else:
  76. raise Exception("Unknown mode.")
  77. # Convert to Tensor
  78. imagepaths = tf.convert_to_tensor(imagepaths, dtype=tf.string)
  79. labels = tf.convert_to_tensor(labels, dtype=tf.int32)
  80. # Build a TF Queue, shuffle data
  81. image, label = tf.train.slice_input_producer([imagepaths, labels],
  82. shuffle=True)
  83. # Read images from disk
  84. image = tf.read_file(image)
  85. image = tf.image.decode_jpeg(image, channels=CHANNELS)
  86. # Resize images to a common size
  87. image = tf.image.resize_images(image, [IMG_HEIGHT, IMG_WIDTH])
  88. # Normalize
  89. image = image * 1.0/127.5 - 1.0
  90. # Create batches
  91. X, Y = tf.train.batch([image, label], batch_size=batch_size,
  92. capacity=batch_size * 8,
  93. num_threads=4)
  94. return X, Y
  95. # -----------------------------------------------
  96. # THIS IS A CLASSIC CNN (see examples, section 3)
  97. # -----------------------------------------------
  98. # Note that a few elements have changed (usage of queues).
  99. # Parameters
  100. learning_rate = 0.001
  101. num_steps = 10000
  102. batch_size = 128
  103. display_step = 100
  104. # Network Parameters
  105. dropout = 0.75 # Dropout, probability to keep units
  106. # Build the data input
  107. X, Y = read_images(DATASET_PATH, MODE, batch_size)
  108. # Create model
  109. def conv_net(x, n_classes, dropout, reuse, is_training):
  110. # Define a scope for reusing the variables
  111. with tf.variable_scope('ConvNet', reuse=reuse):
  112. # Convolution Layer with 32 filters and a kernel size of 5
  113. conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
  114. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  115. conv1 = tf.layers.max_pooling2d(conv1, 2, 2)
  116. # Convolution Layer with 32 filters and a kernel size of 5
  117. conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
  118. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  119. conv2 = tf.layers.max_pooling2d(conv2, 2, 2)
  120. # Flatten the data to a 1-D vector for the fully connected layer
  121. fc1 = tf.contrib.layers.flatten(conv2)
  122. # Fully connected layer (in contrib folder for now)
  123. fc1 = tf.layers.dense(fc1, 1024)
  124. # Apply Dropout (if is_training is False, dropout is not applied)
  125. fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)
  126. # Output layer, class prediction
  127. out = tf.layers.dense(fc1, n_classes)
  128. # Because 'softmax_cross_entropy_with_logits' already apply softmax,
  129. # we only apply softmax to testing network
  130. out = tf.nn.softmax(out) if not is_training else out
  131. return out
  132. # Because Dropout have different behavior at training and prediction time, we
  133. # need to create 2 distinct computation graphs that share the same weights.
  134. # Create a graph for training
  135. logits_train = conv_net(X, N_CLASSES, dropout, reuse=False, is_training=True)
  136. # Create another graph for testing that reuse the same weights
  137. logits_test = conv_net(X, N_CLASSES, dropout, reuse=True, is_training=False)
  138. # Define loss and optimizer (with train logits, for dropout to take effect)
  139. loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
  140. logits=logits_train, labels=Y))
  141. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
  142. train_op = optimizer.minimize(loss_op)
  143. # Evaluate model (with test logits, for dropout to be disabled)
  144. correct_pred = tf.equal(tf.argmax(logits_test, 1), tf.cast(Y, tf.int64))
  145. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
  146. # Initialize the variables (i.e. assign their default value)
  147. init = tf.global_variables_initializer()
  148. # Saver object
  149. saver = tf.train.Saver()
  150. # Start training
  151. with tf.Session() as sess:
  152. # Run the initializer
  153. sess.run(init)
  154. # Start the data queue
  155. tf.train.start_queue_runners()
  156. # Training cycle
  157. for step in range(1, num_steps+1):
  158. if step % display_step == 0:
  159. # Run optimization and calculate batch loss and accuracy
  160. _, loss, acc = sess.run([train_op, loss_op, accuracy])
  161. print("Step " + str(step) + ", Minibatch Loss= " + \
  162. "{:.4f}".format(loss) + ", Training Accuracy= " + \
  163. "{:.3f}".format(acc))
  164. else:
  165. # Only run the optimization op (backprop)
  166. sess.run(train_op)
  167. print("Optimization Finished!")
  168. # Save your model
  169. saver.save(sess, 'my_tf_model')