dataset.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright 2016 Google Inc. 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. """Small library that points to a data set.
  16. Methods of Data class:
  17. data_files: Returns a python list of all (sharded) data set files.
  18. num_examples_per_epoch: Returns the number of examples in the data set.
  19. num_classes: Returns the number of classes in the data set.
  20. reader: Return a reader for a single entry from the data set.
  21. """
  22. from __future__ import absolute_import
  23. from __future__ import division
  24. from __future__ import print_function
  25. from abc import ABCMeta
  26. from abc import abstractmethod
  27. import os
  28. import tensorflow as tf
  29. FLAGS = tf.app.flags.FLAGS
  30. # Basic model parameters.
  31. tf.app.flags.DEFINE_string('data_dir', '/tmp/mydata',
  32. """Path to the processed data, i.e. """
  33. """TFRecord of Example protos.""")
  34. class Dataset(object):
  35. """A simple class for handling data sets."""
  36. __metaclass__ = ABCMeta
  37. def __init__(self, name, subset):
  38. """Initialize dataset using a subset and the path to the data."""
  39. assert subset in self.available_subsets(), self.available_subsets()
  40. self.name = name
  41. self.subset = subset
  42. @abstractmethod
  43. def num_classes(self):
  44. """Returns the number of classes in the data set."""
  45. pass
  46. # return 10
  47. @abstractmethod
  48. def num_examples_per_epoch(self):
  49. """Returns the number of examples in the data subset."""
  50. pass
  51. # if self.subset == 'train':
  52. # return 10000
  53. # if self.subset == 'validation':
  54. # return 1000
  55. @abstractmethod
  56. def download_message(self):
  57. """Prints a download message for the Dataset."""
  58. pass
  59. def available_subsets(self):
  60. """Returns the list of available subsets."""
  61. return ['train', 'validation']
  62. def data_files(self):
  63. """Returns a python list of all (sharded) data subset files.
  64. Returns:
  65. python list of all (sharded) data set files.
  66. Raises:
  67. ValueError: if there are not data_files matching the subset.
  68. """
  69. tf_record_pattern = os.path.join(FLAGS.data_dir, '%s-*' % self.subset)
  70. data_files = tf.gfile.Glob(tf_record_pattern)
  71. if not data_files:
  72. print('No files found for dataset %s/%s at %s' % (self.name,
  73. self.subset,
  74. FLAGS.data_dir))
  75. self.download_message()
  76. exit(-1)
  77. return data_files
  78. def reader(self):
  79. """Return a reader for a single entry from the data set.
  80. See io_ops.py for details of Reader class.
  81. Returns:
  82. Reader object that reads the data set.
  83. """
  84. return tf.TFRecordReader()