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.

141 lines
5.4 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 detection inference."""
  16. from __future__ import division
  17. import tensorflow as tf
  18. from object_detection.core import standard_fields
  19. def build_input(tfrecord_paths):
  20. """Builds the graph's input.
  21. Args:
  22. tfrecord_paths: List of paths to the input TFRecords
  23. Returns:
  24. serialized_example_tensor: The next serialized example. String scalar Tensor
  25. image_tensor: The decoded image of the example. Uint8 tensor,
  26. shape=[1, None, None,3]
  27. """
  28. filename_queue = tf.train.string_input_producer(
  29. tfrecord_paths, shuffle=False, num_epochs=1)
  30. tf_record_reader = tf.TFRecordReader()
  31. _, serialized_example_tensor = tf_record_reader.read(filename_queue)
  32. features = tf.parse_single_example(
  33. serialized_example_tensor,
  34. features={
  35. standard_fields.TfExampleFields.image_encoded:
  36. tf.FixedLenFeature([], tf.string),
  37. })
  38. encoded_image = features[standard_fields.TfExampleFields.image_encoded]
  39. image_tensor = tf.image.decode_image(encoded_image, channels=3)
  40. image_tensor.set_shape([None, None, 3])
  41. image_tensor = tf.expand_dims(image_tensor, 0)
  42. return serialized_example_tensor, image_tensor
  43. def build_inference_graph(image_tensor, inference_graph_path):
  44. """Loads the inference graph and connects it to the input image.
  45. Args:
  46. image_tensor: The input image. uint8 tensor, shape=[1, None, None, 3]
  47. inference_graph_path: Path to the inference graph with embedded weights
  48. Returns:
  49. detected_boxes_tensor: Detected boxes. Float tensor,
  50. shape=[num_detections, 4]
  51. detected_scores_tensor: Detected scores. Float tensor,
  52. shape=[num_detections]
  53. detected_labels_tensor: Detected labels. Int64 tensor,
  54. shape=[num_detections]
  55. """
  56. with tf.gfile.Open(inference_graph_path, 'rb') as graph_def_file:
  57. graph_content = graph_def_file.read()
  58. graph_def = tf.GraphDef()
  59. graph_def.MergeFromString(graph_content)
  60. tf.import_graph_def(
  61. graph_def, name='', input_map={'image_tensor': image_tensor})
  62. g = tf.get_default_graph()
  63. num_detections_tensor = tf.squeeze(
  64. g.get_tensor_by_name('num_detections:0'), 0)
  65. num_detections_tensor = tf.cast(num_detections_tensor, tf.int32)
  66. detected_boxes_tensor = tf.squeeze(
  67. g.get_tensor_by_name('detection_boxes:0'), 0)
  68. detected_boxes_tensor = detected_boxes_tensor[:num_detections_tensor]
  69. detected_scores_tensor = tf.squeeze(
  70. g.get_tensor_by_name('detection_scores:0'), 0)
  71. detected_scores_tensor = detected_scores_tensor[:num_detections_tensor]
  72. detected_labels_tensor = tf.squeeze(
  73. g.get_tensor_by_name('detection_classes:0'), 0)
  74. detected_labels_tensor = tf.cast(detected_labels_tensor, tf.int64)
  75. detected_labels_tensor = detected_labels_tensor[:num_detections_tensor]
  76. return detected_boxes_tensor, detected_scores_tensor, detected_labels_tensor
  77. def infer_detections_and_add_to_example(
  78. serialized_example_tensor, detected_boxes_tensor, detected_scores_tensor,
  79. detected_labels_tensor, discard_image_pixels):
  80. """Runs the supplied tensors and adds the inferred detections to the example.
  81. Args:
  82. serialized_example_tensor: Serialized TF example. Scalar string tensor
  83. detected_boxes_tensor: Detected boxes. Float tensor,
  84. shape=[num_detections, 4]
  85. detected_scores_tensor: Detected scores. Float tensor,
  86. shape=[num_detections]
  87. detected_labels_tensor: Detected labels. Int64 tensor,
  88. shape=[num_detections]
  89. discard_image_pixels: If true, discards the image from the result
  90. Returns:
  91. The de-serialized TF example augmented with the inferred detections.
  92. """
  93. tf_example = tf.train.Example()
  94. (serialized_example, detected_boxes, detected_scores,
  95. detected_classes) = tf.get_default_session().run([
  96. serialized_example_tensor, detected_boxes_tensor, detected_scores_tensor,
  97. detected_labels_tensor
  98. ])
  99. detected_boxes = detected_boxes.T
  100. tf_example.ParseFromString(serialized_example)
  101. feature = tf_example.features.feature
  102. feature[standard_fields.TfExampleFields.
  103. detection_score].float_list.value[:] = detected_scores
  104. feature[standard_fields.TfExampleFields.
  105. detection_bbox_ymin].float_list.value[:] = detected_boxes[0]
  106. feature[standard_fields.TfExampleFields.
  107. detection_bbox_xmin].float_list.value[:] = detected_boxes[1]
  108. feature[standard_fields.TfExampleFields.
  109. detection_bbox_ymax].float_list.value[:] = detected_boxes[2]
  110. feature[standard_fields.TfExampleFields.
  111. detection_bbox_xmax].float_list.value[:] = detected_boxes[3]
  112. feature[standard_fields.TfExampleFields.
  113. detection_class_label].int64_list.value[:] = detected_classes
  114. if discard_image_pixels:
  115. del feature[standard_fields.TfExampleFields.image_encoded]
  116. return tf_example