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.

88 lines
2.6 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. """Utility functions for creating TFRecord data sets."""
  16. import tensorflow as tf
  17. def int64_feature(value):
  18. return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  19. def int64_list_feature(value):
  20. return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
  21. def bytes_feature(value):
  22. return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  23. def bytes_list_feature(value):
  24. return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))
  25. def float_list_feature(value):
  26. return tf.train.Feature(float_list=tf.train.FloatList(value=value))
  27. def read_examples_list(path):
  28. """Read list of training or validation examples.
  29. The file is assumed to contain a single example per line where the first
  30. token in the line is an identifier that allows us to find the image and
  31. annotation xml for that example.
  32. For example, the line:
  33. xyz 3
  34. would allow us to find files xyz.jpg and xyz.xml (the 3 would be ignored).
  35. Args:
  36. path: absolute path to examples list file.
  37. Returns:
  38. list of example identifiers (strings).
  39. """
  40. with tf.gfile.GFile(path) as fid:
  41. lines = fid.readlines()
  42. return [line.strip().split(' ')[0] for line in lines]
  43. def recursive_parse_xml_to_dict(xml):
  44. """Recursively parses XML contents to python dict.
  45. We assume that `object` tags are the only ones that can appear
  46. multiple times at the same level of a tree.
  47. Args:
  48. xml: xml tree obtained by parsing XML file contents using lxml.etree
  49. Returns:
  50. Python dictionary holding XML contents.
  51. """
  52. if not xml:
  53. return {xml.tag: xml.text}
  54. result = {}
  55. for child in xml:
  56. child_result = recursive_parse_xml_to_dict(child)
  57. if child.tag != 'object':
  58. result[child.tag] = child_result[child.tag]
  59. else:
  60. if child.tag not in result:
  61. result[child.tag] = []
  62. result[child.tag].append(child_result[child.tag])
  63. return {xml.tag: result}