PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/py/tf/models/research/slim/datasets/flowers.py

https://gitlab.com/ll14ec/Portfolio
Python | 98 lines | 63 code | 10 blank | 25 comment | 0 complexity | e81cea495a20d69bf35f9b1394822983 MD5 | raw file
  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. """Provides data for the flowers dataset.
  16. The dataset scripts used to create the dataset can be found at:
  17. tensorflow/models/research/slim/datasets/download_and_convert_flowers.py
  18. """
  19. from __future__ import absolute_import
  20. from __future__ import division
  21. from __future__ import print_function
  22. import os
  23. import tensorflow as tf
  24. from datasets import dataset_utils
  25. slim = tf.contrib.slim
  26. _FILE_PATTERN = 'flowers_%s_*.tfrecord'
  27. SPLITS_TO_SIZES = {'train': 3320, 'validation': 350}
  28. _NUM_CLASSES = 5
  29. _ITEMS_TO_DESCRIPTIONS = {
  30. 'image': 'A color image of varying size.',
  31. 'label': 'A single integer between 0 and 4',
  32. }
  33. def get_split(split_name, dataset_dir, file_pattern=None, reader=None):
  34. """Gets a dataset tuple with instructions for reading flowers.
  35. Args:
  36. split_name: A train/validation split name.
  37. dataset_dir: The base directory of the dataset sources.
  38. file_pattern: The file pattern to use when matching the dataset sources.
  39. It is assumed that the pattern contains a '%s' string so that the split
  40. name can be inserted.
  41. reader: The TensorFlow reader type.
  42. Returns:
  43. A `Dataset` namedtuple.
  44. Raises:
  45. ValueError: if `split_name` is not a valid train/validation split.
  46. """
  47. if split_name not in SPLITS_TO_SIZES:
  48. raise ValueError('split name %s was not recognized.' % split_name)
  49. if not file_pattern:
  50. file_pattern = _FILE_PATTERN
  51. file_pattern = os.path.join(dataset_dir, file_pattern % split_name)
  52. # Allowing None in the signature so that dataset_factory can use the default.
  53. if reader is None:
  54. reader = tf.TFRecordReader
  55. keys_to_features = {
  56. 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''),
  57. 'image/format': tf.FixedLenFeature((), tf.string, default_value='png'),
  58. 'image/class/label': tf.FixedLenFeature(
  59. [], tf.int64, default_value=tf.zeros([], dtype=tf.int64)),
  60. }
  61. items_to_handlers = {
  62. 'image': slim.tfexample_decoder.Image(),
  63. 'label': slim.tfexample_decoder.Tensor('image/class/label'),
  64. }
  65. decoder = slim.tfexample_decoder.TFExampleDecoder(
  66. keys_to_features, items_to_handlers)
  67. labels_to_names = None
  68. if dataset_utils.has_labels(dataset_dir):
  69. labels_to_names = dataset_utils.read_label_file(dataset_dir)
  70. return slim.dataset.Dataset(
  71. data_sources=file_pattern,
  72. reader=reader,
  73. decoder=decoder,
  74. num_samples=SPLITS_TO_SIZES[split_name],
  75. items_to_descriptions=_ITEMS_TO_DESCRIPTIONS,
  76. num_classes=_NUM_CLASSES,
  77. labels_to_names=labels_to_names)