cifar_input.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """CIFAR dataset input module.
  16. """
  17. import tensorflow as tf
  18. # backward compatible concat (arg order changed in head)
  19. import inspect
  20. def concat(values, axis):
  21. if 'axis' in inspect.signature(tf.concat).parameters.keys():
  22. return tf.concat(values=values, axis=axis)
  23. else:
  24. assert 'concat_dim' in inspect.signature(tf.concat).parameters.keys()
  25. return tf.concat(concat_dim=axis, values=values)
  26. def build_input(dataset, data_path, batch_size, mode):
  27. """Build CIFAR image and labels.
  28. Args:
  29. dataset: Either 'cifar10' or 'cifar100'.
  30. data_path: Filename for data.
  31. batch_size: Input batch size.
  32. mode: Either 'train' or 'eval'.
  33. Returns:
  34. images: Batches of images. [batch_size, image_size, image_size, 3]
  35. labels: Batches of labels. [batch_size, num_classes]
  36. Raises:
  37. ValueError: when the specified dataset is not supported.
  38. """
  39. image_size = 32
  40. if dataset == 'cifar10':
  41. label_bytes = 1
  42. label_offset = 0
  43. num_classes = 10
  44. elif dataset == 'cifar100':
  45. label_bytes = 1
  46. label_offset = 1
  47. num_classes = 100
  48. else:
  49. raise ValueError('Not supported dataset %s', dataset)
  50. depth = 3
  51. image_bytes = image_size * image_size * depth
  52. record_bytes = label_bytes + label_offset + image_bytes
  53. data_files = tf.gfile.Glob(data_path)
  54. file_queue = tf.train.string_input_producer(data_files, shuffle=True)
  55. # Read examples from files in the filename queue.
  56. reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
  57. _, value = reader.read(file_queue)
  58. # Convert these examples to dense labels and processed images.
  59. record = tf.reshape(tf.decode_raw(value, tf.uint8), [record_bytes])
  60. label = tf.cast(tf.slice(record, [label_offset], [label_bytes]), tf.int32)
  61. # Convert from string to [depth * height * width] to [depth, height, width].
  62. depth_major = tf.reshape(tf.slice(record, [label_bytes], [image_bytes]),
  63. [depth, image_size, image_size])
  64. # Convert from [depth, height, width] to [height, width, depth].
  65. image = tf.cast(tf.transpose(depth_major, [1, 2, 0]), tf.float32)
  66. if mode == 'train':
  67. image = tf.image.resize_image_with_crop_or_pad(
  68. image, image_size+4, image_size+4)
  69. image = tf.random_crop(image, [image_size, image_size, 3])
  70. image = tf.image.random_flip_left_right(image)
  71. # Brightness/saturation/constrast provides small gains .2%~.5% on cifar.
  72. # image = tf.image.random_brightness(image, max_delta=63. / 255.)
  73. # image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
  74. # image = tf.image.random_contrast(image, lower=0.2, upper=1.8)
  75. image = tf.image.per_image_standardization(image)
  76. example_queue = tf.RandomShuffleQueue(
  77. capacity=16 * batch_size,
  78. min_after_dequeue=8 * batch_size,
  79. dtypes=[tf.float32, tf.int32],
  80. shapes=[[image_size, image_size, depth], [1]])
  81. num_threads = 16
  82. else:
  83. image = tf.image.resize_image_with_crop_or_pad(
  84. image, image_size, image_size)
  85. image = tf.image.per_image_standardization(image)
  86. example_queue = tf.FIFOQueue(
  87. 3 * batch_size,
  88. dtypes=[tf.float32, tf.int32],
  89. shapes=[[image_size, image_size, depth], [1]])
  90. num_threads = 1
  91. example_enqueue_op = example_queue.enqueue([image, label])
  92. tf.train.add_queue_runner(tf.train.queue_runner.QueueRunner(
  93. example_queue, [example_enqueue_op] * num_threads))
  94. # Read 'batch' labels + images from the example queue.
  95. images, labels = example_queue.dequeue_many(batch_size)
  96. labels = tf.reshape(labels, [batch_size, 1])
  97. indices = tf.reshape(tf.range(0, batch_size, 1), [batch_size, 1])
  98. labels = tf.sparse_to_dense(
  99. tf.concat(values=[indices, labels], axis=1),
  100. [batch_size, num_classes], 1.0, 0.0)
  101. assert len(images.get_shape()) == 4
  102. assert images.get_shape()[0] == batch_size
  103. assert images.get_shape()[-1] == 3
  104. assert len(labels.get_shape()) == 2
  105. assert labels.get_shape()[0] == batch_size
  106. assert labels.get_shape()[1] == num_classes
  107. # Display the training images in the visualizer.
  108. tf.summary.image('images', images)
  109. return images, labels