You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.0 KiB

6 years ago
  1. # Copyright 2017 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. """Input reader builder.
  16. Creates data sources for DetectionModels from an InputReader config. See
  17. input_reader.proto for options.
  18. Note: If users wishes to also use their own InputReaders with the Object
  19. Detection configuration framework, they should define their own builder function
  20. that wraps the build function.
  21. """
  22. import tensorflow as tf
  23. from object_detection.data_decoders import tf_example_decoder
  24. from object_detection.protos import input_reader_pb2
  25. parallel_reader = tf.contrib.slim.parallel_reader
  26. def build(input_reader_config):
  27. """Builds a tensor dictionary based on the InputReader config.
  28. Args:
  29. input_reader_config: A input_reader_pb2.InputReader object.
  30. Returns:
  31. A tensor dict based on the input_reader_config.
  32. Raises:
  33. ValueError: On invalid input reader proto.
  34. ValueError: If no input paths are specified.
  35. """
  36. if not isinstance(input_reader_config, input_reader_pb2.InputReader):
  37. raise ValueError('input_reader_config not of type '
  38. 'input_reader_pb2.InputReader.')
  39. if input_reader_config.WhichOneof('input_reader') == 'tf_record_input_reader':
  40. config = input_reader_config.tf_record_input_reader
  41. if not config.input_path:
  42. raise ValueError('At least one input path must be specified in '
  43. '`input_reader_config`.')
  44. _, string_tensor = parallel_reader.parallel_read(
  45. config.input_path[:], # Convert `RepeatedScalarContainer` to list.
  46. reader_class=tf.TFRecordReader,
  47. num_epochs=(input_reader_config.num_epochs
  48. if input_reader_config.num_epochs else None),
  49. num_readers=input_reader_config.num_readers,
  50. shuffle=input_reader_config.shuffle,
  51. dtypes=[tf.string, tf.string],
  52. capacity=input_reader_config.queue_capacity,
  53. min_after_dequeue=input_reader_config.min_after_dequeue)
  54. label_map_proto_file = None
  55. if input_reader_config.HasField('label_map_path'):
  56. label_map_proto_file = input_reader_config.label_map_path
  57. decoder = tf_example_decoder.TfExampleDecoder(
  58. load_instance_masks=input_reader_config.load_instance_masks,
  59. instance_mask_type=input_reader_config.mask_type,
  60. label_map_proto_file=label_map_proto_file)
  61. return decoder.decode(string_tensor)
  62. raise ValueError('Unsupported input_reader_config.')