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.

96 lines
4.2 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. r"""Infers detections on a TFRecord of TFExamples given an inference graph.
  16. Example usage:
  17. ./infer_detections \
  18. --input_tfrecord_paths=/path/to/input/tfrecord1,/path/to/input/tfrecord2 \
  19. --output_tfrecord_path_prefix=/path/to/output/detections.tfrecord \
  20. --inference_graph=/path/to/frozen_weights_inference_graph.pb
  21. The output is a TFRecord of TFExamples. Each TFExample from the input is first
  22. augmented with detections from the inference graph and then copied to the
  23. output.
  24. The input and output nodes of the inference graph are expected to have the same
  25. types, shapes, and semantics, as the input and output nodes of graphs produced
  26. by export_inference_graph.py, when run with --input_type=image_tensor.
  27. The script can also discard the image pixels in the output. This greatly
  28. reduces the output size and can potentially accelerate reading data in
  29. subsequent processing steps that don't require the images (e.g. computing
  30. metrics).
  31. """
  32. import itertools
  33. import tensorflow as tf
  34. from object_detection.inference import detection_inference
  35. tf.flags.DEFINE_string('input_tfrecord_paths', None,
  36. 'A comma separated list of paths to input TFRecords.')
  37. tf.flags.DEFINE_string('output_tfrecord_path', None,
  38. 'Path to the output TFRecord.')
  39. tf.flags.DEFINE_string('inference_graph', None,
  40. 'Path to the inference graph with embedded weights.')
  41. tf.flags.DEFINE_boolean('discard_image_pixels', False,
  42. 'Discards the images in the output TFExamples. This'
  43. ' significantly reduces the output size and is useful'
  44. ' if the subsequent tools don\'t need access to the'
  45. ' images (e.g. when computing evaluation measures).')
  46. FLAGS = tf.flags.FLAGS
  47. def main(_):
  48. tf.logging.set_verbosity(tf.logging.INFO)
  49. required_flags = ['input_tfrecord_paths', 'output_tfrecord_path',
  50. 'inference_graph']
  51. for flag_name in required_flags:
  52. if not getattr(FLAGS, flag_name):
  53. raise ValueError('Flag --{} is required'.format(flag_name))
  54. with tf.Session() as sess:
  55. input_tfrecord_paths = [
  56. v for v in FLAGS.input_tfrecord_paths.split(',') if v]
  57. tf.logging.info('Reading input from %d files', len(input_tfrecord_paths))
  58. serialized_example_tensor, image_tensor = detection_inference.build_input(
  59. input_tfrecord_paths)
  60. tf.logging.info('Reading graph and building model...')
  61. (detected_boxes_tensor, detected_scores_tensor,
  62. detected_labels_tensor) = detection_inference.build_inference_graph(
  63. image_tensor, FLAGS.inference_graph)
  64. tf.logging.info('Running inference and writing output to {}'.format(
  65. FLAGS.output_tfrecord_path))
  66. sess.run(tf.local_variables_initializer())
  67. tf.train.start_queue_runners()
  68. with tf.python_io.TFRecordWriter(
  69. FLAGS.output_tfrecord_path) as tf_record_writer:
  70. try:
  71. for counter in itertools.count():
  72. tf.logging.log_every_n(tf.logging.INFO, 'Processed %d images...', 10,
  73. counter)
  74. tf_example = detection_inference.infer_detections_and_add_to_example(
  75. serialized_example_tensor, detected_boxes_tensor,
  76. detected_scores_tensor, detected_labels_tensor,
  77. FLAGS.discard_image_pixels)
  78. tf_record_writer.write(tf_example.SerializeToString())
  79. except tf.errors.OutOfRangeError:
  80. tf.logging.info('Finished processing records')
  81. if __name__ == '__main__':
  82. tf.app.run()