dataset_factory.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. """A factory-pattern class which returns image/label pairs."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. from slim.datasets import mnist
  21. from domain_adaptation.datasets import mnist_m
  22. slim = tf.contrib.slim
  23. def get_dataset(dataset_name,
  24. split_name,
  25. dataset_dir,
  26. file_pattern=None,
  27. reader=None):
  28. """Given a dataset name and a split_name returns a Dataset.
  29. Args:
  30. dataset_name: String, the name of the dataset.
  31. split_name: A train/test split name.
  32. dataset_dir: The directory where the dataset files are stored.
  33. file_pattern: The file pattern to use for matching the dataset source files.
  34. reader: The subclass of tf.ReaderBase. If left as `None`, then the default
  35. reader defined by each dataset is used.
  36. Returns:
  37. A tf-slim `Dataset` class.
  38. Raises:
  39. ValueError: if `dataset_name` isn't recognized.
  40. """
  41. dataset_name_to_module = {'mnist': mnist, 'mnist_m': mnist_m}
  42. if dataset_name not in dataset_name_to_module:
  43. raise ValueError('Name of dataset unknown %s.' % dataset_name)
  44. return dataset_name_to_module[dataset_name].get_split(split_name, dataset_dir,
  45. file_pattern, reader)
  46. def provide_batch(dataset_name, split_name, dataset_dir, num_readers,
  47. batch_size, num_preprocessing_threads):
  48. """Provides a batch of images and corresponding labels.
  49. Args:
  50. dataset_name: String, the name of the dataset.
  51. split_name: A train/test split name.
  52. dataset_dir: The directory where the dataset files are stored.
  53. num_readers: The number of readers used by DatasetDataProvider.
  54. batch_size: The size of the batch requested.
  55. num_preprocessing_threads: The number of preprocessing threads for
  56. tf.train.batch.
  57. file_pattern: The file pattern to use for matching the dataset source files.
  58. reader: The subclass of tf.ReaderBase. If left as `None`, then the default
  59. reader defined by each dataset is used.
  60. Returns:
  61. A batch of
  62. images: tensor of [batch_size, height, width, channels].
  63. labels: dictionary of labels.
  64. """
  65. dataset = get_dataset(dataset_name, split_name, dataset_dir)
  66. provider = slim.dataset_data_provider.DatasetDataProvider(
  67. dataset,
  68. num_readers=num_readers,
  69. common_queue_capacity=20 * batch_size,
  70. common_queue_min=10 * batch_size)
  71. [image, label] = provider.get(['image', 'label'])
  72. # Convert images to float32
  73. image = tf.image.convert_image_dtype(image, tf.float32)
  74. image -= 0.5
  75. image *= 2
  76. # Load the data.
  77. labels = {}
  78. images, labels['classes'] = tf.train.batch(
  79. [image, label],
  80. batch_size=batch_size,
  81. num_threads=num_preprocessing_threads,
  82. capacity=5 * batch_size)
  83. labels['classes'] = slim.one_hot_encoding(labels['classes'],
  84. dataset.num_classes)
  85. # Convert mnist to RGB and 32x32 so that it can match mnist_m.
  86. if dataset_name == 'mnist':
  87. images = tf.image.grayscale_to_rgb(images)
  88. images = tf.image.resize_images(images, [32, 32])
  89. return images, labels